Browse Source

指令化登录,心跳

张泳健 7 years ago
parent
commit
4aa1b0e52d

+ 4 - 1
DotNettyFrom/DotNettyFrom.csproj View File

157
     <Compile Include="form\ItemManagerForm.Designer.cs">
157
     <Compile Include="form\ItemManagerForm.Designer.cs">
158
       <DependentUpon>ItemManagerForm.cs</DependentUpon>
158
       <DependentUpon>ItemManagerForm.cs</DependentUpon>
159
     </Compile>
159
     </Compile>
160
+    <Compile Include="generatecmd\CmdGenetor.cs" />
160
     <Compile Include="MainForm.cs">
161
     <Compile Include="MainForm.cs">
161
       <SubType>Form</SubType>
162
       <SubType>Form</SubType>
162
     </Compile>
163
     </Compile>
209
   <ItemGroup>
210
   <ItemGroup>
210
     <None Include="App.config" />
211
     <None Include="App.config" />
211
   </ItemGroup>
212
   </ItemGroup>
212
-  <ItemGroup />
213
+  <ItemGroup>
214
+    <Folder Include="cmdparser\" />
215
+  </ItemGroup>
213
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
216
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
214
 </Project>
217
 </Project>

+ 6 - 2
DotNettyFrom/MainForm.cs View File

13
 using DotNettyFrom.db;
13
 using DotNettyFrom.db;
14
 using DotNettyFrom.dialog;
14
 using DotNettyFrom.dialog;
15
 using DotNettyFrom.form;
15
 using DotNettyFrom.form;
16
+using DotNettyFrom.generatecmd;
16
 using DotNettyFrom.netty;
17
 using DotNettyFrom.netty;
17
 using DotNettyFrom.util;
18
 using DotNettyFrom.util;
18
 
19
 
153
             Task startNew = Task.Factory.StartNew(() =>
154
             Task startNew = Task.Factory.StartNew(() =>
154
             {
155
             {
155
                 _nettyClient = new NettyClient();
156
                 _nettyClient = new NettyClient();
156
-                _nettyClient.StartNetty(host, port);
157
+                _nettyClient.StartNetty(host, port, null);
157
             });
158
             });
158
         }
159
         }
159
 
160
 
163
 
164
 
164
             Task startNew = Task.Factory.StartNew(() =>
165
             Task startNew = Task.Factory.StartNew(() =>
165
             {
166
             {
167
+                string loginCmd = CmdGenetor.GetInstance().SetDeviceId(TbDeviceId.Text.Trim())
168
+                    .GetLoginCmd(TbCCID.Text.Trim());
166
                 _nettyClient = new NettyClient();
169
                 _nettyClient = new NettyClient();
167
-                _nettyClient.StartNetty(host, port);
170
+                _nettyClient.StartNetty(host, port, loginCmd);
168
             });
171
             });
169
         }
172
         }
170
 
173
 
277
             int index = LBLogShow.SelectedIndex;
280
             int index = LBLogShow.SelectedIndex;
278
             if (index >= 0 && index < LBLogShow.Items.Count)
281
             if (index >= 0 && index < LBLogShow.Items.Count)
279
             {
282
             {
283
+                UIInfoModel uiInfoModel = LBLogShow.SelectedItem as UIInfoModel;
280
             }
284
             }
281
         }
285
         }
282
 
286
 

+ 2 - 2
DotNettyFrom/config/SystemConfig.cs View File

11
         public static int FunctionCodeStartPos = 18;
11
         public static int FunctionCodeStartPos = 18;
12
         public static int FunctionCodeLength = 2;
12
         public static int FunctionCodeLength = 2;
13
 
13
 
14
-        public static int ReaderIdleTimeSeconds = 30;
15
-        public static int WriterIdleTimeSeconds = 35;
14
+        public static int ReaderIdleTimeSeconds = 5;
15
+        public static int WriterIdleTimeSeconds = 5;
16
         public static int AllIdleTimeSeconds = 25;
16
         public static int AllIdleTimeSeconds = 25;
17
 
17
 
18
 
18
 

+ 48 - 0
DotNettyFrom/generatecmd/CmdGenetor.cs View File

