internal override void Update(TradfriDevice device)
        {
            if (DeviceType == DeviceType.ControlOutlet)
            {
                if (device?.Control == null || device.Control.Count == 0)
                {
                    DriverContext.Logger.LogDebug($"Invalid device received, no control outlet found");
                    return;
                }

                _value = device.Control[0].State == Bool.True;
                DispatchValue(_value);
            }
            else if (DeviceType == DeviceType.Light)
            {
                if (device?.LightControl == null || device.LightControl.Count == 0)
                {
                    DriverContext.Logger.LogDebug($"Invalid device received, no control outlet found");
                    return;
                }

                _value = device.LightControl[0].State == Bool.True;
                DispatchValue(_value);
            }
        }
Exemple #2
0
        /// <summary>
        /// Queries a single device by its unique device id
        /// </summary>
        /// <param name="deviceId">Unique device id</param>
        /// <returns></returns>
        public static TradfriDevice GetDevice(int deviceId)
        {
            _coapClient.UriPath = $"{CommandConstants.UniqueDevices}/{deviceId}";
            Response response = _coapClient.Get();

            return(TradfriDevice.Parse(response.PayloadString));
        }
Exemple #3
0
 private void Update(TradfriDevice device)
 {
     foreach (var att in _devices)
     {
         att.Update(device);
     }
 }
        /// <summary>
        /// Set Position for Blind
        /// </summary>
        /// <param name="device">An <see cref="TradfriDevice"/></param>
        /// <param name="position">Position (0-100)</param>
        /// <returns></returns>
        public async Task SetBlind(TradfriDevice device, int position)
        {
            await SetBlind(device.ID, position);

            if (HasBlind(device))
            {
                device.Blind[0].Position = position;
            }
        }
        /// <summary>
        /// Changes the color of the light device
        /// </summary>
        /// <param name="device">A <see cref="TradfriDevice"/></param>
        /// <param name="value">A color from the <see cref="TradfriColors"/> class</param>
        /// <param name="transition">An optional transition duration, defaults to null (no transition)</param>
        /// <returns></returns>
        public async Task SetColor(TradfriDevice device, string value, int?transition = null)
        {
            await SetColor(device.ID, value, transition);

            if (HasLight(device))
            {
                device.LightControl[0].ColorHex = value;
            }
        }
Exemple #6
0
 public async Task Setup()
 {
     BaseSetup();
     controller = tradfriController.DeviceController;
     devices    = new List <TradfriDevice>(await tradfriController.GatewayController.GetDeviceObjects());
     lights     = devices.Where(i => i.DeviceType.Equals(DeviceType.Light)).ToList();
     light      = lights.FirstOrDefault();
     colorLight = lights.FirstOrDefault(i => i.LightControl != null && i.LightControl[0]?.ColorHex != null);
 }
Exemple #7
0
        /// <summary>
        /// Turns a specific light on or off
        /// </summary>
        /// <param name="device">A <see cref="TradfriDevice"/></param>
        /// <param name="state">On (True) or Off(false)</param>
        /// <returns></returns>
        public async Task SetLight(TradfriDevice device, bool state)
        {
            await SetLight(device.ID, state);

            if (HasLight(device))
            {
                device.LightControl[0].State = state ? Bool.True : Bool.False;
            }
        }
Exemple #8
0
        /// <summary>
        /// Changes the color of the light device
        /// </summary>
        /// <param name="device">A <see cref="TradfriDevice"/></param>
        /// <param name="value">A color from the <see cref="TradfriColors"/> class</param>
        /// <returns></returns>
        public async Task SetColor(TradfriDevice device, string value)
        {
            await SetColor(device.ID, value);

            if (HasLight(device))
            {
                device.LightControl[0].ColorHex = value;
            }
        }
        /// <summary>
        /// Acquires TradFriDevice object
        /// </summary>
        /// <param name="refresh">If set to true, than it will ignore existing cached value and ask the gateway for the object</param>
        /// <returns></returns>
        public TradfriDevice GetTradFriDevice(bool refresh = false)
        {
            if (!refresh && _device != null)
            {
                return(_device);
            }
            var data = Get();

            _device = JsonConvert.DeserializeObject <TradfriDevice>(data.PayloadString);
            return(_device);
        }
Exemple #10
0
 private void btnBrightness_Click(object sender, EventArgs e)
 {
     // set brightness of the lights for selected rows in grid (rows, not cells)
     for (int index = 0; index < dgvDevices.SelectedRows.Count; index++)
     {
         TradfriDevice currentSelectedDevice = (TradfriDevice)(dgvDevices.SelectedRows[index]).DataBoundItem;
         if (currentSelectedDevice.DeviceType.Equals(DeviceType.Light))
         {
             tradfriController.DeviceController.SetDimmer(currentSelectedDevice, trbBrightness.Value * 10 * 254 / 100);
         }
     }
 }
