/// <summary> /// Confirm button callback that will send updated data to the view model to be saved /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ConfirmButton_Click(object sender, RoutedEventArgs e) { if (currentSwitch != null) { currentSwitch.Topic = topicBox.Text; currentSwitch.Clickable = Convert.ToBoolean(ClickableCheckbox.IsChecked); Messenger.Default.Send(currentSwitch, "updateIcons"); Messenger.Default.Send((currentSwitch as object), "updateObject"); } else if (currentLabel != null) { currentLabel.Topic = topicBox.Text; currentLabel.Prefix = valueBox.Text; currentLabel.Postfix = postfixBox.Text; Messenger.Default.Send((currentLabel as object), "updateObject"); } else if (currentCamera != null) { currentCamera.Topic = topicBox.Text; Messenger.Default.Send((currentCamera as object), "updateObject"); } Messenger.Default.Send(new SimpleMessage { Type = SimpleMessage.MessageType.SettingsUpdated }); currentLabel = null; currentSwitch = null; currentCamera = null; }
/// <summary> /// Callback to when the MQTT message is recieved and should be processed /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) { string ReceivedMessage = Encoding.UTF8.GetString(e.Message); SubscribedLabel foundedLabel = subscribedLabels.Find(x => x.Topic == e.Topic); SubscribedSwitch foundedSwitch = subscribedSwitches.Find(x => x.Topic == e.Topic); SubscribedCamera foundedCamera = subscribedCameras.Find(x => x.Topic == e.Topic); if (foundedLabel != null) { Messenger.Default.Send(new UpdateLabelMessage { subscribedLabel = foundedLabel, value = ReceivedMessage }, "ChangeLabelValue"); } else if (foundedSwitch != null) { if (ReceivedMessage == "1") { foundedSwitch.State = true; } else { foundedSwitch.State = false; } Application.Current.Dispatcher.Invoke(new Action(() => { ChangeIconSwitch(foundedSwitch.UniqueName); })); } else if (foundedCamera != null) { byte[] imageData = e.Message; Application.Current.Dispatcher.Invoke(new Action(() => { ChangeImage(imageData, foundedCamera); })); Messenger.Default.Send(foundedCamera, "updateCameraImage"); } }
/// <summary> /// This function will load the data about icons for the switch /// </summary> /// <param name="subSwitch">Switch whitch icons should be set</param> private void SetIconValues(SubscribedSwitch subSwitch) { if (subSwitch.OnIconUri != null && subSwitch.OffIconUri != null) { OnIcon = new BitmapImage(new Uri(subSwitch.OnIconUri)); OffIcon = new BitmapImage(new Uri(subSwitch.OffIconUri)); } }
/// <summary> /// Send clicked switch state over MQTT /// </summary> /// <param name="name">Switch control clicked</param> private void SendSwitchState(string name) { SubscribedSwitch subscribedSwitch = subscribedSwitches.Find(x => x.UniqueName == name); try { client.Publish(subscribedSwitch.Topic, Encoding.UTF8.GetBytes((!subscribedSwitch.State).ToString()), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); } catch { Debug.WriteLine("Error in publishing"); } }
/// <summary> /// This function will take care of adding the new switch element /// </summary> /// <param name="sender"></param> private void AddSwitch(object sender) { SimpleSwitch simpleSwitchControl = new SimpleSwitch(); Random RNG = new Random(); int length = 16; var rString = ""; for (var i = 0; i < length; i++) { rString += ((char)(RNG.Next(1, 26) + 64)).ToString().ToLower(); } SubscribedSwitch subscribedSwitch = new SubscribedSwitch { Id = Guid.NewGuid(), UniqueName = rString, SimpleSwitch = simpleSwitchControl, Topic = "", Clickable = true }; simpleSwitchControl.Name = subscribedSwitch.UniqueName; Grid grid = subscribedSwitch.SimpleSwitch.Content as Grid; CheckBox box = grid.Children[0] as CheckBox; Image icon = grid.Children[1] as Image; Binding commandBinding = new Binding("SwitchClickedCommand"); BindingOperations.SetBinding(icon.InputBindings[0], InputBinding.CommandProperty, commandBinding); icon.InputBindings[0].CommandParameter = subscribedSwitch.UniqueName; Binding myBinding = new Binding("Switches[" + subscribedSwitch.Id + "]"); BindingOperations.SetBinding(icon, Image.SourceProperty, myBinding); subscribedSwitches.Add(subscribedSwitch); simpleSwitchControl.MouseDoubleClick += object_MouseDoubleClick; simpleSwitchControl.VerticalAlignment = VerticalAlignment.Center; simpleSwitchControl.HorizontalAlignment = HorizontalAlignment.Center; simpleSwitchControl.Margin = new Thickness(0, 0, 0, 0); controlCanvas.Children.Add(simpleSwitchControl); Messenger.Default.Send(subscribedSwitch, "switchAdd"); }
/// <summary> /// This function will update the data about icons for the switch /// </summary> /// <param name="subSwtich">Switch whitch icons should be updated</param> private void UpdateSwitchIcons(SubscribedSwitch subSwtich) { if (OnIcon != null && OffIcon != null) { subSwtich.OnIconUri = Directory.GetCurrentDirectory() + @"\images\" + (OnIcon.UriSource.AbsolutePath.Split('/')[OnIcon.UriSource.AbsolutePath.Split('/').Length - 1]); subSwtich.OffIconUri = Directory.GetCurrentDirectory() + @"\images\" + (OffIcon.UriSource.AbsolutePath.Split('/')[OnIcon.UriSource.AbsolutePath.Split('/').Length - 1]); Messenger.Default.Send(subSwtich.UniqueName, "getSwitchAndChangeValue"); } else { MessageBox.Show("Please choose both icons"); } }
/// <summary> /// Change switch icon based on the switch state /// </summary> /// <param name="name">Name of the switch control that should be changed</param> private void ChangeIconSwitch(string name) { SubscribedSwitch subscribedSwitch = subscribedSwitches.Find(x => x.UniqueName == name); if (subscribedSwitch.State) { Messenger.Default.Send(new UpdateSwitchMessage { subscribedSwitch = subscribedSwitch, value = subscribedSwitch.OnIconUri }, "ChangeSwitchValue"); } else { Messenger.Default.Send(new UpdateSwitchMessage { subscribedSwitch = subscribedSwitch, value = subscribedSwitch.OffIconUri }, "ChangeSwitchValue"); } }
/// <summary> /// Load switch and add all needed bindings /// </summary> /// <param name="switchData">All needed data for the switch to load</param> private void LoadSwitch(Tuple <SimpleSwitch, string> switchData) { SimpleSwitch subSwitch = switchData.Item1; string uniqueName = switchData.Item2; SubscribedSwitch subscribedSwitch = subscribedSwitches.Find(x => x.UniqueName == uniqueName); subscribedSwitch.SimpleSwitch = subSwitch; Messenger.Default.Send(subscribedSwitch, "addBindingSwitch"); Grid grid = subscribedSwitch.SimpleSwitch.Content as Grid; CheckBox box = grid.Children[0] as CheckBox; Image icon = grid.Children[1] as Image; if (subscribedSwitch.Clickable) { Binding commandBinding = new Binding("SwitchClickedCommand"); BindingOperations.SetBinding(icon.InputBindings[0], InputBinding.CommandProperty, commandBinding); } icon.InputBindings[0].CommandParameter = subscribedSwitch.UniqueName; Messenger.Default.Send(uniqueName, "getSwitchAndChangeValue"); Binding myBinding = new Binding("Switches[" + subscribedSwitch.Id + "]"); BindingOperations.SetBinding(icon, Image.SourceProperty, myBinding); if (subscribedSwitch.State) { Messenger.Default.Send(new UpdateSwitchMessage { subscribedSwitch = subscribedSwitch, value = subscribedSwitch.OnIconUri }, "ChangeSwitchValue"); } else { Messenger.Default.Send(new UpdateSwitchMessage { subscribedSwitch = subscribedSwitch, value = subscribedSwitch.OffIconUri }, "ChangeSwitchValue"); } }
/// <summary> /// Add switch control to the array /// </summary> /// <param name="subSwitch">Switch that should be added</param> private void AddSwitch(SubscribedSwitch subSwitch) { subscribedSwitches.Add(subSwitch); }
/// <summary> /// /// </summary> /// <param name="updatedObject"></param> void CreateOrUpdateSubscribedDevice(object updatedObject) { string topicString = ""; Debug.WriteLine(updatedObject.GetType().Name); switch (updatedObject.GetType().Name) { case "SubscribedLabel": SubscribedLabel updatedLabel = updatedObject as SubscribedLabel; SubscribedLabel foundedLabel = subscribedLabels.Find(x => x.Label == updatedLabel.Label); topicString = updatedLabel.Topic; break; case "SubscribedSwitch": SubscribedSwitch updatedSwitch = updatedObject as SubscribedSwitch; SubscribedSwitch foundedSwitch = subscribedSwitches.Find(x => x.SimpleSwitch == updatedSwitch.SimpleSwitch); topicString = foundedSwitch.Topic; if (!foundedSwitch.Clickable) { SimpleSwitch simpleSwitch = foundedSwitch.SimpleSwitch; Grid grid = simpleSwitch.Content as Grid; Image icon = grid.Children[1] as Image; icon.InputBindings[0].Command = null; } else { SimpleSwitch simpleSwitch = foundedSwitch.SimpleSwitch; Grid grid = simpleSwitch.Content as Grid; Image icon = grid.Children[1] as Image; Binding commandBinding = new Binding("SwitchClickedCommand"); BindingOperations.SetBinding(icon.InputBindings[0], InputBinding.CommandProperty, commandBinding); } break; case "SubscribedCamera": SubscribedCamera updatedCamera = updatedObject as SubscribedCamera; SubscribedCamera foundedCamera = subscribedCameras.Find(x => x.Camera == updatedCamera.Camera); topicString = foundedCamera.Topic; break; default: break; } if (topicString != "" && !subscribedStrings.Contains(topicString)) { subscribedStrings.Add(topicString); } if (subscribedStrings.Count != 0) { byte[] qosArray = new byte[subscribedStrings.Count]; Array.Fill(qosArray, MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE); client.Subscribe(subscribedStrings.ToArray(), qosArray); } }
/// <summary> /// Load the selected element data and show it in the view /// </summary> /// <param name="node">Element that was selected</param> private void GetValues(object node) { switch (node.GetType().Name) { case "SubscribedLabel": currentLabel = (SubscribedLabel)node; topicBox.Text = currentLabel.Topic; valueBox.Visibility = Visibility.Visible; valueLabel.Visibility = Visibility.Visible; postfixLabel.Visibility = Visibility.Visible; postfixBox.Visibility = Visibility.Visible; ClickableCheckbox.Visibility = Visibility.Hidden; OnIcon.Visibility = Visibility.Hidden; OffIcon.Visibility = Visibility.Hidden; OnButton.Visibility = Visibility.Hidden; OffButton.Visibility = Visibility.Hidden; valueBox.Text = currentLabel.Prefix; postfixBox.Text = currentLabel.Postfix; break; case "SubscribedSwitch": currentSwitch = (SubscribedSwitch)node; topicBox.Text = currentSwitch.Topic; valueBox.Visibility = Visibility.Hidden; valueLabel.Visibility = Visibility.Hidden; postfixLabel.Visibility = Visibility.Hidden; postfixBox.Visibility = Visibility.Hidden; ClickableCheckbox.Visibility = Visibility.Visible; ClickableCheckbox.IsChecked = currentSwitch.Clickable; OnIcon.Visibility = Visibility.Visible; OffIcon.Visibility = Visibility.Visible; OnButton.Visibility = Visibility.Visible; OffButton.Visibility = Visibility.Visible; Messenger.Default.Send(currentSwitch, "switchInfo"); break; case "SubscribedCamera": currentCamera = (SubscribedCamera)node; topicBox.Text = currentCamera.Topic; valueBox.Visibility = Visibility.Hidden; valueLabel.Visibility = Visibility.Hidden; ClickableCheckbox.Visibility = Visibility.Hidden; postfixLabel.Visibility = Visibility.Hidden; postfixBox.Visibility = Visibility.Hidden; OnIcon.Visibility = Visibility.Hidden; OffIcon.Visibility = Visibility.Hidden; OnButton.Visibility = Visibility.Hidden; OffButton.Visibility = Visibility.Hidden; break; default: break; } }