1
+using System;
2
+using System.Collections.Generic;
3
+using System.Linq;
4
+using System.Text;
5
+using System.Threading.Tasks;
6
+using DotNetty.Handlers.Timeout;
7
+using DotNettyFrom.util;
8
+
9
+namespace DotNettyFrom.generatecmd
10
+{
11
+    public class CmdGenetor
12
+    {
13
+        public static CmdGenetor Instance = new CmdGenetor();
14
+        public string DeviceId { get; set; }
15
+
16
+
17
+        public static CmdGenetor GetInstance()
18
+        {
19
+            return Instance;
20
+        }
21
+
22
+        public CmdGenetor SetDeviceId(string deviceId)
23
+        {
24
+            this.DeviceId = deviceId;
25
+            return this;
26
+        }
27
+
28
+        private CmdGenetor()
29
+        {
30
+        }
31
+
32
+
33
+        public string GetLoginCmd(string ccid)
34
+        {
35
+            string cmd = DeviceId + "820014" + ccid;
36
+            cmd = "7e7e7e" + CmdUtil.CalcCrcCode(cmd);
37
+            return cmd;
38
+        }
39
+
40
+        public string GetHeartbeat()
41
+        {
42
+            //7e7e7e00000000000184000085
43
+            string cmd = DeviceId + "840000";
44
+            cmd = "7e7e7e" + CmdUtil.CalcCrcCode(cmd);
45
+            return cmd;
46
+        }
47
+    }
48
+}

+ 27 - 14
DotNettyFrom/netty/NettyClient.cs View File

15
 using DotNetty.Transport.Channels.Sockets;
15
 using DotNetty.Transport.Channels.Sockets;
16
 using DotNettyFrom.common;
16
 using DotNettyFrom.common;
17
 using DotNettyFrom.config;
17
 using DotNettyFrom.config;
18
+using DotNettyFrom.generatecmd;
18
 using DotNettyFrom.util;
19
 using DotNettyFrom.util;
19
 
20
 
20
 namespace DotNettyFrom.netty
21
 namespace DotNettyFrom.netty
23
     {
24
     {
24
         private MultithreadEventLoopGroup _group;
25
         private MultithreadEventLoopGroup _group;
25
         private NettyClientHandler _clientHandler;
26
         private NettyClientHandler _clientHandler;
26
-        private static bool _isSendLoginCmd = true;
27
+        private static string LoginCmd = null;
27
 
28
 
28
-        public void StartNetty(string host, int port, bool isSendLoginCmd = true)
29
+        public void StartNetty(string host, int port, string loginCmd)
29
         {
30
         {
30
-            _isSendLoginCmd = isSendLoginCmd;
31
+            LoginCmd = loginCmd;
31
             IPAddress hostIp = IPAddress.Parse(host);
32
             IPAddress hostIp = IPAddress.Parse(host);
32
             RunClientAsync(hostIp, port);
33
             RunClientAsync(hostIp, port);
33
             DataModel.ReceCollection.Add(new UIInfoModel("StartNetty:开始尝试连接").setSendType(SystemConfig.DebugType));
34
             DataModel.ReceCollection.Add(new UIInfoModel("StartNetty:开始尝试连接").setSendType(SystemConfig.DebugType));
106
                 DataModel.ReceCollection.Add(new UIInfoModel("ChannelActive:连接成功").setAction(1)
107
                 DataModel.ReceCollection.Add(new UIInfoModel("ChannelActive:连接成功").setAction(1)
107
                     .setSendType(SystemConfig.DebugType));
108
                     .setSendType(SystemConfig.DebugType));
108
 
109
 
109
-                if (_isSendLoginCmd)
110
+                if (LoginCmd != null)
110
                 {
111
                 {
111
                     //发送登录指令
112
                     //发送登录指令
112
-                    IByteBuffer cmd =
113
-                        AryUtil.Hex2IByteBuffer("7e7e7e0000000000018200143839383630324731313931353530333339393137e3");
113
+                    IByteBuffer cmd = AryUtil.Hex2IByteBuffer(LoginCmd);
114
                     context.WriteAndFlushAsync(cmd);
114
                     context.WriteAndFlushAsync(cmd);
115
-                    DataModel.ReceCollection.Add(
116
-                        new UIInfoModel("发送登录", "7e7e7e0000000000018200143839383630324731313931353530333339393137e3")
117
-                            .setAction(1).setSendType(SystemConfig.SendType));
115
+                    DataModel.ReceCollection.Add(new UIInfoModel("发送登录", LoginCmd)
116
+                        .setAction(1).setSendType(SystemConfig.SendType));
118
                 }
117
                 }
119
             }
118
             }
120
 
119
 
126
                     string recCmd = AryUtil.ByteBuffer2Hex(buffer);
125
                     string recCmd = AryUtil.ByteBuffer2Hex(buffer);
127
                     DataModel.ReceCollection.Add(new UIInfoModel("收到", recCmd));
126
                     DataModel.ReceCollection.Add(new UIInfoModel("收到", recCmd));
128
                 }
127
                 }
