///////////////////////////////////////////////////////////////////////////// // A function responsible for decoding and reacting to received UDP data. private bool InterpreteData(string message) { if (!string.IsNullOrEmpty(message)) { message = EncodeUtilities.StripSplitter(message); if (this.lastMessage != message) { this.lastMessage = message; #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "New message found: " + message); #endif string[] messageComponents = message.Split(new string[] { EncodeUtilities.headerSplitter }, 2, StringSplitOptions.RemoveEmptyEntries); if (messageComponents.Length > 1) { string header = messageComponents[0], content = messageComponents[1]; #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Header: " + header + ", content: " + content); #endif if (header == "MESHSTREAMING") { InterpreteMesh(content, SourceType.UDP); return(true); } else if (header == "CONTROLLER") { InterpreteRobotController(content); return(true); } else if (header == "HOLOTAG") { InterpreteTag(content); return(true); } else if (header == "IPADDRESS") { InterpreteIPAddress(content); return(true); } else { #if DEBUGWARNING DebugUtilities.UniversalWarning(this.sourceName, "Header Not Recognized"); #endif } } else { #if DEBUGWARNING DebugUtilities.UniversalWarning(this.sourceName, "Improper message"); #endif } } } return(true); // Since we have one interpreter anyway }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { // Get inputs. Connection connect = null; if (!DA.GetData(0, ref connect)) { return; } ////////////////////////////////////////////////////// // Process data. if (connect.status) { // If connection open start acting. if (!UIReceiver.flagProcessed) { UIReceiver.flagProcessed = true; // Send local IPAddress for device to communicate back. byte[] bytes = EncodeUtilities.EncodeData("IPADDRESS", NetworkUtilities.LocalIPAddress(), out string currentMessage); connect.udpSender.Send(bytes); bool success = connect.udpSender.success; UniversalDebug("Sent local IP."); } // Prepare to receive UI data. try { if (!connect.udpReceiver.flagDataRead) { connect.udpReceiver.flagDataRead = true; UIReceiver.currentInput = connect.udpReceiver.dataMessages[connect.udpReceiver.dataMessages.Count - 1]; if (UIReceiver.lastInputs != currentInput) { currentInput = EncodeUtilities.StripSplitter(currentInput); UIReceiver.lastInputs = currentInput; UniversalDebug("New Message without Message Splitter removed: " + currentInput); string[] messageComponents = currentInput.Split(new string[] { EncodeUtilities.headerSplitter }, 2, StringSplitOptions.RemoveEmptyEntries); if (messageComponents.Length > 1) { string header = messageComponents[0], content = messageComponents[1]; UniversalDebug("Header: " + header + ", content: " + content); if (header == "UIDATA") { // If any new data received - process it. UIData data = JsonConvert.DeserializeObject <UIData>(content); UIReceiver.currentBools = new List <bool> (data.bools); UIReceiver.currentInts = new List <int> (data.ints); UIReceiver.currentFloats = new List <float> (data.floats); UniversalDebug("Data Received!"); } else { UniversalDebug("Header Not Recognized!", GH_RuntimeMessageLevel.Warning); } } else { UniversalDebug("Data not Received!", GH_RuntimeMessageLevel.Warning); } } else { UniversalDebug("Improper Message!", GH_RuntimeMessageLevel.Warning); } } else { UniversalDebug("No data received."); } } catch { UniversalDebug("Error Processing Data.", GH_RuntimeMessageLevel.Error); } } else { // If connection disabled - stop receiving. UIReceiver.flagProcessed = false; UIReceiver.lastInputs = string.Empty; UIReceiver.currentBools = new List <bool>(); UIReceiver.currentInts = new List <int>(); UIReceiver.currentFloats = new List <float>(); UniversalDebug("Set 'Send' on true in HoloFab 'HoloConnect'", GH_RuntimeMessageLevel.Warning); } ////////////////////////////////////////////////////// // Output. DA.SetDataList(0, UIReceiver.currentBools); DA.SetDataList(1, UIReceiver.currentInts); DA.SetDataList(2, UIReceiver.currentFloats); #if DEBUG DA.SetData(3, this.debugMessages[this.debugMessages.Count - 1]); #endif // Expire Solution. if (connect.status) { GH_Document document = this.OnPingDocument(); if (document != null) { document.ScheduleSolution(UIReceiver.expireDelay, ScheduleCallback); } } }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { // Get inputs. Connection connect = null; if (!DA.GetData(0, ref connect)) { return; } ////////////////////////////////////////////////////// // Process data. if (connect.status) { // If connection open start acting. // Prepare to receive UI data. try { if (connect.udpReceiver.dataMessages.Count > 0) { UIReceiver.currentInput = connect.udpReceiver.dataMessages.Peek(); UIReceiver.currentInput = EncodeUtilities.StripSplitter(UIReceiver.currentInput); if (UIReceiver.lastInputs != UIReceiver.currentInput) { UIReceiver.lastInputs = UIReceiver.currentInput; UniversalDebug("New Message without Message Splitter removed: " + currentInput); string[] messageComponents = UIReceiver.currentInput.Split(new string[] { EncodeUtilities.headerSplitter }, 2, StringSplitOptions.RemoveEmptyEntries); if (messageComponents.Length > 1) { string header = messageComponents[0], content = messageComponents[1]; UniversalDebug("Header: " + header + ", content: " + content); if (header == "UIDATA") { // If any new data received - process it. UIData data = JsonConvert.DeserializeObject <UIData>(content); UIReceiver.currentBools = new List <bool> (data.bools); UIReceiver.currentInts = new List <int> (data.ints); UIReceiver.currentFloats = new List <float> (data.floats); UniversalDebug("Data Received!"); connect.udpReceiver.dataMessages.Dequeue(); // Actually remove from the queue since it has been processed. } // else // UniversalDebug("Header Not Recognized!", GH_RuntimeMessageLevel.Warning); } else { UniversalDebug("Data not Received!", GH_RuntimeMessageLevel.Warning); } } else { UniversalDebug("Improper Message!", GH_RuntimeMessageLevel.Warning); } } else { UniversalDebug("No data received."); } } catch { UniversalDebug("Error Processing Data.", GH_RuntimeMessageLevel.Error); } } else { // If connection disabled - reset memoty. UIReceiver.lastInputs = string.Empty; UIReceiver.currentBools = new List <bool>(); UIReceiver.currentInts = new List <int>(); UIReceiver.currentFloats = new List <float>(); UniversalDebug("Set 'Send' on true in HoloFab 'HoloConnect'", GH_RuntimeMessageLevel.Warning); } ////////////////////////////////////////////////////// // Output. DA.SetDataList(0, UIReceiver.currentBools); DA.SetDataList(1, UIReceiver.currentInts); DA.SetDataList(2, UIReceiver.currentFloats); #if DEBUG DA.SetData(3, this.debugMessages[this.debugMessages.Count - 1]); #endif //// Expire Solution. //if (connect.status) { // GH_Document document = this.OnPingDocument(); // if (document != null) // document.ScheduleSolution(UIReceiver.expireDelay, ScheduleCallback); //} }