wxr
2020-09-11 8df24b0a3dfd5b6f39c5393ef24eab25b70ab858
HDL_ON/DriverLayer/CommonPage.cs
@@ -1,10 +1,8 @@
using System;
using System.Collections.Generic;
using System.Text;
using HDL_ON.DAL.Net;
using HDL_ON.Entity;
using HDL_ON.UI;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Shared;
using Shared.Net;
@@ -12,11 +10,7 @@
{
    public static class CommonPage
    {
        public static bool IsRemote;
        public static Action RefreshAir;
        public static string FindGatewayChilrenIPAddress = new NetWiFi().BroadcastIpAddress.ToString();
        private static string ip = "0.0.0.0";
        public static System.Net.IPEndPoint EndPoint
        {
            get
@@ -30,16 +24,6 @@
                    //防止异常导致程序退出
                    return new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), 6000);
                }
            }
        }
        public static System.Net.IPEndPoint GetGatewayIP_EndPoint
        {
            get
            {
                //防止异常导致程序退出
                return new System.Net.IPEndPoint(System.Net.IPAddress.Parse("224.0.168.188"), 6688);
            }
        }
@@ -67,31 +51,6 @@
                }
            }
        }
        public static bool isHttpListenerStart;
        public static DateTime dt;
        //public static byte currentSubnetID = 0;
        public static void InitHttpListener()
        {
            if (isHttpListenerStart)
            {
                return;
            }
            Console.WriteLine("Init Http Listener !");
            HttpListener.Start(new NetWiFi().IpAddress, 6002);
            HttpListener.EventHandler -= httpListener_EventHandler;
            HttpListener.EventHandler += httpListener_EventHandler;
            isHttpListenerStart = true;
        }
        public static void CloseHttpListener()
        {
            //isHttpListenerStart = false;
            //return;
            if (!isHttpListenerStart)
                return;
            HttpListener.Close();
            isHttpListenerStart = false;
        }
        /// <summary>
        /// 初始化处理socket接收的数据 
        /// </summary>
@@ -115,19 +74,70 @@
                {
                }
                Control.controlLostCount = 0;
                switch (command)
                {
                    case Command.ReadLightSingleLoopBrightnessACK:
                    case Command.SetSingleLightACK:
                        foreach (var light in DB_ResidenceData.residenceData.functionList.lights)
                        var queryList = new List<Function>();
                        queryList.AddRange(DB_ResidenceData.functionList.electricals);
                        queryList.AddRange(DB_ResidenceData.functionList.lights);
                        foreach (var updataObj in queryList)
                        {
                            if (light.GetBusId() == subnetID + "_" + deviceID + "_" + receiveBytes[0])
                            if (updataObj.GetBusId() == subnetID + "_" + deviceID + "_" + receiveBytes[0])
                            {
                                if ( light.functionType != FunctionType.RGB)
                                if (updataObj.functionType != FunctionType.RGB)
                                {
                                    light.on_off = receiveBytes[2] == 0 ? "off" : "on";
                                    if (light.on_off == "on")
                                    var newState = receiveBytes[2] == 0 ? "off" : "on";
                                    if (updataObj.trait_on_off.value.ToString() == newState)
                                    {
                                        //状态一样,不用再刷新页面
                                        break;
                                    }
                                    if (updataObj.trait_on_off.value.ToString() == "on")
                                    {
                                        if (updataObj.functionType == FunctionType.Fan)
                                        {
                                            (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] + "%";
                                        }
                                    }
                                    HomePage.UpdataFunctionStates(updataObj);
                                    RoomPage.UpdataStates(updataObj);
                                    FunctionPage.UpdataStates(updataObj);
                                    ClassificationPage.UpdataInfo(updataObj);
                                    switch (updataObj.functionType)
                                    {
                                        case FunctionType.Relay:
                                            RelayPage.UpdataState(updataObj as Light);
                                            break;
                                        case FunctionType.Dimmer:
                                            DimmerPage.UpdataStates(updataObj as Light);
                                            break;
                                        case FunctionType.Fan:
                                            FanPage.UpdataState(updataObj as Fan);
                                            break;
                                    }
                                    break;
                                }
                            }
                        }
                        break;
                    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));
                            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.brightness = receiveBytes[2];
                                        light.lastState = Language.StringByID(StringId.Brightness) + " : " + receiveBytes[2] + "%";
