hxb
2020-12-17 5eeafe3af80bfd88306bd8ad9e76c8f4b51ca35f
增加本地发送的重发机制
7个文件已修改
178 ■■■■ 已修改文件
HDL_ON/DAL/DriverLayer/Control.cs 45 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
HDL_ON/DAL/DriverLayer/Control_Udp.cs 51 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
HDL_ON/DAL/DriverLayer/Packet.cs 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
HDL_ON/DAL/DriverLayer/UdpSocket.cs 10 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
HDL_ON/DAL/Mqtt/MqttClient.cs 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
HDL_ON/Entity/Function/TV.cs 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
HDL_ON/UI/UI2/4-PersonalCenter/SearchDevice/SearchDevicePage.cs 62 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
HDL_ON/DAL/DriverLayer/Control.cs
@@ -123,7 +123,7 @@
        /// <summary>
        /// 本地udp
        /// </summary>
        public Control_Udp myUdp = null;
        public Control_Udp myUdp1 = null;
        /// <summary>
        /// 通讯方式
@@ -144,11 +144,6 @@
        /// </summary>
        public void OpenUdp(int port)
        {
            if (myUdp == null)
            {
                myUdp = new Control_Udp();
            }
            UdpSocket._BusSocket.Start(port);
        }
        /// <summary>
@@ -156,10 +151,6 @@
        /// </summary>
        public void CloseUdp()
        {
            if (myUdp != null)
            {
                myUdp = null;
            }
            UdpSocket._BusSocket.Stop();
        }
@@ -197,11 +188,11 @@
                        break;
                    if (DB_ResidenceData.Instance.GatewayType == 0)
                    {
                        myUdp.ControlBytesSend(Command.ReadGateway, 255, 255, new byte[] { (byte)new Random().Next(255), (byte)new Random().Next(255) });
                        new Control_Udp().ControlBytesSend(Command.ReadGateway, 255, 255, new byte[] { (byte)new Random().Next(255), (byte)new Random().Next(255) });
                    }
                    else if (DB_ResidenceData.Instance.GatewayType == 1)
                    {
                        myUdp.SearchLocalGateway();
                        new Control_Udp().SearchLocalGateway();
                    }
                    System.Threading.Thread.Sleep(500);
                }
@@ -227,7 +218,7 @@
            {
                if (DB_ResidenceData.Instance.GatewayType == 0)
                {
                    myUdp.ControlBusScenes(scene);
                    new Control_Udp().ControlBusScenes(scene);
                }
            }
        }
@@ -263,10 +254,7 @@
                {
                    try
                    {
                        if (myUdp != null)
                        {
                            myUdp.WriteBusData(function, commandDictionary);
                        }
                        new Control_Udp().WriteBusData(function, commandDictionary);
                    }
                    catch (Exception ex)
                    {
@@ -276,13 +264,10 @@
                //ALink控制
                else
                {
                    if (myUdp != null)
                    {
                        var functionControlDataObj = function.GetGatewayAlinkControlData(commandDictionary);
                        var functionControlDataJson = Newtonsoft.Json.JsonConvert.SerializeObject(functionControlDataObj);
                        var sendBytes = Ins.ConvertSendBodyData(CommunicationTopic.ct.ControlFunctionTopic, functionControlDataJson);
                        Ins.myUdp.SendLocalHdlLinkData(sendBytes);
                    }
                    var functionControlDataObj = function.GetGatewayAlinkControlData(commandDictionary);
                    var functionControlDataJson = Newtonsoft.Json.JsonConvert.SerializeObject(functionControlDataObj);
                    var sendBytes = Ins.ConvertSendBodyData(CommunicationTopic.ct.ControlFunctionTopic, functionControlDataJson);
                    new Control_Udp().SendLocalHdlLinkData(sendBytes, functionControlDataObj.id);
                }
            }
        }
@@ -311,7 +296,7 @@
                {
                    try
                    {
                        Ins.myUdp.ReadBusData(function);
                        new Control_Udp().ReadBusData(function);
                    }
                    catch (Exception ex)
                    {
@@ -334,7 +319,7 @@
                    };
                    var functionControlDataJson = Newtonsoft.Json.JsonConvert.SerializeObject(readDataObj);
                    var sendBytes = Ins.ConvertSendBodyData(CommunicationTopic.ct.ReadStatus, functionControlDataJson);
                    Ins.myUdp.SendLocalHdlLinkData(sendBytes);
                    new Control_Udp().SendLocalHdlLinkData(sendBytes,readDataObj.id);
                }
            }
        }
