From 09dfbfd9b77ec887c17b10f15799a1d3f56e4e17 Mon Sep 17 00:00:00 2001
From: 陈嘉乐 <cjl@hdlchina.com.cn>
Date: 星期二, 15 十二月 2020 12:04:42 +0800
Subject: [PATCH] 2020-12-15-1

---
 HDL_ON/UI/UI2/3-Intelligence/Automation/Send.cs |  216 ++++++++++++++++++-----------------------------------
 1 files changed, 75 insertions(+), 141 deletions(-)

diff --git a/HDL_ON/UI/UI2/3-Intelligence/Automation/Send.cs b/HDL_ON/UI/UI2/3-Intelligence/Automation/Send.cs
index 4412688..998734e 100644
--- a/HDL_ON/UI/UI2/3-Intelligence/Automation/Send.cs
+++ b/HDL_ON/UI/UI2/3-Intelligence/Automation/Send.cs
@@ -1,4 +1,4 @@
-锘縰sing System;
+using System;
 using System.IO;
 using System.Net;
 using HDL_ON.DAL.Server;
@@ -7,152 +7,38 @@
 
 namespace HDL_ON.UI.UI2.Intelligence.Automation
 {
-    public class Send
+    public  class Send
     {
         ///// <summary>
         ///// 浣忓畢ID
         ///// </summary>
         //public static string homeId = Entity.DB_ResidenceData.residenceData.CurReginID;
         ///// <summary>
-        ///// 缃戝叧ID
+        ///// 缃戝叧ID锛堢妗℃湁缃戝叧ID鍗磋繑鍥炴槸MAC锛�
         ///// </summary>
         //public static string gatewayId = DriverLayer.Control.Ins.GatewayId;
 
+        //public static string homeId = UserInfo.Current.CurReginID;
+
+        /// <summary>
+        /// 缃戝叧ID(鑾峰彇鍢変箰缃戝叧ID)
+        /// </summary>
+        public static string gatewayId
+        {
+            get
+            {
+                if (Entity.DB_ResidenceData.Instance.HomeGateway == null)
+                {
+                    return DriverLayer.Control.Ins.GatewayId;
+                }
+                return Entity.DB_ResidenceData.Instance.HomeGateway.gatewayId;
+            }
+        }
+        
         /// <summary>
         /// 浣忓畢ID
         /// </summary>
-        public static string homeId = "1333723044544233473";
-        /// <summary>
-        /// 缃戝叧ID
-        /// </summary>
-        public static string gatewayId = "1333723647223775233";
-        ///// <summary>
-        ///// 鑾峰彇閫昏緫ID鍒楄〃
-        ///// </summary>
-        ///// <returns></returns>
-        //public const string getLogicId = "鑾峰彇閫昏緫ID";
-        ///// <summary>
-        ///// 鑾峰彇閫昏緫
-        ///// </summary>
-        ///// <returns></returns>
-        //public const string getLogic= "鑾峰彇閫昏緫璇︾粏";
-        ///// <summary>
-        ///// 娣诲姞鑷姩鍖栧懡浠�
-        ///// </summary>
-        ///// <returns></returns>
-        //public const string addLogic= "娣诲姞鏂伴�昏緫";
-        ///// <summary>
-        ///// 缂栬緫鏇存柊閫昏緫
-        ///// </summary>
-        ///// <returns></returns>
-        //public const string updateLogic = "缂栬緫閫昏緫";
-        ///// <summary>
-        ///// 鍒犻櫎閫昏緫
-        ///// </summary>
-        ///// <returns></returns>
-        //public const string delLogic = "鍒犻櫎閫昏緫";
-        ///// <summary>
-        ///// 閫昏緫寮�鍏�
-        ///// </summary>
-        ///// <returns></returns>
-        //public const string switchLogic = "閫昏緫寮�鍏�";
-        //public static ResponsePackNew Logic(string type,Logic logic) {
-        //    string url = "";
-        //    var jObject = new JObject { };
-        //    switch (type) {
-        //        case getLogicId: {
-        //                jObject.Add("homeId",homeId);
-        //                url = NewAPI.API_POST_Logic_List;
-        //            }
-        //            break;
-        //        case getLogic: {
-        //                var jArray = new JArray { };
-        //                jArray.Add(logic.userLogicId);
-        //                jObject.Add("userLogicIds", jArray);
-        //                url = NewAPI.API_POST_Logic_Info;
-        //            } break;
-        //        case addLogic: {
-        //                try
-        //                {
-        //                    var cycleJObject = new JObject { };
-        //                    var cyclevaluejArray = new JArray { };
-        //                    cycleJObject.Add("type", logic.cycle.type);
-        //                    foreach (var dictionary in logic.cycle.value)
-        //                    {
-        //                        cyclevaluejArray.Add(dictionary);
-        //                    }
-        //                    cycleJObject.Add("value", cyclevaluejArray);
-
-        //                    var inputArray = new JArray { };
-        //                    foreach (var dictionary in logic.input)
-        //                    {
-        //                        var inputTypeJOb = new JObject { };
-        //                        inputTypeJOb.Add("sid", dictionary.sid);
-        //                        inputTypeJOb.Add("condition_type", dictionary.condition_type);
-
-        //                        var conditionArray = new JArray { };
-        //                        foreach (var dic in dictionary.condition)
-        //                        {
-        //                            var conditionJOb = new JObject { };
-        //                            conditionJOb.Add("key", dic["key"]);
-        //                            conditionJOb.Add("comparator", dic["comparator"]);
-        //                            conditionJOb.Add("data_type", dic["data_type"]);
-        //                            conditionJOb.Add("value", dic["value"]);
-        //                            conditionArray.Add(conditionJOb);
-        //                        }
-        //                        inputTypeJOb.Add("condition", conditionArray);
-        //                        inputArray.Add(inputTypeJOb);
-        //                    }
-
-        //                    var outputArray = new JArray { };
-        //                    foreach (var dictionary in logic.output)
-        //                    {
-        //                        var outputTypeJOb = new JObject { };
-        //                        outputTypeJOb.Add("sid", dictionary.sid);
-        //                        outputTypeJOb.Add("target_type", dictionary.target_type);
-        //                        var statusArray = new JArray { };
-        //                        foreach (var dic in dictionary.status)
-        //                        {
-        //                            var statusJOb = new JObject { };
-        //                            statusJOb.Add("key", dic["key"]);
-        //                            statusJOb.Add("value", dic["value"]);
-        //                            statusArray.Add(statusJOb);
-        //                        }
-        //                        outputTypeJOb.Add("status", statusArray);
-        //                        outputArray.Add(outputTypeJOb);
-        //                    }
-
-        //                    var logicjArray = new JArray { };
-        //                    var logicIfon = new JObject { };
-        //                    logicIfon.Add("sid", logic.sid);
-        //                    logicIfon.Add("gatewayId", gatewayId);
-        //                    logicIfon.Add("name", logic.name);
-        //                    logicIfon.Add("relation", logic.relation);
-        //                    logicIfon.Add("enable", logic.enable);
-        //                    logicIfon.Add("cycle", cycleJObject);
-        //                    logicIfon.Add("input", inputArray);
-        //                    logicIfon.Add("output", outputArray);
-        //                    logicjArray.Add(logicIfon);
-        //                    var jObject = new JObject { { "homeId", homeId }, { "logics", logicjArray } };
-        //                    responsePackNew = RequestServerhomeId(jObject, NewAPI.API_POST_Logic_Add);
-
-        //                }
-        //                catch (Exception e)
-        //                {
-        //                    var dd = e.Message;
-        //                }
-        //            } break;
-        //        case updateLogic: {
-        //            } break;
-        //        case delLogic: { } break;
-        //        case switchLogic: { } break;
-
-
-        //    }
-
-        //    return RequestServerhomeId(jObject, NewAPI.API_POST_Logic_Add); 
-        //}
-
+        public static string homeId = Entity.DB_ResidenceData.Instance.CurrentRegion.RegionID;
 
 
         /// <summary>
@@ -163,18 +49,34 @@
         {
             var jObject = new JObject { { "homeId", homeId } };
             var responsePackNew = RequestServerhomeId(jObject, NewAPI.API_POST_Logic_List);
+            //濡傛灉鏄痶oken杩囨湡鍒欏埛鏂皌oken
+            if (responsePackNew.Code == StateCode.TOKEN_EXPIRED)
+            {
+                RefreshToken();
+                getLogicIdList();
+            }
             return responsePackNew;
         }
         /// <summary>
         /// 鑾峰彇閫昏緫
         /// </summary>
+        /// <param name="listIdList">閫昏緫ID鍒楄〃</param>
         /// <returns></returns>
-        public static ResponsePackNew getLogic(Logic logic)
+        public static ResponsePackNew getLogic(List<string> listIdList)
         {
             var jArray = new JArray { };
-            jArray.Add(logic.userLogicId);
+            for (int i = 0; i < listIdList.Count; i++)
+            {
+                jArray.Add(listIdList[i]);
+            }
             var jObject = new JObject { { "userLogicIds", jArray } };
             var responsePackNew = RequestServerhomeId(jObject, NewAPI.API_POST_Logic_Info);
+            //濡傛灉鏄痶oken杩囨湡鍒欏埛鏂皌oken
+            if (responsePackNew.Code == StateCode.TOKEN_EXPIRED)
+            {
+                RefreshToken();
+                getLogic(listIdList);
+            }
             return responsePackNew;
         }
         /// <summary>
@@ -248,6 +150,12 @@
                 logicjArray.Add(logicIfon);
                 var jObject = new JObject { { "homeId", homeId }, { "logics", logicjArray } };
                 responsePackNew = RequestServerhomeId(jObject, NewAPI.API_POST_Logic_Add);
+                //濡傛灉鏄痶oken杩囨湡鍒欏埛鏂皌oken
+                if (responsePackNew.Code == StateCode.TOKEN_EXPIRED)
+                {
+                    RefreshToken();
+                    AddLogic(logic);
+                }
 
             }
             catch (Exception e)
@@ -330,7 +238,12 @@
                 logicjArray.Add(logicIfon);
                 var jObject = new JObject { { "homeId", homeId }, { "logics", logicjArray } };
                 responsePackNew = RequestServerhomeId(jObject, NewAPI.API_POST_Logic_Update);
-
+                //濡傛灉鏄痶oken杩囨湡鍒欏埛鏂皌oken
+                if (responsePackNew.Code == StateCode.TOKEN_EXPIRED)
+                {
+                    RefreshToken();
+                    updateLogic(logic);
+                }
             }
             catch (Exception e)
             {
@@ -350,6 +263,12 @@
             jArray.Add(logic.userLogicId);
             var jObject = new JObject { { "userLogicIds", jArray } };
             var responsePackNew = RequestServerhomeId(jObject, NewAPI.API_POST_Logic_Delete);
+            //濡傛灉鏄痶oken杩囨湡鍒欏埛鏂皌oken
+            if (responsePackNew.Code == StateCode.TOKEN_EXPIRED)
+            {
+                RefreshToken();
+                delLogic(logic);
+            }
             return responsePackNew;
         }
         /// <summary>