128
+                else
129
+                {
130
+                    Console.WriteLine("Other ChannelRead");
131
+                }
129
             }
132
             }
130
 
133
 
131
-            public override void ChannelReadComplete(IChannelHandlerContext context)
132
-            {
133
-                base.ChannelReadComplete(context);
134
-            }
135
 
134
 
136
             public override void UserEventTriggered(IChannelHandlerContext context, object evt)
135
             public override void UserEventTriggered(IChannelHandlerContext context, object evt)
137
             {
136
             {
138
-                base.UserEventTriggered(context, evt);
137
+                IdleStateEvent state = (IdleStateEvent) evt;
138
+
139
+
140
+                if (state == IdleStateEvent.ReaderIdleStateEvent)
141
+                {
142
+                }
143
+                else if (state == IdleStateEvent.WriterIdleStateEvent)
144
+                {
145
+                    string hb = CmdGenetor.GetInstance().GetHeartbeat();
146
+                    context.Channel.WriteAndFlushAsync(AryUtil.Hex2IByteBuffer(hb));
147
+                    DataModel.ReceCollection.Add(new UIInfoModel("发送", hb).setSendType(SystemConfig.SendType));
148
+                }
149
+                else
150
+                {
151
+                }
139
             }
152
             }
140
 
153
 
141
             public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
154
             public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)

+ 34 - 0
DotNettyFrom/util/CmdUtil.cs View File

62
             int res = Hex2Dec(hex);
62
             int res = Hex2Dec(hex);
63
             return FrontWithZero(Convert.ToString(res, 2), 8);
63
             return FrontWithZero(Convert.ToString(res, 2), 8);
64
         }
64
         }
65
+
66
+        public static string CalcCrcCode2(string str)
67
+        {
68
+            List<string> list = new List<string>();
69
+            for (int i = 0; i < str.Length; i += 2)
70
+            {
71
+                list.Add(str.Substring(i, 2));
72
+            }
73
+
74
+            int result = 0;
75
+            for (int i = 0; i < list.Count(); i++)
76
+            {
77
+                result ^= Hex2Dec(list[i]);
78
+            }
79
+            return FrontWithZero(Dec2Hex(result), 2);
80
+        }
81
+
82
+
83
+        public static string CalcCrcCode(string str)
84
+        {
85
+            List<string> list = new List<string>();
86
+            for (int i = 0; i < str.Length; i += 2)
87
+            {
88
+                list.Add(str.Substring(i, 2));
89
+            }
90
+
91
+            int result = 0;
92
+            for (int i = 0; i < list.Count(); i++)
93
+            {
94
+                result ^= Hex2Dec(list[i]);
95
+            }
96
+            return str + FrontWithZero(Dec2Hex(result), 2);
97
+        }
98
+
65
     }
99
     }
66
 }
100
 }

+ 1 - 0
DotNettyFromTests/DotNettyFromTests.csproj View File

60
   </Choose>
60
   </Choose>
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="Properties\AssemblyInfo.cs" />
64
     <Compile Include="Properties\AssemblyInfo.cs" />
64
     <Compile Include="util\CmdUtilTests.cs" />
65
     <Compile Include="util\CmdUtilTests.cs" />
65
   </ItemGroup>
66
   </ItemGroup>

+ 29 - 0
DotNettyFromTests/generatecmd/CmdGenetorTests.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
+
9
+namespace DotNettyFrom.generatecmd.Tests
10
+{
11
+    [TestClass()]
12
+    public class CmdGenetorTests
13
+    {
14
+        [TestMethod()]
15
+        public void GetLoginCmdTest()
16
+        {
17
+            
18
+
19
+        }
20
+
21
+        [TestMethod()]
22
+        public void GetHeartbeatTest()
23
+        {
24
+            CmdGenetor c = CmdGenetor.GetInstance().SetDeviceId("000000000002");
25
+            string cmd = c.GetLoginCmd("3839383630324731313931353530333339393137");
26
+            Console.WriteLine(cmd);
27
+        }
28
+    }
29
+}