Browse Source

修改设参返回后处理逻辑

yinyujing 4 years ago
parent
commit
3a958eff0f

+ 16 - 11
NB_IOT_TCP_HP_SOCKET/App.config

@@ -1,36 +1,41 @@
-<?xml version="1.0" encoding="utf-8" ?>
+<?xml version="1.0" encoding="utf-8"?>
 <configuration>
   <appSettings>
+    <add key="APPName" value="大表通讯服务器" />
     <add key="db" value="Data Source=127.0.0.1;Initial Catalog=sdww_qz;Persist Security Info=True;User ID=sa;Password=123456;Pooling=yes;MultipleActiveResultSets=True" />
-
     <!--心跳间隔时间(毫秒)-->
     <add key="ConnectionTimeOut" value="120000" />
-
     <!--执行断开静默链接间隔时间毫秒-->
     <add key="ConnectionTimeOutInterval" value="10000" />
-
     <!--设备类型 1:电池供电;2:市电供电MODBUS;3:电池供电和市电供电MODBUS-->
     <add key="IsHaveModBus" value="1" />
-    
     <!--MODBUS轮询获取抄表数据时间毫秒-->
     <add key="ModBusQueryInterval" value="120000" />
-
     <!--电池供电判断超时最大下发指令次数-->
     <add key="ParamSendTimeOutNum" value="3" />
-    
     <!--电池供电最大下发指令次数-->
     <add key="ParamSendMaxNum" value="6" />
-
     <!--电池供电同一设备下发指令每次间隔时间(秒)-->
     <add key="ParamSendInterval" value="5" />
