wei
2020-12-23 d814c978efc068425c500a553cf7ec6b4f628219
HDL_ON/Entity/Function/Function.cs
@@ -1,5 +1,7 @@
using System;
using System;
using System.Collections.Generic;
using HDL_ON.Common;
using HDL_ON.DAL.Server;
using HDL_ON.DriverLayer;
using Shared;
@@ -30,29 +32,28 @@
        public Function()
        {
        }
        #region base info
        /// <summary>
        /// HDL统一协议格式:14bytes
        /// 举例: 来源   厂商代码 通讯方式  产品时间戳   产品类别 物模型类  通道号   大小类别
        /// HDL统一协议格式:14bytes
        /// 举例: 来源   厂商代码 通讯方式  产品时间戳   产品类别 物模型类  通道号   大小类别
        ///       1byte   1byte   1byte     4byte       1byte   2byte   2byte   2byte
        /// 来源:00   默认原生态系统数据 、01   网关或者其他A设备、02   调试软件、03   APP应用程序、04   第三方网关或者平台
        /// 厂商代码:01   HDL
        /// 来源:00   默认原生态系统数据 、01   网关或者其他A设备、02   调试软件、03   APP应用程序、04   第三方网关或者平台
        /// 厂商代码:01   HDL
        /// 通讯方式:01   HDL Bus、02   Zigbee、03 KNX、04 Z-Wave
        /// 产品时间戳:4bytes   以2020年1月1日算出的时间戳0.1s为单位
        /// 产品类别:01   调光器、02 继电器、03   干接点模块、04   传感器、05 面板
        /// 物模型类型:
      ///     01   开关类:01 开关、02 插座、03
      ///     02   照明: 01 开关、02   调光、03   色温、04   LED
        ///     03   遮阳: 01   窗帘电机、02 百叶窗、03 开合帘、04 卷帘
      ///     04   恒温器:01 空调、02 地暖、03 毛细空调
        /// 产品时间戳:4bytes   以2020年1月1日算出的时间戳0.1s为单位
        /// 产品类别:01   调光器、02 继电器、03   干接点模块、04   传感器、05 面板
        /// 物模型类型:
        ///     01   开关类:01 开关、02 插座、03
        ///     02   照明: 01 开关、02   调光、03   色温、04   LED
        ///     03   遮阳: 01   窗帘电机、02 百叶窗、03 开合帘、04 卷帘
        ///     04   恒温器:01 空调、02 地暖、03 毛细空调
        ///     05   新风      
        ///     06   影音      
        ///     07   音乐      
        ///     08   能源      
        ///     09   安防
        /// 大类别   1bytes   (预留)
        /// 大类别   1bytes   (预留)
        /// 小类别   1byte   (预留)
        /// </summary>
        public string sid = "0301011234567801012301230123";
@@ -120,11 +121,15 @@
        /// bus协议数据格式
        /// 使用A协议控制时,改属性为空
        /// </summary>
        public BusData bus_Data;
        public BusData bus;
        /// <summary>
        /// 是否收藏
        /// </summary>
        public bool collect = false;
        /// <summary>
        /// 是否在线
        /// </summary>
        public bool online = true;
        /// <summary>
        /// 云端数据创建的时间
        /// </summary>
@@ -146,37 +151,6 @@
        /// </summary>
        public int delay = 0;
        /// <summary>
        /// 延时显示的文本
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public string delayText
        {
            get
            {
                string text = "";
                switch (delay)
                {
                    case 0:
                        text = Language.StringByID(StringId.NoDelay);
                        break;
                    case 30:
                        text = "30s";
                        break;
                    case 60:
                        text = "1min";
                        break;
                    case 120:
                        text = "2min";
                        break;
                    case 300:
                        text = "5min";
                        break;
                }
                return text;
            }
        }
        /// <summary>
        /// 最后控制的一次状态
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
@@ -190,7 +164,7 @@
            {
                if (_trait_on_off == null)
                {
                    _trait_on_off = attributes.Find((obj) => obj.key == "on_off");
                    _trait_on_off = attributes.Find((obj) => obj.key == FunctionAttributeKey.OnOff);
                    //找不到属性需要声明一个,防止报错闪退
                    if (_trait_on_off == null)
                    {
@@ -201,8 +175,12 @@
                            max = 1,
                            min = 0,
                        };
                        _trait_on_off.curValue = "off";
                    }
                    _trait_on_off.curValue = "on";
                }
                if(_trait_on_off.curValue.ToString() != "on"&& _trait_on_off.curValue.ToString() != "off")
                {
                    _trait_on_off.curValue = "off";
                }
                return _trait_on_off;
            }
