wxr
2020-12-17 014abc547ffd1a9f23c58fd7a471dffc5933fdd3
HDL_ON/DAL/DriverLayer/Packet.cs
@@ -5,10 +5,10 @@
using HDL_ON.UI;
using Shared;
namespace HDL_ON
namespace HDL_ON.DriverLayer
{
    /// <summary>
    /// Udp数据包
    /// bus数据包
    /// </summary>
    public class Packet
    {
@@ -54,12 +54,7 @@
        public virtual void Manager()
        {
            try
            {
                //如果当前通讯方式不是本地bus udp则退出
                if(DriverLayer.Control.ins.communicationMode != DriverLayer.CommunicationMode.local_BusUdp)
                {
                    return;
                }
            {
                //对于操作数据库的时间比较长的,可以创建另一个线程处理
                if (!"HDLMIRACLE".Equals(Encoding.ASCII.GetString(Bytes, 4, 10)))
                {
@@ -78,7 +73,7 @@
                byte targetDeviceID = this.Bytes[24];
                //不是要接收的指令就返回
                if (!((targetSubnetID == 252 && targetDeviceID == 252) || (targetSubnetID == 0xff && targetDeviceID == 0xff)))
                if (!((targetSubnetID == 0 && targetDeviceID == 252) || (targetSubnetID == 0xff && targetDeviceID == 0xff)))
                {
                    return;
                }
@@ -121,25 +116,27 @@
                {
                    case Command.SetSingleLightACK:
                        var queryList = new List<Function>();
                        queryList.AddRange(DB_ResidenceData.functionList.electricals);
                        queryList.AddRange(DB_ResidenceData.functionList.lights);
                        queryList.AddRange(FunctionList.List.electricals);
                        queryList.AddRange(FunctionList.List.lights);
                        foreach (var updataObj in queryList)
                        {
                            if (updataObj.GetBusId() == subnetID + "_" + deviceID + "_" + receiveBytes[0])
                            {
                                if (updataObj.functionType != FunctionType.RGB)
                                {
                                    if (updataObj.trait_on_off.value.ToString() == "on")
                                    updataObj.trait_on_off.curValue = receiveBytes[2] > 0 ? "on" : "off";
                                    if (updataObj.trait_on_off.curValue.ToString() == "on")
                                    {
                                        if (updataObj.functionType == FunctionType.Fan)
                                        switch (updataObj.functionType)
                                        {
                                            (updataObj as Fan).openLevel = receiveBytes[2];
                                            updataObj.lastState = Language.StringByID(StringId.Level) + " : " + receiveBytes[2];
                                        }
                                        else
                                        {
                                            (updataObj as Light).brightness = receiveBytes[2];
                                            updataObj.lastState = Language.StringByID(StringId.Brightness) + " : " + receiveBytes[2] + "%";
                                            case FunctionType.Fan:
                                                (updataObj as Fan).openLevel = receiveBytes[2];
                                                updataObj.lastState = Language.StringByID(StringId.Level) + " : " + receiveBytes[2];
                                                break;
                                            case FunctionType.Dimmer:
                                                (updataObj as Light).brightness = receiveBytes[2];
                                                updataObj.lastState = Language.StringByID(StringId.Brightness) + " : " + receiveBytes[2] + "%";
                                                break;
                                        }
                                    }
                                    HomePage.UpdataFunctionStates(updataObj);
@@ -166,13 +163,13 @@
                    case Command.ReadLightAllLoopBrightnessACK:
                        for (int i = 0; i < receiveBytes[0]; i++)
                        {
                            var light = DB_ResidenceData.functionList.lights.Find((obj) => obj.bus_Data.SubnetID == subnetID && obj.bus_Data.DeviceID == deviceID && obj.bus_Data.LoopID == (i + 1));
                            var light = FunctionList.List.lights.Find((obj) => obj.bus.SubnetID == subnetID && obj.bus.DeviceID == deviceID && obj.bus.LoopId == (i + 1));
                            if (light != null)
                            {
                                if (light.functionType != FunctionType.RGB)
                                {
                                    light.trait_on_off.value = receiveBytes[light.bus_Data.LoopID] == 0 ? "off" : "on";
                                    if (light.trait_on_off.value.ToString() == "on")
                                    light.trait_on_off.curValue = receiveBytes[light.bus.LoopId] == 0 ? "off" : "on";
                                    if (light.trait_on_off.curValue.ToString() == "on")
                                    {
                                        light.brightness = receiveBytes[2];
                                        light.lastState = Language.StringByID(StringId.Brightness) + " : " + receiveBytes[2] + "%";
@@ -194,12 +191,12 @@
                            }
                            else
                            {
                                var e = DB_ResidenceData.functionList.electricals.Find((obj) => obj.bus_Data.SubnetID == subnetID && obj.bus_Data.DeviceID == deviceID && obj.bus_Data.LoopID == i);
                                var e = FunctionList.List.electricals.Find((obj) => obj.bus.SubnetID == subnetID && obj.bus.DeviceID == deviceID && obj.bus.LoopId == i);
                                if (e != null)
                                {
                                    var fan = e as Fan;
                                    fan.trait_on_off.value = receiveBytes[2] == 0 ? "off" : "on";
                                    if (fan.trait_on_off.value.ToString() == "on")
                                    fan.trait_on_off.curValue = receiveBytes[2] == 0 ? "off" : "on";
                                    if (fan.trait_on_off.curValue.ToString() == "on")
                                    {
                                        fan.openLevel = receiveBytes[2];
                                        fan.lastState = Language.StringByID(StringId.Level) + " : " + receiveBytes[2];
@@ -223,21 +220,19 @@
                        break;
                    case Command.SetLogicLoopColorACK:
                    case Command.ReadLogicLoopColorACK:
                        foreach (var rgb in DB_ResidenceData.functionList.lights)
                        foreach (var rgb in FunctionList.List.lights)
                        {
                            if (rgb.GetBusId() == subnetID + "_" + deviceID + "_" + receiveBytes[0])
                            {
                                if (rgb.functionType == FunctionType.RGB)
                                {
                                    rgb.trait_on_off.value = receiveBytes[1] > 0 ? "on" : "off";
                                    rgb.trait_on_off.curValue = receiveBytes[1] > 0 ? "on" : "off";
                                    if (receiveBytes[1] > 0)
                                    {
                                        rgb.brightness = receiveBytes[1];
                                        rgb.lastState = Language.StringByID(StringId.Brightness) + " : " + receiveBytes[1] + "%";
                                    }
                                    rgb.redColor = receiveBytes[6];
                                    rgb.greenColor = receiveBytes[7];
                                    rgb.blueColor = receiveBytes[8];
                                    rgb.SetRGBcolor(new byte[] { receiveBytes[6], receiveBytes[7], receiveBytes[8] });
                                    HomePage.UpdataFunctionStates(rgb);
                                    RoomPage.UpdataStates(rgb);
@@ -250,38 +245,38 @@
                        break;
                    case Command.SetCurtainModelStutasACK:
                    case Command.ReadCurtainStutasACK:
                        foreach (var curtain in DB_ResidenceData.functionList.curtains)
                        foreach (var curtain in FunctionList.List.curtains)
                        {
                            if (curtain.bus_Data.SubnetID == subnetID && curtain.bus_Data.DeviceID == deviceID)
                            if (curtain.bus.SubnetID == subnetID && curtain.bus.DeviceID == deviceID)
                            {
                                if (receiveBytes[0] == 17)
                                {
                                    if (receiveBytes[1] > 1)
                                    {
                                        curtain.trait_on_off.value = "on";
                                        curtain.trait_on_off.curValue = "on";
                                    }
                                    else
                                    {
                                        curtain.trait_on_off.value = "off";
                                        curtain.trait_on_off.curValue = "off";
                                    }
                                    curtain.percent = receiveBytes[1];
                                    curtain.lastState = Language.StringByID(StringId.Open) + curtain.percent + "%";
                                }
                                else
                                {
                                    if (curtain.bus_Data.LoopID != receiveBytes[0])
                                    if (curtain.bus.LoopId != receiveBytes[0])
                                        continue;
                                    switch (receiveBytes[1])
                                    {
                                        case 0:
                                            curtain.trait_on_off.value = "stop";
                                            curtain.trait_on_off.curValue = "stop";
                                            break;
                                        case 1:
                                            curtain.trait_on_off.value = "on";
                                            curtain.trait_on_off.curValue = "on";
                                            curtain.lastState = Language.StringByID(StringId.Open);
                                            break;
                                        case 2:
                                            curtain.trait_on_off.value = "off";
                                            curtain.trait_on_off.curValue = "off";
                                            curtain.lastState = Language.StringByID(StringId.Close);
                                            break;
                                    }
@@ -307,18 +302,18 @@
                        break;
                    case Command.SetACModeACK:
                    case Command.ReadACModeACK:
                        foreach (var ac in DB_ResidenceData.functionList.aCs)
                        foreach (var ac in FunctionList.List.aCs)
                        {
                            if (ac.GetBusId() == subnetID + "_" + deviceID + "_" + receiveBytes[0])
                            {
                                ac.curTempType = receiveBytes[1];
                                ac.indoorTemp = receiveBytes[2];
                                ac.trait_on_off.value = receiveBytes[8] == 1 ? "on" : "off";
                                ac.trait_IndoorTemp.curValue = receiveBytes[2];
                                ac.trait_on_off.curValue = receiveBytes[8] == 1 ? "on" : "off";
                                ac.curModeIndex = receiveBytes[9];
                                ac.curFanIndex = receiveBytes[10];
                                ac.trait_temp.value = receiveBytes[11];
                                ac.trait_temp.curValue = receiveBytes[11];
                                ac.lastState = "";
                                switch (ac.trait_mode.value.ToString())
                                switch (ac.trait_mode.curValue.ToString())
                                {
                                    case "cool":
                                        ac.lastState = Language.StringByID(StringId.Cool);
@@ -336,7 +331,7 @@
                                        ac.lastState = Language.StringByID(StringId.AirSupply);
                                        break;
                                }
                                switch (ac.trait_fan.value.ToString())
                                switch (ac.trait_fan.curValue.ToString())
                                {
                                    case "high":
                                        ac.lastState += " " + Language.StringByID(StringId.HighWindSpeed);
@@ -351,7 +346,7 @@
                                        ac.lastState += " " + Language.StringByID(StringId.Auto);
                                        break;
                                }
                                ac.lastState += " " + ac.trait_temp.value + ac.tempUnitString;
                                ac.lastState += " " + ac.trait_temp.curValue + ac.tempUnitString;
                                RoomPage.UpdataStates(ac);
                                FunctionPage.UpdataStates(ac);
                                HomePage.UpdataFunctionStates(ac);
@@ -362,12 +357,12 @@
                        break;
                    case Command.ReadFloorHeatACK:
                    case Command.SetFloorHeatACK:
                        foreach (var fh in DB_ResidenceData.functionList.floorHeatings)
                        foreach (var fh in FunctionList.List.floorHeatings)
                        {
                            if (fh.GetBusId() == subnetID + "_" + deviceID + "_" + receiveBytes[0])
                            {
                                fh.curTempType = receiveBytes[2];
                                fh.trait_on_off.value = receiveBytes[1] % 2 == 0 ? "off" : "on";
                                fh.trait_on_off.curValue = receiveBytes[1] % 2 == 0 ? "off" : "on";
                                fh.curModeIndex = receiveBytes[3];
                                if (fh.modeTemp.ContainsKey("normal"))
                                {
@@ -402,39 +397,39 @@
                                    fh.modeTemp.Add("away", receiveBytes[7]);
                                }
                                switch (fh.trait_mode.value)
                                switch (fh.trait_mode.curValue)
                                {
                                    case "normal":
                                        fh.lastState = Language.StringByID(StringId.Normal);
                                        fh.trait_temp.value = receiveBytes[4];
                                        fh.trait_temp.curValue = receiveBytes[4];
                                        break;
                                    case "day":
                                        fh.lastState = Language.StringByID(StringId.Day);
                                        fh.trait_temp.value = receiveBytes[5];
                                        fh.trait_temp.curValue = receiveBytes[5];
                                        break;
                                    case "night":
                                        fh.lastState = Language.StringByID(StringId.Night);
                                        fh.trait_temp.value = receiveBytes[6];
                                        fh.trait_temp.curValue = receiveBytes[6];
                                        break;
                                    case "timer":
                                        fh.lastState = Language.StringByID(StringId.Auto);
                                        if (receiveBytes[8] == 0)
                                        {
                                            fh.timeFlag = 0;
                                            fh.trait_temp.value = receiveBytes[5];
                                            fh.trait_temp.curValue = receiveBytes[5];
                                        }
                                        else
                                        {
                                            fh.timeFlag = 1;
                                            fh.trait_temp.value = receiveBytes[6];
                                            fh.trait_temp.curValue = receiveBytes[6];
                                        }
                                        break;
                                    case "away":
                                        fh.trait_temp.value = receiveBytes[7];
                                        fh.trait_temp.curValue = receiveBytes[7];
                                        fh.lastState = Language.StringByID(StringId.Away);
                                        break;
                                }
                                fh.lastState += " " + fh.trait_temp.value + fh.tempUnitString;
                                fh.lastState += " " + fh.trait_temp.curValue + fh.tempUnitString;
                                RoomPage.UpdataStates(fh);
                                FunctionPage.UpdataStates(fh);
                                HomePage.UpdataFunctionStates(fh);
@@ -446,12 +441,12 @@
                    case Command.ReadDeviceLoopInfoACK:
                        FunctionType dt = (FunctionType)(11 * 256 + receiveBytes[1]);
                        string tag = receiveBytes[1] + "_" + subnetID + "_" + deviceID + "_" + receiveBytes[2];
                        foreach (var sensor in DB_ResidenceData.functionList.sensorsEnvironmentalScience)
                        foreach (var sensor in FunctionList.List.sensorsEnvironmentalScience)
                        {
                            if (sensor.bus_Data != null)
                            if (sensor.bus != null)
                            {
                                if ((int)sensor.functionType % 256 == receiveBytes[1] && sensor.bus_Data.SubnetID == subnetID &&
                                    sensor.bus_Data.DeviceID == deviceID && sensor.bus_Data.LoopID == receiveBytes[2])
                                if ((int)sensor.functionType % 256 == receiveBytes[1] && sensor.bus.SubnetID == subnetID &&
                                    sensor.bus.DeviceID == deviceID && sensor.bus.LoopId == receiveBytes[2])
                                {
                                    switch (dt)
                                    {
@@ -479,12 +474,12 @@
                        break;
                    case Command.New_Analog_Quantity_BROADCAST:
                        string tag1 = receiveBytes[1] + "_" + subnetID + "_" + deviceID + "_" + receiveBytes[2];
                        foreach (var sensor in DB_ResidenceData.functionList.sensorsEnvironmentalScience)
                        foreach (var sensor in FunctionList.List.sensorsEnvironmentalScience)
                        {
                            if (sensor.bus_Data != null)
                            if (sensor.bus != null)
                            {
                                if ((int)sensor.functionType % 256 == receiveBytes[1] && sensor.bus_Data.SubnetID == subnetID &&
                                    sensor.bus_Data.DeviceID == deviceID && sensor.bus_Data.LoopID == receiveBytes[2])
                                if ((int)sensor.functionType % 256 == receiveBytes[1] && sensor.bus.SubnetID == subnetID &&
                                    sensor.bus.DeviceID == deviceID && sensor.bus.LoopId == receiveBytes[2])
                                {
                                    //0保留 1无符号4Byte整形  2有符号4Byte整形  3Float形(代±)
                                    switch (receiveBytes[3])
@@ -527,20 +522,20 @@
                        {
                            break;
                        }
                        foreach (var ac in DB_ResidenceData.functionList.aCs)
                        foreach (var ac in FunctionList.List.aCs)
                        {
                            if (ac.GetBusId() == subnetID + "_" + deviceID + "_" + reACPanel)
                            {
                                switch (receiveBytes[0])
                                {
                                    case 3://
                                        ac.trait_on_off.value = receiveBytes[1] == 1 ? "on" : "off";
                                        ac.trait_on_off.curValue = receiveBytes[1] == 1 ? "on" : "off";
                                        break;
                                    case 4:
                                    case 7:
                                    case 8:
                                    case 19:
                                        ac.trait_temp.value = receiveBytes[1];
                                        ac.trait_temp.curValue = receiveBytes[1];
                                        break;
                                    case 5:
                                        ac.curFanIndex = receiveBytes[1];
@@ -551,7 +546,7 @@
                                }
                                ac.lastState = "";
                                ac.lastState += " " + ac.trait_temp.value + ac.tempUnitString;
                                ac.lastState += " " + ac.trait_temp.curValue + ac.tempUnitString;
                                RoomPage.UpdataStates(ac);
                                FunctionPage.UpdataStates(ac);
                                HomePage.UpdataFunctionStates(ac);
@@ -562,20 +557,24 @@
                        break;
                    case Command.ReadPanleTempACK://1944
                    case Command.PanleBroadcastTemp:
                        foreach (var ac in DB_ResidenceData.functionList.aCs)
                        foreach (var ac in FunctionList.List.aCs)
                        {
                            if (ac.GetBusId() == subnetID + "_" + deviceID + "_" + receiveBytes[0])
                            {
                                ac.indoorTemp = receiveBytes[1];
                                ac.trait_IndoorTemp.curValue = receiveBytes[1];
                                FunctionPage.UpdataStates(ac);
                            }
                        }
                        break;
                    case Command.ReadGatewayACK:
                        var mac = ByteToHex16(receiveBytes[5]) + "." + ByteToHex16(receiveBytes[6]) + "." + ByteToHex16(receiveBytes[7]) + "." + ByteToHex16(receiveBytes[8]) + "." + ByteToHex16(receiveBytes[9]) + "." + ByteToHex16(receiveBytes[10]) + "." + ByteToHex16(receiveBytes[11]) + "." + ByteToHex16(receiveBytes[12]);
                        if (DB_ResidenceData.residenceData.residenceGatewayMAC == mac)
                        var mac = ByteToHex16(receiveBytes[5]) + ByteToHex16(receiveBytes[6]) + ByteToHex16(receiveBytes[7]) + ByteToHex16(receiveBytes[8]) + ByteToHex16(receiveBytes[9]) + ByteToHex16(receiveBytes[10]) + ByteToHex16(receiveBytes[11]) + ByteToHex16(receiveBytes[12]);
                        var Name = Encoding.GetEncoding("gb2312").GetString(receiveBytes, 13, 20).Trim('\0'); ;
                        MainPage.Log($"name : {Name} ; mac : {mac}");
                        if (DB_ResidenceData.Instance.residenceGatewayMAC == mac)
                        {
                            DriverLayer.Control.ins.gatewayOnline = true;
                            Control.Ins.GatewayOnline = true;
                            Control.Ins.IsRemote = false;
                            DAL.Mqtt.MqttClient.DisConnectRemote();//断开mqtt
                        }
                        break;
                }
@@ -593,13 +592,12 @@
        string ByteToHex16(byte b)
        {
            string s = Convert.ToString(b, 16).ToUpper();
            if(s.Length<=1)
            if (s.Length <= 1)
            {
                return "0" +s;
                return "0" + s;
            }
            return s;
        }
        /// <summary>
        /// 处理接收回来的数据
@@ -644,12 +642,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)
            {