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;
@@ -33,25 +35,25 @@
        #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 卷帘
        /// 产品时间戳: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";
@@ -162,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)
                    {
@@ -173,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;
            }
@@ -187,7 +193,7 @@
        /// <summary>
        /// 使用次数
        /// </summary>
        public double usageCount = 0;
        public double controlCounter = 0;
        /// <summary>
        /// 使用频率
        /// </summary>
@@ -195,13 +201,73 @@
        {
            get
            {
                return usageCount / 7;
                return controlCounter / 7;
            }
        }
        /// <summary>
        /// 固定的序号
        /// </summary>
        public int fixedSerialNumber = int.MaxValue;
        /// <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()
        {
@@ -231,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;
        }
@@ -250,31 +316,103 @@
                return "FunctionData_" + sid;
            }
        }
        /// <summary>
        /// 保存功能数据
        /// 保存功能文件
        /// </summary>
        public void SaveFunctionData(bool upSevser)
        public void SaveFunctionFile()
        {
            if (upSevser)
            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 pm = new DAL.Server.HttpServerRequest();
                var pack = pm.UpdataDevcieInfo(this);
                if (pack.Code == DAL.Server.StateCode.SUCCESS)
                var pack = ApiUtlis.Ins.HttpRequest.UpdataDevcieBindRoomInfo(this);
                //直接保存本地,
                SaveFunctionFile();
                if (pack.Code == StateCode.SUCCESS)
                {
                    var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
                    Common.FileUtlis.Files.WriteFileByBytes(savePath, ssd);
                }
                else
                {
                    Utlis.ShowTip(Language.StringByID(StringId.EditFunctionInfoFail) + "\r\nCode:" + pack.Code);
                    IMessageCommon.Current.ShowErrorInfoAlter(pack.Code);
                }
            }
            else
            {
                var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
                Common.FileUtlis.Files.WriteFileByBytes(savePath, ssd);
            }
            })
            { 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(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>
@@ -286,19 +424,37 @@
            var sFunc = new SceneFunction();
            foreach (var attr in attributes)
            {
                if (attr.curValue.ToString() == "{}")
                switch (attr.key)
                {
                    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() });
                    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;
@@ -343,6 +499,7 @@
            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()
@@ -503,6 +660,10 @@
        /// 室内温度
        /// </summary>
        public const string IndoorTemp = "room_temp";
        /// <summary>
        /// value
        /// </summary>
        public const string Value = "value";
    }
    /// <summary>
    /// 设备功能oid
@@ -618,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传感器
@@ -638,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 = "electrical.socket";
        public const string ElectricSocket = "electrical.socket";
        /// <summary>
        /// 家电、电视
        /// </summary>
        public const string ElectricTV = "electrical.tv";
        public const string ElectricTV = "electrical.tv";
        /// <summary>
        /// 家电、风扇
        /// </summary>
        public const string ElectricFan = "electrical.fan";
    }
}
}