//private async void PowerToggleSwitch_Toggled(object sender, RoutedEventArgs e) //{ // if (((ToggleSwitch)sender).IsOn) // { // Helper.ShowProgressIndicator("Turning on..."); // foreach (LifxBulb bulb in StorageHelper.SelectedBulbs) // { // LifxPowerStateMessage psm = await bulb.SetPowerStateCommand(LifxPowerState.On); // await HandlePowerStateMessage(psm, bulb); // } // Helper.HideProgressIndicator(); // } // else // { // Helper.ShowProgressIndicator("Turning off..."); // SetControlState(false); // foreach (LifxBulb bulb in StorageHelper.SelectedBulbs) // { // LifxPowerStateMessage psm = await bulb.SetPowerStateCommand(LifxPowerState.Off); // await HandlePowerStateMessage(psm, bulb); // } // Helper.HideProgressIndicator(); // } //} private async void SyncBulbAppBarButton_Click(object sender, RoutedEventArgs e) { Helper.ShowProgressIndicator("Syncing..."); foreach (LifxBulb bulb in StorageHelper.SelectedBulbs) { LifxLightStatusMessage lsm = await bulb.GetLightStatusCommand(); PowerStateAppBarToggleButton.IsChecked = lsm.PowerState == LifxPowerState.On ? true : false; //PowerToggleSwitch.IsOn = lsm.PowerState == LifxPowerState.On ? true : false; if (StorageHelper.SelectedType == SelectionType.IndividualBulb) { MainPivot.Title = APP_HEADING_TEXT + lsm.Label; } bulb.Colour = new LifxColour() { Hue = lsm.Hue, Saturation = lsm.Saturation, Luminosity = lsm.Lumnosity, Kelvin = lsm.Kelvin }; bulb.Label = lsm.Label; bulb.Tags = lsm.Tags; bulb.IsOn = lsm.PowerState; } StorageHelper.SaveToStorage(); Helper.HideProgressIndicator(); }
private void HandleLightStatusMessage(LifxLightStatusMessage message, LifxBulb bulb) { if (message != null) { FadeTimeSlider.Value = message.Dim; bulb.Colour = new LifxColour() { Hue = message.Hue, Saturation = message.Saturation, Luminosity = message.Lumnosity, Kelvin = message.Kelvin }; bulb.Label = message.Label; bulb.Tags = message.Tags; bulb.IsOn = message.PowerState; StorageHelper.SaveToStorage(); if (firstLightStatusFlag) { UnBindValueChangedEventHandlers(); HueSlider.Value = message.Hue; SaturationSlider.Value = message.Saturation; LuminositySlider.Value = message.Lumnosity; KelvinSlider.Value = message.Kelvin; BindValueChangedEventHandlers(); firstLightStatusFlag = false; } } }
public LifxLightStatus GetLightStatus() { LifxGetLightStatusCommand command = new LifxGetLightStatusCommand(); LifxCommunicator.Instance.SendCommand(command, this); LifxLightStatusMessage lsMessage = (LifxLightStatusMessage)command.ReturnMessage; //HSLColor hslColor = new HSLColor((double)(lsMessage.Hue * 240 / 65535), (double)(lsMessage.Saturation * 240 / 65535), (double)(lsMessage.Lumnosity * 240 / 65535)); LifxColor color = new LifxColor(lsMessage.Hue, lsMessage.Saturation, lsMessage.Lumnosity, lsMessage.Kelvin); LifxLightStatus lightsStatus = new LifxLightStatus(color, lsMessage.PowerState, lsMessage.Dim, lsMessage.Label, lsMessage.Tags); return(lightsStatus); }
async void Instance_PanControllerFound(object sender, LifxPanController e) { StorageHelper.StorePanController(e); foreach (LifxBulb bulb in e.Bulbs) { LifxLightStatusMessage lightstatusmessage = await StorageHelper.GetBulb(bulb.UID).GetLightStatusCommand(); if (lightstatusmessage != null) { StorageHelper.GetBulb(lightstatusmessage.ReceivedData.TargetMac).Label = lightstatusmessage.Label; StorageHelper.GetBulb(lightstatusmessage.ReceivedData.TargetMac).Tags = lightstatusmessage.Tags; StorageHelper.GetBulb(lightstatusmessage.ReceivedData.TargetMac).IsOn = lightstatusmessage.PowerState; StorageHelper.GetBulb(lightstatusmessage.ReceivedData.TargetMac).Colour = new LifxColour() { Hue = lightstatusmessage.Hue, Luminosity = lightstatusmessage.Lumnosity, Saturation = lightstatusmessage.Saturation, Kelvin = lightstatusmessage.Kelvin }; await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { NextPageButton.IsEnabled = true; LooksGoodTextBlock.Visibility = Windows.UI.Xaml.Visibility.Visible; BulbListBox.Visibility = Windows.UI.Xaml.Visibility.Visible; BulbListBox.Items.Add(new ListBoxItem() { Content = (lightstatusmessage.Label + " - " + LifxHelper.ByteArrayToString(lightstatusmessage.ReceivedData.TargetMac)) as string }); }); } } StorageHelper.SaveToStorage(); await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { Helper.HideProgressIndicator(); }); }
private async Task HandlePowerStateMessage(LifxPowerStateMessage message, LifxBulb bulb) { if (message != null) { if (message.PowerState == LifxPowerState.On) { TogglePowerSwitch(true); SetControlState(true, true); LifxLightStatusMessage lightstatus = await bulb.GetLightStatusCommand(); HandleLightStatusMessage(lightstatus, bulb); } else if (message.PowerState == LifxPowerState.Off) { TogglePowerSwitch(false); //PowerToggleSwitch.IsEnabled = true; SetControlState(false); bulb.IsOn = LifxPowerState.Off; } } }
/// <summary> /// Sends command to a bulb /// </summary> /// <param name="command"></param> /// <param name="bulb">The bulb to send the command to.</param> /// <returns>Returns the response message. If the command does not trigger a response it will reurn null. </returns> public LifxReceivedMessage SendCommand(LifxCommand command, string macAddress, string panController, IPEndPoint endPoint) { if (!IsInitialized) { throw new InvalidOperationException("The communicator needs to be initialized before sending a command."); } UdpClient client = GetConnectedClient(command, endPoint); LifxDataPacket packet = new LifxDataPacket(command); packet.TargetMac = LifxHelper.StringToByteArray(macAddress); packet.PanControllerMac = LifxHelper.StringToByteArray(panController); client.Send(packet.PacketData, packet.PacketData.Length); DateTime commandSentTime = DateTime.Now; if (command.ReturnMessage == null) { return(null); } while ((DateTime.Now - commandSentTime).TotalMilliseconds < mTimeoutMilliseconds) { if (mIncomingQueue.Count != 0) { IncomingMessage mess = mIncomingQueue.Dequeue(); LifxDataPacket receivedPacket = mess.Data; if (receivedPacket.PacketType == LifxPANGatewayStateMessage.PACKET_TYPE) { //Panhandler identified LifxPANGatewayStateMessage panGateway = new LifxPANGatewayStateMessage(); panGateway.ReceivedData = receivedPacket; AddDiscoveredPanHandler(new LifxPanController( LifxHelper.ByteArrayToString(receivedPacket.TargetMac), mess.BulbAddress)); } else if (receivedPacket.PacketType == LifxLightStatusMessage.PACKET_TYPE && command.IsDiscoveryCommand) { //Panhandler identified LifxLightStatusMessage panGateway = new LifxLightStatusMessage(); panGateway.ReceivedData = receivedPacket; AddDiscoveredBulb( LifxHelper.ByteArrayToString(receivedPacket.TargetMac), LifxHelper.ByteArrayToString(receivedPacket.PanControllerMac)); } else if (receivedPacket.PacketType == command.ReturnMessage.PacketType) { command.ReturnMessage.ReceivedData = receivedPacket; mIncomingQueue.Clear(); return(command.ReturnMessage); } } Thread.Sleep(30); } if (command.IsDiscoveryCommand) { return(null); } if (command.RetryCount > 0) { command.RetryCount -= 1; //Recurssion return(SendCommand(command, macAddress, panController, endPoint)); } else { throw new TimeoutException("Did not get a reply from bulb in a timely fashion"); } }