Ejemplo n.º 1
0
 public AdalightDevice(AdalightData data, ColorService cs) : base(cs)
 {
     Id          = data.Id;
     _data       = data;
     _port       = _data.Port;
     _multiplier = _data.LedMultiplier;
     _gammaTable = ColorUtil.GammaTable(1);
     LoadData();
     cs.ColorSendEventAsync += SetColors;
     _adalight = new AdalightNet.Adalight(_port, _ledCount, _baud);
 }
Ejemplo n.º 2
0
        public async Task Discover(CancellationToken ct, int timeout)
        {
            var sd   = DataUtil.GetSystemData();
            var baud = sd.BaudRate;

            Log.Debug("Adalight: Discovery started.");
            var discoTask = Task.Run(() => {
                var devs = new Dictionary <string, KeyValuePair <int, int> >();
                try {
                    devs = FindDevices(baud);
                } catch (Exception e) {
                    Log.Debug("Exception: " + e.Message);
                }

                Log.Debug("Found" + devs.Count + " devices.");

                foreach (var(key, value) in devs)
                {
                    var count = value.Key;
                    var bri   = value.Value;
                    try {
                        Log.Debug("Trying: " + key);
                        var ac = new AdalightNet.Adalight(key, 20, baud);
                        ac.Connect();
                        if (ac.Connected)
                        {
                            Log.Debug("Connected.");
                            var foo = ac.GetState();
                            count   = foo[0];
                            bri     = foo[0];
                            ac.Disconnect();
                            ac.Dispose();
                            Log.Debug("State got, done.");
                        }
                        else
                        {
                            Log.Debug("Not connected...");
                        }
                    } catch (Exception e) {
                        Log.Debug("Discovery exception: " + e.Message + " at " + e.StackTrace);
                    }


                    var data = new AdalightData(key, count)
                    {
                        Speed = baud
                    };
                    Log.Debug("Creating device.");
                    if (bri != 0)
                    {
                        data.Brightness = bri;
                    }

                    ControlService.AddDevice(data).ConfigureAwait(false);
                    Log.Debug("And added...");
                }
            }, ct);
            await discoTask;

            Log.Debug("Adalight: Discovery complete.");
        }