|
@@ -416,7 +416,7 @@ namespace NB_IOT_TCP_HP_SOCKET
|
|
|
// 接外包操作
|
|
|
ResMsg msg = ProtocolAnalysisTools.UnOuterPack(bytes, psp);
|
|
|
|
|
|
- if (!msg.Result || psp.Data.Length == 0)
|
|
|
+ if (!msg.Result)
|
|
|
{
|
|
|
AddMsg($" > [{clientInfo.ConnId},OnReceive] -> {clientInfo.IpAddress}:{clientInfo.Port}-{clientInfo.SourceNumber} {psp.SourceNumber} ({bytes.Length} bytes) 外包解包失败 \n {msg.Message}", lbErrorData);
|
|
|
|
|
@@ -942,64 +942,61 @@ namespace NB_IOT_TCP_HP_SOCKET
|
|
|
{
|
|
|
insourcingData.Type = "CD_CellMonitor";
|
|
|
CellMonitor cm = new CellMonitor();
|
|
|
- // 抄表数据进行解包操作,解包后抄表数据存入cm.Record
|
|
|
- ResMsg msg = ProtocolAnalysisTools.UnInnerPack(cm, psp.Data);
|
|
|
-
|
|
|
- if (!msg.Result)
|
|
|
+ if (psp._type == FrameType.Battery_Param && psp.Data.Length == 0)
|
|
|
{
|
|
|
- // 阀门控制回包
|
|
|
- if (msg.Message == "新参数包")
|
|
|
+
|
|
|
+ AddMsg($" > [{clientInfo.ConnId},OnReceive] -> {clientInfo.IpAddress}:{clientInfo.Port}-{clientInfo.SourceNumber} {psp.SourceNumber} 设参成功响应 \n ", lbReceiveData);
|
|
|
+ insourcingData.CJT188 = new CJT188();
|
|
|
+ // 加入内包数据处理队列
|
|
|
+ insourcingQueue.Enqueue(insourcingData);
|
|
|
+
|
|
|
+ List<Task> taskList = new List<Task>();
|
|
|
+ if (sendTaskList.ContainsKey(clientInfo.ConnId))
|
|
|
{
|
|
|
- insourcingData.DataType = "0";
|
|
|
- //1.解析参数包
|
|
|
- CJT188 cjt = new CJT188();
|
|
|
- ResMsg resMsg = CJT188.UnPack(cjt, psp.Data);
|
|
|
- //2.判断是否是阀门状态上报功能
|
|
|
- if (resMsg.Result)
|
|
|
+ taskList = sendTaskList[clientInfo.ConnId];
|
|
|
+ }
|
|
|
+
|
|
|
+ if (taskList.Count > 0)
|
|
|
+ {
|
|
|
+ Task task = taskList[0];
|
|
|
+
|
|
|
+ if (psp.Data.Length >= 6)
|
|
|
{
|
|
|
- if (cjt._cmdType == ParamCmdType.AutoReportValveState)
|
|
|
- {
|
|
|
- #region
|
|
|
- ParamCmdTo paramCmdTo = new ParamCmdTo();
|
|
|
- paramCmdTo.data = new byte[0] { };
|
|
|
- paramCmdTo._controlCode = 0xA1;
|
|
|
- paramCmdTo._cmdType = ParamCmdType.AutoReportValveState;
|
|
|
- paramCmdTo._jzqArrress = "00000000000000";
|
|
|
- byte[] backParamSendCode = new byte[0] { };
|
|
|
- CJT188 cjtSend = new CJT188();
|
|
|
- bool rs = cjtSend.Pack(paramCmdTo, ref backParamSendCode);
|
|
|
- if (rs)
|
|
|
- {
|
|
|
- PSProtocol protocol2 = new PSProtocol(psp.DestNumber, psp.SourceNumber, backParamSendCode);
|
|
|
- protocol2.Type = psp.Type;
|
|
|
+ clientInfo.ParamRetrunStatus = 1;
|
|
|
+ server.SetExtra(clientInfo.ConnId, clientInfo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
|
|
|
- if (ProtocolAnalysisTools.OuterPack(protocol2, out byte[] buffer3).Result)
|
|
|
- {
|
|
|
- if (server.Send(clientInfo.ConnId, buffer3, buffer3.Length))
|
|
|
- {
|
|
|
- AddMsg($" > [{clientInfo.ConnId},OnSend] -> {clientInfo.IpAddress}:{clientInfo.Port}-{clientInfo.SourceNumber} {psp.SourceNumber} ({buffer3.Length} bytes) 阀门状态上报回复成功 \n {BitConverter.ToString(buffer3, 0, buffer3.Length).Replace("-", " ")} ", lbReceiveData);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ // 抄表数据进行解包操作,解包后抄表数据存入cm.Record
|
|
|
+ ResMsg msg = ProtocolAnalysisTools.UnInnerPack(cm, psp.Data);
|
|
|
|
|
|
- insourcingData.CJT188 = cjt;
|
|
|
- // 加入内包数据处理队列
|
|
|
- insourcingQueue.Enqueue(insourcingData);
|
|
|
- #endregion
|
|
|
- }
|
|
|
- else if (cjt._cmdType == ParamCmdType.ReadWorkMode)
|
|
|
+ if (!msg.Result)
|
|
|
+ {
|
|
|
+ // 阀门控制回包
|
|
|
+ if (msg.Message == "新参数包")
|
|
|
+ {
|
|
|
+ insourcingData.DataType = "0";
|
|
|
+ //1.解析参数包
|
|
|
+ CJT188 cjt = new CJT188();
|
|
|
+ ResMsg resMsg = CJT188.UnPack(cjt, psp.Data);
|
|
|
+ //2.判断是否是阀门状态上报功能
|
|
|
+ if (resMsg.Result)
|
|
|
{
|
|
|
- if (cjt._controlCode == 0x21)
|
|
|
+ if (cjt._cmdType == ParamCmdType.AutoReportValveState)
|
|
|
{
|
|
|
+ #region
|
|
|
ParamCmdTo paramCmdTo = new ParamCmdTo();
|
|
|
- paramCmdTo._jzqArrress = "00000000000000";
|
|
|
paramCmdTo.data = new byte[0] { };
|
|
|
- paramCmdTo._cmdType = ParamCmdType.ReadWorkMode;
|
|
|
paramCmdTo._controlCode = 0xA1;
|
|
|
+ paramCmdTo._cmdType = ParamCmdType.AutoReportValveState;
|
|
|
+ paramCmdTo._jzqArrress = "00000000000000";
|
|
|
byte[] backParamSendCode = new byte[0] { };
|
|
|
CJT188 cjtSend = new CJT188();
|
|
|
- bool result = cjtSend.Pack(paramCmdTo, ref backParamSendCode);
|
|
|
- if (result)
|
|
|
+ bool rs = cjtSend.Pack(paramCmdTo, ref backParamSendCode);
|
|
|
+ if (rs)
|
|
|
{
|
|
|
PSProtocol protocol2 = new PSProtocol(psp.DestNumber, psp.SourceNumber, backParamSendCode);
|
|
|
protocol2.Type = psp.Type;
|
|
@@ -1008,68 +1005,100 @@ namespace NB_IOT_TCP_HP_SOCKET
|
|
|
{
|
|
|
if (server.Send(clientInfo.ConnId, buffer3, buffer3.Length))
|
|
|
{
|
|
|
- AddMsg($" > [{clientInfo.ConnId},OnSend] -> {clientInfo.IpAddress}:{clientInfo.Port}-{clientInfo.SourceNumber} {psp.SourceNumber} ({buffer3.Length} bytes) 读阀门状态回复成功 \n {BitConverter.ToString(buffer3, 0, buffer3.Length).Replace("-", " ")} ", lbReceiveData);
|
|
|
+ AddMsg($" > [{clientInfo.ConnId},OnSend] -> {clientInfo.IpAddress}:{clientInfo.Port}-{clientInfo.SourceNumber} {psp.SourceNumber} ({buffer3.Length} bytes) 阀门状态上报回复成功 \n {BitConverter.ToString(buffer3, 0, buffer3.Length).Replace("-", " ")} ", lbReceiveData);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
- }
|
|
|
- else //if (cjt._cmdType == ParamCmdType.WriteNetParam)
|
|
|
- {
|
|
|
- if (cjt._controlCode == 0xA4)
|
|
|
- {
|
|
|
- AddMsg($" > [{clientInfo.ConnId},OnReceive] -> {clientInfo.IpAddress}:{clientInfo.Port}-{clientInfo.SourceNumber} {psp.SourceNumber} 设参成功响应 \n ", lbReceiveData);
|
|
|
+
|
|
|
insourcingData.CJT188 = cjt;
|
|
|
// 加入内包数据处理队列
|
|
|
insourcingQueue.Enqueue(insourcingData);
|
|
|
-
|
|
|
- List<Task> taskList = new List<Task>();
|
|
|
- if (sendTaskList.ContainsKey(clientInfo.ConnId))
|
|
|
+ #endregion
|
|
|
+ }
|
|
|
+ else if (cjt._cmdType == ParamCmdType.ReadWorkMode)
|
|
|
+ {
|
|
|
+ if (cjt._controlCode == 0x21)
|
|
|
{
|
|
|
- taskList = sendTaskList[clientInfo.ConnId];
|
|
|
- }
|
|
|
+ ParamCmdTo paramCmdTo = new ParamCmdTo();
|
|
|
+ paramCmdTo._jzqArrress = "00000000000000";
|
|
|
+ paramCmdTo.data = new byte[0] { };
|
|
|
+ paramCmdTo._cmdType = ParamCmdType.ReadWorkMode;
|
|
|
+ paramCmdTo._controlCode = 0xA1;
|
|
|
+ byte[] backParamSendCode = new byte[0] { };
|
|
|
+ CJT188 cjtSend = new CJT188();
|
|
|
+ bool result = cjtSend.Pack(paramCmdTo, ref backParamSendCode);
|
|
|
+ if (result)
|
|
|
+ {
|
|
|
+ PSProtocol protocol2 = new PSProtocol(psp.DestNumber, psp.SourceNumber, backParamSendCode);
|
|
|
+ protocol2.Type = psp.Type;
|
|
|
|
|
|
- if (taskList.Count > 0)
|
|
|
+ if (ProtocolAnalysisTools.OuterPack(protocol2, out byte[] buffer3).Result)
|
|
|
+ {
|
|
|
+ if (server.Send(clientInfo.ConnId, buffer3, buffer3.Length))
|
|
|
+ {
|
|
|
+ AddMsg($" > [{clientInfo.ConnId},OnSend] -> {clientInfo.IpAddress}:{clientInfo.Port}-{clientInfo.SourceNumber} {psp.SourceNumber} ({buffer3.Length} bytes) 读阀门状态回复成功 \n {BitConverter.ToString(buffer3, 0, buffer3.Length).Replace("-", " ")} ", lbReceiveData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else //if (cjt._cmdType == ParamCmdType.WriteNetParam)
|
|
|
+ {
|
|
|
+ if (cjt._controlCode == 0xA4)
|
|
|
{
|
|
|
- Task task = taskList[0];
|
|
|
+ AddMsg($" > [{clientInfo.ConnId},OnReceive] -> {clientInfo.IpAddress}:{clientInfo.Port}-{clientInfo.SourceNumber} {psp.SourceNumber} 设参成功响应 \n ", lbReceiveData);
|
|
|
+ insourcingData.CJT188 = cjt;
|
|
|
+ // 加入内包数据处理队列
|
|
|
+ insourcingQueue.Enqueue(insourcingData);
|
|
|
|
|
|
- if (psp.Data.Length >= 6)
|
|
|
+ List<Task> taskList = new List<Task>();
|
|
|
+ if (sendTaskList.ContainsKey(clientInfo.ConnId))
|
|
|
{
|
|
|
- clientInfo.ParamRetrunStatus = 1;
|
|
|
- server.SetExtra(clientInfo.ConnId, clientInfo);
|
|
|
+ taskList = sendTaskList[clientInfo.ConnId];
|
|
|
+ }
|
|
|
+
|
|
|
+ if (taskList.Count > 0)
|
|
|
+ {
|
|
|
+ Task task = taskList[0];
|
|
|
+
|
|
|
+ if (psp.Data.Length >= 6)
|
|
|
+ {
|
|
|
+ clientInfo.ParamRetrunStatus = 1;
|
|
|
+ server.SetExtra(clientInfo.ConnId, clientInfo);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ else
|
|
|
+ {
|
|
|
+ AddMsg($" > [{clientInfo.ConnId},OnReceive] -> {clientInfo.IpAddress}:{clientInfo.Port}-{clientInfo.SourceNumber} {sourceNumber} ({psp.Data.Length} bytes) CD_CellMonitor内包解包失败 \n {msg.Message}", lbErrorData);
|
|
|
+ }
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- AddMsg($" > [{clientInfo.ConnId},OnReceive] -> {clientInfo.IpAddress}:{clientInfo.Port}-{clientInfo.SourceNumber} {sourceNumber} ({psp.Data.Length} bytes) CD_CellMonitor内包解包失败 \n {msg.Message}", lbErrorData);
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- AddMsg($" > [{clientInfo.ConnId},OnReceive] -> {clientInfo.IpAddress}:{clientInfo.Port}-{clientInfo.SourceNumber} {sourceNumber} ({psp.Data.Length} bytes) CD_CellMonitor内包解包成功 \n {BitConverter.ToString(psp.Data, 0, psp.Data.Length).Replace("-", " ")}", lbReceiveData);
|
|
|
+ AddMsg($" > [{clientInfo.ConnId},OnReceive] -> {clientInfo.IpAddress}:{clientInfo.Port}-{clientInfo.SourceNumber} {sourceNumber} ({psp.Data.Length} bytes) CD_CellMonitor内包解包成功 \n {BitConverter.ToString(psp.Data, 0, psp.Data.Length).Replace("-", " ")}", lbReceiveData);
|
|
|
|
|
|
- insourcingData.DataType = "1";
|
|
|
+ insourcingData.DataType = "1";
|
|
|
|
|
|
- // 如果是最后一包则发送校时指令
|
|
|
- if (cm.IsEnd)
|
|
|
- {
|
|
|
- clientInfo.IsEnd = true;
|
|
|
- clientInfo.TranDevice = trandevice;
|
|
|
- clientInfo.DestNumber = psp.DestNumber;
|
|
|
- clientInfo.SourceNumber = psp.SourceNumber;
|
|
|
- server.SetExtra(clientInfo.ConnId, clientInfo);
|
|
|
-
|
|
|
- // 启动设参线程
|
|
|
- StartOrResumeSetParamThread();
|
|
|
- }
|
|
|
+ // 如果是最后一包则发送校时指令
|
|
|
+ if (cm.IsEnd)
|
|
|
+ {
|
|
|
+ clientInfo.IsEnd = true;
|
|
|
+ clientInfo.TranDevice = trandevice;
|
|
|
+ clientInfo.DestNumber = psp.DestNumber;
|
|
|
+ clientInfo.SourceNumber = psp.SourceNumber;
|
|
|
+ server.SetExtra(clientInfo.ConnId, clientInfo);
|
|
|
+
|
|
|
+ // 启动设参线程
|
|
|
+ StartOrResumeSetParamThread();
|
|
|
+ }
|
|
|
|
|
|
- insourcingData.CellMonitor = cm;
|
|
|
- // 加入内包数据处理队列
|
|
|
- insourcingQueue.Enqueue(insourcingData);
|
|
|
+ insourcingData.CellMonitor = cm;
|
|
|
+ // 加入内包数据处理队列
|
|
|
+ insourcingQueue.Enqueue(insourcingData);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
// MODBUS类设备
|
|
@@ -1627,7 +1656,7 @@ namespace NB_IOT_TCP_HP_SOCKET
|
|
|
|
|
|
int paramSendNum = clientInfo.ParamSendNum; // 已发送次数
|
|
|
int paramSendStatus = clientInfo.ParamSendStatus; // 发送状态
|
|
|
- DateTime paramSendTime = clientInfo.ParamSendTime; // 上次发送时间
|
|
|
+ DateTime paramSendTime = clientInfo.ParamSendTime == null || clientInfo.ParamSendTime < DateTime.Now.AddDays(-1) ? DateTime.Now : clientInfo.ParamSendTime; // 上次发送时间
|
|
|
|
|
|
int paramSendNumTmp = Convert.ToInt32(ConfigurationManager.AppSettings["ParamSendTimeOutNum"]); // 电池供电最大下发指令次数
|
|
|
int paramSendInterval = Convert.ToInt32(ConfigurationManager.AppSettings["ParamSendInterval"]); // 电池供电同一设备下发指令每次间隔时间(秒)
|
|
@@ -1638,6 +1667,7 @@ namespace NB_IOT_TCP_HP_SOCKET
|
|
|
{
|
|
|
Task task = remoteTask.Tasklist[i];
|
|
|
TimeSpan span = (TimeSpan)(DateTime.Now - paramSendTime);
|
|
|
+ AddMsg($" > [{clientInfo.ConnId},OnSend] -> {clientInfo.IpAddress}:{clientInfo.Port} {clientInfo.SourceNumber} \n ParamRetrunStatus:{clientInfo.ParamRetrunStatus}, paramSendStatus: {paramSendStatus},paramSendNum: {paramSendNum},paramSendNumTmp:{paramSendNumTmp},TotalSeconds:{span.TotalSeconds},paramSendInterval:{paramSendInterval},paramSendTime:{paramSendTime.ToString("yyyy-MM-dd HH:mm:ss")} ", lbErrorData);
|
|
|
// 未发送;已发送&&发送次数小于配置的最大发送次数&&两次间隔时间大于配置的间隔时间
|
|
|
if ((clientInfo.ParamRetrunStatus == 0 && (paramSendStatus == 0 || (paramSendStatus == 1 && paramSendNum < paramSendNumTmp && span.TotalSeconds >= paramSendInterval)))|| (clientInfo.ParamRetrunStatus == 1 && i < remoteTask.Tasklist.Count))
|
|
|
{
|
|
@@ -1658,6 +1688,7 @@ namespace NB_IOT_TCP_HP_SOCKET
|
|
|
if (sendFlag)
|
|
|
{
|
|
|
clientInfo.ParamSendStatus = 1;
|
|
|
+ clientInfo.ParamRetrunStatus = 0;
|
|
|
if (sendTaskList.ContainsKey(clientInfo.ConnId))
|
|
|
{
|
|
|
List<Task> list = sendTaskList[clientInfo.ConnId];
|
|
@@ -1681,7 +1712,7 @@ namespace NB_IOT_TCP_HP_SOCKET
|
|
|
ProtocolAnalysisTools.UpdateCommdSendNum(task.TaskID);
|
|
|
}
|
|
|
clientInfo.ParamSendTime = DateTime.Now;
|
|
|
- clientInfo.ParamSendNum = paramSendNum + 1;
|
|
|
+ clientInfo.ParamSendNum ++;
|
|
|
server.SetExtra(clientInfo.ConnId, clientInfo);
|
|
|
}
|
|
|
}
|