Bladeren bron

指令化登录,心跳

张泳健 7 jaren geleden
bovenliggende
commit
4aa1b0e52d

+ 4 - 1
DotNettyFrom/DotNettyFrom.csproj Bestand weergeven

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

+ 6 - 2
DotNettyFrom/MainForm.cs Bestand weergeven

@@ -13,6 +13,7 @@ using DotNettyFrom.config;
13 13
 using DotNettyFrom.db;
14 14
 using DotNettyFrom.dialog;
15 15
 using DotNettyFrom.form;
16
+using DotNettyFrom.generatecmd;
16 17
 using DotNettyFrom.netty;
17 18
 using DotNettyFrom.util;
18 19
 
@@ -153,7 +154,7 @@ namespace DotNettyFrom
153 154
             Task startNew = Task.Factory.StartNew(() =>
154 155
             {
155 156
                 _nettyClient = new NettyClient();
156
-                _nettyClient.StartNetty(host, port);
157
+                _nettyClient.StartNetty(host, port, null);
157 158
             });
158 159
         }
159 160
 
@@ -163,8 +164,10 @@ namespace DotNettyFrom
163 164
 
164 165
             Task startNew = Task.Factory.StartNew(() =>
165 166
             {
167
+                string loginCmd = CmdGenetor.GetInstance().SetDeviceId(TbDeviceId.Text.Trim())
168
+                    .GetLoginCmd(TbCCID.Text.Trim());
166 169
                 _nettyClient = new NettyClient();
167
-                _nettyClient.StartNetty(host, port);
170
+                _nettyClient.StartNetty(host, port, loginCmd);
168 171
             });
169 172
         }
170 173
 
@@ -277,6 +280,7 @@ namespace DotNettyFrom
277 280
             int index = LBLogShow.SelectedIndex;
278 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 Bestand weergeven

@@ -11,8 +11,8 @@ namespace DotNettyFrom.config
11 11
         public static int FunctionCodeStartPos = 18;
12 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 16
         public static int AllIdleTimeSeconds = 25;
17 17
 
18 18
 

+ 48 - 0
DotNettyFrom/generatecmd/CmdGenetor.cs Bestand weergeven

@@ -0,0 +1,48 @@
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 Bestand weergeven

@@ -15,6 +15,7 @@ using DotNetty.Transport.Channels;
15 15
 using DotNetty.Transport.Channels.Sockets;
16 16
 using DotNettyFrom.common;
17 17
 using DotNettyFrom.config;
18
+using DotNettyFrom.generatecmd;
18 19
 using DotNettyFrom.util;
19 20
 
20 21
 namespace DotNettyFrom.netty
@@ -23,11 +24,11 @@ namespace DotNettyFrom.netty
23 24
     {
24 25
         private MultithreadEventLoopGroup _group;
25 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 32
             IPAddress hostIp = IPAddress.Parse(host);
32 33
             RunClientAsync(hostIp, port);
33 34
             DataModel.ReceCollection.Add(new UIInfoModel("StartNetty:开始尝试连接").setSendType(SystemConfig.DebugType));
@@ -106,15 +107,13 @@ namespace DotNettyFrom.netty
106 107
                 DataModel.ReceCollection.Add(new UIInfoModel("ChannelActive:连接成功").setAction(1)
107 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 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,16 +125,30 @@ namespace DotNettyFrom.netty
126 125
                     string recCmd = AryUtil.ByteBuffer2Hex(buffer);
127 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 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 154
             public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)

+ 34 - 0
DotNettyFrom/util/CmdUtil.cs Bestand weergeven

@@ -62,5 +62,39 @@ namespace DotNettyFrom.util
62 62
             int res = Hex2Dec(hex);
63 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 Bestand weergeven

@@ -60,6 +60,7 @@
60 60
   </Choose>
61 61
   <ItemGroup>
62 62
     <Compile Include="common\UIInfoModelTests.cs" />
63
+    <Compile Include="generatecmd\CmdGenetorTests.cs" />
63 64
     <Compile Include="Properties\AssemblyInfo.cs" />
64 65
     <Compile Include="util\CmdUtilTests.cs" />
65 66
   </ItemGroup>

+ 29 - 0
DotNettyFromTests/generatecmd/CmdGenetorTests.cs Bestand weergeven

@@ -0,0 +1,29 @@
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
+}