@@ -215,14 +193,15 @@
        /// <summary>
        /// 使用次数
        /// </summary>
        public double usageCount = 0;
        public double controlCounter = 0;
        /// <summary>
        /// 使用频率
        /// </summary>
        public double usageFrequency {
        public double usageFrequency
        {
            get
            {
                return usageCount / 7;
                return controlCounter / 7;
            }
        }
        /// <summary>
@@ -230,12 +209,72 @@
        /// </summary>
        public int fixedSerialNumber = int.MaxValue;
        public string GetBusId ()
        /// <summary>
        /// 收藏功能
        /// </summary>
        public void CollectFunction()
        {
            var result = "";
            //var waitPage = new Loading();
            new System.Threading.Thread(() =>
            {
                if (collect)
                {
                    result = ApiUtlis.Ins.HttpRequest.CollectDevice(deviceId).Code;
                }
                else
                {
                    result = ApiUtlis.Ins.HttpRequest.CancelCollectDevice(deviceId).Code;
                }
                //提示错误
                if (result != StateCode.SUCCESS)
                {
                    Application.RunOnMainThread(() =>
                    {
                        collect = !collect;
                        IMessageCommon.Current.ShowErrorInfoAlter(result);
                    });
                }
            })
            { IsBackground = true }.Start();
        }
        /// <summary>
        /// 编辑功能绑定房间信息
        /// </summary>
        public void EditBindRoomInfo(string roomId)
        {
            var result = "";
            new System.Threading.Thread(() =>
            {
                if (collect)
                {
                    result = ApiUtlis.Ins.HttpRequest.BindDeviceToRoom(new List<string>() { deviceId }, new List<string>() { roomId }).Code;
                }
                else
                {
                    result = ApiUtlis.Ins.HttpRequest.UnbindDeviceToRoom(deviceId,roomId);
                }
                //提示错误
                if (result != StateCode.SUCCESS)
                {
                    Application.RunOnMainThread(() =>
                    {
                        collect = !collect;
                        IMessageCommon.Current.ShowErrorInfoAlter(result);
                    });
                }
            })
            { IsBackground = true }.Start();
        }
        public string GetBusId()
        {
            string busId = "";
            if (bus_Data != null)
            if (bus != null)
            {
                busId = bus_Data.SubnetID + "_" + bus_Data.DeviceID + "_" + bus_Data.loopId;
                busId = bus.SubnetID + "_" + bus.DeviceID + "_" + bus.LoopId;
            }
            return busId;
        }
@@ -247,9 +286,9 @@
        public string GetRoomListName()
        {
            string roomNameList = "";
            foreach(var roomId in roomIds)
            foreach (var roomId in roomIds)
            {
                var findRoom = DB_ResidenceData.residenceData.Rooms.Find(obj => obj.uid == roomId);
                var findRoom = SpatialInfo.CurrentSpatial.RoomList.Find(obj => obj.roomId == roomId);
                if (findRoom == null)
                {
                    continue;
@@ -258,11 +297,11 @@
                {
                    roomNameList += ",";
                }
                roomNameList += findRoom.floorName + findRoom.roomName;
                roomNameList += findRoom.floorName +"-"+ findRoom.roomName;
            }
            if (roomNameList == "" && functionType == FunctionType.Scene)
            {
                roomNameList = Language.StringByID(StringId.WholeHouseScene);
                roomNameList = Language.StringByID(StringId.WholeZone);
            }
            return roomNameList;
        }
@@ -277,15 +316,104 @@
                return "FunctionData_" + sid;
            }
        }
        /// <summary>
        /// 保存功能文件
        /// </summary>
        public void SaveFunctionFile()
        {
            var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
            FileUtlis.Files.WriteFileByBytes(savePath, ssd);
        }
        /// <summary>
        /// 保存房间绑定信息
        /// </summary>
        public void UpdataRoomIds()
        {
            new System.Threading.Thread(() =>
            {
                var pack = ApiUtlis.Ins.HttpRequest.UpdataDevcieBindRoomInfo(this);
                //直接保存本地,
                SaveFunctionFile();
                if (pack.Code == StateCode.SUCCESS)
                {
                }
                else
                {
                    IMessageCommon.Current.ShowErrorInfoAlter(pack.Code);
                }
            })
            { IsBackground = true }.Start();
        }
        /// <summary>
        /// 更新功能信息
        /// </summary>
        public void UpdataFuncitonInfo()
        {
            {
                var pm = new HttpServerRequest();
                var pack = pm.UpdataDevcieInfo(this);
                SaveFunctionFile();
                if (pack.Code == StateCode.SUCCESS)
                {
                }
                else
                {
                    IMessageCommon.Current.ShowErrorInfoAlter(pack.Code);
                }
            })
            { IsBackground = true }.Start();
        }
        /// <summary>
        /// 更新功能名称
        /// </summary>
        public void UpdataFunctionName()
        {
            new System.Threading.Thread(() =>
            {
                var pm = new HttpServerRequest();
                var pack = pm.UpdataDevcieInfo(this);
                SaveFunctionFile();
                if (pack.Code == StateCode.SUCCESS)
                {
                }
                else
                {
                    IMessageCommon.Current.ShowErrorInfoAlter(pack.Code);
                }
            })
            { IsBackground = true }.Start();
        }
        /// <summary>
        /// 保存功能数据
        /// </summary>
        public void SaveFunctionData()
        {
            var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
            FileUtils.WriteFileByBytes( savePath, ssd);
            MainPage.Log($"Save FunctionData {this.functionType} : {this.sid}");
        }
        //public void SaveFunctionData(bool upSevser)
        //{
        //    if (upSevser)
        //    {
        //        new System.Threading.Thread(() =>
        //        {
        //            var pm = new HttpServerRequest();
        //            var pack = pm.UpdataDevcieInfo(this);
        //            if (pack.Code == StateCode.SUCCESS)
        //            {
        //                var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
        //                FileUtlis.Files.WriteFileByBytes(savePath, ssd);
        //            }
        //            else
        //            {
        //                IMessageCommon.Current.ShowErrorInfoAlter(pack.Code);
        //            }
        //        })
        //        { IsBackground = true }.Start();
        //    }
        //    else
        //    {
        //        var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
        //        FileUtlis.Files.WriteFileByBytes(savePath, ssd);
        //    }
        //}
        /// <summary>
        /// 转换成场景功能对象
