wxr
2022-08-17 d6578b10542226650e263815dea75e598a7090f9
HDL_ON/DAL/DriverLayer/Control.cs
@@ -4,26 +4,12 @@
using HDL_ON.DAL.Server;
using HDL_ON.Entity;
using HDL_ON.UI;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Shared;
namespace HDL_ON.DriverLayer
{
    /// <summary>
    /// 通讯方式
    /// </summary>
    public enum CommunicationMode
    {
        none,
        /// <summary>
        /// 本地udp
        /// </summary>
        local_BusUdp,
        /// <summary>
        /// 本地tcp客户端
        /// </summary>
        tcp_local_client,
    }
    public class Control
    {
@@ -43,6 +29,11 @@
        /// 记录接收到的消息,方便zb的工程师调试他们的设备
        /// </summary>
        public List<string> MsgInfoList = new List<string>();
        /// <summary>
        /// 本地tcp客户端连接是否成功
        /// 是否登录网关成功
        /// </summary>
        public bool LocalTcpClientLogin = false;
        int _msg_id = 1;
        /// <summary>
@@ -200,11 +191,6 @@
        public Control_Udp myUdp1 = null;
        /// <summary>
        /// 通讯方式
        /// </summary>
        public CommunicationMode communicationMode;
        /// <summary>
        /// 打开tcp服务端
        /// </summary>
        public void OpenTcpServer()
@@ -297,6 +283,38 @@
            }
        }
        private System.Threading.Thread loginGatewayThread;
        /// <summary>
        /// 登录网关
        /// </summary>
        public void LoginGateway()
        {
            if(loginGatewayThread== null)
            {
                loginGatewayThread = new System.Threading.Thread(() => {
                    while (true)
                    {
                        if (Ins.GatewayOnline_Local && myTcpClient.isConnected && LocalTcpClientLogin)
                        {
                            var sendData = new { clientType = "app", version = "1.0" };
                            var sendJob = new { id = Control.Ins.msg_id.ToString(), time_stamp = Utlis.GetTimestamp(), objects = sendData };
                            var bodyString = JsonConvert.SerializeObject(sendJob);
                            var sendBytes = ConvertSendBodyData($"/user/{GatewayId}/custom/gateway/login", bodyString);
                            Ins.myTcpClient.SendMessage(sendBytes);
                        }
                        System.Threading.Thread.Sleep(2000);
                    }
                });
            }
        }
        /// <summary>
        /// 场景控制入口
        /// </summary>
@@ -342,15 +360,6 @@
                }
            }
        }
        /// <summary>
        /// 安防控制
        /// </summary>
        public void ControlArm()
        {
            DAL.Server.HttpServerRequest httpServer = new DAL.Server.HttpServerRequest();
            //var pack = httpServer.GetSecurityAlarmLogList
        }
        /// <summary>
        /// 发送命令
@@ -903,6 +912,12 @@
                //}
                receiveObj.BodyDataString = res[1];
                if(receiveObj.Topic == CommunicationTopic.ct.HeartBeat)
                {
                    return null;
                }
                //2021-09-23 过滤不需要解密的主题 目前搜索网关主题不加密
                if (receiveObj.Topic != CommunicationTopic.SearchLoaclGatewayReply)
                {
@@ -943,6 +958,21 @@
                    }
                }
                try
                {
                    var idMsg = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePack>(res[1]);
                    var hasProcess = hasItBeenProcessed(idMsg.id);
                    if (hasProcess)
                    {
                        return null;
                    }
                    MainPage.Log($"处理局域网数据id:{idMsg.id}");
                }
                catch (Exception ex)
                {
                    MainPage.Log($"解析局域网数据异常{ex.Message}");
                }
                if (receiveObj.Topic == CommunicationTopic.SearchLoaclGatewayReply || receiveObj.Topic == CommunicationTopic.GatewayBroadcast)
                {
@@ -975,17 +1005,22 @@
                        //2021-09-23 新增获取当前网关是否本地加密
                        Ins.IsLocalEncrypt = device.isLocalEncrypt;
                        //MainPage.Log("网关本地加密状态:" + device.local_encrypt.ToString());
                        OpenTcpClent();
                        LoginGateway();
                    }
                }
                else if (receiveObj.Topic == CommunicationTopic.ct.ReadStatus + "_reply" ||
                    receiveObj.Topic == CommunicationTopic.ct.ControlFunctionTopic + "_reply" ||
                    receiveObj.Topic == CommunicationTopic.ct.GatewayUpStatus ||
                    receiveObj.Topic.Contains( CommunicationTopic.ct.GatewayUpSortTopic))
                    receiveObj.Topic.Contains(CommunicationTopic.ct.GatewayUpSortTopic))
                {
                    //TODO 暂时不传正确的数据上去,如果后面要优化前面这些代码
                    UpdataFunctionStatus(receiveObj.BodyDataString, null);
                }
                else if (receiveObj.Topic == CommunicationTopic.ct.ControlSeurity +"_reply"
                else if (receiveObj.Topic == CommunicationTopic.ct.ControlSeurity + "_reply"
                    || receiveObj.Topic == CommunicationTopic.ct.ReadSecurityStatus + "_reply"
                    || receiveObj.Topic == CommunicationTopic.ct.SecurityStatusUp)
                {
@@ -993,7 +1028,8 @@
                    {
                        MainPage.Log($"局域网安防信息: {receiveObj.Topic}  : 内容: {res[1]}");
                        var tt = "";
                        lock (tt) {
                        lock (tt)
                        {
                            var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<SecurityStatusObj>(receiveObj.BodyDataString);
                            if (temp != null)
                            {
@@ -1012,8 +1048,16 @@
                            }
                        }
                    }
                    catch (Exception ex){
                    catch (Exception ex)
                    {
                        MainPage.Log($"安防局域网异常:{ex.Message}");
                    }
                }
                else if (receiveObj.Topic == $"/user/{GatewayId}/custom/gateway/login_reply") {
                    var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(receiveObj.BodyDataString);
                    if (temp.GetValue("objects").ToString().Contains("success"))
                    {
                        LocalTcpClientLogin = true;
                    }
                }
                else
@@ -1025,6 +1069,29 @@
            return receiveObj;
        }
        private List<string> processedDataList = new List<string>();
        /// <summary>
        /// 是否已经处理过数据
        /// </summary>
        /// <returns></returns>
        private bool hasItBeenProcessed(string msgId)
        {
            if (processedDataList.Contains(msgId))
            {
                return true;
            }
            else
            {
                if(processedDataList.Count> 50)
                {
                    processedDataList.RemoveAt(0);
                }
                return false;
            }
        }
        /// <summary>
        /// 更新设备状态
        /// A协议数据