wei
2020-12-17 bb6ad792b598927a5459a5fb6f6c27fb1aa9e94e
HDL_ON/Entity/Function/Scene.cs
@@ -1,13 +1,39 @@
using System;
using System.Collections.Generic;
using HDL_ON.DAL.Server;
using Shared;
namespace HDL_ON.Entity
{
    public class SceneApiPack
    {
        static SceneApiPack _ScenePack;
        public static SceneApiPack ScenePack
        {
            get
            {
                if(_ScenePack == null)
                {
                    _ScenePack = new SceneApiPack();
                }
                return _ScenePack;
            }
        }
    }
    public class Scene 
    {
        public Scene()
        {
            sid = NewSid();
            if (!DB_ResidenceData.Instance.CheckWhetherGatewayIdIsNull())
            {
                gatewayId = DB_ResidenceData.Instance.HomeGateway.gatewayId;
            }
        }
        /// <summary>
        /// 远端唯一ID
        /// 云端唯一ID
        /// </summary>
        public string userSceneId = "";
        /// <summary>
@@ -31,39 +57,43 @@
        /// </summary>
        public List<string> roomIds = new List<string>();
        /// <summary>
        /// 所属房间列表
        /// </summary>
        public List<string> uids = new List<string>();
        /// <summary>
        /// 延时
        /// </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;
            }
        }
        public string 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>
        /// 场景分组
@@ -78,11 +108,33 @@
        /// </summary>
        public bool collect = false;
        ///// <summary>
        ///// 场景背景
        ///// </summary>
        //public string ImagePath = "Intelligence/Gallery/scenebg1.png";//"FunctionIcon/Scene/s1.png";
        /// <summary>
        /// 场景背景
        /// </summary>
        public string ImagePath = "Intelligence/Gallery/scenebg1.png";//"FunctionIcon/Scene/s1.png";
        ///// <summary>
        ///// 场景背景
        ///// </summary>
        public string ImagePath
        {
            get
            {
                if (string.IsNullOrEmpty(image))
                {
                    return "Intelligence/Gallery/scenebg1.png";
                }
                else
                {
                    return image;
                }
            }
            set
            {
                image = value;
            }
        }
        /// <summary>
        /// 场景功能列表
@@ -94,21 +146,18 @@
        public List<ScenePushConfig> pushConfigs = new List<ScenePushConfig>();
        /// <summary>
        /// 生成场景sid
        /// 生成逻辑sid方法
        /// </summary>
        public void NewSid()
        public string NewSid()
        {
            string sSceneid = "";
            string sceneId = "";
            try
            {
                string sOidBeginsWith = "000101";//厂商 + 通讯方式
                DateTime dt = DateTime.Now;
                DateTime startTime = TimeZoneInfo.ConvertTimeToUtc(new DateTime(2020, 1, 1));
                long m = (long)((dt - startTime).TotalMilliseconds / 10);
                string sTimeSpan = "00000000";
                byte[] arry = new byte[4];
                arry[0] = (byte)(m & 0xFF);
@@ -127,25 +176,29 @@
                    sTimeSpan = "00000000";
                }
                sSceneid = sOidBeginsWith + sTimeSpan;
                sceneId = sOidBeginsWith + sTimeSpan;
                sSceneid += "0A";
                sSceneid += "0A01";
                //0A01 物模型为场景, 0001 表示 1 号场景功能
                int iTopSceneId = 1;
                sceneId += "0A";
                sceneId += "0A01";
                int maxId = 1;
                Random random = new Random();
                iTopSceneId = random.Next(0, 255);
                iTopSceneId += random.Next(0, 255);
                sSceneid += iTopSceneId.ToString("X4");//场景号 两个byte
                sSceneid += "1100";
                sid = sSceneid;
                for (int i = 0; i < FunctionList.List.scenes.Count; i++)
                {
                    string s = FunctionList.List.scenes[i].sid.Substring(20, 4);
                    int iThisSceneId = Convert.ToInt16(s, 16);
                    if (iThisSceneId > maxId)
                        maxId = iThisSceneId;
                }
                sceneId += (maxId + 1).ToString("X4");
                sceneId += "0000";
            }
            catch
            {
                sid = sSceneid;
                return sceneId;
            }
            return sceneId;
        }
        /// <summary>