@@ -296,8 +424,40 @@
            var sFunc = new SceneFunction();
            foreach (var attr in attributes)
            {
                sFunc.status.Add(new SceneFunctionStatus() { key = attr.key, value = attr.curValue.ToString() });
                switch (attr.key)
                {
                    case FunctionAttributeKey.OnOff:
                    case FunctionAttributeKey.Brightness:
                    case FunctionAttributeKey.Mode:
                    case FunctionAttributeKey.SetTemp:
                    case FunctionAttributeKey.FanSpeed:
                    case FunctionAttributeKey.Percent:
                    //case FunctionAttributeKey.FadeTime:
                        if (attr.curValue.ToString() == "{}")
                        {
                            if (attr.key == FunctionAttributeKey.OnOff)
                            {
                                attr.curValue = "off";
                            }
                            else
                            {
                                attr.curValue = "0";
                            }
                        }
                        if (attr.key == FunctionAttributeKey.SetTemp)
                        {
                            double vv = 16;
                            Double.TryParse(attr.curValue.ToString(), out vv);
                            sFunc.status.Add(new SceneFunctionStatus() { key = attr.key, value = Convert.ToInt32(vv).ToString() });
                        }
                        else
                        {
                            sFunc.status.Add(new SceneFunctionStatus() { key = attr.key, value = attr.curValue.ToString() });
                        }
                        break;
                }
            }
            sFunc.sid = this.sid;
            return sFunc;
        }
