/// <summary> /// 心跳线程 /// </summary> private void Heart() { while (CommonFunction.flag_listener) { lock (CommonFunction.obj_66) { foreach (HeartInfo hi in CommonFunction.hearts.Values) { try { if (hi.Ke_al > 3) { hi.State = "异常"; hi.Ke_al = 00; Showmsg(hi.Id + "/" + hi.Type); //Thread me = new Thread(() => Message_f(hi)) //{ // IsBackground = true //}; //me.Start(); hi.State = "禁用"; //修改检测平台状态为异常 switch (hi.Type) { case "命令检测": Myadapter_p.UpdatePlatCom("异常", hi.Id); break; case "IC卡卡控": Myadapter_p.UpdatePlatIc("异常", hi.Id); break; case "膜式表初检": Myadapter_p.UpdatePlatChu_msb("异常", hi.Id); break; case "修正仪初检": Myadapter_p.UpdatePlatChu_xzy("异常", hi.Id); break; case "超声波初检": Myadapter_p.UpdatePlatChu_csb("异常", hi.Id); break; case "终检": Myadapter_p.UpdatePlatZho("异常", hi.Id); break; } Thread.Sleep(50); CommonFunction.platm.ChangePlat(); PostLl(hi.Id.ToString(), hi.Type, "异常"); DT_WaringPlatTableAdapter myadapter_wp = new DT_WaringPlatTableAdapter(); DataTable dtw = myadapter_wp.GetDataBy_it(hi.Id.ToString(), hi.Type, 0); if (dtw.Rows.Count == 0) { myadapter_wp.InsertWP(hi.Id.ToString(), hi.Type, DateTime.Now, 0, 0); } else { myadapter_wp.UpdateMP(0, DateTime.Now, hi.Type, hi.Id.ToString(), 0); } //异常,给ll推送异常,修改检测平台状态为异常 } if (hi.State.Equals("正常")) { try { Debug.Print("2"); if (CommonFunction.flag_listener) { Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ip = IPAddress.Parse(hi.IP); int port = Convert.ToInt32(hi.Port); socket.Connect(ip, port); string s = DownCommond.GenerateCorekeep(); socket.Send(DataHelper.Str2Byte(s)); socket.Close(); } } catch (Exception) { Debug.Print("3"); LogHelper.Info("检测平台异常"); } hi.Ke_al++; } Thread.Sleep(500); } catch (Exception) { hi.Ke_al++; //此处应警报,没有办法发送心跳包 } } } Debug.Print("1"); Thread.Sleep(10000); } }
/// <summary> /// id+ip+port+type+入网/出网(1+4+2+1+1) /// </summary> /// <param name="session"></param> /// <param name="requestInfo"></param> public override void ExecuteCommand(MySession session, MyRequestInfo requestInfo) { lock (CommonFunction.obj_66) { string re = "00"; bool f; bool isallowexit; Myadapter_p = new Dt_PlateInfoTableAdapter(); myadapter_w = new DT_WaringPlatTableAdapter(); var info = requestInfo; int id = Convert.ToInt32(info.Data.Substring(2, 2), 16); string ip = DataHelper.Str2IP(info.Data.Substring(4, 8)); string port = Convert.ToInt32(info.Data.Substring(12, 4), 16).ToString(); string type = info.Data.Substring(16, 2); string cmd = info.Data.Substring(18, 2); //入网 根据id查询数据库(有,更新数据)(没有,插入数据) //出网 根据id查询数据库(有,更新数据) (没有,不做操作) 检查其状态是否可以立即出网,并回复。若不可以标记状态为待出网,通知其下次上传。 //更新platinfo 和 heart 入网成功加入heart,出网成功移除heart,dictionary存储heartinfo //加锁,当执行01操作时不能执行出网操作,避免 if (cmd.Equals("AA")) { //入网 try { pl = Replat(id); if (pl == null) { pl = new PlatInfo(); f = false; } else { f = true; } switch (type) { case "01": heart = new HeartInfo(id, "命令检测", ip, port, 00, "正常"); pl.PlatComs = "空闲"; pl.PlatComp = port; break; case "02": heart = new HeartInfo(id, "IC卡卡控", ip, port, 00, "正常"); pl.PlatIcs = "空闲"; pl.PlatIcp = port; break; case "03": heart = new HeartInfo(id, "膜式表初检", ip, port, 00, "正常"); pl.PlatChus_msb = "空闲"; pl.PlatChup_msb = port; break; case "04": heart = new HeartInfo(id, "修正仪初检", ip, port, 00, "正常"); pl.PlatChus_xzy = "空闲"; pl.PlatChup_xzy = port; break; case "05": heart = new HeartInfo(id, "超声波初检", ip, port, 00, "正常"); pl.PlatChus_csb = "空闲"; pl.PlatChup_csb = port; break; case "06": heart = new HeartInfo(id, "终检", ip, port, 00, "正常"); pl.PlatZhos = "空闲"; pl.PlatZhop = port; break; } pl.IP = ip; pl.id = id; if (f) { Myadapter_p.UpdatePlat( pl.IP, pl.PlatComs, pl.PlatIcs, pl.PlatChus_msb, pl.PlatChus_xzy, pl.PlatChus_csb, pl.PlatZhos, pl.PlatComp, pl.PlatIcp, pl.PlatChup_msb, pl.PlatChup_xzy, pl.PlatChup_csb, pl.PlatZhop, pl.id); } else { Myadapter_p.InsertPlat( pl.id, pl.IP, pl.PlatComs, pl.PlatIcs, pl.PlatChus_msb, pl.PlatChus_xzy, pl.PlatChus_csb, pl.PlatZhos, pl.PlatComp, pl.PlatIcp, pl.PlatChup_msb, pl.PlatChup_xzy, pl.PlatChup_csb, pl.PlatZhop); } //入网成功 CommonFunction.platInfos[id] = pl; CommonFunction.hearts[id + type] = heart; re = "AA"; //此检测平台是否为异常的平台,修改数据库 DataTable dtw = myadapter_w.GetDataBy_it(id.ToString(), heart.Type, 0); if (dtw.Rows.Count != 0) { myadapter_w.UpdateMP(1, DateTime.Now, heart.Type, id.ToString(), 0); PostLl(id.ToString(), heart.Type, "正常"); } LogHelper.Info(id + " " + type + " 入网成功"); } catch (Exception e) { LogHelper.Error("入网失败" + e); re = "55"; } } else if (cmd.Equals("55")) { //出网 lock (CommonFunction.obj_44) { try { pl = Replat(id); if (pl != null) { switch (type) { case "01": if (pl.PlatComs.Equals("空闲") || pl.PlatComs.Equals("待出网")) { pl.PlatComs = "未启用"; isallowexit = true; } else { pl.PlatComs = "待出网"; isallowexit = false; } break; case "02": if (pl.PlatIcs.Equals("空闲") || pl.PlatIcs.Equals("待出网")) { pl.PlatIcs = "未启用"; isallowexit = true; } else { pl.PlatIcs = "待出网"; isallowexit = false; } break; case "03": if (pl.PlatChus_msb.Equals("空闲") || pl.PlatChus_msb.Equals("待出网")) { pl.PlatChus_msb = "未启用"; isallowexit = true; } else { pl.PlatChus_msb = "待出网"; isallowexit = false; } break; case "04": if (pl.PlatChus_xzy.Equals("空闲") || pl.PlatChus_xzy.Equals("待出网")) { pl.PlatChus_xzy = "未启用"; isallowexit = true; } else { pl.PlatChus_xzy = "待出网"; isallowexit = false; } break; case "05": if (pl.PlatChus_csb.Equals("空闲") || pl.PlatChus_csb.Equals("待出网")) { pl.PlatChus_csb = "未启用"; isallowexit = true; } else { pl.PlatChus_csb = "待出网"; isallowexit = false; } break; case "06": if (pl.PlatZhos.Equals("空闲") || pl.PlatZhos.Equals("待出网")) { pl.PlatZhos = "未启用"; isallowexit = true; } else { pl.PlatZhos = "待出网"; isallowexit = false; } break; default: isallowexit = false; break; } Myadapter_p.UpdatePlat( pl.IP, pl.PlatComs, pl.PlatIcs, pl.PlatChus_msb, pl.PlatChus_xzy, pl.PlatChus_csb, pl.PlatZhos, pl.PlatComp, pl.PlatIcp, pl.PlatChup_msb, pl.PlatChup_xzy, pl.PlatChup_csb, pl.PlatZhop, pl.id); CommonFunction.platInfos[id] = pl; if (isallowexit) { CommonFunction.hearts.Remove(id + type); re = "AA"; LogHelper.Info(id + " " + type + " 出网成功"); } else { re = "BB"; } } } catch (Exception e) { LogHelper.Error("出网失败" + e); re = "55"; } } } try { //回复检测平台,入网成功或失败,出网成功或失败或等一次检测 IPAddress Ip = IPAddress.Parse(ip); int Port = Convert.ToInt32(port); string send_66 = DownCommond.GenerateSendFrame("66" + re, info, true, false); Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(Ip, Port); socket.Send(DataHelper.Str2Byte(send_66)); socket.Close(); LogHelper.Info("回复66:" + send_66); } catch (Exception e) { LogHelper.Error("无法连接入网检测平台" + e); } } }