private void UpdateMQTT(Grid parentGrid, TouchDevice touchDevice) { Point position = touchDevice.GetPosition(parentGrid); Rect touchDeviceRect = touchDevice.GetBounds(this); double?touchDeviceOrientation = touchDevice.GetOrientation(parentGrid); var message = new MQTT_SUR40_Message { Id = touchDevice.Id, Type = GetTouchDeviceTypeString(touchDevice), X = Math.Round(position.X, 2), Y = Math.Round(position.Y, 2), Orientation = Math.Round(touchDeviceOrientation ?? 0.0, 2), Width = Math.Round(touchDeviceRect.Width, 2), Height = Math.Round(touchDeviceRect.Height, 2) }; if (touchDevice.GetTagData() != TagData.None) { message.TagSchema = touchDevice.GetTagData().Schema.ToString("x8", CultureInfo.InvariantCulture); message.TagSeries = touchDevice.GetTagData().Series.ToString("x16", CultureInfo.InvariantCulture); message.TagExtendedValue = touchDevice.GetTagData().ExtendedValue.ToString("x16", CultureInfo.InvariantCulture); message.TagValue = touchDevice.GetTagData().Value.ToString("x16", CultureInfo.InvariantCulture); } MQTTClient.Instance.PublishMessage(message); }
/// <summary> /// Gets a string that describes the type of touch device on the surface /// </summary> /// <param name="touchDevice">the touch device to be examined</param> /// <returns>a string that describes the type of touch device</returns> private static string GetTouchDeviceTypeString(TouchDevice touchDevice) { if (touchDevice.GetTagData() != TagData.None) { return("Tag"); } if (touchDevice.GetIsFingerRecognized()) { return("Finger"); } return("Blob"); }
/// <summary> /// Update the text description with the most recent property values. Position /// the textbox so that it does not go offscreen (outside parentGrid). Also /// position the connecting line between the touch device and the textbox. /// </summary> /// <param name="parentGrid">the container for this diagram- /// description text will not go outside of this container's bounds</param> /// <param name="touchDevice">the touch device to diagram</param> /// <param name="showTouchDeviceInfo">Whether or not the touch device info will be visible</param> private void UpdateDescription(Grid parentGrid, TouchDevice touchDevice, bool showTouchDeviceInfo) { // Show or hide the touchDevice info based on showTouchDeviceInfo Description.Visibility = showTouchDeviceInfo ? Visibility.Visible : Visibility.Hidden; ConnectingLine.Visibility = showTouchDeviceInfo ? Visibility.Visible : Visibility.Hidden; if (!showTouchDeviceInfo) { // Don't need to do the calculations if info isn't going to be shown return; } Point position = touchDevice.GetPosition(parentGrid); Rect bounds = new Rect(0, 0, parentGrid.ActualWidth, parentGrid.ActualHeight); // Determine where around the touchDevice the description should be displayed. // The default position is above and to the left. bool isAbove = true; bool isLeft = true; // Description text for tags is different than non-tags double descriptionXDistance; bool isTag = touchDevice.GetIsTagRecognized(); if (isTag) { descriptionXDistance = tagDescriptionXDistance; } else { descriptionXDistance = nonTagDescriptionXDistance; } // Put description below touchDevice if default location is out of bounds. Rect upperLeftBounds = GetDescriptionBounds(position, isAbove, isLeft, descriptionXDistance, descriptionYDistance); if (upperLeftBounds.Top < bounds.Top) { isAbove = false; } // Put description to the right of the touchDevice if default location is out of bounds. if (upperLeftBounds.Left < bounds.Left) { isLeft = false; } // Calculate the final bounds that will be used for the textbox position // based on the updated isAbove and isLeft values. Rect finalBounds = GetDescriptionBounds(position, isAbove, isLeft, descriptionXDistance, descriptionYDistance); Canvas.SetLeft(Description, finalBounds.Left); Canvas.SetTop(Description, finalBounds.Top); // Set the justification of the type in the textbox based // on which side of the touchDevice the textbox is on. if (isLeft) { Description.TextAlignment = TextAlignment.Right; } else { Description.TextAlignment = TextAlignment.Left; } // Create the description string. StringBuilder descriptionText = new StringBuilder(); descriptionText.AppendLine(String.Format(CultureInfo.InvariantCulture, "RecognizedTypes: {0}", GetTouchDeviceTypeString(touchDevice))); descriptionText.AppendLine(String.Format(CultureInfo.InvariantCulture, "Id: {0}", touchDevice.Id)); // Use the "f1" format specifier to limit the amount of decimal positions shown. descriptionText.AppendLine(String.Format(CultureInfo.InvariantCulture, "X: {0}", position.X.ToString("f1", CultureInfo.InvariantCulture))); descriptionText.AppendLine(String.Format(CultureInfo.InvariantCulture, "Y: {0}", position.Y.ToString("f1", CultureInfo.InvariantCulture))); // Display "null" for Orientation if the touchDevice does not have an orientation value. string orientationString; double?touchDeviceOrientation = touchDevice.GetOrientation(parentGrid); if (touchDeviceOrientation == null) { orientationString = "null"; } else { orientationString = ((double)touchDeviceOrientation).ToString("f1", CultureInfo.InvariantCulture); } descriptionText.AppendLine(String.Format(CultureInfo.InvariantCulture, "Orientation: {0}", orientationString)); if (touchDevice.GetTagData() != TagData.None) { descriptionText.AppendLine("Schema: 0x" + touchDevice.GetTagData().Schema.ToString("x8", CultureInfo.InvariantCulture)); descriptionText.AppendLine("Series: 0x" + touchDevice.GetTagData().Series.ToString("x16", CultureInfo.InvariantCulture)); descriptionText.AppendLine("ExtendedValue: 0x" + touchDevice.GetTagData().ExtendedValue.ToString("x16", CultureInfo.InvariantCulture)); descriptionText.AppendLine("Value: 0x" + touchDevice.GetTagData().Value.ToString("x16", CultureInfo.InvariantCulture)); } // Update the description textbox. Description.Text = descriptionText.ToString(); // Update the line that connects the touchDevice to the description textbox. double x2; if (isLeft) { x2 = finalBounds.Right; } else { x2 = finalBounds.Left; } // Position (X1,Y1) is the center of the touchDevice. // Position (X2,Y2) is the edge of the description text box. ConnectingLine.X1 = position.X; ConnectingLine.Y1 = position.Y; ConnectingLine.X2 = x2; ConnectingLine.Y2 = finalBounds.Top + finalBounds.Height * 0.5; }