wxr
2020-06-16 f6fd8acd7c53c44187e70b4709443318a628f4b5
HDL_ON/DriverLayer/Control_Bus.cs
@@ -1,8 +1,14 @@
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
using HDL_ON.DAL;
using HDL_ON.DAL.Net;
using HDL_ON.Entity;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Shared;
using Shared.Net;
namespace HDL_ON
{
@@ -16,28 +22,9 @@
        private static List<Control> controlList = new List<Control>(50);
        /// <summary>
        /// 发送数据,等待有反馈
        /// </summary>
        /// <returns>The bytes send has return.</returns>
        static byte[] ControlBytesSendHasReturn(Command command, byte subnetID, byte deviceID, byte[] gatewayBytes, int sendCount = 3)
        {
            Control control = new Control();
            control.Send(new Target()
            {
                IPEndPoint = CommonPage.EndPoint,
                Command = command,
                SubnetID = subnetID,
                DeviceID = deviceID,
                AddData = gatewayBytes,
            }, sendCount, true);
            return control.UsefulBytes;
        }
        /// <summary>
        /// 发送数据,不需要等待回复
        /// </summary>
        static void ControlBytesSend(Command command, byte subnetID, byte deviceID, byte[] gatewayBytes, int sendCount = 3, System.Net.IPEndPoint ipEndpoint = null)
       public static void ControlBytesSend(Command command, byte subnetID, byte deviceID, byte[] gatewayBytes, int sendCount = 3, System.Net.IPEndPoint ipEndpoint = null)
        {
            Control control = new Control();
            control.Send(new Target()
@@ -49,9 +36,90 @@
                AddData = gatewayBytes,
            }, sendCount, false);
            MainPage.Log(command.ToString() + "::" + CommonPage.EndPoint.ToString());
        }
        public static byte[] ReadGatewayIPAddress()
        {
            Control control = new Control();
            control.ReadGatewayIP();
            return control.UsefulBytes;
        }
        /// <summary>
        /// 000E 搜索回复
        /// </summary>
        /// <param name="usefullBytes"></param>
        static void ReceiveReadRemark(byte[] usefullBytes)
        {
            try
            {
                //账号没登录不回复
                if (MainPage.LoginUser == null || !MainPage.LoginUser.IsLogin)
                {
                    return;
                }
                if (!CommonPage.isHttpListenerStart)
                {
                    //数据接收端口没打开,不回复
                    CommonPage.InitHttpListener();
                }
                var sendStr = MainPage.LoginUser.accountString;
                if (usefullBytes.Length == 0)
                {
                    SenRemark(sendStr);
                }
                else
                {
                    bool isExit = false;
                    for (int i = 0, len = usefullBytes.Length; i < len; i++)
                    {
                        if (i % 2 == 0)
                        {
                            if ((usefullBytes[i] & 0xFF) == 252
                                    && (usefullBytes[i + 1] & 0xFF) == 252)
                            {
                                isExit = true;
                                break;
                            }
                        }
                    }
                    if (!isExit)
                    {
                        //不存在,代表没收到本机的发送,继续回复
                        SenRemark(sendStr);
                    }
                }
            }
            catch { }
        }
        /// <summary>
        /// 000F回复备注
        /// </summary>
        /// <param name="sendStr"></param>
        static void SenRemark(string sendStr)
        {
            byte[] sendBytes = new byte[20];
            byte[] b1 = CommonPage.MyEncodingGB2312.GetBytes(sendStr);
            //Remote_GroupName = CommonPage.MyEncodingGB2312.GetString (b1);
            Array.Copy(b1, 0, sendBytes, 0, 20 < b1.Length ? 20 : b1.Length);
            var control = new Control();
            control.Send(new Target()
            {
                IPEndPoint = CommonPage.EndPoint,
                Command = Command.ReadRemarkACK,
                SubnetID = 0xFF,
                DeviceID = 0xFF,
                AddData = sendBytes,
            }, 0, false); //设置当前发送指令方式为:任何情况下本地发送、不限制、不加密明文发送
        }
        /// <summary>
        /// 处理接收回来的数据
        /// </summary>