@@ -363,19 +282,27 @@
             jArray.Add(job);
             var jObject = new JObject { { "logics", jArray } };
             var responsePackNew = RequestServerhomeId(jObject, NewAPI.API_POST_Logic_Enable);
+            //濡傛灉鏄痶oken杩囨湡鍒欏埛鏂皌oken
+            if (responsePackNew.Code == StateCode.TOKEN_EXPIRED)
+            {
+                RefreshToken();
+                switchLogic(logic);
+            }
             return responsePackNew;
         }
         /// <summary>
         ///璇锋眰鏈嶅姟鍣紙涓庝綇瀹呮湁鍏�:渚嬪锛沨omeId锛�
         /// </summary>
         /// <returns></returns>
-        public static ResponsePackNew RequestServerhomeId(object o, string api_Url, int mTimeout = 10)
+        public static ResponsePackNew RequestServerhomeId(object o, string api_Url, int mTimeout = 3)
         {
+
+
+
             var requestJson = HttpUtil.GetSignRequestJson(o);
             return HttpUtil.RequestHttpsPostFroHome(api_Url, requestJson, mTimeout);
 
         }
-
         /// <summary>
         /// 璇锋眰鏈嶅姟鍣�
         /// </summary>
@@ -386,6 +313,13 @@
             return HttpUtil.RequestHttpsPost(api_Url, requestJson);
 
         }
-      
+        /// <summary>
+        /// 鍒锋柊Token
+        /// </summary>
+        public static void RefreshToken()
+        {
+            IMessageCommon.Current.StartRefreshToken();
+        }
+
     }
 }

--
Gitblit v1.8.0