@@ -309,18 +469,18 @@
        /// <summary>
        /// 获取本地控制数据
        /// </summary>
        public GatewayAlinkControlObj GetGatewayAlinkControlData(Dictionary<string, string> commandDictionary)
        public AlinkFunctionStatusObj GetGatewayAlinkControlData(Dictionary<string, string> commandDictionary)
        {
            var sendDataObj = new GatewayAlinkControlObj();
            var sendDataObj = new AlinkFunctionStatusObj();
            sendDataObj.id = Control.Ins.msg_id.ToString();
            sendDataObj.time_stamp = Control.Ins.Get_TimeStamp();
            var acd = new AlinkControlData();
            acd.sid = sid;
            var aca = new AlinkControlAttributes();
            foreach(var dic in commandDictionary)
            foreach (var dic in commandDictionary)
            {
                var aca = new AlinkControlAttributes();
                aca.key = dic.Key;
                aca.value = dic.Value;
                acd.status.Add(aca);
@@ -334,11 +494,12 @@
        /// 获取Api控制数据
        /// </summary>
        /// <returns></returns>
        public ApiAlinkControlActionObj GetApiControlData(Dictionary<string,string> keyValues)
        public ApiAlinkControlActionObj GetApiControlData(Dictionary<string, string> keyValues)
        {
            ApiAlinkControlActionObj aaao = new ApiAlinkControlActionObj();
            aaao.deviceId = this.deviceId;
            aaao.spk = this.spk;
            aaao.bus = this.bus;
            foreach (var kv in keyValues)
            {
                aaao.attributes.Add(new AlinkControlAttributes()
@@ -368,18 +529,29 @@
        public string spk = "";
        public List<AlinkControlAttributes> attributes = new List<AlinkControlAttributes>();
        //[Newtonsoft.Json.JsonIgnore]
        public BusData bus = new BusData();
    }
    /// <summary>
    /// 本地控制
    /// A协议控制数据的对象
    /// </summary>
    public class GatewayAlinkControlObj
    public class AlinkFunctionStatusObj
    {
        public List<AlinkControlData> objects = new List<AlinkControlData>();
        public string time_stamp = "";
        public string id = "";
    }
    /// <summary>
    /// 本地状态读取
    /// A协议状态读取格式对象
    /// </summary>
    public class AlinkReadFunctionStatusObj
    {
        public string id = "0";
        public List<Dictionary<string, string>> objects = new List<Dictionary<string, string>>();
        public string time_stamp = "";
    }
    /// <summary>
    /// A协议控制数据
@@ -410,6 +582,7 @@
    /// 功能属性
    /// 属性字段解析:attri :属性内容,value 属性的值,max 最大值 min 最小值
    /// </summary>
    [System.Serializable]
    public class FunctionAttributes
    {
        /// <summary>
@@ -423,11 +596,11 @@
        /// <summary>
        /// 最大值
        /// </summary>
        public int max;
        public int max = 100;
        /// <summary>
        /// 最小值
        /// </summary>
        public int min;
        public int min = 0;
        /// <summary>
        /// 数据类型
        /// </summary>
@@ -437,270 +610,6 @@
        /// </summary>
        public object curValue = new object();
        //----app自定义
        /// <summary>
        /// 属性值单位
        /// </summary>
        string _uintString;
        [Newtonsoft.Json.JsonIgnore]
        public string uintString
        {
            get
            {
                if (!string.IsNullOrEmpty(_uintString))
                {
                    return _uintString;
                }
                var us = "";
                switch (key)
                {
                    case "temperature":
                    case "set_temperature":
                        us = "°C";
                        break;
                    case "percent":
                    case "brightness":
                        us = "%";
                        break;
                }
                return us;
            }
        }
        /// <summary>
        /// 属性名称显示文本
        /// 中英文显示
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public string name_text
        {
            get
            {
                string text = "";
                switch (key)
                {
                    case "on_off":
                        text = Language.StringByID(StringId.OnOff);
                        break;
                    case "brightness":
                        text = Language.StringByID(StringId.Brightness);
                        break;
                    case "color":
                        text = Language.StringByID(StringId.ColorValue);
                        break;
                    case "mode":
                        text = Language.StringByID(StringId.Mode);
                        break;
                    case "fan":
                        text = Language.StringByID(StringId.FanSpeed);
                        break;
                    case "temperature":
                case "set_temperature":
                        text = Language.StringByID(StringId.Temp);
                        break;
                    case "delay":
                        text = Language.StringByID(StringId.Delay);
                        break;
                    case "cct":
                        text = "CCT";
                        break;
                    //case "fade_time":
                    case "percent":
                        text = Language.StringByID(StringId.PercentAdjustment);
                        break;
                        //case "lock":
                        //case "ico":
                        //case "swing":
                        //case "set_point":
                        //case "pm25":
                        //case "volume":
                        //case "vol_step":
                        //case "source":
                        //case "treble":
                        //case "bass":
                        //case "playlist":
                        //case "song_name":
                        //case "current_status":
                        //case "enable":
                        //case "lux":
                        //case "adjust_value":
                        //case "range":
                        //case "humidity":
                        //case "type":
                        //case "state":
                        //case "sensitivity":
                        //case "pm25value":
                }
                return text;
            }
        }
        /// <summary>
        /// 获取当前属性的显示文本
        /// 中英文显示
        /// </summary>
        public string GetCurValueText()
        {
            return GetValueText(curValue.ToString());
        }
        /// <summary>
        /// 获取指定属性的显示文本
        /// 中英文显示
        /// </summary>
        public string GetValueText(string value)
        {
            string text = "";
            value = value.Replace("{}", "");
            switch (key)
            {
                case "on_off":
                    text = value == "on" ? Language.StringByID(StringId.On) : Language.StringByID(StringId.OFF);
                    break;
                case "temperature":
                case "set_temperature":
                case "brightness":
                case "percent":
                    if (value == "")
                    {
                        this.curValue = this.min;
                        text = this.min.ToString();
                    }
                    else
                    {
                        text = value;
                    }
                    break;
                //case "color":
                case "mode":
                    switch (value)
                    {
                        //----空调
                        case "auto":
                            text = Language.StringByID(StringId.Auto);
                            break;
                        case "cool":
                            text = Language.StringByID(StringId.Cool);
                            break;
                        case "heat":
                            text = Language.StringByID(StringId.Heat);
                            break;
                        case "dry":
                            text = Language.StringByID(StringId.Dry);
                            break;
                        case "fan":
                            text = Language.StringByID(StringId.AirSupply);
                            break;
                        //-----地热
                        case "day":
                            text = Language.StringByID(StringId.Day);
                            break;
                        case "night":
                            text = Language.StringByID(StringId.Night);
                            break;
                        case "away":
                            text = Language.StringByID(StringId.Away);
                            break;
                        case "normal":
                            text = Language.StringByID(StringId.Normal);
                            break;
                        case "timer":
                            text = Language.StringByID(StringId.Timer);
                            break;
                        default:
                            if (this.value.Contains("cool"))
                            {
                                this.curValue = "cool";
                                text = Language.StringByID(StringId.Cool);
                            }
                            else if (this.value.Contains("day"))
                            {
                                this.curValue = "day";
                                text = Language.StringByID(StringId.Day);
                            }
                            else
                            {
                                foreach (var v in this.value)
                                {
                                    this.curValue = v;
                                    text = GetCurValueText();
                                }
                            }
                            break;
                    }
                    break;
                case "fan":
                    switch (value)
                    {
                        case "high":
                            text = Language.StringByID(StringId.HighWindSpeed);
                            break;
                        case "medium":
                            text = Language.StringByID(StringId.MiddleWindSpeed);
                            break;
                        case "low":
                            text = Language.StringByID(StringId.LowWindSpeed);
                            break;
                        case "auto":
                            text = Language.StringByID(StringId.Auto);
                            break;
                        default:
                            if (this.value.Contains("low"))
                            {
                                this.curValue = "low";
                                text = Language.StringByID(StringId.LowWindSpeed);
                            }
                            else
                            {
                                foreach (var v in this.value)
                                {
                                    this.curValue = v;
                                    text = GetCurValueText();
                                }
                            }
                            break;
                    }
                    break;
                //case "delay":
                //case "cct":
                //case "fade_time":
                //case "lock":
                //case "ico":
                //case "swing":
                //case "set_point":
                //case "pm25":
                //case "volume":
                //case "vol_step":
                //case "source":
                //case "treble":
                //case "bass":
                //case "playlist":
                //case "song_name":
                //case "current_status":
                //case "enable":
                //case "lux":
                //case "adjust_value":
                //case "range":
                //case "humidity":
                //case "type":
                //case "state":
                //case "sensitivity":
                //case "pm25value":
                case "high":
                    text = Language.StringByID(StringId.HighWindSpeed);
                    break;
                case "medium":
                    text = Language.StringByID(StringId.MiddleWindSpeed);
                    break;
                case "low":
                    text = Language.StringByID(StringId.LowWindSpeed);
                    break;
                case "auto":
                    text = Language.StringByID(StringId.Auto);
                    break;
            }
            return text;
        }
    }
    /// <summary>
    /// 功能属性键名列表
@@ -718,7 +627,11 @@
        /// <summary>
        /// 颜色
        /// </summary>
        public const string Color = "color";
        public const string RGB = "rgb";
        /// <summary>
        /// 渐变时间
        /// </summary>
        public const string FadeTime = "fade_time";
        /// <summary>
        /// 模式
        /// </summary>
@@ -728,9 +641,9 @@
        /// </summary>
        public const string FanSpeed = "fan";
        /// <summary>
        /// 温度
        /// 设置温度
        /// </summary>
        public const string Temp = "temperature";
        public const string SetTemp = "set_temp";
        /// <summary>
        /// 延时
        /// </summary>
@@ -743,6 +656,14 @@
        /// 百分比
        /// </summary>
        public const string Percent = "percent";
        /// <summary>
        /// 室内温度
        /// </summary>
        public const string IndoorTemp = "room_temp";
        /// <summary>
        /// value
        /// </summary>
        public const string Value = "value";
    }
    /// <summary>
    /// 设备功能oid
@@ -763,6 +684,7 @@
    public class BusData
    {
        public string addresses = "FFFF";
        [Newtonsoft.Json.JsonIgnore]
        public byte SubnetID
        {
            get
@@ -770,6 +692,7 @@
                return Convert.ToByte(addresses.Substring(0, 2), 16);
            }
        }
        [Newtonsoft.Json.JsonIgnore]
        public byte DeviceID
        {
            get
@@ -777,7 +700,15 @@
                return Convert.ToByte(addresses.Substring(2, 2), 16);
            }
        }
        public byte loopId;
        public byte LoopId
        {
            get
            {
                return Convert.ToByte(loopId, 16);
            }
        }
        public string loopId;
    }
    public static class SPK
@@ -848,11 +779,11 @@
        public const string SensorPir = "sensor.pir";
        /// <summary>
        /// (门窗传感器)
        /// </summary>
        /// </summary>
        public const string SensorDoorWindow = "sensor.doorwindow";
        /// <summary>
        /// (PM2.5传感器)
        /// </summary>
        /// </summary>
        public const string SensorPm25 = "sensor.pm25";
        /// <summary>
        /// co2传感器
@@ -868,20 +799,20 @@
        public const string SensorHumidity = "sensor.humidity";
        /// <summary>
        /// (干接点)
        /// </summary>
        public const string DryContact = "dryContact.standard";
        /// </summary>
        public const string DryContact = "dryContact.standard";
        /// <summary>
        /// 家电、插座
        /// </summary>
        public const string ElectricSocket = "electric.socket ";
        public const string ElectricSocket = "electrical.socket";
        /// <summary>
        /// 家电、电视
        /// </summary>
        public const string ElectricTV = "electric.tv";
        public const string ElectricTV = "electrical.tv";
        /// <summary>
        /// 家电、风扇
        /// </summary>
        public const string ElectricFan = "electric.fan";
        public const string ElectricFan = "electrical.fan";
    }
}