WJC
2020-01-15 c6ff92e84b6cb2815cb98065ecb0ecf95d0689c3
ZigbeeApp/Shared/Phone/Device/Logic/Send.cs
@@ -5,6 +5,8 @@
using ZigBee.Common;
using Shared.Common;
using System.Collections.Specialized;
using System.Net;
using System.IO;
namespace Shared.Phone.Device.Logic
{
@@ -477,6 +479,7 @@
                                        {
                                            ["Type"] = int.Parse(dictionary["Type"]),
                                            ["Account"] = dictionary["Account"],
                                            ["AccountName"] = dictionary["AccountName"],
                                            ["UserId"] = dictionary["UserId"],
                                        };
@@ -508,6 +511,7 @@
                        { "Relationship",logic.Relationship} ,
                         { "LogicType",logic.LogicType} ,
                        { "LogicCustomPushText",logic.LogicCustomPushText} ,
                        { "LogicIsCustomPushText",logic.LogicIsCustomPushText} ,
                        { "TimeAttribute", timeAttribute} ,
                        { "Conditions", conditions },
                        { "Actions", actions },
@@ -574,6 +578,7 @@
                date.Add("LogicName", logic.LogicName);
                date.Add("Relationship", logic.Relationship);
                date.Add("LogicCustomPushText", logic.LogicCustomPushText);
                date.Add("LogicIsCustomPushText", logic.LogicIsCustomPushText);
                mainGateWay?.Send("Logic/ReviseAttribute", jObject.ToString());
                //await System.Threading.Tasks.Task.Run(async () =>
                //{
@@ -621,6 +626,8 @@
                        logic.LogicName = Logicifon["LogicName"].ToString();
                        logic.LogicType = int.Parse(Logicifon["LogicType"].ToString());
                        logic.Relationship = int.Parse(Logicifon["Relationship"].ToString());
                        logic.LogicCustomPushText =Logicifon["LogicCustomPushText"].ToString();
                        logic.LogicIsCustomPushText = int.Parse(Logicifon["LogicIsCustomPushText"].ToString());
                        logic.TimeAttribute = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.TimeAttributeObj>(Logicifon["TimeAttribute"].ToString());
                        logic.Conditions = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(Logicifon["Conditions"].ToString());
                        logic.Accounts = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(Logicifon["Accounts"].ToString());
@@ -708,7 +715,7 @@
            });
        }
        // <summary>
        /// 通过设备找到区域(房间)名称
        /// 通过设备找到区域(房间)名称
        /// </summary>
        /// <param name="btnregionname">Btnregionname.</param>
        /// <param name="device">Device.</param>
@@ -717,7 +724,11 @@
            var room = new Common.Room();
            btnregionname.Text = room.GetRoomNameByDevice(device);
        }
        /// <summary>
        /// 找出楼层所有房间
        /// </summary>
        /// <param name="floorId">楼层ID</param>
        /// <returns></returns>
        public static List<Common.Room> GetRoomList(string floorId)
        {
            return Common.Room.Lists.FindAll((obj) => obj.FloorId == floorId);
@@ -744,8 +755,6 @@
            /////返回房间列表;
            //return list1;
        }
        /// <summary>
        /// 获取场景信息的方法
        /// </summary>