@@ -147,17 +157,14 @@
                                    }
                                }
                            }
                        }
                        foreach (var e in DB_ResidenceData.residenceData.functionList.electricals)
                        {
                            if (e.GetBusId() == subnetID + "_" + deviceID + "_" + receiveBytes[0])
                            else
                            {
                                if (e.functionType == FunctionType.Fan)
                                var e = DB_ResidenceData.functionList.electricals.Find((obj) => obj.bus_Data.SubnetID == subnetID && obj.bus_Data.DeviceID == deviceID && obj.bus_Data.LoopID == i);
                                if (e != null)
                                {
                                    var fan = e as Fan;
                                    fan.on_off = receiveBytes[2] == 0 ? "off" : "on";
                                    if (fan.on_off == "on")
                                    fan.trait_on_off.value = receiveBytes[2] == 0 ? "off" : "on";
                                    if (fan.trait_on_off.value.ToString() == "on")
                                    {
                                        fan.openLevel = receiveBytes[2];
                                        fan.lastState = Language.StringByID(StringId.Level) + " : " + receiveBytes[2];
@@ -178,18 +185,17 @@
                                }
                            }
                        }
                        break;
                    case Command.SetLogicLoopColorACK:
                    case Command.ReadLogicLoopColorACK:
                        foreach (var rgb in DB_ResidenceData.residenceData.functionList.lights)
                        foreach (var rgb in DB_ResidenceData.functionList.lights)
                        {
                            if (rgb.GetBusId() == subnetID + "_" + deviceID + "_" + receiveBytes[0])
                            {
                                if (rgb.functionType == FunctionType.RGB)
                                {
                                    rgb.on_off = receiveBytes[1] > 0 ? "on" : "off";
                                    if (receiveBytes[1] >0)
                                    rgb.trait_on_off.value = receiveBytes[1] > 0 ? "on" : "off";
                                    if (receiveBytes[1] > 0)
                                    {
                                        rgb.brightness = receiveBytes[1];
                                        rgb.lastState = Language.StringByID(StringId.Brightness) + " : " + receiveBytes[1] + "%";
@@ -209,40 +215,47 @@
                        break;
                    case Command.SetCurtainModelStutasACK:
                    case Command.ReadCurtainStutasACK:
                        foreach (var curtain in DB_ResidenceData.residenceData.functionList.curtains)
                        foreach (var curtain in DB_ResidenceData.functionList.curtains)
                        {
                            if (curtain.GetBusId() == subnetID + "_" + deviceID + "_" + receiveBytes[0])
                            if (curtain.bus_Data.SubnetID == subnetID && curtain.bus_Data.DeviceID == deviceID)
                            {
                                switch(receiveBytes[1])
                                if (receiveBytes[0] == 17)
                                {
                                    case 0:
                                        curtain.on_off = "stop";
                                        break;
                                    case 1:
                                        curtain.on_off = "on";
                                        curtain.lastState = Language.StringByID(StringId.Open);
                                        break;
                                    case 2:
                                        curtain.on_off = "off";
                                        curtain.lastState = Language.StringByID(StringId.Close);
                                        break;
                                    default:
                                        if (receiveBytes[1] > 1)
                                        {
                                            curtain.on_off = "on";
                                        }
                                        else
                                        {
                                            curtain.on_off = "off";
                                        }
                                        curtain.openLevel = receiveBytes[1];
                                        curtain.lastState = Language.StringByID(StringId.Open) + curtain.openLevel + "%";
                                        break;
                                    if (receiveBytes[1] > 1)
                                    {
                                        curtain.trait_on_off.value = "on";
                                    }
                                    else
                                    {
                                        curtain.trait_on_off.value = "off";
                                    }
                                    curtain.percent = receiveBytes[1];
                                    curtain.lastState = Language.StringByID(StringId.Open) + curtain.percent + "%";
                                }
                                else
                                {
                                    if (curtain.bus_Data.LoopID != receiveBytes[0])
                                        continue;
                                    switch (receiveBytes[1])
                                    {
                                        case 0:
                                            curtain.trait_on_off.value = "stop";
                                            break;
                                        case 1:
                                            curtain.trait_on_off.value = "on";
                                            curtain.lastState = Language.StringByID(StringId.Open);
                                            break;
                                        case 2:
                                            curtain.trait_on_off.value = "off";
                                            curtain.lastState = Language.StringByID(StringId.Close);
                                            break;
                                    }
                                }
                                RoomPage.UpdataStates(curtain);
                                FunctionPage.UpdataStates(curtain);
                                HomePage.UpdataFunctionStates(curtain);
                                ClassificationPage.UpdataInfo(curtain);
                                RollingShutterPage.UpdataState(curtain);
                                switch (curtain.functionType)
                                {
                                    case FunctionType.Curtain:
@@ -254,18 +267,18 @@
                        break;
                    case Command.SetACModeACK:
                    case Command.ReadACModeACK:
                        foreach (var ac in DB_ResidenceData.residenceData.functionList.aCs)
                        foreach (var ac in DB_ResidenceData.functionList.aCs)
                        {
                            if (ac.GetBusId() == subnetID + "_" + deviceID + "_" + receiveBytes[0])
                            {
                                ac.curTempType = receiveBytes[1];
                                ac.indoorTemp = receiveBytes[2];
                                ac.on_off = receiveBytes[8] == 1 ? "on" : "off";
                                ac.trait_on_off.value = receiveBytes[8] == 1 ? "on" : "off";
                                ac.curModeIndex = receiveBytes[9];
                                ac.curFanIndex = receiveBytes[10];
                                ac.curTemp = receiveBytes[11];
                                ac.trait_temp.value = receiveBytes[11];
                                ac.lastState = "";
                                switch (ac.trait_mode.curValues.ToString())
                                switch (ac.trait_mode.value.ToString())
                                {
                                    case "cool":
                                        ac.lastState = Language.StringByID(StringId.Cool);
@@ -283,7 +296,7 @@
                                        ac.lastState = Language.StringByID(StringId.AirSupply);
                                        break;
                                }
                                switch (ac.trait_fan.curValues.ToString())
                                switch (ac.trait_fan.value.ToString())
                                {
                                    case "high":
                                        ac.lastState += " " + Language.StringByID(StringId.HighWindSpeed);
@@ -298,7 +311,7 @@
                                        ac.lastState += " " + Language.StringByID(StringId.Auto);
                                        break;
                                }
                                ac.lastState += " " + ac.curTemp + ac.curTempTypeString;
                                ac.lastState += " " + ac.trait_temp.value + ac.tempUnitString;
                                RoomPage.UpdataStates(ac);
                                FunctionPage.UpdataStates(ac);
                                HomePage.UpdataFunctionStates(ac);
@@ -309,20 +322,20 @@
                        break;
                    case Command.ReadFloorHeatACK:
                    case Command.SetFloorHeatACK:
                        foreach (var fh in DB_ResidenceData.residenceData.functionList.floorHeatings)
                        foreach (var fh in DB_ResidenceData.functionList.floorHeatings)
                        {
                            if (fh.GetBusId() == subnetID + "_" + deviceID + "_" + receiveBytes[0])
                            {
                                fh.curTempType = receiveBytes[2];
                                fh.on_off = receiveBytes[1] % 2 ==0?"off":"on";
                                fh.trait_on_off.value = receiveBytes[1] % 2 == 0 ? "off" : "on";
                                fh.curModeIndex = receiveBytes[3];
                                if (fh.modeTemp.ContainsKey("ordinary"))
                                if (fh.modeTemp.ContainsKey("normal"))
                                {
                                    fh.modeTemp["ordinary"]= receiveBytes[4];
                                    fh.modeTemp["normal"] = receiveBytes[4];
                                }
                                else
                                {
                                    fh.modeTemp.Add("ordinary", receiveBytes[4]);
                                    fh.modeTemp.Add("normal", receiveBytes[4]);
                                }
                                if (fh.modeTemp.ContainsKey("day"))
                                {
@@ -332,7 +345,6 @@
                                {
                                    fh.modeTemp.Add("day", receiveBytes[5]);
                                }
                                if (fh.modeTemp.ContainsKey("night"))
                                {
                                    fh.modeTemp["night"] = receiveBytes[6];
@@ -341,7 +353,6 @@
                                {
                                    fh.modeTemp.Add("night", receiveBytes[6]);
                                }
                                if (fh.modeTemp.ContainsKey("away"))
                                {
                                    fh.modeTemp["away"] = receiveBytes[7];
@@ -350,23 +361,32 @@
                                {
                                    fh.modeTemp.Add("away", receiveBytes[7]);
                                }
                                switch (fh.curMode)
                                switch (fh.trait_mode.value)
                                {
                                    case "ordinary":
                                        fh.lastState = Language.StringByID(StringId.Ordinary);
                                    case "normal":
                                        fh.lastState = Language.StringByID(StringId.Normal);
                                        fh.curTemp = receiveBytes[4];
                                        break;
                                    case "day":
                                        fh.lastState = Language.StringByID(StringId.Day);
                                        fh.curTemp = receiveBytes[5];
                                        break;
                                    case "night":
                                        fh.lastState = Language.StringByID(StringId.Night);
                                        fh.curTemp = receiveBytes[6];
                                        break;
                                    case "timer":
                                        fh.lastState = Language.StringByID(StringId.Auto);
                                        if(receiveBytes[8]==0)
                                        if (receiveBytes[8] == 0)
                                        {
                                        }else
                                            fh.timeFlag = 0;
                                            fh.curTemp = receiveBytes[5];
                                        }
                                        else
                                        {
                                            fh.timeFlag = 1;
                                            fh.curTemp = receiveBytes[6];
                                        }
                                        break;
                                    case "away":
@@ -374,7 +394,7 @@
                                        fh.lastState = Language.StringByID(StringId.Away);
                                        break;
                                }
                                fh.lastState += " " + fh.curTemp + fh.curTempTypeString;
                                fh.lastState += " " + fh.curTemp + fh.tempUnitString;
                                RoomPage.UpdataStates(fh);
                                FunctionPage.UpdataStates(fh);
                                HomePage.UpdataFunctionStates(fh);
@@ -386,8 +406,7 @@
                    case Command.ReadDeviceLoopInfoACK:
                        FunctionType dt = (FunctionType)(11 * 256 + receiveBytes[1]);
                        string tag = receiveBytes[1] + "_" + subnetID + "_" + deviceID + "_" + receiveBytes[2];
                        foreach (var sensor in DB_ResidenceData.residenceData.functionList.sensorsEnvironmentalScience)
                        foreach (var sensor in DB_ResidenceData.functionList.sensorsEnvironmentalScience)
                        {
                            if (sensor.bus_Data != null)
                            {
@@ -420,18 +439,18 @@
                        break;
                    case Command.New_Analog_Quantity_BROADCAST:
                        string tag1 = receiveBytes[1] + "_" + subnetID + "_" + deviceID + "_" + receiveBytes[2];
                        foreach (var sensor in DB_ResidenceData.residenceData.functionList.sensorsEnvironmentalScience)
                        foreach (var sensor in DB_ResidenceData.functionList.sensorsEnvironmentalScience)
                        {
                            if (sensor.bus_Data != null)
                            {
                                if ((int)sensor.functionType % 256 == receiveBytes[1] && sensor.bus_Data.SubnetID == subnetID &&
                                    sensor.bus_Data.DeviceID == deviceID && sensor.bus_Data.LoopID == receiveBytes[2])
                                {
                                    //0保留   1无符号4Byte整形  2有符号4Byte整形  3Float形(代±)
                                    //0保留 1无符号4Byte整形  2有符号4Byte整形  3Float形(代±)
                                    switch (receiveBytes[3])
                                    {
                                        case 1:
                                            sensor.values = (receiveBytes[5]*256 * 256 * 256)+ (receiveBytes[6] * 256 * 256)+ (receiveBytes[7] * 256)+ receiveBytes[8];
                                            sensor.values = (receiveBytes[5] * 256 * 256 * 256) + (receiveBytes[6] * 256 * 256) + (receiveBytes[7] * 256) + receiveBytes[8];
                                            break;
                                        case 2:
                                            sensor.values = -1 * ((receiveBytes[5] * 256 * 256 * 256) + (receiveBytes[6] * 256 * 256) + (receiveBytes[7] * 256) + receiveBytes[8]);
@@ -441,8 +460,74 @@
                                            sensor.values = Math.Round(BitConverter.ToSingle(tempBytes, 0), 1);
                                            break;
                                    }
                                    switch (receiveBytes[4])
                                    {
                                        case 2:
                                            if (receiveBytes[1] == 5)//TVOC需求除以100000
                                                sensor.values /= 100000;
                                            break;
                                    }
                                    EnvironmentalSciencePage.LoadEvent_UpdataStatus(sensor);
                                }
                            }
                        }
                        break;
                    case Command.InstructionPanelKeyACK:
                    case Command.ReadInstructionPanelKeyACK:
                        byte reACPanel = 0;
                        if (receiveBytes.Length == 2)
                        {
                            reACPanel = 1;
                        }
                        else if (receiveBytes.Length == 3)
                        {
                            reACPanel = receiveBytes[2];
                        }
                        else
                        {
                            break;
                        }
                        foreach (var ac in DB_ResidenceData.functionList.aCs)
                        {
                            if (ac.GetBusId() == subnetID + "_" + deviceID + "_" + reACPanel)
                            {
                                switch (receiveBytes[0])
                                {
                                    case 3://
                                        ac.trait_on_off.value = receiveBytes[1] == 1 ? "on" : "off";
                                        break;
                                    case 4:
                                    case 7:
                                    case 8:
                                    case 19:
                                        ac.trait_temp.value = receiveBytes[1];
                                        break;
                                    case 5:
                                        ac.curFanIndex = receiveBytes[1];
                                        break;
                                    case 6:
                                        ac.curModeIndex = receiveBytes[1];
                                        break;
                                }
                                ac.lastState = "";
                                ac.lastState += " " + ac.trait_temp.value + ac.tempUnitString;
                                RoomPage.UpdataStates(ac);
                                FunctionPage.UpdataStates(ac);
                                HomePage.UpdataFunctionStates(ac);
                                ClassificationPage.UpdataInfo(ac);
                                ACPage.UpdataStates(ac);
                            }
                        }
                        break;
                    case Command.ReadPanleTempACK://1944
                    case Command.PanleBroadcastTemp:
                        foreach (var ac in DB_ResidenceData.functionList.aCs)
                        {
                            if (ac.GetBusId() == subnetID + "_" + deviceID + "_" + receiveBytes[0])
                            {
                                ac.indoorTemp = receiveBytes[1];
                                FunctionPage.UpdataStates(ac);
                            }
                        }
                        break;
@@ -466,98 +551,6 @@
                return "0" + s;
            }
            return s;//
        }
        static void httpListener_EventHandler(System.Collections.Specialized.NameValueCollection nameValueCollection, System.IO.Stream outputStream, System.IO.Stream inputStream)
        {
            try
            {
                if (nameValueCollection["Command"] != null && nameValueCollection["Command"].StartsWith("Get"))
                {
                    string tempFileName = nameValueCollection["Command"].Replace("Get", "");
                    if ("AllFiles" == tempFileName)
                    {
                        byte[] bytes = Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(FileUtils.ReadFiles()));
                        outputStream.Write(bytes, 0, bytes.Length);
                        outputStream.Flush();
                    }
                    else
                    {
                        byte[] bytes;
                        if (!FileUtils.Exists(tempFileName))
                        {
                            bytes = Encoding.UTF8.GetBytes("文件名不存在!");
                            outputStream.Write(bytes, 0, bytes.Length);
                            outputStream.Flush();
                            return;
                        }
                        System.IO.FileStream fs = new System.IO.FileStream(Application.RootPath + tempFileName, System.IO.FileMode.Open);
                        bytes = new byte[fs.Length];
                        fs.Read(bytes, 0, bytes.Length);
                        fs.Close();
                        outputStream.Write(bytes, 0, bytes.Length);
                        outputStream.Flush();
                    }
                }
                else if (nameValueCollection["Command"].StartsWith("Upload"))
                {
                    string path = Application.RootPath + nameValueCollection["Command"].Replace("Upload", "");
                    string dePath = nameValueCollection["Command"].Replace("Upload", "");
                    FileUtils.WriteFileByInputStream(path, inputStream);
                    var reString = CommonPage.MyEncodingUTF8.GetString(FileUtils.ReadFile(path));
                    var common = JsonConvert.DeserializeObject<AProtocolEntity>(reString);
                    //Console.WriteLine(common.ToString());
                    if(dePath == "FunctionList")
                    {
                        foreach(var obj in common.objects)
                        {
                            DB_ResidenceData.residenceData.functionList.AddFunction(obj);
                        }
                        DB_ResidenceData.residenceData.functionList.GetAllFunction();
                    }
                }
                else
                {
                    byte[] bytes = System.Text.Encoding.UTF8.GetBytes("请求命令无效!");
                    outputStream.Write(bytes, 0, bytes.Length);
                    outputStream.Flush();
                }
            }
            catch (Exception ex)
            {
                MainPage.Log("httpListener_EventHandler" + ex.ToString());
            }
        }
        /// <summary>
        /// 监听请求的Http端口
        /// </summary>
        public static int Port = 5555;
        static void httpListener_EventHandler(string rawUrl, System.IO.Stream outputStream, System.IO.Stream inputStream)
        {
            foreach (var musicInfo in MusicInfo.MusicInfoList)
            {
                if ("audio-item-" + musicInfo.ID == rawUrl.TrimStart('/'))
                {
                    var file = new System.IO.FileStream(musicInfo.Data, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                    byte[] bytes = new byte[1024];
                    try
                    {
                        while (file.CanRead)
                        {
                            int len = file.Read(bytes, 0, bytes.Length);
                            if (len == 0)
                            {
                                break;
                            }
                            outputStream.Write(bytes, 0, len);
                        }
                    }
                    catch { }
                    file.Close();
                    break;
                }
            }
        }
    }
}