/// <summary> /// This function will update the camera image but only if some camera is hovered /// </summary> /// <param name="cameraMessage">The message with the camera image</param> void ChangeCameraImage(SubscribedCamera cameraMessage) { if (currentCamera == cameraMessage) { CameraLastImage = currentCamera.lastImage; } }
/// <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> /// Function that will show the last recieved image next to the hovered camera element /// </summary> /// <param name="cameraObject"Camera element that is hovered over></param> void CameraMouseOver(SubscribedCamera cameraObject) { if (cameraObject.lastImage != null) { currentCamera = cameraObject; ImagePosition = new Thickness(cameraObject.Camera.Margin.Left + 300, cameraObject.Camera.Margin.Top + 300, cameraObject.Camera.Margin.Right, cameraObject.Camera.Margin.Bottom); CameraLastImage = cameraObject.lastImage; } }
/// <summary> /// Load camera /// </summary> /// <param name="cameraData">All the data needed for the camera to load</param> private void LoadCamera(Tuple <SimpleCamera, string> cameraData) { SimpleCamera camera = cameraData.Item1; string uniqueName = cameraData.Item2; SubscribedCamera subscribedCamera = subscribedCameras.Find(x => x.UniqueName == uniqueName); subscribedCamera.Camera = camera; Grid grid = subscribedCamera.Camera.Content as Grid; Image cameraImage = grid.Children[0] as Image; cameraImage.Name = subscribedCamera.UniqueName + "_image"; }
/// <summary> /// Change the shown camera image /// </summary> /// <param name="img">Image in byte array</param> /// <param name="camera">Camera whitch image should be changed</param> void ChangeImage(byte[] img, SubscribedCamera camera) { var image = new BitmapImage(); using (var mem = new MemoryStream(img)) { mem.Position = 0; image.BeginInit(); image.CreateOptions = BitmapCreateOptions.PreservePixelFormat; image.CacheOption = BitmapCacheOption.OnLoad; image.UriSource = null; image.StreamSource = mem; image.EndInit(); } camera.lastImage = image; }
/// <summary> /// This function will take care of adding the new camera element /// </summary> /// <param name="sender"></param> private void AddCamera(object sender) { SimpleCamera simpleCamera = new SimpleCamera(); 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(); } SubscribedCamera subscribedCamera = new SubscribedCamera { Id = Guid.NewGuid(), UniqueName = rString, Camera = simpleCamera, Topic = "" }; simpleCamera.Name = subscribedCamera.UniqueName; Grid grid = subscribedCamera.Camera.Content as Grid; Image camera = grid.Children[0] as Image; camera.Name = subscribedCamera.UniqueName + "_image"; subscribedCameras.Add(subscribedCamera); simpleCamera.MouseDoubleClick += object_MouseDoubleClick; simpleCamera.VerticalAlignment = VerticalAlignment.Center; simpleCamera.HorizontalAlignment = HorizontalAlignment.Center; simpleCamera.Margin = new Thickness(0, 0, 0, 0); controlCanvas.Children.Add(simpleCamera); Messenger.Default.Send(subscribedCamera, "cameraAdd"); }
/// <summary> /// When the camera hover is done hide the image /// </summary> /// <param name="sender"></param> void CameraMouseOverLeave(string sender) { currentCamera = null; CameraLastImage = null; }
/// <summary> /// Add camera control to the array /// </summary> /// <param name="subCamera">Camera that should be added</param> private void AddCamera(SubscribedCamera subCamera) { subscribedCameras.Add(subCamera); }
/// <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> /// Camera control mouse hover /// </summary> /// <param name="name">Name of the camera control hovered</param> void CameraMouseOver(string name) { SubscribedCamera foundedCamera = subscribedCameras.Find(x => x.UniqueName == (name.Remove(name.Length - 6))); Messenger.Default.Send(foundedCamera, "cameraMouseOver"); }
/// <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; } }