-
     <!-- 农村供水预付费系统阀门状态同步接口 (http://xxxxx/RuralWater/MeterValveStatusApi.ashx)-->
     <add key="RuralWaterURL" value="" />
     <!-- 农村供水预付费系统阀门状态控制结果通知接口 (http://xxxxx/RuralWater/MeterValveStatusApi.ashx)-->
     <add key="RuralWaterCommdURL" value="" />
-
+    <add key="ClientSettingsProvider.ServiceUri" value="" />
   </appSettings>
-  <startup> 
+  <startup>
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
   </startup>
+  <system.web>
+    <membership defaultProvider="ClientAuthenticationMembershipProvider">
+      <providers>
+        <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
+      </providers>
+    </membership>
+    <roleManager defaultProvider="ClientRoleProvider" enabled="true">
+      <providers>
+        <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
+      </providers>
+    </roleManager>
+  </system.web>
 </configuration>

+ 83 - 57
NB_IOT_TCP_HP_SOCKET/MainForm_TCP.cs

@@ -31,6 +31,7 @@ namespace NB_IOT_TCP_HP_SOCKET
         private static System.Timers.Timer timer;
         private static bool isTimeRuning = false; // MODBUS定时器是否执行中
         private static bool isHeartBeatRuning = false; // 心跳进程是否执行中
+        private static bool isSetParamRuning = false; // 设参进程是否执行中
 
         private static Dictionary<string, object> deviceTypes;
 
@@ -66,6 +67,7 @@ namespace NB_IOT_TCP_HP_SOCKET
 
         private void MainForm_TCP_Load(object sender, EventArgs e)
         {
+            this.Text = ConfigurationManager.AppSettings["APPName"];
             this.WindowState = FormWindowState.Maximized;
             try
             {
@@ -122,12 +124,12 @@ namespace NB_IOT_TCP_HP_SOCKET
                     string deviceType = ConfigurationManager.AppSettings["IsHaveModBus"];
 
                     // 启动处理数据进程 外包和内包两个
-                    StartOrResumeSendThread();
-                    StartOrResumeInsourcingThread();
+                    //StartOrResumeSendThread();
+                    //StartOrResumeInsourcingThread();
 
                     if ("1".Equals(deviceType))
                     {
-                        StartOrResumeSetParamThread(); // 启动电池供电设备设参线程
+                        //StartOrResumeSetParamThread(); // 启动电池供电设备设参线程
                     }
                     else if ("2".Equals(deviceType))
                     {
@@ -136,7 +138,7 @@ namespace NB_IOT_TCP_HP_SOCKET
                     }
                     else if ("3".Equals(deviceType))
                     {
-                        StartOrResumeSetParamThread(); // 启动电池供电设备设参线程
+                        //StartOrResumeSetParamThread(); // 启动电池供电设备设参线程
                         timer = new System.Timers.Timer(Convert.ToInt32(ConfigurationManager.AppSettings["ModBusQueryInterval"]));
                         StartModBusSendTimer(); // 启动ModBus定时发送读取数据任务
                     }
@@ -364,21 +366,29 @@ namespace NB_IOT_TCP_HP_SOCKET
 
                     if (!msg.Result)
                     {
-                        AddMsg($" > [{clientInfo.ConnId},OnReceive] -> {clientInfo.IpAddress}:{clientInfo.Port} {psp.SourceNumber} ({bytes.Length} bytes) 外包解包失败 \n {msg.Message}", lbErrorData);
+                        AddMsg($" > [{clientInfo.ConnId},OnReceive] -> {clientInfo.IpAddress}:{clientInfo.Port}-{clientInfo.SourceNumber} {psp.SourceNumber} ({bytes.Length} bytes) 外包解包失败 \n {msg.Message}", lbErrorData);
 
                     }
                     else
                     {
+                        if (!string.IsNullOrEmpty(clientInfo.SourceNumber))
+                        {
+                            psp.SourceNumber = clientInfo.SourceNumber;
+                        }
+                        if (!string.IsNullOrEmpty(clientInfo.DestNumber))
+                        {
+                            clientInfo.DestNumber = psp.DestNumber;
+                        }
                         lock (deveiceConnId)
                         {
                             deveiceConnId[psp.SourceNumber] = connId;
                         }
-                        clientInfo.SourceNumber = psp.SourceNumber;
-                        clientInfo.DestNumber = psp.DestNumber;
+                        //clientInfo.SourceNumber = psp.SourceNumber;
+                        //clientInfo.DestNumber = psp.DestNumber;
                         PSProtocol protocol2;
                         byte[] array = new byte[psp.Data.Length];
                         psp.Data.CopyTo(array, 0);
-                        AddMsg($" > [{clientInfo.ConnId},OnReceive] -> {clientInfo.IpAddress}:{clientInfo.Port} {psp.SourceNumber} ({bytes.Length} bytes) 外包解包成功 \n {BitConverter.ToString(psp.Data, 0, psp.Data.Length).Replace("-", " ")}", lbReceiveData);
+                        AddMsg($" > [{clientInfo.ConnId},OnReceive] -> {clientInfo.IpAddress}:{clientInfo.Port}-{clientInfo.SourceNumber} {psp.SourceNumber} ({bytes.Length} bytes) 外包解包成功 \n {BitConverter.ToString(psp.Data, 0, psp.Data.Length).Replace("-", " ")}", lbReceiveData);
 
                         // 判断包类型 心跳或数据包
                         if (psp.Type == FrameType.GPRS_Link)
@@ -388,11 +398,11 @@ namespace NB_IOT_TCP_HP_SOCKET
                             if (!ProtocolAnalysisTools.OuterPack(protocol2, out byte[] buffer2).Result)
                             {
 
-                                AddMsg($" > [{clientInfo.ConnId},OnReceive] -> {clientInfo.IpAddress}:{clientInfo.Port} {psp.SourceNumber} ({bytes.Length} bytes) 心跳回应打包失败 ", lbErrorData);
+                                AddMsg($" > [{clientInfo.ConnId},OnReceive] -> {clientInfo.IpAddress}:{clientInfo.Port}-{clientInfo.SourceNumber} {psp.SourceNumber} ({bytes.Length} bytes) 心跳回应打包失败 ", lbErrorData);
                             }
                             else
                             {
-                                AddMsg($" > [{clientInfo.ConnId},OnSend] -> {clientInfo.IpAddress}:{clientInfo.Port} {psp.SourceNumber} ({bytes.Length} bytes) 心跳回应成功 \n {BitConverter.ToString(buffer2, 0, buffer2.Length).Replace("-", " ")} ", lbReceiveData);
+                                AddMsg($" > [{clientInfo.ConnId},OnSend] -> {clientInfo.IpAddress}:{clientInfo.Port}-{clientInfo.SourceNumber} {psp.SourceNumber} ({bytes.Length} bytes) 心跳回应成功 \n {BitConverter.ToString(buffer2, 0, buffer2.Length).Replace("-", " ")} ", lbReceiveData);
                                 server.Send(clientInfo.ConnId, buffer2, buffer2.Length);// 发送数据
                             }
                         }
@@ -413,17 +423,18 @@ namespace NB_IOT_TCP_HP_SOCKET
                                 if (!ProtocolAnalysisTools.OuterPack(protocol2, out buffer3).Result)
                                 {
 
-                                    AddMsg($" > [{clientInfo.ConnId},OnReceive] -> {clientInfo.IpAddress}:{clientInfo.Port} {psp.SourceNumber} ({bytes.Length} bytes) 历史记录回应打包失败 ", lbErrorData);
+                                    AddMsg($" > [{clientInfo.ConnId},OnReceive] -> {clientInfo.IpAddress}:{clientInfo.Port}-{clientInfo.SourceNumber} {psp.SourceNumber} ({bytes.Length} bytes) 历史记录回应打包失败 ", lbErrorData);
 
                                 }
                                 else
                                 {
 
-                                    AddMsg($" > [{clientInfo.ConnId},OnSend] -> {clientInfo.IpAddress}:{clientInfo.Port} {psp.SourceNumber} ({bytes.Length} bytes) 历史记录回应成功 \n {BitConverter.ToString(buffer3, 0, buffer3.Length).Replace("-", " ")} ", lbReceiveData);
+                                    AddMsg($" > [{clientInfo.ConnId},OnSend] -> {clientInfo.IpAddress}:{clientInfo.Port}-{clientInfo.SourceNumber} {psp.SourceNumber} ({bytes.Length} bytes) 历史记录回应成功 \n {BitConverter.ToString(buffer3, 0, buffer3.Length).Replace("-", " ")} ", lbReceiveData);
                                     server.Send(clientInfo.ConnId, buffer3, buffer3.Length);// 发送数据
                                 }
 
                                 StartOrResumeSendThread();
+                                StartOrResumeSetParamThread();
                             }
                         }
                     }
@@ -618,7 +629,6 @@ namespace NB_IOT_TCP_HP_SOCKET
                     isHeartBeatRuning = true;
                     try
                     {
-
                         // 获取所有连接
                         IntPtr[] allConnIds = server.GetAllConnectionIDs();
 
@@ -897,7 +907,7 @@ namespace NB_IOT_TCP_HP_SOCKET
                                                 {
                                                     if (server.Send(clientInfo.ConnId, buffer3, buffer3.Length))
                                                     {
-                                                        AddMsg($" > [{clientInfo.ConnId},OnSend] -> {clientInfo.IpAddress}:{clientInfo.Port} {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);
                                                     }
                                                 }
                                             }
@@ -928,14 +938,17 @@ namespace NB_IOT_TCP_HP_SOCKET
                                                     {
                                                         if (server.Send(clientInfo.ConnId, buffer3, buffer3.Length))
                                                         {
-                                                            AddMsg($" > [{clientInfo.ConnId},OnSend] -> {clientInfo.IpAddress}:{clientInfo.Port} {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._controlCode == 0xA4)
+                                        }
+                                        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);
@@ -962,12 +975,12 @@ namespace NB_IOT_TCP_HP_SOCKET
                                 }
                                 else
                                 {
-                                    AddMsg($" > [{clientInfo.ConnId},OnReceive] -> {clientInfo.IpAddress}:{clientInfo.Port} {sourceNumber} ({psp.Data.Length} bytes) CD_CellMonitor内包解包失败 \n {msg.Message}", lbErrorData);
+                                    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} {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";
 
@@ -1050,12 +1063,12 @@ namespace NB_IOT_TCP_HP_SOCKET
 
                             if (!unPackFlag)
                             {
-                                AddMsg($" > [{clientInfo.ConnId},OnReceive] -> {clientInfo.IpAddress}:{clientInfo.Port} {sourceNumber} ({psp.Data.Length} bytes) CD_PSController内包解包失败,数据类型{code} \n {unPackRs}", lbErrorData);
+                                AddMsg($" > [{clientInfo.ConnId},OnReceive] -> {clientInfo.IpAddress}:{clientInfo.Port}-{clientInfo.SourceNumber} {sourceNumber} ({psp.Data.Length} bytes) CD_PSController内包解包失败,数据类型{code} \n {unPackRs}", lbErrorData);
                             }
                             else
                             {
 
-                                AddMsg($" > [{clientInfo.ConnId},OnReceive] -> {clientInfo.IpAddress}:{clientInfo.Port} {sourceNumber} ({psp.Data.Length} bytes) CD_PSController内包解包成功,数据类型{code} \n {BitConverter.ToString(modbus.UpDataArea, 0, modbus.UpDataArea.Length).Replace("-", " ")}", lbReceiveData);
+                                AddMsg($" > [{clientInfo.ConnId},OnReceive] -> {clientInfo.IpAddress}:{clientInfo.Port}-{clientInfo.SourceNumber} {sourceNumber} ({psp.Data.Length} bytes) CD_PSController内包解包成功,数据类型{code} \n {BitConverter.ToString(modbus.UpDataArea, 0, modbus.UpDataArea.Length).Replace("-", " ")}", lbReceiveData);
                                 // 设参回包数据
                                 if (code == Modbus.FunctionCode.F10)
                                 {
@@ -1173,7 +1186,7 @@ namespace NB_IOT_TCP_HP_SOCKET
                                         }
                                     }
                                 }
-                                else if (cjt._cmdType == ParamCmdType.ReadWorkMode)
+                                else if (cjt._cmdType == ParamCmdType.WriteNetParam)
                                 {
                                     // 设参参返回指令
                                     if (cjt._controlCode == 0xA4)
@@ -1320,47 +1333,60 @@ namespace NB_IOT_TCP_HP_SOCKET
         {
             while (this.Visible)
             {
-                // 获取所有链接ID
-                IntPtr[] allConnIds = server.GetAllConnectionIDs();
-                if (allConnIds == null || allConnIds.Length == 0)
-                {
-                    Thread.Sleep(1000);
-                    continue;
-                }
-
-                foreach (IntPtr connId in allConnIds)
-                {
-                    ClientInfo clientInfo = (ClientInfo)server.GetExtra(connId);
-                    if (clientInfo == null)
-                    {
-                        continue;
-                    }
-                    // 如果链接中数据包是最后一包进行下发设参和校时指令
-                    if (!clientInfo.IsEnd)
-                    {
-                        continue;
-                    }
-                    if (clientInfo.TranDevice == null)
-                    {
-                        continue;
-                    }
-                    Dictionary<string, string> trandevice = clientInfo.TranDevice;
-                    string destNumber = clientInfo.DestNumber;
-                    string sourceNumber = clientInfo.SourceNumber;
-                    // 一个传输设备可能对应多个设备信息及通讯设备
-                    foreach (KeyValuePair<string, string> keyValuePair in trandevice)
+                //if (!isSetParamRuning)
+                //{
+                //    isSetParamRuning = true;
+                    try
                     {
-                        try
+                        // 获取所有链接ID
+                        IntPtr[] allConnIds = server.GetAllConnectionIDs();
+                        if (allConnIds == null || allConnIds.Length > 0)
                         {
-                            // 发送设参指令
-                            SendSetParam(clientInfo, keyValuePair.Key, keyValuePair.Value);
+                            setParamThread.Suspend();
                         }
-                        catch (Exception ex)
+
+                        foreach (IntPtr connId in allConnIds)
                         {
-                            AddMsg($" > [{clientInfo.ConnId},OnSend] -> {clientInfo.IpAddress}:{clientInfo.Port} {sourceNumber}  CD_CellMonitor最后发送设参指令失败 \n {ex.StackTrace}", lbErrorData);
+                            ClientInfo clientInfo = (ClientInfo)server.GetExtra(connId);
+                            if (clientInfo == null)
+                            {
+                                continue;
+                            }
+                            // 如果链接中数据包是最后一包进行下发设参和校时指令
+                            if (!clientInfo.IsEnd)
+                            {
+                                continue;
+                            }
+                            if (clientInfo.TranDevice == null)
+                            {
+                                continue;
+                            }
+                            Dictionary<string, string> trandevice = clientInfo.TranDevice;
+                            string destNumber = clientInfo.DestNumber;
+                            string sourceNumber = clientInfo.SourceNumber;
+                            // 一个传输设备可能对应多个设备信息及通讯设备
+                            foreach (KeyValuePair<string, string> keyValuePair in trandevice)
+                            {
+                                try
+                                {
+                                    // 发送设参指令
+                                    SendSetParam(clientInfo, keyValuePair.Key, keyValuePair.Value);
+                                }
+                                catch (Exception ex)
+                                {
+                                    AddMsg($" > [{clientInfo.ConnId},OnSend] -> {clientInfo.IpAddress}:{clientInfo.Port} {sourceNumber}  CD_CellMonitor最后发送设参指令失败 \n {ex.StackTrace}", lbErrorData);
+                                }
+                            }
                         }
+                        isSetParamRuning = false;
                     }
-                }
+                    catch (Exception)
+                    {
+                        isSetParamRuning = false;
+                    }
+
+                    Thread.Sleep(1000);
+                //}
             }
         }
 

+ 1 - 0
NB_IOT_TCP_HP_SOCKET/NB_IOT_TCP_HP_SOCKET.csproj

@@ -39,6 +39,7 @@
     <Reference Include="System" />
     <Reference Include="System.Configuration" />
     <Reference Include="System.Core" />
+    <Reference Include="System.Web.Extensions" />
     <Reference Include="System.Xml.Linq" />
     <Reference Include="System.Data.DataSetExtensions" />
     <Reference Include="Microsoft.CSharp" />

+ 1 - 0
NB_IOT_TCP_HP_SOCKET/WWKJUtil/RemoteTask.cs

@@ -78,6 +78,7 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
                     {
                         return;
                     }
+                    this.TaskDirectory = FilePath;
                     // 将文件按创建时间进行排序
                     Array.Sort(files, new MyDateSorter());
                     foreach (FileInfo info3 in files)