//获取任务的线程 public void TaskThreadStart() { var devCommStateTimer = 0; var dt = new DataTable(); var sendTask = new Dictionary <int, int>(); var bll = new ESMonitor.BLL.Tasks(); while (_mThread.IsAlive) { var numOfDevs = _mList.Count; if (devCommStateTimer < _mCheckDevCommStateTime) { devCommStateTimer += _mThreadCycleTime; } else { DevsManage.ManageDevCommState(_mCheckDevCommStateTime); devCommStateTimer = 0; } if (numOfDevs > 0) { dt.Clear(); sendTask.Clear(); try { dt = bll.GetList("Status=0").Tables[0]; } catch (Exception) { continue; } for (var i = 0; i < dt.Rows.Count; i++) { if (!_mThread.IsAlive) { break; } try { var devId = Convert.ToInt32(dt.Rows[i]["DevId"]); //DevsManage.GetLocalCtrlAddr(localCtrlId, ref addr); if (sendTask.ContainsKey(devId)) { continue; } else { sendTask.Add(devId, 1); } if (devId > 0) { switch ((CommStatus)DevsManage.GetDevStatus(devId)) { case CommStatus.Busy: //发送时间超过最大等待时间,删除该发送任务;等下一个周期发送新的任务 if (DevsManage.IsCommTimeout(devId, _mWaittimeout)) { var taskId = DevsManage.GetCurTaskId(devId); bll.UpdateStatus(taskId, 2); //发送超时 //DevsManage.SetRecvMsgState(devId, false); DevsManage.SetCurTaskId(devId, 0); DevsManage.SetDevStatus(devId, (int)CommStatus.Free); //重新发送N次 //N次后依然不成功,就提示用户并保留不成功的日志 } break; //仅通信准备就绪,空闲时发送数据 case CommStatus.Free: if (DevsManage.IsRegistered(devId)) { var taskId = Convert.ToInt64(dt.Rows[i]["TaskId"]); var length = Convert.ToInt32(dt.Rows[i]["Length"]); var buffer = (byte[])(dt.Rows[i]["Data"]); var protocol = Element(devId); //根据id取协议 if (protocol.AsyncSocketUserToken.ConnectSocket != null) { lock (protocol) { if (protocol.DoSendResult(buffer, 0, length)) //true表示发送成功 { DevsManage.SetCurTaskId(devId, taskId); DevsManage.SetDevStatus(devId, (int)CommStatus.Busy); DevsManage.UpdateSendTime(devId); } } } } break; //通信状态断开,不发送 case CommStatus.DisConnect: break; } } } catch (Exception e) { Program.Logger.ErrorFormat("Task thread send command error, message: {0}", e.Message); Program.Logger.Error(e.StackTrace); Program.OutputLog.LogFormat("Task thread send command error, message: {0}", e.Message); } } } if (!_mThread.IsAlive) { break; } Thread.Sleep(_mThreadCycleTime); //每秒钟 } dt.Clear(); }
public override bool ProcessCommand(byte[] buffer, int offset, int packetLen)//packetLen是总的数据长度 { var res = _responseCmd.DecodeFrame(buffer, packetLen); if (!res) { return(false); } var devId = DevsManage.GetDevId(_responseCmd.NodeId); if (devId <= 0) { return(false); } //Check command switch ((ProtocolCmdType)_responseCmd.CmdType) { case ProtocolCmdType.SysComm: if (_responseCmd.CmdByte == (Byte)ProtocolCmdByte.HeartBeat) { if (!DevsManage.IsRegistered(devId)) { DevsManage.Register(devId); //注册 DevsManage.SetDevStatus(devId, (int)CommStatus.Free); EsProtocolMgr.Add(devId, this); AdjustTime(); } else { if (!EsProtocolMgr.IsSocketConect(devId, this)) { EsProtocolMgr.Add(devId, this); //此处顺序有问题 } } DevsManage.SetTimeOutCount(devId, 3); //更新心跳时间 DevsManage.UpdateHeartBeatTime(devId); DoHeartBeat(); } break; case ProtocolCmdType.ModuleCfg: break; case ProtocolCmdType.ModuleCtrl: if (_responseCmd.CmdByte == (byte)ProtocolCmdByte.GetGpsInfo) { DevsManage.UpdateRecvTime(devId); AddTaskResponse(devId, buffer, packetLen); } break; case ProtocolCmdType.DevCtrl: break; case ProtocolCmdType.DevResponse: if (_responseCmd.CmdByte == (Byte)ProtocolCmdByte.AutoUploadAllEsParams) { var esParams = new EsData(); if (_responseCmd.DecodeAutoUploadEsParamsCmd(ref esParams)) { var dataId = AddEsMin(devId, esParams); var model = new ESMonitor.Model.TaskNotice(); try { model.DevId = devId; model.Data = buffer; model.Length = packetLen; model.TaskId = dataId; model.UpdateTime = DateTime.Now; _tnBll.Add(model); } catch (Exception ex) { Program.Logger.ErrorFormat("保存TaskNotice错误: {0},ESMIN_ID{1},TaskNoticeID{2}", ex.Message, dataId, model.TaskId); Program.Logger.Error(ex.StackTrace); } DevsManage.UpdateAutoUploadTime(devId); } } else { DevsManage.UpdateRecvTime(devId); AddTaskResponse(devId, buffer, packetLen); } break; case ProtocolCmdType.DevStatus: if (_responseCmd.CmdByte == (byte)ProtocolCmdByte.GetGpsInfo) { DevsManage.UpdateRecvTime(devId); UpdateDevGps(_responseCmd, devId); } break; } return(true); }