Browse Source

完善DTU部分

张泳健 7 years ago
parent
commit
3d756a5121

+ 1 - 0
.gitignore View File

1
 .vs/*
1
 .vs/*
2
+.idea/*
2
 packages/*
3
 packages/*
3
 DotNettyFromTests/obj/*
4
 DotNettyFromTests/obj/*
4
 DotNettyFromTests/bin/*
5
 DotNettyFromTests/bin/*

BIN
DotNettyFrom.accdb View File


+ 6 - 0
DotNettyFrom/DotNettyFrom.csproj View File

34
   <PropertyGroup>
34
   <PropertyGroup>
35
     <StartupObject />
35
     <StartupObject />
36
   </PropertyGroup>
36
   </PropertyGroup>
37
+  <PropertyGroup>
38
+    <ApplicationIcon>DotNettyFrom.ico</ApplicationIcon>
39
+  </PropertyGroup>
37
   <ItemGroup>
40
   <ItemGroup>
38
     <Reference Include="DotNetty.Buffers, Version=0.4.6.0, Culture=neutral, PublicKeyToken=bc13ca065fa06c29, processorArchitecture=MSIL">
41
     <Reference Include="DotNetty.Buffers, Version=0.4.6.0, Culture=neutral, PublicKeyToken=bc13ca065fa06c29, processorArchitecture=MSIL">
39
       <HintPath>..\packages\DotNetty.Buffers.0.4.6\lib\net45\DotNetty.Buffers.dll</HintPath>
42
       <HintPath>..\packages\DotNetty.Buffers.0.4.6\lib\net45\DotNetty.Buffers.dll</HintPath>
241
   <ItemGroup>
244
   <ItemGroup>
242
     <Folder Include="cmdparser\" />
245
     <Folder Include="cmdparser\" />
243
   </ItemGroup>
246
   </ItemGroup>
247
+  <ItemGroup>
248
+    <Content Include="DotNettyFrom.ico" />
249
+  </ItemGroup>
244
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
250
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
245
 </Project>
251
 </Project>

BIN
DotNettyFrom/DotNettyFrom.ico View File


+ 2 - 0
DotNettyFrom/MainForm.Designer.cs View File

29
         private void InitializeComponent()
29
         private void InitializeComponent()
30
         {
30
         {
31
             this.components = new System.ComponentModel.Container();
31
             this.components = new System.ComponentModel.Container();
32
+            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
32
             this.LBLogShow = new System.Windows.Forms.ListBox();
33
             this.LBLogShow = new System.Windows.Forms.ListBox();
33
             this.ListBoxLogContextMenu = new System.Windows.Forms.ContextMenuStrip(this.components);
34
             this.ListBoxLogContextMenu = new System.Windows.Forms.ContextMenuStrip(this.components);
34
             this.LB_Context_CpCmd = new System.Windows.Forms.ToolStripMenuItem();
35
             this.LB_Context_CpCmd = new System.Windows.Forms.ToolStripMenuItem();
607
             this.Controls.Add(this.LBLogShow);
608
             this.Controls.Add(this.LBLogShow);
608
             this.Controls.Add(this.menuStrip1);
609
             this.Controls.Add(this.menuStrip1);
609
             this.Font = new System.Drawing.Font("微软雅黑", 8.5F);
610
             this.Font = new System.Drawing.Font("微软雅黑", 8.5F);
611
+            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
610
             this.MainMenuStrip = this.menuStrip1;
612
             this.MainMenuStrip = this.menuStrip1;
611
             this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
613
             this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
612
             this.Name = "MainForm";
614
             this.Name = "MainForm";

+ 15 - 14
DotNettyFrom/MainForm.cs View File

51
 
51
 
52
             ChangeDtu();
52
             ChangeDtu();
53
 
53
 
54
-            if ("".Equals(TbDeviceId.Text.Trim()))
55
-            {
56
-                CmdGenetor.Genetor.DeviceId = "000000000001";
57
-            }
58
-            else
59
-            {
60
-                CmdGenetor.Genetor.DeviceId = TbDeviceId.Text.Trim();
61
-            }
54
+            TbDeviceId.Text = SystemConfig.DefaultNo;
55
+
62
 
56
 
63
             var consumer = Task.Factory.StartNew(() =>
57
             var consumer = Task.Factory.StartNew(() =>
64
             {
58
             {
153
                 this.host = hostAddress.ToString();
147
                 this.host = hostAddress.ToString();
154
                 this.port = int.Parse(TbPort.Text.Trim());
148
                 this.port = int.Parse(TbPort.Text.Trim());
155
 
149
 
150
+                //设置地址
151
+                CmdGenetor.Genetor.DeviceId = TbDeviceId.Text.Trim();
152
+
156
                 UpdateUiComponent();
153
                 UpdateUiComponent();
157
             }
154
             }
158
             catch (SocketException exception)
155
             catch (SocketException exception)
176
             water2ToolStripMenuItem.Checked = true;
173
             water2ToolStripMenuItem.Checked = true;
177
             CmdGenetor.Genetor = null;
174
             CmdGenetor.Genetor = null;
178
             CmdGenetor.Genetor = new Water2CmdGenetor();
175
             CmdGenetor.Genetor = new Water2CmdGenetor();
179
-
180
             ConfigChange.ChangeDevice(SystemConfig.Water2);
176
             ConfigChange.ChangeDevice(SystemConfig.Water2);
177
+            TbDeviceId.Text = SystemConfig.DefaultNo;
178
+            TbPort.Text = "" + SystemConfig.DefaultPort;
181
         }
179
         }
182
 
180
 
181
+        /// <summary>
182
+        /// 记得Reload数据库
183
+        /// </summary>
183
         private void ChangeDtu()
184
         private void ChangeDtu()
184
         {
185
         {
185
             this.Text = "模拟器-DTU";
186
             this.Text = "模拟器-DTU";
188
             CmdGenetor.Genetor = null;
189
             CmdGenetor.Genetor = null;
189
             CmdGenetor.Genetor = new DtuCmdGenetor();
190
             CmdGenetor.Genetor = new DtuCmdGenetor();
190
             ConfigChange.ChangeDevice(SystemConfig.Dtu);
191
             ConfigChange.ChangeDevice(SystemConfig.Dtu);
192
+            TbDeviceId.Text = SystemConfig.DefaultNo;
193
+            TbPort.Text = "" + SystemConfig.DefaultPort;
191
         }
194
         }
192
 
195
 
193
         #region Component Event
196
         #region Component Event
196
         {
199
         {
197
             BeforeConnection();
200
             BeforeConnection();
198
 
201
 
199
-            CmdGenetor.Genetor.DeviceId = TbDeviceId.Text.Trim();
200
             Task startNew = Task.Factory.StartNew(() =>
202
             Task startNew = Task.Factory.StartNew(() =>
201
             {
203
             {
202
                 _nettyClient = new NettyClient();
204
                 _nettyClient = new NettyClient();
210
 
212
 
211
             Task startNew = Task.Factory.StartNew(() =>
213
             Task startNew = Task.Factory.StartNew(() =>
212
             {
214
             {
213
-                CmdGenetor.Genetor.DeviceId = TbDeviceId.Text.Trim();
214
                 string loginCmd = CmdGenetor.Genetor.GetLoginCmd(TbCCID.Text.Trim());
215
                 string loginCmd = CmdGenetor.Genetor.GetLoginCmd(TbCCID.Text.Trim());
215
                 _nettyClient = new NettyClient();
216
                 _nettyClient = new NettyClient();
216
                 _nettyClient.StartNetty(host, port, loginCmd);
217
                 _nettyClient.StartNetty(host, port, loginCmd);
381
             if (index >= 0 && index < LBLogShow.Items.Count)
382
             if (index >= 0 && index < LBLogShow.Items.Count)
382
             {
383
             {
383
                 UIInfoModel uiInfoModel = LBLogShow.SelectedItem as UIInfoModel;
384
                 UIInfoModel uiInfoModel = LBLogShow.SelectedItem as UIInfoModel;
384
-                string line = uiInfoModel.ToString();
385
+                string line = uiInfoModel?.ToString();
385
                 if (!line.Trim().Equals(""))
386
                 if (!line.Trim().Equals(""))
386
                     Clipboard.SetDataObject(line);
387
                     Clipboard.SetDataObject(line);
387
             }
388
             }
393
             if (index >= 0 && index < LBLogShow.Items.Count)
394
             if (index >= 0 && index < LBLogShow.Items.Count)
394
             {
395
             {
395
                 UIInfoModel uiInfoModel = LBLogShow.SelectedItem as UIInfoModel;
396
                 UIInfoModel uiInfoModel = LBLogShow.SelectedItem as UIInfoModel;
396
-                string line = CmdUtil.FilterCmd(uiInfoModel.ToString());
397
+                string line = CmdUtil.FilterCmd(uiInfoModel?.ToString());
397
                 if (!line.Trim().Equals(""))
398
                 if (!line.Trim().Equals(""))
398
                 {
399
                 {
399
                 }
400
                 }
406
             if (index >= 0 && index < LBLogShow.Items.Count)
407
             if (index >= 0 && index < LBLogShow.Items.Count)
407
             {
408
             {
408
                 UIInfoModel uiInfoModel = LBLogShow.SelectedItem as UIInfoModel;
409
                 UIInfoModel uiInfoModel = LBLogShow.SelectedItem as UIInfoModel;
409
-                string line = CmdUtil.FilterCmd(uiInfoModel.ToString());
410
+                string line = CmdUtil.FilterCmd(uiInfoModel?.ToString());
410
                 if (!line.Trim().Equals(""))
411
                 if (!line.Trim().Equals(""))
411
                 {
412
                 {
412
                 }
413
                 }

File diff suppressed because it is too large
+ 4512 - 0
DotNettyFrom/MainForm.resx


+ 1 - 1
DotNettyFrom/Properties/AssemblyInfo.cs View File

8
 [assembly: AssemblyTitle("DotNettyFrom")]
8
 [assembly: AssemblyTitle("DotNettyFrom")]
9
 [assembly: AssemblyDescription("")]
9
 [assembly: AssemblyDescription("")]
10
 [assembly: AssemblyConfiguration("")]
10
 [assembly: AssemblyConfiguration("")]
11
-[assembly: AssemblyCompany("")]
11
+[assembly: AssemblyCompany("Vcarecity")]
12
 [assembly: AssemblyProduct("DotNettyFrom")]
12
 [assembly: AssemblyProduct("DotNettyFrom")]
13
 [assembly: AssemblyCopyright("Copyright ©  2017")]
13
 [assembly: AssemblyCopyright("Copyright ©  2017")]
14
 [assembly: AssemblyTrademark("")]
14
 [assembly: AssemblyTrademark("")]

+ 4 - 2
DotNettyFrom/common/UIInfoModel..cs View File

75
             {
75
             {
76
                 try
76
                 try
77
                 {
77
                 {
78
-                    string cmdIdString = cmd.Substring(SystemConfig.FunctionCodeStartPos, SystemConfig.FunctionCodeLength);
78
+                    string cmdIdString = cmd.Substring(SystemConfig.FunctionCodeStartPos * 2,
79
+                        SystemConfig.FunctionCodeLength * 2);
79
                     res += ". cmdId=" + cmdIdString;
80
                     res += ". cmdId=" + cmdIdString;
80
                     cmdId = CalcCmdId(cmd);
81
                     cmdId = CalcCmdId(cmd);
81
                 }
82
                 }
91
         {
92
         {
92
             try
93
             try
93
             {
94
             {
94
-                string cmdId = cmd.Substring(SystemConfig.FunctionCodeStartPos, SystemConfig.FunctionCodeLength);
95
+                string cmdId = cmd.Substring(SystemConfig.FunctionCodeStartPos * 2,
96
+                    SystemConfig.FunctionCodeLength * 2);
95
                 return int.Parse(cmdId);
97
                 return int.Parse(cmdId);
96
             }
98
             }
97
             catch (Exception)
99
             catch (Exception)

+ 13 - 4
DotNettyFrom/config/ConfigChange.cs View File

3
 using System.Linq;
3
 using System.Linq;
4
 using System.Text;
4
 using System.Text;
5
 using System.Threading.Tasks;
5
 using System.Threading.Tasks;
6
+using DotNettyFrom.db;
6
 
7
 
7
 namespace DotNettyFrom.config
8
 namespace DotNettyFrom.config
8
 {
9
 {
18
                 case SystemConfig.Water2:
19
                 case SystemConfig.Water2:
19
                     changeWater2();
20
                     changeWater2();
20
                     break;
21
                     break;
21
-                default:
22
-                    break;
23
             }
22
             }
24
         }
23
         }
25
 
24
 
39
 
38
 
40
             SystemConfig.MeasurePointLen = Water2Config.MeasurePointLen;
39
             SystemConfig.MeasurePointLen = Water2Config.MeasurePointLen;
41
             SystemConfig.MessageFlowLen = Water2Config.MessageFlowLen;
40
             SystemConfig.MessageFlowLen = Water2Config.MessageFlowLen;
42
-      
43
-    }
41
+
42
+
43
+            SystemConfig.DefaultNo = Water2Config.DefaultWater2No;
44
+            SystemConfig.DefaultPort = Water2Config.DefaulWater2tPort;
45
+            
46
+            
47
+            
48
+            
49
+        }
44
 
50
 
45
         private static void changeDTU()
51
         private static void changeDTU()
46
         {
52
         {
58
 
64
 
59
             SystemConfig.MeasurePointLen = DtuConfig.MeasurePointLen;
65
             SystemConfig.MeasurePointLen = DtuConfig.MeasurePointLen;
60
             SystemConfig.MessageFlowLen = DtuConfig.MessageFlowLen;
66
             SystemConfig.MessageFlowLen = DtuConfig.MessageFlowLen;
67
+
68
+            SystemConfig.DefaultNo = DtuConfig.DefaultDtuNo;
69
+            SystemConfig.DefaultPort = DtuConfig.DefaulDtutPort;
61
         }
70
         }
62
     }
71
     }
63
 }
72
 }

+ 7 - 2
DotNettyFrom/config/DtuConfig.cs View File

8
 {
8
 {
9
     public class DtuConfig
9
     public class DtuConfig
10
     {
10
     {
11
+        public static string DefaultDtuNo = "000000000001";
12
+
13
+        public static int DefaulDtutPort = 15440;
14
+
11
         //功能码的位置,长度
15
         //功能码的位置,长度
12
-        public static int FunctionCodeStartPos = 18;
13
-        public static int FunctionCodeLength = 2;
16
+        public static int FunctionCodeStartPos = 9;
17
+
18
+        public static int FunctionCodeLength = 1;
14
 
19
 
15
 
20
 
16
         //心跳指令的功能码,用来判定是否是心跳
21
         //心跳指令的功能码,用来判定是否是心跳

+ 11 - 4
DotNettyFrom/config/SystemConfig.cs View File

14
         //当前是哪一个设备
14
         //当前是哪一个设备
15
         public static int CurrentType;
15
         public static int CurrentType;
16
 
16
 
17
+        public static string DefaultNo = DtuConfig.DefaultDtuNo;
18
+        public static int DefaultPort = DtuConfig.DefaulDtutPort;
19
+
20
+
17
         //是否打印心跳指令
21
         //是否打印心跳指令
18
         public static bool IsLogHeartBeatInfo = false;
22
         public static bool IsLogHeartBeatInfo = false;
19
 
23
 
21
         public static int HeartbeatDownCmdCode = 4;
25
         public static int HeartbeatDownCmdCode = 4;
22
 
26
 
23
         //功能码的位置,长度
27
         //功能码的位置,长度
24
-        public static int FunctionCodeStartPos = 18;
28
+        public static int FunctionCodeStartPos = 9;
25
 
29
 
26
-        public static int FunctionCodeLength = 2;
30
+        public static int FunctionCodeLength = 1;
27
 
31
 
28
         //定时发送心中
32
         //定时发送心中
29
         public static bool OpenHeartBeat = true;
33
         public static bool OpenHeartBeat = true;
37
 
41
 
38
         //测量点&消息流水
42
         //测量点&消息流水
39
         public static int MeasurePointLen = 1;
43
         public static int MeasurePointLen = 1;
40
-        public static int MessageFlowLen = 2;
41
 
44
 
45
+        public static int MessageFlowLen = 2;
42
 
46
 
43
 
47
 
44
-        //粘包-半包问题
48
+        //粘包-半包问题,长度
45
         public static int LengthFieldOffset = 10;
49
         public static int LengthFieldOffset = 10;
50
+
46
         public static int LengthFieldLength = 2;
51
         public static int LengthFieldLength = 2;
47
         public static int LengthAdjustment = 1;
52
         public static int LengthAdjustment = 1;
48
         public static int InitialBytesToStrip = 0;
53
         public static int InitialBytesToStrip = 0;
54
 
59
 
55
         //1 发送的. 0:接收的, -1.Debug.日常信息
60
         //1 发送的. 0:接收的, -1.Debug.日常信息
56
         public static int SendType = 1;
61
         public static int SendType = 1;
62
+
57
         public static int RecType = 0;
63
         public static int RecType = 0;
58
         public static int DebugType = -1;
64
         public static int DebugType = -1;
59
         public static int ReplyType = 2;
65
         public static int ReplyType = 2;
60
 
66
 
61
         //数据库对应
67
         //数据库对应
62
         public const int IpHisType = 1;
68
         public const int IpHisType = 1;
69
+
63
         public const int AddrHisType = 2;
70
         public const int AddrHisType = 2;
64
         public const int SendHisType = 3;
71
         public const int SendHisType = 3;
65
         public const int CcidHisType = 4;
72
         public const int CcidHisType = 4;

+ 6 - 2
DotNettyFrom/config/Water2Config.cs View File

8
 {
8
 {
9
     public class Water2Config
9
     public class Water2Config
10
     {
10
     {
11
+        public static string DefaultWater2No = "000000000002";
12
+
13
+        public static int DefaulWater2tPort = 15443;
14
+
11
         //功能码的位置,长度
15
         //功能码的位置,长度
12
-        public static int FunctionCodeStartPos = 18;
13
-        public static int FunctionCodeLength = 2;
16
+        public static int FunctionCodeStartPos = 9;
17
+        public static int FunctionCodeLength = 1;
14
 
18
 
15
         //心跳指令的功能码,用来判定是否是心跳
19
         //心跳指令的功能码,用来判定是否是心跳
16
         public static int HeartbeatDownCmdCode = 3;
20
         public static int HeartbeatDownCmdCode = 3;

+ 26 - 0
DotNettyFrom/db/DBHelper.cs View File

237
             }
237
             }
238
         }
238
         }
239
 
239
 
240
+        /// <summary>
241
+        /// 首次数据库加载到内存
242
+        /// </summary>
243
+        /// <param name="sql"></param>
244
+        public void ReloadMoneryCache(string sql = null)
245
+        {
246
+            if (sql == null)
247
+            {
248
+                string table = DBConfig.GetTableName();
249
+                sql = @"select * from " + table;
250
+            }
251
+            using (OleDbCommand sqlcmd = new OleDbCommand(sql, _DbConn))
252
+            {
253
+                using (OleDbDataReader reader = sqlcmd.ExecuteReader())
254
+                {
255
+                    while (reader != null && reader.Read()) //这个read调用很重要!不写的话运行时将提示找不到数据  
256
+                    {
257
+                        var myId = reader.GetString(2);
258
+                        var userValue = CheckUtil.StringIsEmpty(reader.GetString(8));
259
+                        if (!_idInfo.ContainsKey(myId))
260
+                            _idInfo.Add(myId, userValue);
261
+                    }
262
+                }
263
+            }
264
+        }
265
+
240
         //Select
266
         //Select
241
         public DataTable SelectCmds(string sql = null)
267
         public DataTable SelectCmds(string sql = null)
242
         {
268
         {

+ 6 - 4
DotNettyFrom/form/ItemManagerForm.cs View File

7
 using System.Text;
7
 using System.Text;
8
 using System.Threading.Tasks;
8
 using System.Threading.Tasks;
9
 using System.Windows.Forms;
9
 using System.Windows.Forms;
10
+using DotNettyFrom.config;
10
 using DotNettyFrom.db;
11
 using DotNettyFrom.db;
11
 using DotNettyFrom.excel;
12
 using DotNettyFrom.excel;
12
 using DotNettyFrom.model;
13
 using DotNettyFrom.model;
167
 //                名字 - IDInfo
168
 //                名字 - IDInfo
168
 //                描述 - Desp
169
 //                描述 - Desp
169
                 int mType = CbFindType.SelectedIndex;
170
                 int mType = CbFindType.SelectedIndex;
171
+                string table = DBConfig.GetTableName();
170
                 string sql = "";
172
                 string sql = "";
171
                 switch (mType)
173
                 switch (mType)
172
                 {
174
                 {
173
                     case 0:
175
                     case 0:
174
-                        sql = @"select* from fw where my_index = " + query;
176
+                        sql = @"select* from " + table + " where my_index = " + query;
175
                         break;
177
                         break;
176
                     case 1:
178
                     case 1:
177
-                        sql = @"select* from fw where MY_ID = '" + query + "'";
179
+                        sql = @"select* from " + table + " where MY_ID = '" + query + "'";
178
                         break;
180
                         break;
179
                     case 2:
181
                     case 2:
180
-                        sql = @"select* from fw where idInfo like '%" + query + "%'";
182
+                        sql = @"select* from " + table + " where idInfo like '%" + query + "%'";
181
                         break;
183
                         break;
182
                     case 3:
184
                     case 3:
183
-                        sql = @"select* from fw where desp like '%" + query + "%'";
185
+                        sql = @"select* from " + table + " where desp like '%" + query + "%'";
184
                         break;
186
                         break;
185
                     default:
187
                     default:
186
                         ReLoadDatatable();
188
                         ReLoadDatatable();

+ 3 - 2
DotNettyFrom/generatecmd/CmdGenetor.cs View File

6
 using DotNetty.Handlers.Timeout;
6
 using DotNetty.Handlers.Timeout;
7
 using DotNettyFrom.config;
7
 using DotNettyFrom.config;
8
 using DotNettyFrom.util;
8
 using DotNettyFrom.util;
9
+using NUnit.Framework;
9
 
10
 
10
 namespace DotNettyFrom.generatecmd
11
 namespace DotNettyFrom.generatecmd
11
 {
12
 {
25
         {
26
         {
26
             try
27
             try
27
             {
28
             {
28
-                string cmdId = receiveCmd.Substring(SystemConfig.FunctionCodeStartPos, SystemConfig.FunctionCodeLength);
29
+                string cmdId = receiveCmd.Substring(2 * SystemConfig.FunctionCodeStartPos,
30
+                    2 * SystemConfig.FunctionCodeLength);
29
                 return Utils.hexToTen(cmdId);
31
                 return Utils.hexToTen(cmdId);
30
             }
32
             }
31
             catch (Exception)
33
             catch (Exception)
33
             }
35
             }
34
             return int.MinValue;
36
             return int.MinValue;
35
         }
37
         }
36
-
37
     }
38
     }
38
 }
39
 }

+ 200 - 2
DotNettyFrom/generatecmd/DtuCmdGenetor.cs View File

1
 using System;
1
 using System;
2
 using System.Collections.Generic;
2
 using System.Collections.Generic;
3
 using System.Linq;
3
 using System.Linq;
4
+using System.Security.AccessControl;
4
 using System.Text;
5
 using System.Text;
5
 using System.Threading.Tasks;
6
 using System.Threading.Tasks;
7
+using System.Windows.Forms;
8
+using DotNettyFrom.common;
9
+using DotNettyFrom.config;
10
+using DotNettyFrom.db;
6
 using DotNettyFrom.generatecmd;
11
 using DotNettyFrom.generatecmd;
12
+using DotNettyFrom.model;
7
 using DotNettyFrom.util;
13
 using DotNettyFrom.util;
8
 
14
 
9
 namespace DotNettyFrom.generatecmd
15
 namespace DotNettyFrom.generatecmd
10
 {
16
 {
11
     public class DtuCmdGenetor : CmdGenetor
17
     public class DtuCmdGenetor : CmdGenetor
12
     {
18
     {
13
-        public override string GetLoginCmd( string ccid)
19
+        //ID LEN
20
+        private const int ID_LEN = 2;
21
+
22
+        private const int FUNC_REPLY_READ = 16; //要回复的功能码
23
+        private const int FUNC_REPLY_WRITE = 32; //要回复的功能码
24
+
25
+        public override string GetLoginCmd(string ccid)
14
         {
26
         {
15
             string cmd = DeviceId + "820014" + ccid;
27
             string cmd = DeviceId + "820014" + ccid;
16
             cmd = "7e7e7e" + CmdUtil.CalcCrcCode(cmd);
28
             cmd = "7e7e7e" + CmdUtil.CalcCrcCode(cmd);
27
 
39
 
28
         public override string GetReplyCmd(string receiveCmd)
40
         public override string GetReplyCmd(string receiveCmd)
29
         {
41
         {
30
-            throw new NotImplementedException();
42
+            int cmdId = GetCmdId(receiveCmd);
43
+            Console.WriteLine("收到功能码=" + cmdId + " 16进制=" + Utils.tenToHex(cmdId));
44
+
45
+
46
+            //跳过心跳包
47
+            if (cmdId == SystemConfig.HeartbeatDownCmdCode)
48
+            {
49
+                if (SystemConfig.IsLogHeartBeatInfo)
50
+                {
51
+                    DataModel.ReceCollection.Add(new UIInfoModel("收到心跳", receiveCmd));
52
+                }
53
+                return null;
54
+            }
55
+
56
+            //处理其他的主动上报的回复
57
+
58
+
59
+            //处理其他的主动上报的回复
60
+
61
+
62
+            if (cmdId == int.MinValue)
63
+            {
64
+                DataModel.ReceCollection.Add(new UIInfoModel("帧错误", receiveCmd)
65
+                    .setAction(-1)
66
+                    .setSendType(SystemConfig.ReplyType));
67
+                return null;
68
+            }
69
+
70
+            int position = SystemConfig.FunctionCodeStartPos * 2;
71
+            //前面相同的, 到功能码前面
72
+            string resp = receiveCmd.Substring(0, position);
73
+            Console.WriteLine("前面回复相同的=" + resp);
74
+
75
+            position += SystemConfig.FunctionCodeLength * 2;
76
+
77
+            string frameDataLen = receiveCmd.Substring(position, SystemConfig.LengthFieldLength * 2);
78
+
79
+            //帧中定义的数据长度.10进制的
80
+            int frameDefineLen = Utils.hexToTen(frameDataLen);
81
+
82
+            Console.WriteLine("数据长度= " + frameDataLen + "  -- 10进制=" + Utils.hexToTen(frameDataLen));
83
+
84
+            position += SystemConfig.LengthFieldLength * 2;
85
+
86
+            //测量点&消息流水-所占长度
87
+            int msgFlowLength = SystemConfig.MeasurePointLen + SystemConfig.MessageFlowLen;
88
+
89
+            //测量点&消息流水
90
+            string msgFlow = receiveCmd.Substring(position, msgFlowLength * 2);
91
+            position += msgFlowLength * 2;
92
+            Console.WriteLine("测量点+消息流水=" + msgFlow);
93
+
94
+            string codeIdHex = receiveCmd.Substring(position, ID_LEN * 2);
95
+            int codeId = Utils.hexToTen(codeIdHex);
96
+            //0301 这些写死,2位
97
+            position += ID_LEN * 2;
98
+            Console.WriteLine("功能码=" + codeIdHex + "; 10进制=" + Utils.hexToTen(codeIdHex));
99
+
100
+
101
+            if (FUNC_REPLY_READ == cmdId)
102
+            {
103
+                DataModel.ReceCollection.Add(new UIInfoModel("收到读取", receiveCmd));
104
+
105
+                //读,回复
106
+                WvBean wb = DbHelper.GetInstance().SelectOneCmdByIndex(codeId);
107
+
108
+                if (wb == null)
109
+                {
110
+                    DataModel.ReceCollection.Add(
111
+                        new UIInfoModel(
112
+                                "读取-字典中不存在这个功能码=" + codeId + " 16进制=" + codeIdHex,
113
+                                receiveCmd)
114
+                            .setAction(-1)
115
+                            .setSendType(SystemConfig.ReplyType));
116
+                    return null;
117
+                }
118
+
119
+                string ud = wb.userValue;
120
+                Console.WriteLine(ud);
121
+                string lenMsgFlowData = ReadMessage(wb, msgFlow, codeIdHex);
122
+
123
+                int replyCmdId = cmdId + 128;
124
+                resp += CmdUtil.FrontWithZero(Utils.tenToHex(replyCmdId), SystemConfig.FunctionCodeLength * 2);
125
+                resp += lenMsgFlowData;
126
+                resp += CmdUtil.CalcCrcCodeSkip(resp, 3);
127
+
128
+                DataModel.ReceCollection.Add(
129
+                    new UIInfoModel("读取成功,回复", resp)
130
+                        .setAction(-1)
131
+                        .setSendType(SystemConfig.ReplyType));
132
+
133
+                return resp;
134
+            }
135
+            else if (FUNC_REPLY_WRITE == cmdId)
136
+            {
137
+                DataModel.ReceCollection.Add(new UIInfoModel("收到设置", receiveCmd));
138
+
139
+                //写
140
+                WvBean wb = DbHelper.GetInstance().SelectOneCmdByIndex(codeId);
141
+
142
+                if (wb == null)
143
+                {
144
+                    DataModel.ReceCollection.Add(
145
+                        new UIInfoModel(
146
+                                "设置-字典中不存在这个功能码=" + codeId + " 16进制=" + codeIdHex,
147
+                                receiveCmd)
148
+                            .setAction(-1)
149
+                            .setSendType(SystemConfig.ReplyType));
150
+                    return null;
151
+                }
152
+
153
+                //取出设置数据
154
+                Console.WriteLine("长度   " + (frameDefineLen - msgFlowLength - ID_LEN));
155
+                string setData = receiveCmd.Substring(position, 2 * (frameDefineLen - msgFlowLength - ID_LEN));
156
+
157
+                wb.userValue = setData;
158
+
159
+                string resultData = "";
160
+                string tip = "";
161
+                int result = DbHelper.GetInstance().UpdateCmd(wb);
162
+                if (result > 0)
163
+                {
164
+                    resultData = "00";
165
+                    tip = "设置成功,回复";
166
+                }
167
+                else
168
+                {
169
+                    resultData = "01";
170
+                    tip = "设置失败,回复";
171
+                }
172
+
173
+
174
+//                7E7E7E000000000001a000a1
175
+
176
+                int replyCmdId = cmdId + 128;
177
+                resp += CmdUtil.FrontWithZero(Utils.tenToHex(replyCmdId), SystemConfig.FunctionCodeLength * 2); //功能码
178
+
179
+                string replyData = msgFlow + codeIdHex + resultData;
180
+                if (replyData.Length % 2 != 0)
181
+                {
182
+                    replyData += "0";
183
+                }
184
+                //长度
185
+                string lenHex =
186
+                    CmdUtil.FrontWithZero(Utils.tenToHex(replyData.Length / 2), SystemConfig.LengthFieldLength * 2);
187
+                resp += lenHex;
188
+                resp += replyData; //消息体
189
+                resp += CmdUtil.CalcCrcCodeSkip(resp, 3);
190
+                DataModel.ReceCollection.Add(
191
+                    new UIInfoModel(tip + " set(" + codeIdHex + ")=value(" + setData + ")", resp)
192
+                        .setAction(-1)
193
+                        .setSendType(SystemConfig.ReplyType));
194
+
195
+                return resp;
196
+            }
197
+
198
+
199
+//            LengthAdjustment
200
+//            string codeData = receiveCmd.Substring(position, receiveCmd.Length - SystemConfig.LengthAdjustment * 2);
201
+//            Console.WriteLine("收到数量=" + codeData);
202
+
203
+            //处理主动上报的回复&心跳.那些不用回复
204
+
205
+            //处理主动上报的回复&心跳.那些不用回复
206
+
207
+
208
+//            WvBean wb = DbHelper.GetInstance().SelectOneCmdById(cmdId);
209
+//            Console.WriteLine(wb);
210
+
211
+            DataModel.ReceCollection.Add(new UIInfoModel("收到", receiveCmd));
212
+
213
+
214
+            return null;
215
+        }
216
+
217
+        private string ReadMessage(WvBean wb, string msgFlow, string codeIdHex)
218
+        {
219
+            string content = msgFlow + codeIdHex + wb.userValue.Trim();
220
+            if (content.Length % 2 != 0)
221
+            {
222
+                content = "0" + content;
223
+            }
224
+
225
+            string lenHex =
226
+                CmdUtil.FrontWithZero(Utils.tenToHex(content.Length / 2), SystemConfig.LengthFieldLength * 2);
227
+
228
+            return lenHex + content;
31
         }
229
         }
32
     }
230
     }
33
 }
231
 }

+ 16 - 2
DotNettyFrom/generatecmd/Water2CmdGenetor.cs View File

12
 
12
 
13
 namespace DotNettyFrom.generatecmd
13
 namespace DotNettyFrom.generatecmd
14
 {
14
 {
15
+    /// <summary>
16
+    /// 不用缓存了,直接直接Select(查询)数据库
17
+    /// </summary>
15
     public class Water2CmdGenetor : CmdGenetor
18
     public class Water2CmdGenetor : CmdGenetor
16
     {
19
     {
17
         public override string GetLoginCmd(string ccid)
20
         public override string GetLoginCmd(string ccid)
30
         {
33
         {
31
             //功能码
34
             //功能码
32
             int cmdId = GetCmdId(receiveCmd);
35
             int cmdId = GetCmdId(receiveCmd);
33
-
34
             Console.WriteLine("功能码=" + cmdId + "; 16=" + Utils.tenToHex(cmdId));
36
             Console.WriteLine("功能码=" + cmdId + "; 16=" + Utils.tenToHex(cmdId));
35
 
37
 
38
+
39
+            if (SystemConfig.HeartbeatDownCmdCode == cmdId)
40
+            {
41
+                if (SystemConfig.IsLogHeartBeatInfo)
42
+                {
43
+                    DataModel.ReceCollection.Add(new UIInfoModel("收到心跳", receiveCmd));
44
+                }
45
+                return null;
46
+            }
47
+
36
             //处理主动上报的回复&心跳.那些不用回复
48
             //处理主动上报的回复&心跳.那些不用回复
37
 
49
 
38
             //处理主动上报的回复&心跳.那些不用回复
50
             //处理主动上报的回复&心跳.那些不用回复
39
 
51
 
52
+            DataModel.ReceCollection.Add(new UIInfoModel("收到", receiveCmd));
53
+
40
 
54
 
41
             if (cmdId == int.MinValue)
55
             if (cmdId == int.MinValue)
42
             {
56
             {
59
                 return null;
73
                 return null;
60
             }
74
             }
61
 
75
 
62
-            int pos = SystemConfig.FunctionCodeStartPos + SystemConfig.FunctionCodeLength;
76
+            int pos = 2 * (SystemConfig.FunctionCodeStartPos + SystemConfig.FunctionCodeLength);
63
 
77
 
64
 
78
 
65
             //前面的,回复用的
79
             //前面的,回复用的

+ 3 - 10
DotNettyFrom/netty/NettyClient.cs View File

124
                 if (message is IByteBuffer buffer)
124
                 if (message is IByteBuffer buffer)
125
                 {
125
                 {
126
                     string recCmd = AryUtil.ByteBuffer2Hex(buffer);
126
                     string recCmd = AryUtil.ByteBuffer2Hex(buffer);
127
-                    DataModel.ReceCollection.Add(new UIInfoModel("收到", recCmd));
127
+
128
                     Console.WriteLine("收到:" + recCmd);
128
                     Console.WriteLine("收到:" + recCmd);
129
 
129
 
130
                     string cmd = CmdGenetor.Genetor?.GetReplyCmd(recCmd);
130
                     string cmd = CmdGenetor.Genetor?.GetReplyCmd(recCmd);
133
                         IByteBuffer replyBuffer = AryUtil.Hex2IByteBuffer(cmd);
133
                         IByteBuffer replyBuffer = AryUtil.Hex2IByteBuffer(cmd);
134
                         context.WriteAndFlushAsync(replyBuffer);
134
                         context.WriteAndFlushAsync(replyBuffer);
135
                     }
135
                     }
136
-                    else
137
-                    {
138
-                        Console.WriteLine("reply is null");
139
-                    }
140
                 }
136
                 }
141
                 else
137
                 else
142
                 {
138
                 {
159
                     {
155
                     {
160
                         string hb = CmdGenetor.Genetor.GetHeartbeat();
156
                         string hb = CmdGenetor.Genetor.GetHeartbeat();
161
                         context.Channel.WriteAndFlushAsync(AryUtil.Hex2IByteBuffer(hb));
157
                         context.Channel.WriteAndFlushAsync(AryUtil.Hex2IByteBuffer(hb));
162
-                        Console.WriteLine("发心跳:" + hb + ".log=" + SystemConfig.IsLogHeartBeatInfo);
158
+                        Console.WriteLine("发心跳:" + hb + ".log=" + SystemConfig.IsLogHeartBeatInfo);
163
                         if (SystemConfig.IsLogHeartBeatInfo)
159
                         if (SystemConfig.IsLogHeartBeatInfo)
164
                         {
160
                         {
165
-                            DataModel.ReceCollection.Add(new UIInfoModel("发送", hb).setSendType(SystemConfig.SendType));
161
+                            DataModel.ReceCollection.Add(new UIInfoModel("发送心跳", hb).setSendType(SystemConfig.SendType));
166
                         }
162
                         }
167
                     }
163
                     }
168
-                    else
169
-                    {
170
-                    }
171
                 }
164
                 }
172
             }
165
             }
173
 
166
 

+ 19 - 1
DotNettyFrom/util/CmdUtil.cs View File

72
             }
72
             }
73
 
73
 
74
             int result = 0;
74
             int result = 0;
75
-            for (int i = 0; i < list.Count(); i++)
75
+            for (int i = 0; i < list.Count; i++)
76
             {
76
             {
77
                 result ^= Hex2Dec(list[i]);
77
                 result ^= Hex2Dec(list[i]);
78
             }
78
             }
96
             return str + FrontWithZero(Dec2Hex(result), 2);
96
             return str + FrontWithZero(Dec2Hex(result), 2);
97
         }
97
         }
98
 
98
 
99
+
100
+        public static string CalcCrcCodeSkip(string str, int skip)
101
+        {
102
+            List<string> list = new List<string>();
103
+            for (int i = 0; i < str.Length; i += 2)
104
+            {
105
+                list.Add(str.Substring(i, 2));
106
+            }
107
+
108
+            int result = 0;
109
+            for (int i = skip; i < list.Count; i++)
110
+            {
111
+                result ^= Hex2Dec(list[i]);
112
+            }
113
+            return FrontWithZero(Dec2Hex(result), 2);
114
+        }
115
+
116
+
99
         /// <summary>
117
         /// <summary>
100
         /// 只生成CRC16的校验码
118
         /// 只生成CRC16的校验码
101
         /// </summary>
119
         /// </summary>

+ 1 - 0
DotNettyFromTests/DotNettyFromTests.csproj View File

61
   <ItemGroup>
61
   <ItemGroup>
62
     <Compile Include="common\UIInfoModelTests.cs" />
62
     <Compile Include="common\UIInfoModelTests.cs" />
63
     <Compile Include="generatecmd\CmdGenetorTests.cs" />
63
     <Compile Include="generatecmd\CmdGenetorTests.cs" />
64
+    <Compile Include="generatecmd\DtuCmdGenetorTests.cs" />
64
     <Compile Include="generatecmd\Water2CmdGenetorTests.cs" />
65
     <Compile Include="generatecmd\Water2CmdGenetorTests.cs" />
65
     <Compile Include="Properties\AssemblyInfo.cs" />
66
     <Compile Include="Properties\AssemblyInfo.cs" />
66
     <Compile Include="UnitTest1.cs" />
67
     <Compile Include="UnitTest1.cs" />

+ 28 - 0
DotNettyFromTests/generatecmd/DtuCmdGenetorTests.cs View File

1
+using Microsoft.VisualStudio.TestTools.UnitTesting;
2
+using DotNettyFrom.generatecmd;
3
+using System;
4
+using System.Collections.Generic;
5
+using System.Linq;
6
+using System.Text;
7
+using System.Threading.Tasks;
8
+using DotNettyFrom.db;
9
+using DotNettyFrom.config;
10
+
11
+namespace DotNettyFrom.generatecmd.Tests
12
+{
13
+    [TestClass()]
14
+    public class DtuCmdGenetorTests
15
+    {
16
+        [TestMethod()]
17
+        public void GetReplyCmdTest()
18
+        {
19
+
20
+            DbHelper.Init(@"D:\CSharp\DotNettyFrom\DotNettyFrom\bin\Debug\DotNettyFrom.accdb");
21
+            ConfigChange.ChangeDevice(SystemConfig.Dtu);
22
+
23
+            DtuCmdGenetor dtuCmd = new DtuCmdGenetor();
24
+            //dtuCmd.GetReplyCmd("7e7e7e000000000001100005010101031006");
25
+            dtuCmd.GetReplyCmd("7e7e7e00000000000120000801010103100101f4cf");
26
+        }
27
+    }
28
+}

BIN
art/DotNettyFrom.ico View File


BIN
art/DotNettyFrom.png View File