using System; using System.Collections.Generic; using Newtonsoft.Json.Linq; using Shared; using Shared.SimpleControl.R; namespace SmartHome { public class Send { #region 计时器的命令方法 /// /// 获取计时器列表 /// /// public static ResponsePackNew GetTimerList () { var jObject = new JObject { }; jObject.Add ("gatewayId", GatewayId); jObject.Add ("homeId", HomeId); jObject.Add ("pageSize", 1000); jObject.Add ("pageNo", 1); var responsePackNew = RequestServerhomeId (jObject, API.GetTimerList); return responsePackNew; } /// /// 添加计时器命令 /// /// /// public static ResponsePackNew AddTimer (Timer timer) { ResponsePackNew responsePackNew = null; try { var jArray = new JArray { }; foreach (var dic in timer.whichDay) { jArray.Add (dic); } var deviceDateJObject = new JObject { }; deviceDateJObject.Add ("gatewayId", GatewayId); deviceDateJObject.Add ("homeId", HomeId); var funjArray = new JArray { }; for (int i = 0; i < timer.controlData.actions.Count; i++) { var fun = timer.controlData.actions [i]; var funJObject = new JObject { }; funJObject.Add ("deviceId", fun.deviceId); funJObject.Add ("spk", fun.spk); var attributesJArray = new JArray { }; for (int i1 = 0; i1 < fun.attributes.Count; i1++) { var attributes = fun.attributes [i1]; var attributesJObject = new JObject { }; attributesJObject.Add ("key", attributes.key); attributesJObject.Add ("value", attributes.value); attributesJArray.Add (attributesJObject); } funJObject.Add ("attributes", attributesJArray); var busJObject = new JObject { }; busJObject.Add ("addresses", fun.bus.addresses); busJObject.Add ("loopId", fun.bus.loopId); funJObject.Add ("bus", busJObject); funjArray.Add (funJObject); } deviceDateJObject.Add ("actions", funjArray); var jObject = new JObject { }; jObject.Add ("gatewayId", GatewayId); jObject.Add ("homeId", HomeId); jObject.Add ("timerName", timer.timerName); jObject.Add ("room", timer.RoomName); jObject.Add ("isEnable", timer.isEnable); jObject.Add ("executeUtcTime", Method.GetUtcTime (timer.executeUtcTime)); jObject.Add ("controlData", deviceDateJObject); jObject.Add ("timerType", timer.timerType.GetHashCode()); jObject.Add ("whichDay", jArray); responsePackNew = RequestServerhomeId (jObject, API.AddTimer); } catch (Exception e) { var dd = e.Message; } return responsePackNew; } /// /// 编辑更新 /// /// /// public static ResponsePackNew UpdateTimer (Timer timer) { ResponsePackNew responsePackNew = null; try { var jArray = new JArray { }; foreach (var dic in timer.whichDay) { jArray.Add (dic); } var deviceDateJObject = new JObject { }; deviceDateJObject.Add ("gatewayId", GatewayId); deviceDateJObject.Add ("homeId", HomeId); var funjArray = new JArray { }; for (int i = 0; i < timer.controlData.actions.Count; i++) { var fun = timer.controlData.actions [i]; var funJObject = new JObject { }; funJObject.Add ("deviceId", fun.deviceId); funJObject.Add ("spk", fun.spk); var attributesJArray = new JArray { }; for (int i1 = 0; i1 < fun.attributes.Count; i1++) { var attributes = fun.attributes [i1]; var attributesJObject = new JObject { }; attributesJObject.Add ("key", attributes.key); attributesJObject.Add ("value", attributes.value); attributesJArray.Add (attributesJObject); } funJObject.Add ("attributes", attributesJArray); var busJObject = new JObject { }; busJObject.Add ("addresses", fun.bus.addresses); busJObject.Add ("loopId", fun.bus.loopId); funJObject.Add ("bus", busJObject); funjArray.Add (funJObject); } deviceDateJObject.Add ("actions", funjArray); var jObject = new JObject { }; jObject.Add ("gatewayId", GatewayId); jObject.Add ("homeId", HomeId); jObject.Add ("id", timer.id); jObject.Add ("timerName", timer.timerName); jObject.Add ("room", timer.RoomName); jObject.Add ("isEnable", timer.isEnable); jObject.Add ("executeUtcTime", Method.GetUtcTime (timer.executeUtcTime)); jObject.Add ("controlData", deviceDateJObject); jObject.Add ("timerType", timer.timerType.GetHashCode ()); jObject.Add ("whichDay", jArray); responsePackNew = RequestServerhomeId (jObject, API.EditTimer); } catch (Exception e) { var dd = e.Message; } return responsePackNew; } /// /// 删除 /// /// public static ResponsePackNew DelTimer (string id) { var jObject = new JObject { }; jObject.Add ("id", Int64.Parse (id)); jObject.Add ("gatewayId", GatewayId); jObject.Add ("homeId", HomeId); var responsePackNew = RequestServerhomeId (jObject, API.DeleteTimer); return responsePackNew; } /// /// 开关 /// /// public static ResponsePackNew SwitchTimer (Timer timer) { var jObject = new JObject { }; jObject.Add ("id", timer.id); jObject.Add ("gatewayId", GatewayId); jObject.Add ("homeId", HomeId); var responsePackNew = RequestServerhomeId (jObject, API.IsEnableTimer); return responsePackNew; } #endregion /// /// 网关ID /// public static string GatewayId { get { return UserConfig.Instance.HomeGateway.gatewayId; } } /// /// 住宅ID /// public static string HomeId { get { return UserConfig.Instance.CurrentRegion.Id; } } /// ///请求服务器(与住宅有关:例如;homeId) /// /// public static ResponsePackNew RequestServerhomeId (object o, string api_Url, int mTimeout = 3) { var requestJson = HttpUtil.GetSignRequestJson (o); return HttpUtil.RequestHttpsPostFroHome (api_Url, requestJson, mTimeout); } /// /// 请求服务器 /// /// public static ResponsePackNew RequestServer (object o, string api_Url) { var requestJson = HttpUtil.GetSignRequestJson (o); return HttpUtil.RequestHttpsPost (api_Url, requestJson); } } }