@@ -917,6 +926,7 @@
        {
            var list = new List<User>();
            var s = await ReadUserList(doorlockMac);
            //var s = await Data("读取云端门锁数据", "/DoorLock/GetDoorLockPager", "POST", doorlockMac);
            var jObject = JObject.Parse(s);
            if (jObject == null || jObject["StateCode"].ToString() != "Success")
            {
@@ -1007,48 +1017,7 @@
            lockifon.doorlockmac = residential.doorlockmac;
            return await WebClientAsync(2, lockifon.Url, lockifon);
        }
        /// <summary>
        /// 请求服务器的方法
        /// </summary>
        /// <param name="postValues">post参数</param>
        /// <param name="url"></param>
        /// <returns></returns>
        public static async System.Threading.Tasks.Task<string> WebClientAsync(int value, string url, Residential residential = null)
        {
            NameValueCollection postValues = new NameValueCollection();
            if (value == 0)
            {
                postValues.Add("RequestVersion", CommonPage.RequestVersion);
                postValues.Add("ReqDto.LoginAccessToken", Config.Instance.Token);
                postValues.Add("ReqDto.PageSetting.PageSize", Int32.MaxValue.ToString());//
            }
            else if (value == 1)
            {
                postValues.Add("RequestVersion", CommonPage.RequestVersion);
                postValues.Add("LoginAccessToken", Config.Instance.Token);
                postValues.Add("MainAccountId", residential.MainUserDistributedMark);
                postValues.Add("SharedHid", residential.Id);
            }
            else if (value == 2)
            {
                postValues.Add("RequestVersion", CommonPage.RequestVersion);
                postValues.Add("LoginAccessToken", residential.Token);
                postValues.Add("HomeId", residential.Id);
                postValues.Add("DoorLockId", residential.doorlockmac);//门锁Mac+端口
                postValues.Add("IsOtherAccountCtrl", residential.IsOtherAccountCtrl.ToString());
                postValues.Add("PageSetting.PageSize", Int32.MaxValue.ToString());
            }
            System.Net.WebClient webClient = new System.Net.WebClient();
            byte[] responseArray = webClient.UploadValues(url, postValues);
            var s = System.Text.Encoding.UTF8.GetString(responseArray);
            return s;
        }
        #region 定义解析门锁的对象
        public class Residential
        {
            /// <summary>
@@ -1142,7 +1111,6 @@
            public bool IsFreezeUser;
        }
        /// <summary>
        /// 门锁列表
        /// </summary>
@@ -1156,6 +1124,171 @@
        /// </summary>
        public static UserCenter.MemberInfoRes UserMemberInfoRes = null;
        #endregion
        #endregion
        /// <summary>
        ///  自己用
        /// </summary>
        public static void Zj(bool tag, Common.Logic logic)
        {
            new System.Threading.Thread(() =>
            {
                if (logic.LogicId != 0)
                {
                    if (tag)
                    {
                        Data("添加/更新", "/App/HomeLogicConfig", "POST");
                    }
                    else
                    {
                        Data("删除", "/App/DelHomeLogicConfig", "POST");
                    }
                    ///只改推送内容;
                    LogicControlSwitch(logic);
                }
            })
            { IsBackground = true }.Start();
        }
        #region  请求服务器方法---
        /// <summary>
        /// 请求数据的封装方法
        /// </summary>
        /// <param name="command">识别命令判断字符串</param>
        /// <param name="url">请求地址</param>
        /// <param name="method">请求方式为POST/GET</param>
        /// <param name="obj">存储发送数据的对象</param>
        /// <returns></returns>
        public static async System.Threading.Tasks.Task<string> Data(string command, string url, string method, object obj = null)
        {
            var getUrl = "";
            var jObject = new JObject();
            if (UserCenter.UserCenterResourse.UserInfo.AuthorityNo == 1)
            {
                getUrl = CommonPage.RequestHttpsHost + url;//请求地址;
                jObject.Add("IsOtherAccountCtrl", false);
                jObject.Add("LoginAccessToken", Config.Instance.Token);
            }
            else
            {
                getUrl = Config.Instance.AdminRequestBaseUrl + url;//请求地址;
                jObject.Add("IsOtherAccountCtrl", true);
                jObject.Add("LoginAccessToken", Config.Instance.AdminRequestToken);
            }
            switch (command)
            {
                case "添加/更新":
                    {
                        jObject.Add("RequestVersion", CommonPage.RequestVersion);
                        //jObject.Add("LoginAccessToken", Config.Instance.Token);
                        jObject.Add("HomeId", Config.Instance.HomeId);
                        jObject.Add("LogicID", Common.Logic.CurrentLogic.LogicId);
                        jObject.Add("PushUserIds", new JArray { Config.Instance.Guid });
                        jObject.Add("PushContent", Common.Logic.CurrentLogic.LogicCustomPushText);
                    }
                    break;
                case "删除":
                    {
                        jObject.Add("RequestVersion", CommonPage.RequestVersion);
                        //jObject.Add("LoginAccessToken", Config.Instance.Token);
                        jObject.Add("HomeId", Config.Instance.HomeId);
                        jObject.Add("LogicID", Common.Logic.CurrentLogic.LogicId);
                    }
                    break;
                case "读取云端门锁数据":
                    {
                        jObject.Add("RequestVersion", CommonPage.RequestVersion);
                        jObject.Add("HomeId", Config.Instance.HomeId);
                        jObject.Add("DoorLockId", obj.ToString());//门锁Mac+端口
                        jObject.Add("PageSetting.PageSize", Int32.MaxValue.ToString());
                    }
                    break;
            }
            return await HttpWebRequest(getUrl, jObject.ToString(), method);
        }
        /// <summary>
        ///  请求服务器的方法(支持请求方式为POST/GET)
        /// </summary>
        /// <param name="getUrl">请求的地址</param>
        /// <param name="str">请求数据</param>
        /// <param name="method">请求方式为POST/GET</param>
        /// <param name="second">超时时间</param>
        /// <returns></returns>
        public static async System.Threading.Tasks.Task<string>HttpWebRequest(string getUrl, string str, string method,int second= 3)
        {
            HttpWebRequest request = WebRequest.Create(getUrl) as HttpWebRequest; //创建请求
            request.Method = method; //请求方式为POST/GET
            request.ContentType = "application/json";
            request.Timeout = second * 1000;//超时时间
            if (method == "POST")
            {
                byte[] jsonbyte = System.Text.Encoding.UTF8.GetBytes(str);
                request.ContentLength = jsonbyte.Length;
                Stream postStream = request.GetRequestStream();
                postStream.Write(jsonbyte, 0, jsonbyte.Length);
                postStream.Close();
            }
            //发送请求并获取相应回应数据
            HttpWebResponse res;
            try
            {
                res = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                res = (HttpWebResponse)ex.Response;
            }
            StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.UTF8);
            string content = sr.ReadToEnd(); //获得响应字符串
            return content;
        }
        /// <summary>
        /// 请求服务器的方法
        /// </summary>
        /// <param name="value">区分发送命令的数据判断值</param>
        /// <param name="url">请求的地址</param>
        /// <param name="residential">装数据的对象</param>
        /// <returns></returns>
        public static async System.Threading.Tasks.Task<string> WebClientAsync(int value, string url, Residential residential = null)
        {
            NameValueCollection postValues = new NameValueCollection();
            switch (value)
            {
                case 0:
                    {
                        postValues.Add("RequestVersion", CommonPage.RequestVersion);
                        postValues.Add("ReqDto.LoginAccessToken", Config.Instance.Token);
                        postValues.Add("ReqDto.PageSetting.PageSize", Int32.MaxValue.ToString());//
                    }
                    break;
                case 1:
                    {
                        postValues.Add("RequestVersion", CommonPage.RequestVersion);
                        postValues.Add("LoginAccessToken", Config.Instance.Token);
                        postValues.Add("MainAccountId", residential.MainUserDistributedMark);
                        postValues.Add("SharedHid", residential.Id);
                    }
                    break;
                case 2:
                    {
                        postValues.Add("RequestVersion", CommonPage.RequestVersion);
                        postValues.Add("LoginAccessToken", residential.Token);
                        postValues.Add("HomeId", residential.Id);
                        postValues.Add("DoorLockId", residential.doorlockmac);//门锁Mac+端口
                        postValues.Add("IsOtherAccountCtrl", residential.IsOtherAccountCtrl.ToString());
                        postValues.Add("PageSetting.PageSize", Int32.MaxValue.ToString());
                    }
                    break;
            }
            //PrintKeysAndValues2(postValues);
            System.Net.WebClient webClient = new System.Net.WebClient();
            byte[] responseArray = webClient.UploadValues(url, postValues);
            var s = System.Text.Encoding.UTF8.GetString(responseArray);
            return s;
        }
        #endregion
        #region  存取本地文件的方法 ---暂时不用合并该方法---
        /// <summary>