Exemplo n.º 1
0
        public YeelightDevice(YeelightData yd, ColorService cs) : base(cs)
        {
            _data     = yd;
            Id        = _data.Id;
            IpAddress = _data.IpAddress;
            Enable    = _data.Enable;
            LoadData();
            Log.Debug("Created new yee device at " + yd.IpAddress);
            cs.ColorSendEventAsync += SetColors;
            _colorService           = cs;

            _data.LastSeen = DateTime.Now.ToString(CultureInfo.InvariantCulture);
            DataUtil.AddDeviceAsync(_data, false).ConfigureAwait(false);
            _yeeDevice = new Device(IpAddress);
            _colorService.ColorSendEventAsync += SetColors;
        }
Exemplo n.º 2
0
        private async Task TriggerRefresh(object?o, DynamicEventArgs?dynamicEventArgs)
        {
            Log.Information("Beginning device discovery...");
            var cs      = new CancellationTokenSource();
            var sd      = DataUtil.GetSystemData();
            var timeout = sd.DiscoveryTimeout;

            if (timeout < 3)
            {
                timeout = 3;
            }

            cs.CancelAfter(TimeSpan.FromSeconds(timeout));
            await DeviceDiscovery(timeout, cs.Token);

            var devs = DataUtil.GetDevices();

            if (!sd.AutoRemoveDevices)
            {
                return;
            }

            foreach (var dev in devs)
            {
                var device   = (IColorTargetData)dev;
                var lastSeen = DateTime.Parse(device.LastSeen, CultureInfo.InvariantCulture);
                if (DateTime.Now - lastSeen < TimeSpan.FromDays(sd.AutoRemoveDevicesAfter))
                {
                    continue;
                }

                bool online = SystemUtil.IsOnline(dev.IpAddress);
                if (online)
                {
                    dev.LastSeen = DateTime.Now.ToString(CultureInfo.InvariantCulture);
                    DataUtil.AddDeviceAsync(dev);
                }

                DataUtil.DeleteDevice(device.Id);
            }

            cs.Dispose();
            Log.Information("Device discovery completed.");
        }