Exemplo n.º 1
0
        private async void SetNameButton_Click(object sender, EventArgs e)
        {
            string value = "";

            if (this.InputStringDialog(ref value, true, "Light name:"))
            {
                await Yeelight.SetNameAsync(_deviceViewModel.Device, value);

                _deviceViewModel.Name = value;

                _deviceViewModel.IsOn = _deviceViewModel.Device.Properties.FirstOrDefault(m => m.Key == "power").Value.ToString();
                Text = _deviceViewModel.ToString();
                MainForm        mainForm     = (MainForm)this.Parent.Parent.Parent;
                TreeNode        firstNode    = mainForm.FormAside.Nodes[0];                // Lights
                List <TreeNode> subNodesList = firstNode.Nodes.Cast <TreeNode>().ToList(); // Lights sub nodes to list.

                List <NavMenuItem> lightTab = (from t in subNodesList
                                               let ts = t.Tag as NavMenuItem
                                                        where ts.PageGuid == _deviceViewModel.PageGuid
                                                        select ts).ToList();

                var node = subNodesList.FirstOrDefault(m => m.Tag == lightTab.FirstOrDefault());
                if (node != null)
                {
                    node.Text = _deviceViewModel.ToString();
                }
                NotifyUser(SetNameButton, "Light name set");
            }
        }
Exemplo n.º 2
0
        private async void TurnOnButton_Click(object sender, EventArgs e)
        {
            NotifyUser(TurnOnButton, "Turning on");
            FlashBulb();
            await Yeelight.TurnOnAsync(_deviceViewModel.Device, SmoothnessTrackBar.Value * 10);

            UpdateBulbState();
        }
Exemplo n.º 3
0
 private void SetBrightness()
 {
     if (_deviceViewModel.Device.Properties.FirstOrDefault(m => m.Key == "power").Value.ToString() == "off")
     {
         _ = Yeelight.TurnOnAsync(_deviceViewModel.Device);
     }
     _ = Yeelight.SetBrightnessAsync(_deviceViewModel.Device, BrightnessTrackBar.Value, SmoothnessTrackBar.Value * 10);
 }
Exemplo n.º 4
0
        private async void ToggleButton_Click(object sender, EventArgs e)
        {
            NotifyUser(ToggleButton, "Toggling");

            FlashBulb();
            await Yeelight.ToggleAsync(_deviceViewModel.Device);

            UpdateBulbState();
        }
Exemplo n.º 5
0
 private void SetColor()
 {
     if (_deviceViewModel.Device.Properties.FirstOrDefault(m => m.Key == "power").Value.ToString() == "off")
     {
         _ = Yeelight.TurnOnAsync(_deviceViewModel.Device, SmoothnessTrackBar.Value * 10);
     }
     _         = Yeelight.SetRGBAsync(_deviceViewModel.Device, ColorPickerComboBox.Value, SmoothnessTrackBar.Value * 10);
     _isColour = true;
 }
Exemplo n.º 6
0
 private void SetTemperature()
 {
     if (_deviceViewModel.Device.Properties.FirstOrDefault(m => m.Key == "power").Value.ToString() == "off")
     {
         _ = Yeelight.TurnOnAsync(_deviceViewModel.Device);
     }
     _         = Yeelight.SetColorTemperatureAsync(_deviceViewModel.Device, TemperatureTrackBar.Value, SmoothnessTrackBar.Value * 10);
     _isColour = false;
 }
Exemplo n.º 7
0
        /// <summary>
        /// 对设备进行操作
        /// </summary>
        /// <param name="type">操作类型</param>
        /// <param name="param">参数</param>
        /// <param name="device">设备</param>
        private async Task DeviceAction(string type, string param, Yeelight device)
        {
            try
            {
                // 更新设备信息
                await device.UpdateDeviceInfo();

                switch (Enum.Parse(typeof(ActionType), type))
                {
                case ActionType.PowerOn:
                    await device.SetPower(YeelightPower.on);

                    break;

                case ActionType.PowerOff:
                    await device.SetPower(YeelightPower.off);

                    break;

                case ActionType.BrightUp:
                    await device.SetBright(Math.Min(100, device.Bright + Convert.ToInt32(param)));

                    break;

                case ActionType.BrightDown:
                    await device.SetBright(Math.Max(1, device.Bright - Convert.ToInt32(param)));

                    break;

                case ActionType.SwitchColor:
                    var hsv = param.Split(',');
                    await device.SetHSV(Convert.ToInt32(Convert.ToDouble(hsv[0])), Convert.ToInt32(Convert.ToDouble(hsv[1]) * 100));

                    break;

                default:
                    break;
                }
            }
            catch (Exception ex)
            {
                await Log(string.Format("{0}\n{1}\n\n", ex.Message, ex.StackTrace));
            }
        }
