private async void RecvData(DatagramSocket socket, DatagramSocketMessageReceivedEventArgs args) { StreamReader reader = new StreamReader(args.GetDataStream().AsStreamForRead()); string data = reader.ReadToEnd(); NodeBroadcastInfo nodeInfo = ParseData(ref data); await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, new DispatchedHandler(() => { nodeBroadcastInfoViewModel.updateNode(nodeInfo); })); if (NodeTable.ContainsKey(nodeInfo.Name)) { MaoNode node = NodeTable[nodeInfo.Name]; if (node.IP != nodeInfo.IP) { node.updateIP(nodeInfo.IP); } node.seeAgain(); } else { NodeTable.Add(nodeInfo.Name, new MaoNode(nodeInfo.Name, nodeInfo.IP)); } }
public void updateNode(NodeBroadcastInfo nodeInfo) { if (infos.Contains(nodeInfo)) { infos[infos.IndexOf(nodeInfo)] = nodeInfo; } else { infos.Add(nodeInfo); } }
private NodeBroadcastInfo ParseData(ref string data) { NodeBroadcastInfo.Builder nodeBroadcastInfoBuilder = NodeBroadcastInfo.builder(); string[] dataSet = data.Split(';'); foreach (string s in dataSet) { string[] pair = s.Split('='); switch (pair[0]) { case "IP": nodeBroadcastInfoBuilder.IP(pair[1]); break; case "CPU_Temp": nodeBroadcastInfoBuilder.CpuTemp(double.Parse(pair[1])); break; case "GPU_Temp": nodeBroadcastInfoBuilder.GpuTemp(double.Parse(pair[1])); break; case "Count": nodeBroadcastInfoBuilder.Count(int.Parse(pair[1])); break; case "SysTime": nodeBroadcastInfoBuilder.SysTime(pair[1]); break; case "NodeName": nodeBroadcastInfoBuilder.Name(pair[1]); break; case "GPS": string[] gpsDataList = pair[1].Split(','); if (!gpsDataList[0].Equals("lost")) { nodeBroadcastInfoBuilder.Latitude(double.Parse(gpsDataList[0])); } if (!gpsDataList[1].Equals("lost")) { nodeBroadcastInfoBuilder.Longitude(double.Parse(gpsDataList[1])); } nodeBroadcastInfoBuilder.Satellite(int.Parse(gpsDataList[2])); break; case "GpsTime": nodeBroadcastInfoBuilder.GpsTime(pair[1]); break; case "Temperature": nodeBroadcastInfoBuilder.EnvTemp(double.Parse(pair[1])); break; } } return(nodeBroadcastInfoBuilder.build()); }