wxr
2020-01-18 4cc04639fbadde3a7f3b9875353df0d90fc09043
HDL_ON/DAL/Net/CommonPage.cs
@@ -1,7 +1,8 @@
using System;
using System.Text;
using HDL_ON.DAL.Net;
using HDL_ON.Entity.Enumerative;
using HDL_ON.Entity;
using HDL_ON.UI;
using Shared;
using Shared.Net;
@@ -11,30 +12,20 @@
    {
        public static bool IsRemote;
        //public static AirQuality AirQuality;
        public static Action RefreshAir;
        public static bool FindGateway = false;
        public static bool FindGatewayChilren = false;
        public static string FindGatewayChilrenIPAddress = new NetWiFi ().BroadcastIpAddress.ToString ();
        public static bool LocalPhoneFindDevice = false;
        private static string ip = "0.0.0.0";
        public static System.Net.IPEndPoint EndPoint {
            get {
                try {
                    if (FindGateway) {
                        return new System.Net.IPEndPoint (System.Net.IPAddress.Parse (new NetWiFi ().BroadcastIpAddress.ToString ()), 6000);
                    } else if (FindGatewayChilren) {
                        try {
                            return new System.Net.IPEndPoint (System.Net.IPAddress.Parse (FindGatewayChilrenIPAddress), 6000);
                        } catch {
                            return new System.Net.IPEndPoint (System.Net.IPAddress.Parse ("224.0.168.188"), 6000);
                        }
                    } else {
                        return new System.Net.IPEndPoint (System.Net.IPAddress.Parse (new NetWiFi ().BroadcastIpAddress.ToString ()), 6000);
                    }
                } catch {
            get
            {
                try
                {
                    return new System.Net.IPEndPoint(System.Net.IPAddress.Parse(new NetWiFi().BroadcastIpAddress.ToString()), 6000);
                }
                catch
                {
                    //防止异常导致程序退出
                    return new System.Net.IPEndPoint (System.Net.IPAddress.Parse ("127.0.0.1"), 6000);
                    return new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), 6000);
                }
            }
        }
@@ -42,16 +33,23 @@
        ///保存设备备注才用gb2312,其他情况用utf8
        public static Encoding MyEncodingUTF8 = Encoding.UTF8;//Get
        public static Encoding MyEncodingGB2312 {
            get {
                try {
                    if (System.Globalization.CultureInfo.InstalledUICulture.EnglishName.ToUpper().StartsWith("CZECH")) {
                        return System.Text.Encoding.GetEncoding (1250);
                    } else {
                        return Encoding.GetEncoding ("gb2312");
            get
            {
                try
                {
                    if (System.Globalization.CultureInfo.InstalledUICulture.EnglishName.ToUpper().StartsWith("CZECH"))
                    {
                        return Encoding.GetEncoding(1250);
                    }
                }catch (Exception ex) {
                    MainPage.Log ("MyEncodingGB2312 Erorr : "+ex.Message);
                    return Encoding.GetEncoding ("gb2312");
                    else
                    {
                        return Encoding.GetEncoding("gb2312");
                    }
                }
                catch (Exception ex)
                {
                    MainPage.Log("MyEncodingGB2312 Erorr : " + ex.Message);
                    return Encoding.GetEncoding("gb2312");
                }
            }
        }
@@ -88,15 +86,57 @@
        public static float floatChange (byte b1, byte b2, byte b3, byte b4)
        {
            byte [] byteTemp = new byte [4] { b4, b3, b2, b1 };
            byte[] byteTemp = { b4, b3, b2, b1 };
            return BitConverter.ToSingle (byteTemp, 0);
        }
        static void Packet_ReceiveEvent (byte subnetID, byte deviceID, Command command, byte [] usefullBytes, string revGatewayIP)
        static void Packet_ReceiveEvent(byte subnetID, byte deviceID, Command command, byte[] receiveBytes, string revGatewayIP)
        {
            try
            {
                switch (command)
                {
                    case Command.SetSingleLightACK:
                        foreach (var function in DB_ResidenceData.residenceData.functions)
                        {
                            if (function.GetBusId() == subnetID + "_" + deviceID + "_" + receiveBytes[0])
                            {
                                if (function.funcType == FunctionType.Light)
                                {
                                    var light = function as Light;
                                    if (receiveBytes[2] == 0 && light.state == 0)
                                    {
                                        break;
                                    }
                                    if (receiveBytes[2] == 100 && light.state == 1)
                                    {
                                        break;
                                    }
                                    if (function.bus_DeviceType == "Relay")
                                    {
                                        var relay = function as Light;
                                        relay.state = receiveBytes[2] == 0 ? 0 : 1;
                                        HomePage.UpdataLightView(relay);
                                    }
                                    else if (function.bus_DeviceType == "Dimmer")
                                    {
                                        var dimmer = function as Dimmer;
                                        dimmer.state = receiveBytes[2] == 0 ? 0 : 1;
                                        dimmer.brightness = receiveBytes[1];
                                        if (receiveBytes[2] > 0)
                                            dimmer.lastState = Language.StringByID(StringId.Brightness) + " : " + receiveBytes[2] + "%";
                                        HomePage.UpdataLightView(dimmer);
                                    }
                                }
                            }
                        }
                        break;
                }
            }
            catch (Exception ex) {
                MainPage.Log($"Bus Rev Erorr : {ex.Message}");
            }
        }
        /// <summary>
        /// byte 变字符串
@@ -118,12 +158,12 @@
                if (nameValueCollection ["Command"] != null && nameValueCollection ["Command"].StartsWith ("Get")) {
                    string tempFileName = nameValueCollection ["Command"].Replace ("Get", "");
                    if ("AllFiles" == tempFileName) {
                        byte [] bytes = System.Text.Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (MyIO.FileUtils.ReadFiles ()));
                        byte [] bytes = System.Text.Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (FileUtils.ReadFiles ()));
                        outputStream.Write (bytes, 0, bytes.Length);
                        outputStream.Flush ();
                    } else {
                        byte [] bytes;
                        if (!MyIO.FileUtils.Exists (tempFileName)) {
                        if (!FileUtils.Exists (tempFileName)) {
                            bytes = System.Text.Encoding.UTF8.GetBytes ("文件名不存在!");
                            outputStream.Write (bytes, 0, bytes.Length);
                            outputStream.Flush ();
@@ -147,26 +187,6 @@
            } catch (Exception ex) {
                MainPage.Log ("httpListener_EventHandler" + ex.ToString ());
            }
        }
        /// <summary>
        /// 随机数高位
        /// </summary>
        public static byte RandomHigh;
        /// <summary>
        /// 随机数低位
        /// </summary>
        public static byte RandomLow;
        /// <summary>
        /// Updates Device's remake.
        /// </summary>
        public static void UpdateRemark (byte subnetID, byte deviceID, string remark)
        {
            byte [] updateBytes = new byte [20];
            byte [] remakeBytes = MyEncodingGB2312.GetBytes (remark);
            Array.Copy (remakeBytes, 0, updateBytes, 0, remakeBytes.Length < 20 ? remakeBytes.Length : 20);
            Control.ControlBytesSend (Command.UpdataRemake, subnetID, deviceID, updateBytes);
        }
        /// <summary>