using System;
|
using System.Collections.Generic;
|
using Newtonsoft.Json.Linq;
|
using Shared;
|
using Shared.SimpleControl.R;
|
|
namespace SmartHome
|
{
|
public class Send
|
{
|
#region 计时器的命令方法
|
/// <summary>
|
/// 获取计时器列表
|
/// </summary>
|
/// <returns></returns>
|
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;
|
}
|
/// <summary>
|
/// 添加计时器命令
|
/// </summary>
|
/// <param name="timer"></param>
|
/// <returns></returns>
|
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 ("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;
|
}
|
/// <summary>
|
/// 编辑更新
|
/// </summary>
|
/// <param name="timer"></param>
|
/// <returns></returns>
|
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 ("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;
|
}
|
/// <summary>
|
/// 删除
|
/// </summary>
|
/// <returns></returns>
|
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;
|
}
|
/// <summary>
|
/// 开关
|
/// </summary>
|
/// <returns></returns>
|
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
|
/// <summary>
|
/// 网关ID
|
/// </summary>
|
public static string GatewayId {
|
get {
|
return UserConfig.Instance.HomeGateway.gatewayId;
|
}
|
}
|
/// <summary>
|
/// 住宅ID
|
/// </summary>
|
public static string HomeId {
|
get {
|
return UserConfig.Instance.CurrentRegion.Id;
|
}
|
}
|
|
/// <summary>
|
///请求服务器(与住宅有关:例如;homeId)
|
/// </summary>
|
/// <returns></returns>
|
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>
|
/// <returns></returns>
|
public static ResponsePackNew RequestServer (object o, string api_Url)
|
{
|
var requestJson = HttpUtil.GetSignRequestJson (o);
|
return HttpUtil.RequestHttpsPost (api_Url, requestJson);
|
|
}
|
|
|
}
|
}
|