/// <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> /// 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> /// Function that will take care of deleting selected label control /// </summary> /// <param name="deletedLabel">Label that should be deleted</param> private void DeleteLabel(SubscribedLabel deletedLabel) { SubscribedLabel foundedLabel = subscribedLabels.Find(x => x.Label == deletedLabel.Label); foundedLabel = deletedLabel; controlCanvas.Children.Remove(foundedLabel.Label); subscribedLabels.Remove(foundedLabel); HideConfig(); }
/// <summary> /// Load label and add all needed bindings /// </summary> /// <param name="labelData">All the data needed for the label to load</param> private void LoadLabel(Tuple <SimpleLabel, string> labelData) { SimpleLabel label = labelData.Item1; string uniqueName = labelData.Item2; SubscribedLabel subscribedLabel = subscribedLabels.Find(x => x.UniqueName == uniqueName); subscribedLabel.Label = label; Binding myBinding = new Binding("Labels[" + subscribedLabel.Id + "]"); subscribedLabel.Label.Label.SetBinding(ContentControl.ContentProperty, myBinding); Messenger.Default.Send(new UpdateLabelMessage { subscribedLabel = subscribedLabel, value = "null" }, "ChangeLabelValue"); }
/// <summary> /// This function will take care of adding the new label element /// </summary> /// <param name="sender"></param> private void AddLabel(object sender) { SimpleLabel labelControl = new SimpleLabel(); 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(); } SubscribedLabel _subscribedLabel = new SubscribedLabel { Id = Guid.NewGuid(), UniqueName = rString, Label = labelControl, Topic = "" }; Label label = labelControl.Label; label.Name = _subscribedLabel.UniqueName; Binding myBinding = new Binding("Labels[" + _subscribedLabel.Id + "]"); label.SetBinding(ContentProperty, myBinding); Messenger.Default.Send(new UpdateLabelMessage { subscribedLabel = _subscribedLabel, value = "Waiting for first Value..." }, "ChangeLabelValue"); labelControl.MouseDoubleClick += object_MouseDoubleClick; labelControl.VerticalAlignment = VerticalAlignment.Center; labelControl.HorizontalAlignment = HorizontalAlignment.Center; labelControl.Margin = new Thickness(0, 30, 0, 0); controlCanvas.Children.Add(labelControl); Messenger.Default.Send(_subscribedLabel, "labelAdd"); }
/// <summary> /// Add label control to the array /// </summary> /// <param name="label">Label that should be added</param> private void AddLabel(SubscribedLabel label) { subscribedLabels.Add(label); }
/// <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; } }