Exemplo n.º 8
0
        /*
         *  Use async main in C#7.1 and after.
         *  Devices = await Yeelight.DiscoverDevices()
         *  instead of
         *  Devices = Yeelight.DiscoverDevices().Result
         */
        static void Main(string[] args)
        {
            Console.WriteLine("Discovering Devices...");

            List <Device> Devices = new List <Device>();

            //Search for devices in the local network
            Devices = Yeelight.DiscoverDevices().Result;

            foreach (var device in Devices)
            {
                Console.WriteLine("Device: {0}, Name: {1}, State: {2}", device[DeviceProperty.Id], device[DeviceProperty.Name], device[DeviceProperty.Power]);

                //Print when a property changes
                device.onPropertyChanged += (dp) => { Console.WriteLine("Property Changed: {0}", dp.ToString()); };
            }

            if (Devices.Count == 0)
            {
                Console.WriteLine("Couldn't find any devices. Make sure to enable lan control over yeelight app");
                return;
            }

            /*Command examples
             *
             * toggle 1 --Toggle light with index 1
             * rgb 0 --Set rgb value for light with index 0
             * bright 2 --Set brightness for light with index 2
             *
             */
            while (true)
            {
                Console.Write("\nCommand: ");
                var command = Console.ReadLine();

                string function = command.Split(' ')[0];
                int    mIndex   = 0;

                try
                {
                    mIndex = int.Parse(command.Split(' ')[1]);

                    if (mIndex < 0 || mIndex >= Devices.Count)
                    {
                        mIndex = 0;
                    }
                }
                catch
                {
                }

                Device mDevice = Devices[mIndex];


                if (function == "toggle")
                {
                    mDevice.Toggle();
                }
                else if (function == "blink")
                {
                    mDevice.Blink();
                }
                else if (function == "ct")
                {
                    Console.Write("\nNew Temperature: ");
                    int temp = int.Parse(Console.ReadLine());

                    mDevice.SetColorTemperature(temp);
                }
                else if (function == "rgb")
                {
                    Console.Write("\nNew Color(R,G,B): ");
                    string[] rgb = Console.ReadLine().Split(',');

                    mDevice.SetRgbColor(int.Parse(rgb[0]), int.Parse(rgb[1]), int.Parse(rgb[2]));
                }
                else if (function == "bright")
                {
                    Console.Write("\nNew Brightness: ");
                    int bright = int.Parse(Console.ReadLine());

                    mDevice.SetBrightness(bright);
                }
                else if (function == "test")
                {
                    Console.Write("\nTesting...");

                    mDevice.Toggle().WaitCmd(2000).SetBrightness(20).WaitCmd(5000).SetRgbColor(0, 255, 0).WaitCmd(5000).SetBrightness(5).SetColorTemperature(2000).WaitCmd(5000).Toggle();
                }
                else if (function == "name")
                {
                    Console.Write("\nCurrent name:{0}", mDevice[DeviceProperty.Name]);
                    Console.Write("\nNew name: ");
                    var newName = Console.ReadLine();

                    mDevice.SetName(newName);
                }
                else if (function == "status")
                {
                    Console.WriteLine("\nStatus:");

                    Console.WriteLine("Id: {0}", mDevice[DeviceProperty.Id]);
                    Console.WriteLine("Location: {0}", mDevice[DeviceProperty.Location]);
                    Console.WriteLine("Name: {0}", mDevice[DeviceProperty.Name]);
                    Console.WriteLine("State: {0}", mDevice[DeviceProperty.Power]);
                    Console.WriteLine("Brightness: {0}", mDevice[DeviceProperty.Brightness]);
                    Console.WriteLine("Color Mode: {0}", mDevice[DeviceProperty.ColorMode]);
                    Console.WriteLine("RGB: {0},{1},{2}", mDevice[DeviceProperty.RGB] >> 16, (Devices[0][DeviceProperty.RGB] >> 8) & 255, Devices[0][DeviceProperty.RGB] & 255);
                    Console.WriteLine("Color Temperature: {0} K", mDevice[DeviceProperty.ColorTemperature]);
                }
                else if (function == "quit")
                {
                    break;
                }
            }
        }
Exemplo n.º 9
0
 public Device(Yeelight rawDevice)
 {
     this.rawDevice = rawDevice;
 }
Exemplo n.º 10
0
 public Device(string rawDeviceInfo)
 {
     rawDevice = new Yeelight(rawDeviceInfo);
 }