@@ -380,7 +365,7 @@
                };
                var aLinkJson = Newtonsoft.Json.JsonConvert.SerializeObject(aLinkData);
                var sendBytes = Ins.ConvertSendBodyData(CommunicationTopic.ct.ControlScene, aLinkJson);
                Ins.myUdp.SendLocalHdlLinkData(sendBytes);
                new Control_Udp().SendLocalHdlLinkData(sendBytes,aLinkData.id);
            }
        }
        /// <summary>
@@ -468,7 +453,8 @@
                else if(receiveObj.Topic == CommunicationTopic.ct.ReadStatus+"_reply" ||
                    receiveObj.Topic == CommunicationTopic.ct.GatewayUpStatus)
                {
                    UpdataFunctionStatus(receiveObj.BodyDataString);
                    //TODO 暂时不传正确的数据上去,如果后面要优化前面这些代码
                    UpdataFunctionStatus(receiveObj.BodyDataString,null);
                }
            }
            return receiveObj;
@@ -479,12 +465,13 @@
        /// A协议数据
        /// </summary>
        /// <param name="updateBytes"></param>
        public void UpdataFunctionStatus(string revString)
        public void UpdataFunctionStatus(string revString,byte []usefulBytes)
        {
            MainPage.Log($"A协议更新状态:{revString}");
            var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<AlinkFunctionStatusObj>(revString);
            if (temp != null)
            {
                Control_Udp.ReceiveRepeatManager(temp.id,usefulBytes);
                var allLocalFuntion = FunctionList.List.GetDeviceFunctionList();
                foreach (var updateTemp in temp.objects)
                {
HDL_ON/DAL/DriverLayer/Control_Udp.cs
@@ -29,6 +29,7 @@
        /// 所有对一端口的控制都会放到这个集合里
        /// </summary>
        static List<Control_Udp> controlList = new List<Control_Udp>(50);
        public System.Net.IPEndPoint EndPoint
        {
@@ -65,7 +66,7 @@
        /// <summary>
        /// 重发验证
        /// </summary>
        public void ReceiveRepeatManager(string receiveFlag)
        public static void ReceiveRepeatManager(string receiveFlag,byte []usefulBytes)
        {
            for (int i = 0; i < controlList.Count; i++)
            {
@@ -623,7 +624,7 @@
                {
                    for (int i = 0; i < controlList.Count;)
                    {
                        if (controlList[i] == null || 3 <= controlList[i].Packet.HaveSendCount)
                        if (controlList[i] == null || 3 <= controlList[i].packet.HaveSendCount)
                        {
                            controlList.RemoveAt(i);
                        }
@@ -692,7 +693,7 @@
        void run()
        {
            allDone.Set();
            Packet.HaveSendCount = 4;
            packet.HaveSendCount = 4;
        }
        //数据发送处理
@@ -706,7 +707,7 @@
                new System.Threading.Thread(() =>
                {
                    System.Threading.Thread.Sleep(1000);
                    UdpSocket._BusSocket.Start(6000);
                    UdpSocket._BusSocket.Start(UdpSocket._BusSocket.Port);
                    controlLostCount = 0;
                })
                { IsBackground = true }.Start();
@@ -715,16 +716,16 @@
            try
            {
                //MainPage.Log("发送数据:" + SendFlag);
                UdpSocket._BusSocket.AsyncBeginSend(Packet);
                Packet.HaveSendCount--;
                UdpSocket._BusSocket.AsyncBeginSend(packet);
                packet.HaveSendCount--;
                //这里是重发两次
                while (Packet.HaveSendCount < 3)
                while (packet.HaveSendCount < 3)
                {
                    if (Packet.FlagDateTime.AddMilliseconds(1000).Ticks <= DateTime.Now.Ticks)
                    if (packet.FlagDateTime.AddMilliseconds(1000).Ticks <= DateTime.Now.Ticks)
                    {
                        MainPage.Log("重发数据:" + SendFlag);
                        UdpSocket._BusSocket.AsyncBeginSend(Packet);
                        UdpSocket._BusSocket.AsyncBeginSend(packet);
                        controlLostCount++;
                    }
                    System.Threading.Thread.Sleep(100);
@@ -743,7 +744,7 @@
        /// <summary>
        /// 当前数据包
        /// </summary>
        Packet Packet;
        Packet packet;
        /// <summary>
        /// 记录发送数据包
        /// </summary>
@@ -796,14 +797,14 @@
        {
            try
            {
                Packet = new Packet(target.SendBytes, target.IPEndPoint);
                Packet.HaveSendCount = 3 - sendCount;
                packet = new Packet(target.SendBytes, target.IPEndPoint);
                packet.HaveSendCount = 3 - sendCount;
                signPacket(target);
                System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount));
                thread.IsBackground = true;
                thread.Start(Packet);
                thread.Start(packet);
                if (isWait)
                {
@@ -828,11 +829,11 @@
                
                var sendBytes = Control.Ins.ConvertSendBodyData(CommunicationTopic.SearchLoaclGateway, bodyString);
                //组播发送
                Packet = new Packet(sendBytes, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("239.0.168.188"), 8585));
                Packet.HaveSendCount = 4;//ps:没有重发,后期有时间可以增加重发判断
                packet = new Packet(sendBytes, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("239.0.168.188"), 8585));
                packet.HaveSendCount = 4;//ps:没有重发,后期有时间可以增加重发判断
                System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount));
                thread.IsBackground = true;
                thread.Start(Packet);
                thread.Start(packet);
                //wait();不需要等待
            }
            catch (Exception ex)
@@ -840,20 +841,24 @@
                MainPage.Log($"Send bus data error {ex.Message}");
            }
        }
        /// <summary>
        /// 发送udp A协议数据
        /// </summary>
        public void SendLocalHdlLinkData(byte[] sendBytes)
        public void SendLocalHdlLinkData(byte[] sendBytes,string id, bool isWait=false)
        {
            Packet = new Packet(sendBytes, new System.Net.IPEndPoint(System.Net.IPAddress.Parse(Control.Ins.reportIp), 8585));
            Packet.HaveSendCount = 4;//ps:没有重发,后期有时间可以增加重发判断
            packet = new Packet(sendBytes, new System.Net.IPEndPoint(System.Net.IPAddress.Parse(Control.Ins.reportIp), 8585));
            sendFlag = id;
            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount));
            thread.IsBackground = true;
            thread.Start(Packet);
            //wait();不需要等待
            thread.Start(packet);
            if (isWait)
            {
                wait();
            }
            //MainPage.Log($"发送Hdl-Link数据,IP:{Control.Ins.reportIp}:8585");
        }
    }
}
HDL_ON/DAL/DriverLayer/Packet.cs
@@ -644,12 +644,12 @@
                        receiveFlag += string.Format("{0},{1}", usefulBytes[0], usefulBytes[1]);
                        break;
                    case Command.ReadRemark:
                        DriverLayer.Control.Ins.myUdp.ReceiveReadRemark(usefulBytes);
                        new Control_Udp().ReceiveReadRemark(usefulBytes);
                        break;
                    default:
                        break;
                }
                DriverLayer.Control.Ins.myUdp.ReceiveRepeatManager(receiveFlag);
                Control_Udp.ReceiveRepeatManager(receiveFlag,usefulBytes);
            }
            catch (Exception ex)
            {
HDL_ON/DAL/DriverLayer/UdpSocket.cs
@@ -21,7 +21,7 @@
        //本地Socket
        private Socket busSocket;
        int _port = 0;
        public int Port = 0;
        /// <summary>
        /// 启动Socket接收和发送功能
        /// </summary>
@@ -29,7 +29,7 @@
        {
            if (IsRunning)
            {
                if (port == _port)
                if (port == Port)
                {
                    return;
                }
@@ -39,15 +39,15 @@
                }
            }
            if (port != 0)
                _port = port;
            if (_port == 0)
                Port = port;
            if (Port == 0)
                return;
            busSocket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            busSocket.EnableBroadcast = true;
            try {
                //busSocket.Bind (new IPEndPoint (IPAddress.Any, 6000));
                busSocket.Bind(new IPEndPoint(IPAddress.Any, _port));
                busSocket.Bind(new IPEndPoint(IPAddress.Any, Port));
                busSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.0.168.188")));
            }
HDL_ON/DAL/Mqtt/MqttClient.cs
@@ -289,7 +289,7 @@
                                    {
                                        //A协议数据处理
                                        var revString = Encoding.UTF8.GetString(bytes);
                                        Control.Ins.UpdataFunctionStatus(revString);
                                        Control.Ins.UpdataFunctionStatus(revString,null);
                                    }
                                }
                            }
