陈嘉乐
2020-06-16 7167334c0e89dd84827d59e726123d14776e3a09
HDL_ON/DriverLayer/Control_Bus.cs
@@ -3,6 +3,8 @@
using HDL_ON.DAL;
using HDL_ON.DAL.Net;
using HDL_ON.Entity;
using Shared;
using Shared.Net;
namespace HDL_ON
{
@@ -14,25 +16,6 @@
        /// 所有微信对一端口的控制都会放到这个集合里
        /// </summary>
        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>
        /// 发送数据,不需要等待回复
@@ -50,6 +33,72 @@
            }, sendCount, false);
            MainPage.Log(command.ToString() + "::" + CommonPage.EndPoint.ToString());
        }
        /// <summary>
        /// 000E 搜索回复
        /// </summary>
        /// <param name="usefullBytes"></param>
        static void ReceiveReadRemark(byte[] usefullBytes)
        {
            try
            {
                //账号没登录不回复
                if (MainPage.LoginUser == null || !MainPage.LoginUser.IsLogin)
                {
                    return;
                }
                //if (!isHttpListenerStart)
                //{
                //    //数据接收端口没打开,不回复
                //    return;
                //}
                var sendStr = MainPage.LoginUser.accountString;
                //如果IP地址获取成功 显示最后一位
                var mAddressBytes = new Shared.Net.NetWiFi().IpAddress.GetAddressBytes();
                if (mAddressBytes != null && mAddressBytes.Length == 4)
                {
                    sendStr = (mAddressBytes[3] & 0xFF).ToString() + ":" + sendStr;
                }
                if (usefullBytes.Length <= 2)
                {
                    byte[] sendBytes = new byte[20];
                    byte[] b1 = CommonPage.MyEncodingGB2312.GetBytes(sendStr);
                    Array.Copy(b1, 0, sendBytes, 0, 20 < b1.Length ? 20 : b1.Length);
                    Control.ControlBytesSend(Command.ReadRemarkACK, 0xFF, 0xFF, sendBytes, 1);
                }
                else
                {
                    for (int i = 2, len = usefullBytes.Length; i < len; i++)
                    {
                        if (i % 2 == 0)
                        {
                            if ((usefullBytes[i] & 0xFF) == 0xFC
                                    && (usefullBytes[i + 1] & 0xFF) == 0xFC
                            )
                            {
                                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);
                                Control.ControlBytesSend(Command.ReadRemarkACK, 0xFF, 0xFF, sendBytes, 1);
                                break;
                            }
                        }
                    }
                }
            }
            catch { }
        }
        /// <summary>
@@ -74,7 +123,17 @@
                    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 读备注回复
                        ReceiveReadRemark(usefulBytes);
                        break;
                    //case Command.YIPanelDeviceInofACK:
                    //    for (int i = 0; i < 4; i++)
                    //    {
@@ -132,8 +191,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:
@@ -254,7 +311,6 @@
                    //case Command.AssignedAddressACK:
                    //case Command.UpdataCurtainModelRunTimeACK:
                    //case Command.ReadCurtainStutasACK:
                    //case Command.ReadLogicLoopColorACK:
                    //case Command.ReadPanleTempACK:
                    //case Command.FreshAirReadACK:
                    //case Command.FreshAirControlACK:
@@ -460,6 +516,11 @@
            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 +557,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:
@@ -620,7 +679,6 @@
                //case Command.AssignedAddress:
                //case Command.UpdataCurtainModelRunTime:
                //case Command.ReadCurtainStatus:
                //case Command.ReadLogicLoopColor:
                //case Command.ReadPanleTemp:
                //case Command.FreshAirRead:
                //case Command.FreshAirControl:
@@ -665,14 +723,21 @@
        /// <param name="sendCount">重发次数</param>
        public 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}");
            }
        }