public static async Task InitializeYeelights()
        {
            DeviceLocator.MaxRetryCount = 2;

            // notify on discovered bulbs
            var progressReporter = new Progress <Device>(device => Console.WriteLine($"[Yeelight] Device found: {device}"));

            // find bulbs on the network
            var devices = await DeviceLocator.DiscoverAsync(progressReporter);

            // once we collect devices on the network, add them to bulbs list,
            // connect to them, turn them on, set their brightness to max
            foreach (var device in devices)
            {
                Console.WriteLine($"[Yeelight] Trying to connect to device: {device}");
                bulbs.Add(device);

                // device.OnNotificationReceived += DeviceOnOnNotificationReceived;
                device.OnError += DeviceOnError;

                device.Connect();
                device.TurnOn();
                device.SetBrightness(100);
            }
        }
示例#2
0
        /// <summary>
        /// Package start
        /// </summary>
        public override void OnStart()
        {
            PackageHost.WriteInfo("Package starting - IsRunning: {0} - IsConnected: {1}", PackageHost.IsRunning, PackageHost.IsConnected);

            //parallel connection to the devices
            PackageHost.GetSettingAsJsonObject <IEnumerable <DeviceConfig> >("Devices").AsParallel().ForAll(dc =>
            {
                //create new device and connect
                Device device = new Device(dc.Hostname, dc.Port)
                {
                    Name = dc.Name
                };
                try
                {
                    if (device.Connect().Result)
                    {
                        PackageHost.WriteInfo($"Device {dc.Name} ({dc.Hostname}:{dc.Port}) connected");
                        device.OnNotificationReceived += (object sender, NotificationReceivedEventArgs e) =>
                        {
                            //updated device properties
                            PackageHost.PushStateObject(dc.Name, device.Properties);
                            PackageHost.WriteDebug(e.Result);
                        };

                        device.OnError += (object sender, UnhandledExceptionEventArgs e) =>
                        {
                            PackageHost.WriteError(e.ExceptionObject);
                        };

                        //initial device properties
                        PackageHost.PushStateObject(dc.Name, device.Properties);
                    }

                    _all.Add(dc.Name, device);
                }
                catch (Exception ex)
                {
                    PackageHost.WriteError($"Unable to connect to device {dc.Name} ({dc.Hostname}:{dc.Port}) : {ex.Message}");;
                }
            });

            //creation of groups
            foreach (DeviceGroupConfig gc in PackageHost.GetSettingAsJsonObject <IEnumerable <DeviceGroupConfig> >("Groups"))
            {
                DeviceGroup group = new DeviceGroup();
                foreach (Device device in gc.Devices.Select(x => _all.SingleOrDefault(d => d.Key == x).Value))
                {
                    group.Add(device);
                }

                _all.Add(gc.Name, group);
            }
        }
        /// <summary>
        /// Package start
        /// </summary>
        public override void OnStart()
        {
            PackageHost.WriteInfo("Package starting - IsRunning: {0} - IsConnected: {1}", PackageHost.IsRunning, PackageHost.IsConnected);

            //parallel connection to the devices
            PackageHost.GetSettingAsJsonObject <IEnumerable <DeviceConfig> >("Devices").AsParallel().ForAll(dc =>
            {
                //create new device and connect
                Device device = new Device(dc.Hostname, dc.Port)
                {
                    Name = dc.Name
                };
                try
                {
                    device.Connect().Wait();
                    if (device.IsConnected)
                    {
                        device.OnError -= ErrorEvent;
                        device.OnError += ErrorEvent;

                        device.SetName(dc.Name).Wait();
                        PackageHost.WriteInfo($"device '{dc.Name}' ({device.Hostname}:{device.Port}) connected");
                    }
                }
                catch (Exception ex)
                {
                    PackageHost.WriteWarn($"Cannot connect device '{device.Name}' ({device.Hostname}:{device.Port}), message : {ex.Message}");
                }

                _all.Add(dc.Name, device);
            });

            //creation of groups
            foreach (DeviceGroupConfig gc in PackageHost.GetSettingAsJsonObject <IEnumerable <DeviceGroupConfig> >("Groups"))
            {
                DeviceGroup group = new DeviceGroup();
                foreach (Device device in gc.Devices.Select(x => _all.SingleOrDefault(d => d.Key == x).Value))
                {
                    group.Add(device);
                }

                _all.Add(gc.Name, group);
            }

            UpdateDevicesStateObjects();
        }
示例#4
0
        private async void MainForm_Load(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "YeeControl " + GlobalVariables.VERSION + " | www.yeecontrol.com";
            this.Size = defaultSize;

            foreach (YeeControlDevice ycd in YeeControlDeviceHelper.GetYeeControlDevices())
            {
                allDevices.Add(new Device(ycd.Hostname));
            }
            await allDevices.Connect();


            RefreshPresets();
            RefreshCheckedListBox();
            RefreshLightState();

            listBox1.SelectedIndexChanged += listBox1_SelectedIndexChanged;

            RT(() => RefreshLightState(), 7, cts.Token);
        }
示例#5
0
        public DeviceGroup GetSelectedLights()
        {
            DeviceGroup devices = new DeviceGroup();

            for (int i = 0; i < checkedListBox_Devices.Items.Count; i++)
            {
                string hostname = hostnamesByListIndex[i];
                if (checkedListBox_Devices.GetItemChecked(i))
                {
                    foreach (Device d in allDevices)
                    {
                        if (d.Hostname == hostname)
                        {
                            devices.Add(d);
                        }
                    }
                }
            }

            return(devices);
        }