HDL_ON/Entity/Function/TV.cs
@@ -14,7 +14,7 @@
        {
            int RandKey = new Random().Next(0, 255);
            var textByte = (byte)iCode;
            Control.Ins.myUdp.ControlBytesSend(Command.InfraredControl, bus_Data.SubnetID, bus_Data.DeviceID, new byte[] { 1, bus_Data.loopId, textByte, 0, 0, (byte)RandKey }, 0);
            new Control_Udp().ControlBytesSend(Command.InfraredControl, bus_Data.SubnetID, bus_Data.DeviceID, new byte[] { 1, bus_Data.loopId, textByte, 0, 0, (byte)RandKey }, 0);
        }
        public void ControlTV(int number)
        {
@@ -24,7 +24,7 @@
            {
                textByte = 18;
            }
            Control.Ins.myUdp.ControlBytesSend(Command.InfraredControl, bus_Data.SubnetID, bus_Data.DeviceID, new byte[] { 1, bus_Data.loopId, textByte, 0, 0, (byte)RandKey }, 0);
            new Control_Udp().ControlBytesSend(Command.InfraredControl, bus_Data.SubnetID, bus_Data.DeviceID, new byte[] { 1, bus_Data.loopId, textByte, 0, 0, (byte)RandKey }, 0);
        }
    }
}
HDL_ON/UI/UI2/4-PersonalCenter/SearchDevice/SearchDevicePage.cs
@@ -4,7 +4,7 @@
using Newtonsoft.Json;
using HDL_ON.Entity;
using System.Collections.Generic;
using HDL_ON.DriverLayer;
namespace HDL_ON.UI
{
@@ -35,36 +35,36 @@
                deviceIpList.Clear();
                contentView.RemoveAll();
                //DriverLayer.Control.ins.ChangeCommunicationMode(DriverLayer.CommunicationMode.local_BusUdp);
//                UdpSocket._BusSocket.SearchNetDeviceAction = (jto) =>
//                {
//                    var device = JsonConvert.DeserializeObject<DeviceModule>(jto.ToString());
//                    if (device != null)
//                    {
//                        lock (deviceIpList)
//                        {
//                            if (!deviceIpList.Contains(device.ip_address))
//                            {
//                                deviceIpList.Add(device.ip_address);
//                                MainPage.Log($"搜索到网络设备:{device.ip_address}");
//                                Application.RunOnMainThread(() =>
//                                {
//#if DEBUG
//#else
//                                    if(device.gateway_type== 4)
//#endif
//                                    {
//                                        LoadRow(device);
//                                    }
//                                });
//                            }
//                            else
//                            {
//                                MainPage.Log($"已加载该IP设备:{device.ip_address}");
//                            }
//                        }
//                    }
//                };
                DriverLayer.Control.Ins.myUdp.SearchLocalGateway();
                //                UdpSocket._BusSocket.SearchNetDeviceAction = (jto) =>
                //                {
                //                    var device = JsonConvert.DeserializeObject<DeviceModule>(jto.ToString());
                //                    if (device != null)
                //                    {
                //                        lock (deviceIpList)
                //                        {
                //                            if (!deviceIpList.Contains(device.ip_address))
                //                            {
                //                                deviceIpList.Add(device.ip_address);
                //                                MainPage.Log($"搜索到网络设备:{device.ip_address}");
                //                                Application.RunOnMainThread(() =>
                //                                {
                //#if DEBUG
                //#else
                //                                    if(device.gateway_type== 4)
                //#endif
                //                                    {
                //                                        LoadRow(device);
                //                                    }
                //                                });
                //                            }
                //                            else
                //                            {
                //                                MainPage.Log($"已加载该IP设备:{device.ip_address}");
                //                            }
                //                        }
                //                    }
                //                };
                new Control_Udp().SearchLocalGateway();
                contentView.EndHeaderRefreshing();
            };