public NodeData insertNode(MyAppSession session, BinaryRequestInfo package) { string id = package.Key; string ip = session.SocketSession.RemoteEndPoint.Address.ToString(); string port = session.SocketSession.RemoteEndPoint.Port.ToString(); //Console.WriteLine(id); if (!_allNode.ContainsKey(ip)) { NodeData nd = new NodeData(); nd.ID = id; nd.IP = ip; nd.PORT = port; _allNode.Add(ip, nd); } SensorData sd = new SensorData(); sd.parsePackage(package, _allNode[ip]); for (int i = 0; i < 42; i++) { //if(i < 40) //{ // //_allNode[ip].Data[i].Add(sd.Data[i]); // if(_allNode[ip].Data[i].Count == 0) // _allNode[ip].Data[i].Add(sd.Data[i]); // else // _allNode[ip].Data[i][0] = sd.Data[i]; // } //_allNode[ip].Data[i].Add(sd.Data[i]); if (_allNode[ip].Data[i].Count == 0) { _allNode[ip].Data[i].Add(sd.Data[i]); } else { if (i < 40) { _allNode[ip].Data[i][0] = sd.Data[i]; } else { _allNode[ip].Data[i][0] = sd.Data[(i - 40) * 350 + 40]; } } // else //{ // for (int j = 0; j < 250; j++) // { // _allNode[ip].Data[i].Add(sd.Data[((i-40)*250+40+j)]); // } //} } return(_allNode[ip]); }
private void appServer_SessionClosed(MyAppSession session, SuperSocket.SocketBase.CloseReason reason) { string ip = session.SocketSession.RemoteEndPoint.Address.ToString(); Console.WriteLine(reason.ToString()); Console.WriteLine("断开连接:" + reason); listView1.Items[ip].Remove(); }
/// <summary> /// 有新连接时的事件处理函数 /// </summary> /// <param name="session"></param> private void appServer_NewSessionConnected(MyAppSession session) //这个里面和断开连接里面的逻辑还有点问题 有待优化 { this.textBox1.Text = session.SocketSession.LocalEndPoint.Address.ToString(); //显示当前终端的地址和端口 this.textBox2.Text = session.SocketSession.LocalEndPoint.Port.ToString(); string ip = session.SocketSession.RemoteEndPoint.Address.ToString(); //得到远程节点的地址和端口 string port = session.SocketSession.RemoteEndPoint.Port.ToString(); if (!listView1.Items.ContainsKey(ip)) { try { this.listView1.Items.Add(ip, ip, null); this.listView1.Items[ip].SubItems.AddRange(new string[] { port, "已连接", "根节点" }); } catch { if (listView1 == null) { this.listView1.Items.Clear(); } return; } } else { try { this.listView1.Items[ip].SubItems[1].Text = port; this.listView1.Items[ip].SubItems[2].Text = "已连接"; this.listView1.Items[ip].SubItems[3].Text = "根节点"; } catch { if (listView1 == null) { this.listView1.Items.Clear(); } return; } } if (listView1 != null) { this.listView1.Items[ip].Selected = true; } }
/// <summary> /// 新请求处理函数 主要的数据处理做在这里 /// </summary> /// <param name="session"></param> /// <param name="requestInfo"></param> private void appServer_NewRequestReceived(MyAppSession session, BinaryRequestInfo requestInfo) { //session.Send("OK"); try { if (true) { MAC = Convert.ToString(requestInfo.Key[4], 16); //获取当前接收到的节点的MAC地址 int i = 0; if (MAC.Equals("e618")) //不同节点精度不同 针对相应节点的数据精度优化 { for (i = 0; i < 700 / 2; i++) { int temp1 = 0; int temp2 = 0; temp1 = requestInfo.Body[2 * i + 0]; temp2 = requestInfo.Body[2 * i + 1]; int temp = 0; temp = (ushort)(temp2); temp |= (ushort)((temp1 << 8) & 0xff00); trans_data[i] = (temp * 1.0 / 65535.0 * 4.096 - 2.562f) * 37.69f; } for (i = 350; i < 1400 / 2; i++) { int temp1 = 0; int temp2 = 0; temp1 = requestInfo.Body[2 * i + 0]; temp2 = requestInfo.Body[2 * i + 1]; int temp = 0; temp = (ushort)(temp2); temp |= (ushort)((temp1 << 8) & 0xff00); trans_data[i] = (temp * 1.0 / 65535.0 * 4.096 - 2.519f) * 39.54f; } } else if (MAC.Equals("f2c7")) //不同节点精度不同 针对相应节点的数据精度优化 { for (i = 0; i < 700 / 2; i++) { int temp1 = 0; int temp2 = 0; temp1 = requestInfo.Body[2 * i + 0]; temp2 = requestInfo.Body[2 * i + 1]; int temp = 0; temp = (ushort)(temp2); temp |= (ushort)((temp1 << 8) & 0xff00); trans_data[i] = (temp * 1.0 / 65535.0 * 4.096 - 2.566f) * 38.66f; } for (i = 350; i < 1400 / 2; i++) { int temp1 = 0; int temp2 = 0; temp1 = requestInfo.Body[2 * i + 0]; temp2 = requestInfo.Body[2 * i + 1]; int temp = 0; temp = (ushort)(temp2); temp |= (ushort)((temp1 << 8) & 0xff00); trans_data[i] = (temp * 1.0 / 65535.0 * 4.096 - 2.542f) * 38.12f; } } data1_show_b4 = (trans_data[100] + trans_data[103] + trans_data[105] + trans_data[107] + trans_data[109]) / 5.0f; //采用平均值的形式取一组数据 data2_show_b4 = (trans_data[500] + trans_data[503] + trans_data[505] + trans_data[507] + trans_data[509]) / 5.0f; } } catch (Exception er) { MessageBox.Show(er.ToString()); } finally { DateTime Time = DateTime.Now.AddMilliseconds(DateTime.Now.Millisecond); current_ds.rtime = Time; current_ds.hor = data1_show_b4; current_ds.ver = data2_show_b4; current_ds.macaddr = MAC.ToString(); que.Enqueue(current_ds); //向队列中加入最新的数据 由于存储速度的限制 显示功能不能用这个队列 package_cnt++; } }