Exemple #11
0
 private void SetSelectedLights(bool state)
 {
     // turn off the lights for selected rows in grid (rows, not cells)
     for (int index = 0; index < dgvDevices.SelectedRows.Count; index++)
     {
         TradfriDevice currentSelectedDevice = (TradfriDevice)(dgvDevices.SelectedRows[index]).DataBoundItem;
         if (currentSelectedDevice.DeviceType.Equals(DeviceType.Light))
         {
             tradfriController.DeviceController.SetLight(currentSelectedDevice, state);
         }
     }
 }
        /// <summary>
        /// Changes the color of the light device
        /// </summary>
        /// <param name="device">A <see cref="TradfriDevice"/></param>
        /// <param name="r">Red component, 0-255</param>
        /// <param name="g">Green component, 0-255</param>
        /// <param name="b">Blue component, 0-255</param>
        /// <returns></returns>
        public async Task SetColor(TradfriDevice device, int r, int g, int b, int?transition = null)
        {
            (int x, int y) = ColorExtension.CalculateCIEFromRGB(r, g, b);
            int intensity = ColorExtension.CalculateIntensity(r, g, b);

            await SetColor(device.ID, x, y, intensity, transition);

            if (HasLight(device))
            {
                device.LightControl[0].ColorX = x;
                device.LightControl[0].ColorY = y;
            }
        }
        /// <summary>
        /// Observes a device and gets update notifications
        /// </summary>
        /// <param name="callback">Action to take for each device update</param>
        /// <param name="error">Action to take on internal error</param>
        /// <returns></returns>
        public CoapObserveRelation ObserveDevice(CoapClient cc, TradfriDevice device, Action <TradfriDevice> callback, Action <CoapClient.FailReason> error = null)
        {
            Action <Response> update = delegate(Response response)
            {
                OnDeviceObservationUpdate(device, response, callback);
            };

            return(cc.Observe(
                       $"/{(int)TradfriConstRoot.Devices}/{device.ID}",
                       update,
                       error
                       ));
        }
Exemple #14
0
        private void btnColor_Click(object sender, EventArgs e)
        {
            PropertyInfo propertyInfo = cmbColors.Items[0].GetType().GetProperty("ColorId");

            // set color of the lights for selected rows in grid (rows, not cells)
            for (int index = 0; index < dgvDevices.SelectedRows.Count; index++)
            {
                TradfriDevice currentSelectedDevice = (TradfriDevice)(dgvDevices.SelectedRows[index]).DataBoundItem;
                if (currentSelectedDevice.DeviceType.Equals(DeviceType.Light))
                {
                    tradfriController.DeviceController.SetColor(currentSelectedDevice, (string)propertyInfo.GetValue(cmbColors.SelectedItem, null));
                }
            }
        }
Exemple #15
0
        public List <TradfriDevice> LoadDevices()
        {
            var ret = new List <TradfriDevice>();
            GatewayController gwc = new GatewayController(_client);

            foreach (long deviceId in gwc.GetDevices())
            {
                DeviceController dc     = new DeviceController(deviceId, this);
                TradfriDevice    device = dc.GetTradFriDevice();
                ret.Add(device);
            }

            return(ret);
        }
        /// <summary>
        /// Changes the color of the light device based on Hue and Saturation values
        /// </summary>
        /// <param name="device">A <see cref="TradfriDevice"/></param>
        /// <param name="hue">Hue component of the color, 0-65535</param>
        /// <param name="saturation">Y component of the color, 0-65000</param>
        /// <param name="value">Optional Dimmer, 0-254</param>
        /// <param name="transition">An optional transition duration, defaults to null (no transition)</param>
        /// <returns></returns>
        public async Task SetColorHSV(TradfriDevice device, int hue, int saturation, int?value, int?transition = null)
        {
            await SetColorHSV(device.ID, hue, saturation, value, transition);

            if (HasLight(device))
            {
                device.LightControl[0].ColorHue        = hue;
                device.LightControl[0].ColorSaturation = saturation;
                if (value != null)
                {
                    device.LightControl[0].Dimmer = (int)value;
                }
            }
        }