@@ -157,7 +210,7 @@
            string roomNameList = "";
            foreach (var roomId in roomIds)
            {
                var findRoom = DB_ResidenceData.Rooms.Find(obj => obj.uid == roomId);
                var findRoom = SpatialInfo.CurrentSpatial.RoomList.Find(obj => obj.roomId == roomId);
                if (findRoom == null)
                {
                    continue;
@@ -186,20 +239,124 @@
                return "SceneData_" + sid;
            }
        }
        /// <summary>
        /// 保存功能数据
        /// </summary>
        public void SaveSceneData()
        public void SaveSceneData(bool upServer)
        {
            var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
            FileUtils.WriteFileByBytes(savePath + this.sid, ssd);
            MainPage.Log($"Save Scene Data : {this.sid}");
            if (upServer)
            {
                UploadScene();
            }
            else
            {
                var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
                Common.FileUtlis.Files.WriteFileByBytes(savePath, ssd);
                MainPage.Log($"Save Scene Data : {savePath}");
            }
        }
        /// <summary>
        /// 收藏场景
        /// </summary>
        /// <returns></returns>
        public string CollectScene()
        {
            var pm = new HttpServerRequest();
            if (this.collect)
            {
                var revPack = pm.CollectScene(this.userSceneId);
                return revPack;
            }
            else
            {
                var revPack = pm.CancelCollectScene(this.userSceneId);
                return revPack;
            }
        }
        /// <summary>
        /// 更新时间
        /// 编辑场景
        /// </summary>
        public DateTime refreshTime = DateTime.MinValue;
        public string EditScene()
        {
            var pm = new HttpServerRequest();
            var revPack = pm.EditScene(this);
            if (revPack.Code == StateCode.SUCCESS)
            {
                var scenes = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Scene>>(revPack.Data.ToString());
                var tempScene = scenes.Find((obj) => obj.sid == sid);
                if (tempScene != null)
                {
                    var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(tempScene));
                    Common.FileUtlis.Files.WriteFileByBytes(savePath, ssd);
                }
            }
            return revPack.Code;
        }
        /// <summary>
        /// 修改场景
        /// </summary>
        public string UpdateScene()
        {
            var pm = new HttpServerRequest();
            var revPack = pm.EditScene(this);
            if (revPack.Code == StateCode.SUCCESS)
            {
                var scenes = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Scene>>(revPack.Data.ToString());
                var tempScene = scenes.Find((obj) => obj.sid == sid);
                if (tempScene != null)
                {
                    var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(tempScene));
                    Common.FileUtlis.Files.WriteFileByBytes(savePath, ssd);
                }
            }
            return revPack.Code;
        }
        /// <summary>
        /// 上传数据到云端
        /// </summary>
        void UploadScene()
        {
            var pm = new HttpServerRequest();
            ResponsePackNew revPack;
            if (string.IsNullOrEmpty(createTime) && string.IsNullOrEmpty(modifyTime))
            {
                revPack = pm.AddScene(this);
                if (revPack.Code == StateCode.SUCCESS)
                {
                    var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
                    Common.FileUtlis.Files.WriteFileByBytes(savePath, ssd);
                }
            }
            else if (!string.IsNullOrEmpty(createTime) && !string.IsNullOrEmpty(modifyTime))
            {
                revPack = pm.EditScene(this);
                if (revPack.Code == StateCode.SUCCESS)
                {
                    var scenes = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Scene>>(revPack.Data.ToString());
                    var tempScene = scenes.Find((obj) => obj.sid == sid);
                    if (tempScene != null)
                    {
                        var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(tempScene));
                        Common.FileUtlis.Files.WriteFileByBytes(savePath, ssd);
                    }
                }
            }
        }
        /// <summary>
        /// 云端数据创建的时间
        /// </summary>
        public string createTime = "";
        /// <summary>
        /// 云端数据修改的最后时间
        /// </summary>
        public string modifyTime = "";
    }
    /// <summary>
    /// 场景功能对象
@@ -219,10 +376,11 @@
        /// 本地对应的功能
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public Function localFunction {
        public Function localFunction
        {
            get
            {
                if(_localFunction == null)
                if (_localFunction == null)
                {
                    _localFunction = ConvertFunctionObject();
                }
@@ -236,14 +394,10 @@
        /// <returns></returns>
        Function ConvertFunctionObject()
        {
            var localFunction = DB_ResidenceData.functionList.GetAllDeviceFunctionList().Find((obj) => obj.sid == sid);
            foreach (var s in status)
            {
                localFunction.attributes.Add(new FunctionAttributes() { key = s.key, curValue = s.value, value = new List<string>() { s.value } });
            }
            var localFunction = FunctionList.List.GetDeviceFunctionList().Find((obj) => obj.sid == sid);
            return localFunction;
        }
    }
    /// <summary>