using System; using System.Collections.Generic; using HDL_ON.Common; using HDL_ON.DAL.Server; using Shared; namespace HDL_ON.Entity { /// /// 安防中心 /// public class SecurityCenter { static SecurityCenter _center; public static SecurityCenter Security { get { if(_center == null) { _center = new SecurityCenter(); } return _center; } } SecurityAlarm _securityAlarm; /// /// 当前布防模式 /// public SecurityAlarm CurrentDefenseMode { get { return _securityAlarm; } set { _securityAlarm = value; UI.HomePage.LoadEvent_RefreshSecurityStatus(); } } HttpServerRequest httpServerRequest = new HttpServerRequest(); List _securityAlarmList; /// /// 防区列表 /// public List securityAlarmList { get { if(_securityAlarmList == null) { _securityAlarmList = GetSecurityAlarmList(); } return _securityAlarmList; } } /// /// 读取云端安防列表 /// /// public List GetSecurityAlarmList() { var pack = httpServerRequest.GetSecurityList(); if(pack.Code == StateCode.SUCCESS) { var revData = Newtonsoft.Json.JsonConvert.DeserializeObject>(pack.Data.ToString()); if(revData!= null) { _securityAlarmList = revData; return revData; } } return new List(); } /// /// 状态 enable布防、disable撒防 /// public string status { get { string result = "disable"; foreach(var s in securityAlarmList) { if(s.status == "enable") { result = "enable"; break; } } return result; } } } public class SecurityAlarm { /// /// 安防云端id /// public string userSecurityId; /// /// 安防sid /// public string sid; /// /// 安防名称 /// public string name; /// /// 安防延时 /// public string delay; /// /// 状态 enable布防、disable撒防 /// public string status = "disable"; /// /// 类型 /// "all"--全宅布防, /// "normal"--普通模式, /// "all_day":24小时, /// "mute":静音 /// public string type = "normal"; /// /// 更新时间 /// public string modifyTime; /// /// 安防输入条件 /// public List input = new List(); /// /// 安防输出 /// public List output = new List(); /// /// 安防通知配置 /// public SecurityNoticeConfig noticeConfig = new SecurityNoticeConfig(); /// /// 安防推送配置 /// public List pushConfigs = new List(); /// /// 数据存储文件名 /// [Newtonsoft.Json.JsonIgnore] public string savePath { get { return "SecurityData_" + sid; } } /// /// 保存文件 /// public void SaveFile() { var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this)); FileUtlis.Files.WriteFileByBytes(savePath, ssd); } /// /// 保存安防信息 /// public string SaveInfo() { HttpServerRequest hsr = new HttpServerRequest(); if (string.IsNullOrEmpty(sid)) { NewSid(); var pack = hsr.AddSecurity(new List() { this }); if (pack.Code == StateCode.SUCCESS) { SaveFile(); }else { sid = ""; } return pack.Code; } else { var pack = hsr.EditSecurity(new List() { this }); if (pack.Code == StateCode.SUCCESS) { SaveFile(); } return pack.Code; } } /// /// 生成sid方法 /// public void NewSid() { 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); arry[1] = (byte)((m & 0xFF00) >> 8); arry[2] = (byte)((m & 0xFF0000) >> 16); arry[3] = (byte)((m >> 24) & 0xFF); sTimeSpan = arry[0].ToString("X2") + arry[1].ToString("X2") + arry[2].ToString("X2") + arry[3].ToString("X2"); if (sTimeSpan.Length >= 8) { sTimeSpan = sTimeSpan.Substring(0, 8); } else { sTimeSpan = Guid.NewGuid().ToString().Substring(0, 8); //sTimeSpan = "00000000"; } sid = sOidBeginsWith + sTimeSpan; sid += "16"; sid += "1601"; int maxId = 1; for (int i = 0; i < SecurityCenter.Security.securityAlarmList.Count; i++) { string s = SecurityCenter.Security.securityAlarmList[i].sid.Substring(20, 4); int iThisSceneId = Convert.ToInt16(s, 16); if (iThisSceneId > maxId) maxId = iThisSceneId; } sid += (maxId + 1).ToString("X4"); sid += "0000"; } catch { } } } /// /// 安防状态配置 /// public class SecurityState { /// /// 安防云端id /// public string userSecurityId; /// /// 网关id 网关id 如果传的是sid该字段需要传 /// public string gatewayId; /// /// 安防sid /// public string sid; /// /// 状态 enable布防、disable撒防 /// public string status; } /// /// 安防bypass设置对象 /// public class SecurityBypass { /// /// 安防云端id /// public string userSecurityId; /// /// 输入设备bypass状态列表 /// public List input = new List(); } /// /// 输入设备bypass状态 /// public class SecurityBypassInput { /// /// 设备sid /// public string sid; /// /// true:启用 /// false:停用(临时bypass) /// public string bypass; } /// /// 安防输入 /// public class SecurityInput { /// /// 输入条件(功能)的sid /// 输入设备sid /// public string sid; /// /// Bypass设置 true:启用中、false:临时bypass中 /// public string bypass; /// /// 安防输入条件 /// public List condition = new List(); /// /// 加入输入条件 /// 本地判断使用 /// [Newtonsoft.Json.JsonIgnore] public bool addCondition = false; [Newtonsoft.Json.JsonIgnore] Function _function = null; /// /// 对应的功能对象 /// /// public Function GetFunction() { if (_function == null) { _function = FunctionList.List.Functions.Find((obj) => obj.sid == sid); } return _function; } /// /// 获取单个状态的显示文本 /// /// /// public string GetStateText(string inputValue) { string text = ""; if (GetFunction() == null) return text; switch (GetFunction().spk) { case SPK.SensorGas: case SPK.SensorSmoke: if (inputValue == "true") { text += Language.StringByID(StringId.InAlarm) + " "; } else if (inputValue == "false") { text += Language.StringByID(StringId.Normal) + " "; } break; case SPK.SensorPir: if (inputValue == "true") { text += Language.StringByID(StringId.youren) + " "; } else if (inputValue == "false") { text += Language.StringByID(StringId.wuren) + " "; } break; case SPK.SensorWater: if (inputValue == "true") { text += Language.StringByID(StringId.WaterLeakage) + " "; } else if (inputValue == "false") { text += Language.StringByID(StringId.Normal) + " "; } break; case SPK.SensorDoorWindow: if (inputValue == "true") { text += Language.StringByID(StringId.Open) + " "; } else if (inputValue == "false") { text += Language.StringByID(StringId.Close) + " "; } break; } return text; } /// /// 状态文本 /// /// public string StateText () { string text = ""; switch(GetFunction().spk) { case SPK.SensorGas: case SPK.SensorSmoke: foreach (var con in condition) { if (con.value == "true") { text += Language.StringByID(StringId.InAlarm) + " "; } else if (con.value == "false") { text += Language.StringByID(StringId.Normal) + " "; } } break; case SPK.SensorPir: foreach (var con in condition) { if (con.value == "true") { text += Language.StringByID(StringId.youren) + " "; } else if (con.value == "false") { text += Language.StringByID(StringId.wuren) + " "; } } break; case SPK.SensorWater: foreach (var con in condition) { if (con.value == "true") { text += Language.StringByID(StringId.WaterLeakage) + " "; } else if (con.value == "false") { text += Language.StringByID(StringId.Normal) + " "; } } break; case SPK.SensorDoorWindow: foreach (var con in condition) { if (con.value == "true") { text += Language.StringByID(StringId.Open) + " "; } else if (con.value == "false") { text += Language.StringByID(StringId.Close) + " "; } } break; } return text; } } /// /// 安防输入条件 /// public class SecurityInputCondition { /// /// 属性(条件)名称 /// public string key; /// /// 操作(执行)条件 /// < 小于 > 大于 ==等于 /// public string comparator; /// /// 条件值类型 /// int \float\ string /// public string data_type = "string"; /// /// 值 /// public string value; } /// /// 安防输出 /// public class SecurityOutput { /// /// 控制目标类型 /// 0:设备 /// 1:场景 /// 2:自动化 /// public string target_type ="0"; /// /// 输出目标的sid /// public string sid; /// /// 安防输出状态 /// public List status = new List(); /// /// 加入输入条件 /// 本地判断使用 /// [Newtonsoft.Json.JsonIgnore] public bool addCondition = false; [Newtonsoft.Json.JsonIgnore] Function _function = null; /// /// 对应的功能对象 /// /// public Function GetFunction() { if (_function == null) { _function = FunctionList.List.Functions.Find((obj) => obj.sid == sid); } return _function; } /// /// 状态文本 /// /// public string StateText() { string text = ""; if (target_type == "0") { switch (GetFunction().spk) { case SPK.LightDimming: case SPK.LightCCT: case SPK.LightRGB: case SPK.LightSwitch: foreach (var state in status) { if (state.key == FunctionAttributeKey.Brightness) { if (state.value == "0") { text = Language.StringByID(StringId.Close); } else { text = Language.StringByID(StringId.Open); } } } break; } }else if(target_type == "1") { } return text; } } /// /// 安防输出状态 /// public class SecurityOutputStatus { public string key; public string value; } /// /// 安防通知配置 /// public class SecurityNoticeConfig { /// /// 是否开启通知 /// public bool enable; /// /// 通知内容 /// public string noticeContent; } /// /// 安防推送配置 /// public class SecurityPushConfig { /// /// 推送方式 /// APP:app push /// SMS:短信 /// public string pushMethod; /// /// 推送目标 /// public List pushTarget = new List(); } }