@@ -62,6 +130,25 @@
        /// <param name="remoteEndPoint">源网络套接字</param>
        public static void ManagerReceive(byte subnetID, byte deviceID, Command command, byte targetSubnetID, byte targetDeviceID, byte[] usefulBytes, System.Net.EndPoint remoteEndPoint)
        {
            if (((System.Net.IPEndPoint)remoteEndPoint).Port == 6688)
            {
                for (int i = 0; i < controlList.Count; i++)
                {
                    try
                    {
                        var control = controlList[i];
                        control.LatestDateTime = DateTime.Now;
                        control.UsefulBytes = usefulBytes;//
                        control.run();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"control error : {ex.Message}");
                    }
                }
            }
            try
            {
                string receiveFlag = string.Format("{0},{1},{2},", subnetID, deviceID, (int)command);
@@ -74,7 +161,18 @@
                    case Command.SetLogicLoopColorACK:
                        receiveFlag += string.Format("{0},{1},{2}", usefulBytes[0], usefulBytes[1], usefulBytes[2]);
                        break;
                    case Command.ReadLogicLoopColorACK:
                    case Command.ReadACModeACK:
                    case Command.SetACModeACK:
                    case Command.ReadFloorHeatACK:
                    case Command.SetFloorHeatACK:
                        receiveFlag += string.Format("{0}", usefulBytes[0]);
                        break;
                    case Command.ReadRemark:
                        //buspro 读备注回复
                        Console.WriteLine("buspro 读备注回复");
                        ReceiveReadRemark(usefulBytes);
                        break;
                    //case Command.YIPanelDeviceInofACK:
                    //    for (int i = 0; i < 4; i++)
                    //    {
@@ -132,8 +230,6 @@
                    //    break;
                    //case Command.SetCommonACK:
                    //case Command.InfraredChannelControlACK:
                    //case Command.ReadACModeACK:
                    //case Command.SetACModeACK:
                    //case Command.Serverx_FH_CMD_ACK:
                    //case Command.ReadSensorHistoryACK:
                    //case Command.SetSensorAutomationTargetLevelEnableACK:
@@ -223,14 +319,14 @@
                    //case Command.Remote3thACK:
                    //    receiveFlag = string.Format("{0},", (int)command);
                    //    break;
                    //case Command.ReadDeviceLoopInfoACK:
                    //    if (usefulBytes[0] == 1)
                    //    {
                    //        receiveFlag += string.Format("{0},{1}", usefulBytes[0], usefulBytes[2]); ;
                    //    }
                    //    else
                    //        receiveFlag += string.Format("{0},{1},{2}", usefulBytes[0], usefulBytes[1], usefulBytes[2]);
                    //    break;
                    case Command.ReadDeviceLoopInfoACK:
                        if (usefulBytes[0] == 1)
                        {
                            receiveFlag += string.Format("{0},{1}", usefulBytes[0], usefulBytes[2]); ;
                        }
                        else
                            receiveFlag += string.Format("{0},{1},{2}", usefulBytes[0], usefulBytes[1], usefulBytes[2]);
                        break;
                    //case Command.SetDeviceLoopInfoACK:
                    //case Command.InfraredControlACK:
                    //case Command.SetSecurityByPassACK:
@@ -254,7 +350,6 @@
                    //case Command.AssignedAddressACK:
                    //case Command.UpdataCurtainModelRunTimeACK:
                    //case Command.ReadCurtainStutasACK:
                    //case Command.ReadLogicLoopColorACK:
                    //case Command.ReadPanleTempACK:
                    //case Command.FreshAirReadACK:
                    //case Command.FreshAirControlACK:
@@ -297,9 +392,9 @@
                            control.run();
                        }
                    }
                    catch
                    catch (Exception ex)
                    {
                        Console.WriteLine($"control error : {ex.Message}");
                    }
                }
            }
