黄学彪
2020-04-08 4dce704aaf8587cf3f91cf88f2208315a03c4cbb
ZigbeeApp/Shared/Phone/UserCenter/DoorLock/DoorLockCommonInfo.cs
@@ -9,7 +9,11 @@
{
    public class DoorLockCommonInfo : CommonDevice
    {
        #region  本地变量
        #region  本地变量
        /// <summary>
        /// 网关(门锁)所在的时间
        /// </summary>
        public static DateTime DoorlockZoneTime = DateTime.Now;
        /// <summary>
        /// 常开模式执行时间
        /// </summary>
@@ -752,18 +756,59 @@
        /// <summary>
        /// 将系统时间转换成UNIX时间戳(精确到秒)
        /// </summary>
        /// <param name="dateTime">北京时间</param>
        /// <param name="accurateToMilliseconds">精确到毫秒,否到秒</param>
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns>返回一个长整数时间戳</returns>
        public static string GetUnixTimeStamp(DateTime dateTime)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            //DateTime dtNow = DateTime.Parse(DateTime.Now.ToString());
            TimeSpan toNow = dateTime.Subtract(dtStart);
            string timeStamp = toNow.Ticks.ToString();
            timeStamp = timeStamp.Substring(0, timeStamp.Length - 7);
            return timeStamp;
        }
        /// <summary>
        /// 获取时区的时间
        /// </summary>
        /// <returns></returns>
        public static DateTime GetDoorLockZoneTime()
        {
            //通过经度获取时区
            var gwZone = int.Parse(CaculateTimeZone(Common.Config.Instance.Home.Longitude));
            //将本地时间转换成世界时
            var utc = DateTime.Now.AddHours(-gwZone);
            //世界时转换成时间戳
            var utcUnix = GetUnixTimeStamp(utc);
            //时区的时间戳
            var zoneUnix = int.Parse(utcUnix) + gwZone * 3600;
            var zoneDateTime = GetLocalTime(zoneUnix);
            DoorlockZoneTime = zoneDateTime;
            return zoneDateTime;
        }
        /// <summary>
        ///  获取时区
        /// </summary>
        /// <param name="currentLon">currentLon:"+"为东区,“-”为西区</param>
        /// <returns></returns>
        public static String CaculateTimeZone(double currentLon)
        {
            int timeZone;
            int shangValue = (int)(currentLon / 15);
            double yushuValue = Math.Abs(currentLon % 15);
            if (yushuValue <= 7.5)
            {
                timeZone = shangValue;
            }
            else
            {
                timeZone = shangValue + (currentLon > 0 ? 1 : -1);
            }
            return timeZone >= 0 ? "+" + Math.Abs(timeZone) : "-" + Math.Abs(timeZone);
        }
        /// <summary>
@@ -834,5 +879,150 @@
        }
        #endregion
        public enum DoorLockMessType
        {
            /// <summary>
            /// app操作
            /// </summary>
            AppOperate = 0,
            /// <summary>
            /// 设备上报
            /// </summary>
            DeviceReport = 1,
            /// <summary>
            /// 服务器推送
            /// </summary>
            ServicePush = 2,
        }
        /// <summary>
        /// 常开模式失效处理
        /// </summary>
        public static async void NomallyOpenModeInvalidDialog(ZigBee.Device.DoorLock doorLock, DoorLockMessType doorLockMessType, Action action)
        {
            if (UserCenterResourse.UserInfo.AuthorityNo != 1)
            {
                return;
            }
            string msg = Language.StringByID(R.MyInternationalizationString.NomallyModeIsCanceled).Replace("{0}", "\r\n");
            var confirm = Language.StringByID(R.MyInternationalizationString.SureCancel);
            var alert = new ShowDoorLockMsgControl(ShowDoorLockMsgControl.DoorLockMsgType.NomallyOpenMode, msg, confirm);
            var HaveLogicNormallyOpenMode = await Shared.Phone.Device.Logic.SkipView.Exist();
            if (HaveLogicNormallyOpenMode)
            {
                //有逻辑设置的弹窗
                alert = new ShowDoorLockMsgControl(ShowDoorLockMsgControl.DoorLockMsgType.CancelNomallyOpenModeWithLogic, msg, confirm);
            }
            alert.Show();
            alert.MsgControlClickEvent += async () =>
            {
                if (doorLockMessType == DoorLockMessType.DeviceReport || doorLockMessType == DoorLockMessType.ServicePush)
                {
                    //取消常开
                    NormallyOpenModeValue(doorLock, false);
                }
                else
                {
                    //保持常开
                    NormallyOpenModeValue(doorLock, true);
                }
                action?.Invoke();
                action = null;
            };
            alert.CancelClickEvent += async () =>
            {
                if (doorLockMessType == DoorLockMessType.DeviceReport || doorLockMessType == DoorLockMessType.ServicePush)
                {
                    var result = await doorLock.SetNormallyOpenModeFuncAsync(true);
                    if (result == null || result.defaultControlResponseData == null)
                    {
                        string msg0 = Language.StringByID(R.MyInternationalizationString.GwResponseOvertime);
                        Application.RunOnMainThread(() =>
                        {
                            new Tip() { MaxWidth = 150, Text = msg0, Direction = AMPopTipDirection.None, CloseTime = 1 }.Show(CommonPage.Instance);
                        });
                        NormallyOpenModeValue(doorLock, false);
                        return;
                    }
                    if (result.defaultControlResponseData.status != 0)
                    {
                        string msg1 = Language.StringByID(R.MyInternationalizationString.OpenNormallyOpenModeFailed);
                        Application.RunOnMainThread(() =>
                        {
                            new Tip() { MaxWidth = 150, Text = msg1, Direction = AMPopTipDirection.None, CloseTime = 1 }.Show(CommonPage.Instance);
                        });
                        NormallyOpenModeValue(doorLock, false);
                        return;
                    }
                    else
                    {
                        NormallyOpenModeValue(doorLock, true);
                    }
                }
                else
                {
                    NormallyOpenModeValue(doorLock, true);
                }
                action?.Invoke();
                action = null;
            };
            alert.ConfirmClickEvent += async () =>
            {
                if (doorLockMessType == DoorLockMessType.DeviceReport || doorLockMessType == DoorLockMessType.ServicePush)
                {
                    NormallyOpenModeValue(doorLock, false);
                }
                else
                {
                    var result = await doorLock.SetNormallyOpenModeFuncAsync(false);
                    if (result == null || result.defaultControlResponseData == null)
                    {
                        Application.RunOnMainThread(() =>
                        {
                            new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.GwResponseOvertime), Direction = AMPopTipDirection.None, CloseTime = 1 }.Show(CommonPage.Instance);
                        });
                        return;
                    }
                    if (result.defaultControlResponseData.status != 0)
                    {
                        string msg1 = Language.StringByID(R.MyInternationalizationString.CloseNormallyOpenModeFailed);
                        Application.RunOnMainThread(() => { });
                        new Tip() { MaxWidth = 150, Text = msg1, Direction = AMPopTipDirection.None, CloseTime = 1 }.Show(CommonPage.Instance);
                        return;
                    }
                    else
                    {
                        NormallyOpenModeValue(doorLock, false);
                        string msg2 = Language.StringByID(R.MyInternationalizationString.CloseNormallyOpenModeSuccess);
                        Application.RunOnMainThread(() =>
                        {
                            new Tip() { MaxWidth = 150, Text = msg2, Direction = AMPopTipDirection.None, CloseTime = 1 }.Show(CommonPage.Instance);
                        });
                        Shared.Phone.Device.Logic.SkipView.LockCloseLogic(doorLock);
                    }
                }
                action?.Invoke();
                action = null;
            };
        }
        /// <summary>
        /// 门锁常开模式值
        /// </summary>
        public static void NormallyOpenModeValue(ZigBee.Device.DoorLock doorLock, bool value)
        {
            string key = doorLock.DeviceAddr + "_" + doorLock.DeviceEpoint;
            if (doorLock.IsDoorLockNormallyMode.ContainsKey(key))
            {
                doorLock.IsDoorLockNormallyMode[key] = value;
            }
            else
            {
                doorLock.IsDoorLockNormallyMode.Add(key, value);
            }
        }
    }
}