|
@@ -3,10 +3,12 @@ using System.Collections.Generic;
|
3
|
3
|
using System.Linq;
|
4
|
4
|
using System.Text;
|
5
|
5
|
using System.Threading.Tasks;
|
|
6
|
+using DotNettyFrom.common;
|
6
|
7
|
using DotNettyFrom.config;
|
7
|
8
|
using DotNettyFrom.db;
|
8
|
9
|
using DotNettyFrom.model;
|
9
|
10
|
using DotNettyFrom.util;
|
|
11
|
+using NUnit.Framework;
|
10
|
12
|
|
11
|
13
|
namespace DotNettyFrom.generatecmd
|
12
|
14
|
{
|
|
@@ -26,89 +28,143 @@ namespace DotNettyFrom.generatecmd
|
26
|
28
|
|
27
|
29
|
public override string GetReplyCmd(string receiveCmd)
|
28
|
30
|
{
|
|
31
|
+ //功能码
|
29
|
32
|
int cmdId = GetCmdId(receiveCmd);
|
30
|
33
|
|
|
34
|
+ Console.WriteLine("功能码=" + cmdId + "; 16=" + Utils.tenToHex(cmdId));
|
|
35
|
+
|
|
36
|
+ //处理主动上报的回复&心跳.那些不用回复
|
|
37
|
+
|
|
38
|
+ //处理主动上报的回复&心跳.那些不用回复
|
|
39
|
+
|
31
|
40
|
|
32
|
41
|
if (cmdId == int.MinValue)
|
33
|
42
|
{
|
|
43
|
+ DataModel.ReceCollection.Add(new UIInfoModel("帧错误", receiveCmd)
|
|
44
|
+ .setAction(-1)
|
|
45
|
+ .setSendType(SystemConfig.ReplyType));
|
34
|
46
|
return null;
|
35
|
47
|
}
|
36
|
48
|
|
|
49
|
+
|
37
|
50
|
WvBean wb = DbHelper.GetInstance().SelectOneCmdByIndex(cmdId);
|
38
|
51
|
|
39
|
52
|
if (wb == null)
|
40
|
53
|
{
|
|
54
|
+ DataModel.ReceCollection.Add(
|
|
55
|
+ new UIInfoModel("本地数据库没有这样的功能码=(" + cmdId + ") ;16进制=(" +
|
|
56
|
+ Utils.tenToHex(cmdId) + ")", receiveCmd)
|
|
57
|
+ .setAction(-1)
|
|
58
|
+ .setSendType(SystemConfig.ReplyType));
|
41
|
59
|
return null;
|
42
|
60
|
}
|
|
61
|
+
|
43
|
62
|
int pos = SystemConfig.FunctionCodeStartPos + SystemConfig.FunctionCodeLength;
|
44
|
63
|
|
45
|
|
- //前面的
|
|
64
|
+
|
|
65
|
+ //前面的,回复用的
|
46
|
66
|
string resp = receiveCmd.Substring(0, pos);
|
|
67
|
+
|
47
|
68
|
//消息长度
|
48
|
|
- string msgLen = receiveCmd.Substring(pos, SystemConfig.LengthFieldLength * 2);
|
|
69
|
+ int msgLen = Utils.hexToTen(receiveCmd.Substring(pos, SystemConfig.LengthFieldLength * 2));
|
49
|
70
|
pos += SystemConfig.LengthFieldLength * 2;
|
50
|
|
- //测量点&消息流水
|
51
|
71
|
|
52
|
|
- int MeasureFlow = (SystemConfig.MeasurePointLen + SystemConfig.MessageFlowLen);
|
53
|
|
- string msgFlow =
|
54
|
|
- receiveCmd.Substring(pos, MeasureFlow * 2);
|
|
72
|
+ //测量点&消息流水
|
|
73
|
+ int msgFlowLength = (SystemConfig.MeasurePointLen + SystemConfig.MessageFlowLen);
|
|
74
|
+ string msgFlow = receiveCmd.Substring(pos, msgFlowLength * 2);
|
55
|
75
|
|
56
|
|
- pos += MeasureFlow * 2;
|
|
76
|
+ pos += msgFlowLength * 2;
|
57
|
77
|
|
58
|
78
|
Console.WriteLine(resp);
|
59
|
79
|
Console.WriteLine("长度:" + msgLen);
|
60
|
80
|
Console.WriteLine("消息流水:" + msgFlow);
|
61
|
81
|
string bodyFrame = "";
|
62
|
82
|
|
63
|
|
- if (!wb.remark.Trim().Equals(""))
|
|
83
|
+ string tip = "";
|
|
84
|
+ if (wb.mode.Trim().ToUpper().Equals("W"))
|
64
|
85
|
{
|
65
|
|
- //写
|
66
|
|
- try
|
67
|
|
- {
|
68
|
|
- int updateId = int.Parse(wb.remark.Trim());
|
69
|
|
- int msgLenTen = Utils.hexToTen(msgLen) - MeasureFlow;
|
70
|
|
-
|
71
|
|
- string content = receiveCmd.Substring(pos, msgLenTen * 2);
|
72
|
|
-
|
73
|
|
-
|
74
|
|
- Console.WriteLine("updateId:" + updateId + ";;" + msgLenTen);
|
75
|
|
- Console.WriteLine("Conte=" + content);
|
76
|
|
- WvBean updateBean = DbHelper.GetInstance().SelectOneCmdByIndex(updateId);
|
77
|
|
- if (updateBean != null)
|
78
|
|
- {
|
79
|
|
- updateBean.userValue = content;
|
80
|
|
- int result = DbHelper.GetInstance().UpdateCmd(updateBean);
|
81
|
|
- if (result > 0)
|
82
|
|
- {
|
83
|
|
- bodyFrame = "03" + msgFlow + "00";
|
84
|
|
- }
|
85
|
|
- else
|
86
|
|
- {
|
87
|
|
- bodyFrame = "03" + msgFlow + "01";
|
88
|
|
- }
|
89
|
|
- }
|
90
|
|
-
|
91
|
|
- }
|
92
|
|
- catch (Exception exception)
|
93
|
|
- {
|
94
|
|
- bodyFrame = "03" + msgFlow + "01";
|
95
|
|
- Console.WriteLine(exception.Message);
|
96
|
|
- Console.WriteLine(exception);
|
97
|
|
- }
|
|
86
|
+ bodyFrame = WriteMessage(receiveCmd, wb, cmdId, msgLen, msgFlow, msgFlowLength, pos);
|
|
87
|
+ tip = "设置回复";
|
98
|
88
|
}
|
99
|
|
- else
|
|
89
|
+ else if (wb.mode.Trim().ToUpper().Equals("R"))
|
100
|
90
|
{
|
101
|
91
|
//读
|
102
|
|
- Console.WriteLine(wb.userValue);
|
|
92
|
+ bodyFrame = ReadMessage(wb, msgFlow);
|
|
93
|
+ tip = "读取回复";
|
|
94
|
+ }
|
|
95
|
+ else
|
|
96
|
+ {
|
|
97
|
+ return null;
|
103
|
98
|
}
|
104
|
99
|
|
105
|
100
|
|
106
|
|
- return null;
|
|
101
|
+ resp += bodyFrame;
|
|
102
|
+ resp += CmdUtil.CalcCrc16CodeSkip(resp, 3);
|
|
103
|
+ Console.WriteLine("回复:" + resp);
|
|
104
|
+ DataModel.ReceCollection.Add(new UIInfoModel(tip, resp).setSendType(SystemConfig.SendType));
|
|
105
|
+ return resp;
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+ private string ReadMessage(WvBean wb, string msgFlow)
|
|
109
|
+ {
|
|
110
|
+ string content = msgFlow + wb.userValue.Trim();
|
|
111
|
+ if (content.Length % 2 != 0)
|
|
112
|
+ {
|
|
113
|
+ content = "0" + content;
|
|
114
|
+ }
|
|
115
|
+ string lenHex =
|
|
116
|
+ CmdUtil.FrontWithZero(Utils.tenToHex(content.Length / 2), SystemConfig.LengthFieldLength * 2);
|
|
117
|
+ return lenHex + content;
|
107
|
118
|
}
|
108
|
119
|
|
109
|
|
- private string GetFrontFrame(string cmd)
|
|
120
|
+ private string WriteMessage(string receiveCmd, WvBean wb, int funWord, int msgLen, string msgFlow,
|
|
121
|
+ int msgFlowLength, int pos)
|
110
|
122
|
{
|
111
|
|
- return cmd.Substring(0, SystemConfig.FunctionCodeLength + SystemConfig.FunctionCodeStartPos);
|
|
123
|
+ try
|
|
124
|
+ {
|
|
125
|
+ int contentLen = msgLen - msgFlowLength;
|
|
126
|
+ string content = receiveCmd.Substring(pos, contentLen * 2);
|
|
127
|
+ Console.WriteLine("下发的内容是:" + content);
|
|
128
|
+
|
|
129
|
+ int updateId = funWord;
|
|
130
|
+ if (!wb.remark.Trim().Equals(""))
|
|
131
|
+ {
|
|
132
|
+ updateId = int.Parse(wb.remark.Trim());
|
|
133
|
+ }
|
|
134
|
+
|
|
135
|
+ Console.WriteLine("更新Id=" + updateId + ";内容长度=" + contentLen);
|
|
136
|
+ Console.WriteLine("内容=" + content);
|
|
137
|
+
|
|
138
|
+ WvBean updateBean = DbHelper.GetInstance().SelectOneCmdByIndex(updateId);
|
|
139
|
+ if (updateBean != null)
|
|
140
|
+ {
|
|
141
|
+ updateBean.userValue = content;
|
|
142
|
+ int result = DbHelper.GetInstance().UpdateCmd(updateBean);
|
|
143
|
+ if (result > 0)
|
|
144
|
+ {
|
|
145
|
+ DataModel.ReceCollection.Add(
|
|
146
|
+ new UIInfoModel("设置成功.更新=(" + updateId + ") ;16进制=(" +
|
|
147
|
+ Utils.tenToHex(updateId) + "); 内容=" + content)
|
|
148
|
+ .setAction(-1)
|
|
149
|
+ .setSendType(SystemConfig.ReplyType));
|
|
150
|
+ return "03" + msgFlow + "00";
|
|
151
|
+ }
|
|
152
|
+ }
|
|
153
|
+ //不存在这样的Key
|
|
154
|
+ DataModel.ReceCollection.Add(
|
|
155
|
+ new UIInfoModel("设置失败.本地数据库没有这样的功能码=(" + updateId + ") ;16进制=(" +
|
|
156
|
+ Utils.tenToHex(updateId) + ")")
|
|
157
|
+ .setAction(-1)
|
|
158
|
+ .setSendType(SystemConfig.ReplyType));
|
|
159
|
+ }
|
|
160
|
+ catch (Exception exception)
|
|
161
|
+ {
|
|
162
|
+ DataModel.ReceCollection.Add(
|
|
163
|
+ new UIInfoModel("设置数据时,出错." + exception.Message)
|
|
164
|
+ .setAction(-1)
|
|
165
|
+ .setSendType(SystemConfig.ReplyType));
|
|
166
|
+ }
|
|
167
|
+ return "03" + msgFlow + "01";
|
112
|
168
|
}
|
113
|
169
|
}
|
114
|
170
|
}
|