xm
2021-12-01 6d73bf6e816570291865674bef8bce8972e4de3f
HDL_ON/Entity/Function/SecurityAlarm.cs
@@ -1,11 +1,29 @@
using System;
using System.Collections.Generic;
using HDL_ON.Common;
using HDL_ON.DAL.Server;
using Shared;
namespace HDL_ON.Entity
{
    /// <summary>
    ///
    /// </summary>
    public class SecurityAlarm
    {
        public SecurityAlarm()
        {
            pushConfigs.Add(new SecurityPushConfig()
            {
                pushMethod = "APP",
                pushTarget = new List<string>() { UserInfo.Current.ID }
            });
        }
        /// <summary>
        /// 安防云端id
        /// </summary>
        public string userSecurityId;
        /// <summary>
        /// 安防sid
        /// </summary>
@@ -19,9 +37,15 @@
        /// </summary>
        public string delay;
        /// <summary>
        /// 状态 enable布防、disable撒防
        /// 状态
        /// 布防:enable
        /// 撒防:disable
        /// </summary>
        public string status;
        public string status = "disable";
        /// <summary>
        /// 报警状态
        /// </summary>
        public bool alarm = false;
        /// <summary>
        /// 类型
        /// "all"--全宅布防,
@@ -29,7 +53,7 @@
        /// "all_day":24小时,
        /// "mute":静音
        /// </summary>
        public string type;
        public string type = "normal";
        /// <summary>
        /// 更新时间
        /// </summary>
@@ -70,6 +94,96 @@
            var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
            FileUtlis.Files.WriteFileByBytes(savePath, ssd);
        }
        /// <summary>
        /// 保存安防信息
        /// </summary>
        public string SaveInfo()
        {
            HttpServerRequest hsr = new HttpServerRequest();
            this.noticeConfig.enable = true;
            this.noticeConfig.noticeContent = Language.StringByID(StringId.SecurityAlarmTipString).Replace("{0}", name);
            if (string.IsNullOrEmpty(sid))
            {
                NewSid();
                var pack = hsr.AddSecurity(new List<SecurityAlarm>() { this });
                if (pack.Code == StateCode.SUCCESS)
                {
                    SaveFile();
                }
                else
                {
                    sid = "";
                }
                return pack.Code;
            }
            else
            {
                var pack = hsr.EditSecurity(new List<SecurityAlarm>() { this });
                if (pack.Code == StateCode.SUCCESS)
                {
                    SaveFile();
                }
                return pack.Code;
            }
        }
        /// <summary>
        /// 生成sid方法
        /// </summary>
        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 < FunctionList.List.securities.Count; i++)
                {
                    string s = FunctionList.List.securities[i].sid.Substring(20, 4);
                    int iThisSceneId = Convert.ToInt16(s, 16);
                    if (iThisSceneId > maxId)
                        maxId = iThisSceneId;
                }
                sid += (maxId + 1).ToString("X4");
                sid += "0000";
            }
            catch
            {
            }
        }
    }
    /// <summary>
@@ -126,11 +240,10 @@
        public string bypass;
    }
    /// <summary>
    /// 安防输入
    /// </summary>
public class SecurityInput
    public class SecurityInput
    {
        /// <summary>
        /// 输入条件(功能)的sid
@@ -145,6 +258,159 @@
        /// 安防输入条件
        /// </summary>
        public List<SecurityInputCondition> condition = new List<SecurityInputCondition>();
        /// <summary>
        /// 判断条件
        /// </summary>
        public string comparator = "=";
        /// <summary>
        /// 加入输入条件
        /// 本地判断使用
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public bool addCondition = false;
        [Newtonsoft.Json.JsonIgnore]
        Function _function = null;
        /// <summary>
        /// 对应的功能对象
        /// </summary>
        /// <returns></returns>
        public Function GetFunction()
        {
            if (_function == null)
            {
                _function = FunctionList.List.Functions.Find((obj) => obj.sid == sid);
            }
            return _function;
        }
        /// <summary>
        /// 获取单个状态的显示文本
        /// </summary>
        /// <param name="inputValue"></param>
        /// <returns></returns>
        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;
        }
        /// <summary>
        /// 状态文本
        /// </summary>
        /// <returns></returns>
        public string StateText ()
        {
            string text = "";
            switch(GetFunction().spk)
            {
                case SPK.SensorGas:
                case SPK.SensorSmoke:
                    foreach (var con in condition)
                    {
                        if (con.value == "true" || con.value == "alarm")
                        {
                            text += Language.StringByID(StringId.InAlarm) + " ";
                        }
                        else if (con.value == "false" || con.value == "normal")
                        {
                            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" || con.value == "alarm")
                        {
                            text += Language.StringByID(StringId.WaterLeakage) + " ";
                        }
                        else if (con.value == "false" || con.value == "normal")
                        {
                            text += Language.StringByID(StringId.Normal) + " ";
                        }
                    }
                    break;
                case SPK.SensorDoorWindow:
                    foreach (var con in condition)
                    {
                        if (con.value == "true" || con.value == "open")
                        {
                            text += Language.StringByID(StringId.Open) + " ";
                        }
                        else if (con.value == "false" || con.value == "close")
                        {
                            text += Language.StringByID(StringId.Close) + " ";
                        }
                    }
                    break;
            }
            return text;
        }
        
    }
    /// <summary>
@@ -179,9 +445,11 @@
    {
        /// <summary>
        /// 控制目标类型
        /// 设备:0,场景1,自动化2
        /// 0:设备
        /// 1:场景
        /// 2:自动化
        /// </summary>
        public string target_type;
        public string target_type ="0";
        /// <summary>
        /// 输出目标的sid
        /// </summary>
@@ -190,6 +458,69 @@
        /// 安防输出状态
        /// </summary>
        public List<SecurityOutputStatus> status = new List<SecurityOutputStatus>();
        /// <summary>
        /// 加入输入条件
        /// 本地判断使用
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public bool addCondition = false;
        [Newtonsoft.Json.JsonIgnore]
        Function _function = null;
        /// <summary>
        /// 对应的功能对象
        /// </summary>
        /// <returns></returns>
        public Function GetFunction()
        {
            if (_function == null)
            {
                _function = FunctionList.List.Functions.Find((obj) => obj.sid == sid);
            }
            return _function;
        }
        /// <summary>
        /// 状态文本
        /// </summary>
        /// <returns></returns>
        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;
        }
    }
    /// <summary>
    /// 安防输出状态
@@ -231,4 +562,23 @@
        /// </summary>
        public List<string> pushTarget = new List<string>();
    }
    /// <summary>
    /// A协议控制数据的对象
    /// </summary>
    public class SecurityStatusObj
    {
        public List<SecurityStatusData> objects = new List<SecurityStatusData>();
        public string time_stamp = "";
        public string id = "";
    }
    public class SecurityStatusData
    {
        public string sid;
        public string status ="disable" ;
        public bool alarm = false;
    }
}