@@ -404,20 +499,7 @@
            add();
            if (CommonPage.IsRemote)
            {
                MqttCommon.MqttRemoteSend(Packet.Bytes);
                Packet.FlagDateTime = DateTime.Now;
                Packet.HaveSendCount--;
                //这里是重发两次
                while (Packet.HaveSendCount < 3)
                {
                    if (Packet.FlagDateTime.AddMilliseconds(1000).Ticks <= System.DateTime.Now.Ticks)
                    {
                        MqttCommon.MqttRemoteSend(Packet.Bytes);
                        Packet.FlagDateTime = DateTime.Now;
                        Packet.HaveSendCount++;
                    }
                    System.Threading.Thread.Sleep(100);
                }
                allDone.Set();
            }
             
            }
            else
            {
                try
@@ -453,13 +535,18 @@
        /// </summary>
        Packet Packet;
        private void ini(Target target, bool send = true)
        private void ini(Target target)
        {
            this.SendFlag = string.Format("{0},{1},{2},", target.SubnetID, target.DeviceID, (int)target.Command + 1);
            switch (target.Command)
            {
                case Command.SetSingleLight:
                case Command.ReadLogicLoopColor:
                case Command.ReadACMode:
                case Command.SetACMode:
                case Command.ReadFloorHeat:
                case Command.SetFloorHeat:
                    this.sendFlag += string.Format("{0}", target.AddData[0]);
                    break;
                case Command.SetLogicLoopColor:
@@ -496,8 +583,6 @@
                //    break;
                //case Command.SetCommonSwitch:
                //case Command.InfraredChannelControl:
                //case Command.ReadACMode:
                //case Command.SetACMode:
                //case Command.ReadFoolHeat:
                //case Command.SetFoolHeat:
                //case Command.Serverx_FH_CMD:
@@ -592,12 +677,12 @@
                //case Command.Remote3th:
                //    this.SendFlag = string.Format("{0},", (int)target.Command + 1);
                //    break;
                //case Command.ReadDeviceLoopInfo:
                //    if (target.AddData[0] == 1)//特殊处理灯光类,DMX模块每一个回路不固定小类,根据具体设置来回复
                //        this.sendFlag += string.Format("{0},{1}", target.AddData[0], target.AddData[2]);
                //    else
                //        this.sendFlag += string.Format("{0},{1},{2}", target.AddData[0], target.AddData[1], target.AddData[2]);
                //    break;
                case Command.ReadDeviceLoopInfo:
                    if (target.AddData[0] == 1)//特殊处理灯光类,DMX模块每一个回路不固定小类,根据具体设置来回复
                        this.sendFlag += string.Format("{0},{1}", target.AddData[0], target.AddData[2]);
                    else
                        this.sendFlag += string.Format("{0},{1},{2}", target.AddData[0], target.AddData[1], target.AddData[2]);
                    break;
                //case Command.SetDeviceLoopInfo:
                //case Command.InfraredControl:
                //case Command.SetSecurityByPass:
@@ -620,7 +705,6 @@
                //case Command.AssignedAddress:
                //case Command.UpdataCurtainModelRunTime:
                //case Command.ReadCurtainStatus:
                //case Command.ReadLogicLoopColor:
                //case Command.ReadPanleTemp:
                //case Command.FreshAirRead:
                //case Command.FreshAirControl:
@@ -650,12 +734,10 @@
                    //return;
                    break;
            }
            if (send)
            {
                System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount));
                thread.IsBackground = true;
                thread.Start(Packet);
            }
            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount));
            thread.IsBackground = true;
            thread.Start(Packet);
        }
        /// <summary>
@@ -663,18 +745,49 @@
        /// </summary>
        /// <param name="target">发送对象</param>
        /// <param name="sendCount">重发次数</param>
        public void Send(Target target, int sendCount, bool isWait)
        void Send(Target target, int sendCount, bool isWait)
        {
            Packet = new Packet(target.SendBytes, target.IPEndPoint);
            Packet.HaveSendCount = 3 - sendCount;
            ini(target);
            if (isWait)
            try
            {
                this.wait();
                Packet = new Packet(target.SendBytes, target.IPEndPoint);
                Packet.HaveSendCount = 3 - sendCount;
                ini(target);
                if (isWait)
                {
                    this.wait();
                }
            }
            catch(Exception ex)
            {
                MainPage.Log($"Send bus data error {ex.Message}");
            }
        }
        /// <summary>
        /// 读取网关IP
        /// </summary>
        void ReadGatewayIP()
        {
            try
            {
                var sendJob = new JObject { { "vendor_code", "HDL" }, { "command", "search" } };
                var SearchGateway = JsonConvert.SerializeObject(sendJob);
                var SearchGatewayPayload = Encoding.ASCII.GetBytes(SearchGateway);
                Packet = new Packet(SearchGatewayPayload, CommonPage.GetGatewayIP_EndPoint);
                System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount));
                thread.IsBackground = true;
                thread.Start(Packet);
                wait();
            }
            catch (Exception ex)
            {
                MainPage.Log($"Send bus data error {ex.Message}");
            }
        }
    }
}