Exemple #17
0
        /// <summary>
        /// Observes a device and gets update notifications
        /// </summary>
        /// <param name="device">Device on which you want to be notified</param>
        /// <param name="callback">Action to take for each device update</param>
        public void ObserveDevice(TradfriDevice device, Action <TradfriDevice> callback)
        {
            Action <Response> update = (Response response) =>
            {
                if (!string.IsNullOrWhiteSpace(response?.PayloadString))
                {
                    device = JsonConvert.DeserializeObject <TradfriDevice>(response.PayloadString);
                    callback.Invoke(device);
                }
            };

            // this specific combination of parameter values is handled in TradfriController's HandleRequest for Observable
            HandleRequest($"/{(int)TradfriConstRoot.Devices}/{device.ID}", Call.GET, null, null, update, HttpStatusCode.Continue);
        }
 private void btnRenameDevice_Click(object sender, EventArgs e)
 {
     try
     {
         TradfriDevice currentSelectedDevice = (TradfriDevice)dgvDevices.SelectedRows[0].DataBoundItem;
         tradfriController.DeviceController.RenameTradfriDevice(currentSelectedDevice).Wait();
         MessageBox.Show("Done", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.InnerException.Message
                         + Environment.NewLine
                         + "First change the Name in column, leave the row selected and press the 'Rename' button."
                         , "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Exemple #19
0
 public static ApiTradfriDevice Map(TradfriDevice device)
 {
     return(device != null ? new ApiTradfriDevice
     {
         CreatedAt = device.CreatedAt,
         DeviceType = (ApiDeviceType)device.DeviceType,
         ID = device.ID,
         Info = Map(device.Info),
         LastSeen = device.LastSeen,
         LightControl = device.LightControl?.Select(lc => Map(lc)).ToList(),
         Name = device.Name,
         OtaUpdateState = device.OtaUpdateState,
         ReachableState = device.ReachableState,
         RootSwitch = device.RootSwitch?.Select(rs => Map(rs)).ToList()
     } : null);
 }
Exemple #20
0
        private void btnRGBColor_Click(object sender, EventArgs e)
        {
            if (colorDlg.ShowDialog() == DialogResult.OK)
            {
                Color clr = colorDlg.Color;

                // set color of the lights for selected rows in grid (rows, not cells)
                for (int index = 0; index < dgvDevices.SelectedRows.Count; index++)
                {
                    TradfriDevice currentSelectedDevice = (TradfriDevice)(dgvDevices.SelectedRows[index]).DataBoundItem;
                    if (currentSelectedDevice.DeviceType.Equals(DeviceType.Light))
                    {
                        tradfriController.DeviceController.SetColor(currentSelectedDevice, clr.R, clr.G, clr.B);
                    }
                }
            }
        }
Exemple #21
0
        /// <summary>
        /// Observes a device and gets update notifications
        /// </summary>
        /// <param name="device">Device on which you want to be notified</param>
        /// <param name="callback">Action to take for each device update</param>
        public void ObserveDevice(TradfriDevice device, Action <TradfriDevice> callback)
        {
            Action <Response> update = (Response response) =>
            {
                if (!string.IsNullOrWhiteSpace(response?.PayloadString))
                {
                    device = JsonConvert.DeserializeObject <TradfriDevice>(response.PayloadString);
                    callback.Invoke(device);
                }
            };

            _ = MakeRequest(new WatchRequest($"/{(int)TradfriConstRoot.Devices}/{device.ID}")
            {
                EventHandler       = update,
                RequestHandler     = (resp) => { },
                ExpectedStatusCode = System.Net.HttpStatusCode.OK
            });
        }
Exemple #22
0
 private void dgvDevices_SelectionChanged(object sender, EventArgs e)
 {
     if (dgvDevices.SelectedRows.Count > 0)
     {
         TradfriDevice firstSelectedLight = (TradfriDevice)dgvDevices.SelectedRows[0].DataBoundItem;
         if (firstSelectedLight.DeviceType.Equals(DeviceType.Light))
         {
             trbBrightness.Value = Convert.ToInt16(firstSelectedLight.LightControl[0].Dimmer * (double)10 / 254);
         }
         if (!loadingSelectedRows)
         {
             userData.SelectedDevices = new List <long>();
             foreach (object item in dgvDevices.SelectedRows)
             {
                 userData.SelectedDevices.Add(((TradfriDevice)((DataGridViewRow)item).DataBoundItem).ID);
             }
             File.WriteAllText(UserAppDataFilePath, JsonConvert.SerializeObject(userData));
         }
     }
 }
Exemple #23
0
        /// <summary>
        /// Set Dimmer for Light Device
        /// </summary>
        /// <param name="device">A <see cref="TradfriDevice"/></param>
        /// <param name="value">Dimmer intensity (0-255)</param>
        public async Task SetDimmer(TradfriDevice device, int value)
        {
            await SetDimmer(device.ID, value);

            device.LightControl[0].Dimmer = value;
        }
Exemple #24
0
 private bool HasLight(TradfriDevice device)
 {
     return(device?.LightControl != null);
 }
 private void OnDeviceObservationUpdate(TradfriDevice device, Response response, Action <TradfriDevice> callback)
 {
     device = JsonConvert.DeserializeObject <TradfriDevice>(response.PayloadString);
     callback.Invoke(device);
 }
Exemple #26
0
 internal override void Update(TradfriDevice device)
 {
     _value = device.LightControl[0].Dimmer;
     DispatchValue(_value);
 }
Exemple #27
0
 internal abstract void Update(TradfriDevice device);
 private static bool HasBlind(TradfriDevice device)
 {
     return(device?.Blind != null);
 }
 private static bool HasControl(TradfriDevice device)
 {
     return(device?.Control != null);
 }
        /// <summary>
        /// Set Dimmer for Light Device
        /// </summary>
        /// <param name="device">A <see cref="TradfriDevice"/></param>
        /// <param name="value">Dimmer intensity (0-255)</param>
        /// <param name="transition">An optional transition duration, defaults to null (no transition)</param>
        public async Task SetDimmer(TradfriDevice device, int value, int?transition = null)
        {
            await SetDimmer(device.ID, value, transition);

            device.LightControl[0].Dimmer = value;
        }