/** * * 所有电表信息查询线程 * 查询所有电表信息,并更新数据库 * * */ private void updateAllInfo_Tick(object sender, EventArgs e) { //byte[] Response = System.Text.Encoding.Default.GetBytes(Conmmend.GET_ALL_COLLECTER_INFO);//响应登录 //获取所有网关ip与端口号 DataOp dataOp = new DataOp(this.oracleip, this.oracleport, this.database, this.userid, this.password); //查询数据库中已注册的网关信息,找到其ip与端口, OleDbDataReader data = dataOp.GetAllRowByTable("GATEWAY_T"); //遍历data,向网关发送数据包 try { while (data.Read()) { string ip = data[2].ToString(); string port = data[3].ToString(); string msg = Conmmend.GET_ALL_COLLECTER_INFO;//查询所有电表信息 byte[] Msg = Encoding.UTF8.GetBytes(msg); System.Net.IPAddress Ip = System.Net.IPAddress.Parse(ip); int Port = int.Parse(port); //Console.WriteLine("send to ip:" + ip + " port: " + port + " " + msg); UDPSocket_serv.Send(Ip, Port, Msg); } } catch (Exception ex) { Console.WriteLine("ex : " + ex.Message); } dataOp.Close(); }
/** * * 网关心跳线程 * * * * */ private void heartBeat_Tick(object sender, EventArgs e) { DataOp dataOp = new DataOp(this.oracleip, this.oracleport, this.database, this.userid, this.password); //查询数据库中已注册的网关信息,找到其ip与端口,发送心跳包 OleDbDataReader data = dataOp.GetAllRowByTable("GATEWAY_T"); //遍历data,向网关发送心跳包 try { while (data.Read()) { string ip = data[2].ToString(); string port = data[3].ToString(); string msg = Conmmend.HEART_BEAT;//心跳协议 byte[] Msg = Encoding.UTF8.GetBytes(msg); System.Net.IPAddress Ip = System.Net.IPAddress.Parse(ip); int Port = int.Parse(port); //Console.WriteLine("send to ip:" + ip + " port: " + port + " " + msg); UDPSocket_serv.Send(Ip, Port, Msg); try { //heartBeatResCounts.TryGetValue(ip,out count); int nowCount = heartBeatResCounts[ip]["SendTimes"]++;//该ip节点,发送次数加1; if (nowCount >= 5) { if (heartBeatResCounts[ip]["RecvFlag"] == 1) { //节点活着呢,修改状态 string newvalue = "set state = 'Active' "; int rownum = dataOp.UpdateOneRow("GATEWAY_T", newvalue, "ADDRESS", ip); if (rownum != 1) { Console.WriteLine("UpdateOneRow gateway states update error"); } } else { //节点已死,修改状态 string newvalue = "set state = 'Dead' "; int rownum = dataOp.UpdateOneRow("GATEWAY_T", newvalue, "ADDRESS", ip); if (rownum != 1) { Console.WriteLine("UpdateOneRow gateway states update error"); } } heartBeatResCounts[ip]["RecvFlag"] = 0; //修改标识,进入下一轮 heartBeatResCounts[ip]["SendTimes"] = 1; //重新计数 } } catch (KeyNotFoundException ex1) {//注意捕获异常顺序,如果第一次无此键, 则初始化 Console.WriteLine("ex1 : " + ex1.Message); Dictionary <string, int> beatRes = new Dictionary <string, int>(); beatRes["SendTimes"] = 1; beatRes["RecvFlag"] = 0; heartBeatResCounts[ip] = beatRes;//初始化发送次数 } } } catch (Exception ex) { Console.WriteLine("ex : " + ex.Message); } dataOp.Close(); }
/** * 托管代码段:当有数据传输到达时进行数据处理 * * * * */ private void DataArrival(byte[] Data, System.Net.IPAddress Ip, int Port) //当有数据到达后的处理进程 { Console.WriteLine("IP : " + Ip + " PORT: " + Port); //UDPSocket_serv.Send(Ip, Port, Data); string msg = Encoding.UTF8.GetString(Data);//linux 端用char[],,c#端用utf8解码 //infoGetter infos = new infoGetter(); //<head>id</head><data>datas</data> //将数字转化成enum类型,, try { MsgCommand msgkind = (MsgCommand)int.Parse(dataGetter.getHead(msg)); switch (msgkind) { case MsgCommand.CONNECT: //1 { //MessageBox.Show("1 Registering msg has come"); byte[] Response = System.Text.Encoding.Default.GetBytes(Conmmend.CONNECTION_RES); //响应链接 UDPSocket_serv.Send(Ip, Port, Response); //string data = infos.getData(msg); //RegisterUser(data); } break; case MsgCommand.LOGIN: //3 { try { UserLogin(msg, Ip, Port); byte[] Response = System.Text.Encoding.Default.GetBytes(Conmmend.LOGIN_MSG_RES); //响应登录 UDPSocket_serv.Send(Ip, Port, Response); } catch (Exception ex) { MessageBox.Show("user login : "******"IP : " + Ip + "Heart Beat Response...."); heartBeatResCounts[Ip.ToString()]["RecvFlag"] = 1; //修改该ip节点的心跳包响应状态 } break; case MsgCommand.UPDATE_NODE_NUM: { Console.WriteLine("IP : " + Ip + " : Node number update...."); bool updateFlag = UpdateGatewayOfNode(msg, Ip); //更新电表附属的网关信息 if (updateFlag) { byte[] Response = System.Text.Encoding.Default.GetBytes(Conmmend.UPDATE_NODE_NUM_RES); //响应电表更新 UDPSocket_serv.Send(Ip, Port, Response); } } break; case MsgCommand.COLLECTER_INFO_RES: { Console.WriteLine("IP : " + Ip + " : " + msg); bool updateFlag = UpdateHistoryOfInfo(msg); //更新电表历史信息表 } break; } } catch (Exception e) { MessageBox.Show(e.Message); } }