public static BaseCmd getEndCmd() { BaseCmd baseCmd = new BaseCmd(); baseCmd.canUse = false; baseCmd.commandType = BaseCmdType.End; return(baseCmd); }
public static BaseCmd getRuntimeCmd() { BaseCmd baseCmd = new BaseCmd(); baseCmd.canUse = true; baseCmd.commandType = BaseCmdType.Runtime; return(baseCmd); }
public static BaseCmd getPollingCmd() { BaseCmd baseCmd = new BaseCmd(); baseCmd.canUse = false; baseCmd.commandType = BaseCmdType.Polling; return(baseCmd); }
public static BaseCmd getInitCmd() { BaseCmd baseCmd = new BaseCmd(); baseCmd.canUse = true; baseCmd.commandType = BaseCmdType.Init; return(baseCmd); }
/// <summary> /// 弹出一个要发送的指令,必然是可用的指令 /// </summary> /// <returns></returns> public BaseCmd ReadCmd() { lock (sendCmd) { //首先考虑DTU自己的运行时指令 if (selfStation.runtimeComand.Count > 0) { return(selfStation.runtimeComand.Dequeue()); } //在考虑其下属的模块的运行时指令 foreach (string key in baseStationDic.Keys) { if (baseStationDic[key].runtimeComand.Count > 0) { return(baseStationDic[key].runtimeComand.Dequeue()); } } //最后考虑大队列 if (sendCmd.Count > 0) { //开头是End类型了,说明已经轮询一遍了 if (sendCmd.ElementAt(0).commandType == BaseCmdType.End) { TimeSpan pollingtime = DateTime.Now - pollingStartTime; if (pollingtime.TotalMilliseconds < dtu.polling_time) { return(null); } else { pollingStartTime = DateTime.Now; } } BaseCmd baseCmd = sendCmd.Dequeue(); sendCmd.Enqueue(baseCmd); while (!baseCmd.canUse) { if (baseCmd.commandType == BaseCmdType.End) { TimeSpan pollingtime = DateTime.Now - pollingStartTime; if (pollingtime.TotalMilliseconds < dtu.polling_time) { return(null); } else { pollingStartTime = DateTime.Now; } } baseCmd = sendCmd.Dequeue(); sendCmd.Enqueue(baseCmd); } return(baseCmd); } return(null); } }
public void beat(Object obj) { trList.RemoveAll((item) => item.nextTime == DateTime.MaxValue); // trList.Sort((a, b) => a.nextTime.CompareTo(b.nextTime)); trList.OrderBy(a => a.nextTime); for (int i = 0; i < trList.Count; i++) { TimedRule t1 = trList[i]; if (DateTime.Now >= t1.nextTime) { //符合定时条件 for (int j = 0; j < t1.cmdList.Count; j++) { BaseCmd bcm = t1.cmdList[j]; BaseStation bastation = baseStationDic.Values.Where(item => item.stationEntity.Md_ID == bcm.stationId).FirstOrDefault(); if (bastation != null) { bastation.runtimeComand.Enqueue(bcm); } } t1.SetNextTime(); } else { break; } } bool istosend = false; //第一次发送的时候 if (sendTime == DateTime.MinValue) { istosend = true; pollingStartTime = DateTime.Now; } else { TimeSpan interval = DateTime.Now - sendTime; //单个指令延迟 if (interval.TotalMilliseconds > dtu.polling_delay_time) { istosend = true; } } if (istosend) { istosend = false; sendPollingCMD(); } }
/// <summary> /// 初始化发送指令 /// </summary> private void InitSendCmd() { InitSendCmd(selfStation); foreach (String key in baseStationDic.Keys) { InitSendCmd(baseStationDic[key]); } BaseCmd endCMD = new BaseCmd(); endCMD.commandType = BaseCmdType.End; endCMD.canUse = false;//endCMD一定不能发 sendCmd.Enqueue(endCMD); }
/// <summary> /// 发送指令 /// 此处得到的发送指令,一定是可用的且不为End类型的 /// </summary> private void sendPollingCMD() { BaseCmd baseCmd = ReadCmd(); if (baseCmd != null) { currentCmd = baseCmd; byte[] tempCmd = baseCmd.GetSendCommand(); if (server.Send(connId, tempCmd, tempCmd.Length)) { sendTime = DateTime.Now; } } }
/// <summary> /// 针对这个模块的所有初始化指令, /// 有一个匹配上,就返回true ,具体规则自己写 /// 初始化时候,服务器每收到一个数据,都要调用下这个函数 /// 判断下是否初始化完成 /// </summary> /// <param name="bytes"></param> /// <returns></returns> public virtual bool isInit(byte[] bytes) { //处理这个isInitSccess for (int i = 0; i < initComand.Count; i++) { BaseCmd ce = initComand[i]; if (ce.canUse) { if (ce.GetLastCommand() == null) { break; } if (ByteUtils.ByteArrayEquals2(ce.GetLastCommand(), bytes)) { //如果相等,那就是在这个初始化指令的对应回复值 ce.canUse = false; var query = initComand.Where(item => item.canUse == true && item.stationId == ce.stationId); if (!query.Any()) { var pollingQuery = pollingComand.Where(item => item.stationId == ce.stationId); foreach (var bas in pollingQuery) { bas.canUse = true; } } break; } } } var queryAll = initComand.Where(item => item.canUse == true); if (!queryAll.Any()) { isInitSccess = true; } return(false); }
/// <summary> /// 把临时运行时指令加入队列 /// </summary> /// <param name="baseCmd"></param> public virtual void PushRuntimeCmd(BaseCmd baseCmd) { baseCmd.commandType = BaseCmdType.Runtime; runtimeComand.Enqueue(baseCmd); }