using System;
|
using System.Collections.Generic;
|
using Newtonsoft.Json.Linq;
|
|
namespace HDL_ON.Entity
|
{
|
public class Light : Function
|
{
|
/*
|
灯光类:trait: [switch,brightness,color,cct,delay,fadeTime]
|
属性 描述
|
switch on/off;
|
brightness 0-100;
|
color int (red (0-255) green (0-255) blue (0-255))
|
cct int (warm light(0-255) cold light (0-255) )
|
delay 0-3600s
|
fadetime 0-3600s
|
*/
|
public Light()
|
{
|
}
|
/// <summary>
|
/// 亮度值
|
/// </summary>
|
[Newtonsoft.Json.JsonIgnore]
|
public int brightness
|
{
|
get
|
{
|
string b = "0";
|
dicPropert.TryGetValue("brightness", out b);
|
return Convert.ToInt32(b == "" ? "0" : b);
|
}
|
set
|
{
|
try
|
{
|
dicPropert["brightness"] = value.ToString();
|
MainPage.Log($"brightness 数据刷新{value}.");
|
}
|
catch
|
{
|
MainPage.Log("brightness 数据刷新失败.");
|
}
|
}
|
}
|
|
/// <summary>
|
/// RGB颜色
|
/// 255255255
|
/// </summary>
|
[Newtonsoft.Json.JsonIgnore]
|
public string color
|
{
|
get
|
{
|
string c = "255255255";
|
dicPropert.TryGetValue("color", out c);
|
if (c.Length != 9)
|
{
|
dicPropert["color"] = "255255255";
|
}
|
return c.Length == 9 ? c : "255255255";
|
}
|
set
|
{
|
try
|
{
|
dicPropert["color"] = value.ToString();
|
}
|
catch
|
{
|
MainPage.Log("color 数据刷新失败.");
|
}
|
}
|
}
|
[Newtonsoft.Json.JsonIgnore]
|
public byte redColor
|
{
|
get {
|
try
|
{
|
return Convert.ToByte(color.Substring(0, 3));
|
}
|
catch (Exception ex)
|
{
|
MainPage.Log($"Get red color error : {ex.Message}");
|
return 0;
|
}
|
}
|
set
|
{
|
try
|
{
|
dicPropert["color"] = dicPropert["color"].ToString().Remove(0, 3).Insert(0, value.ToString().PadLeft(3, '0'));
|
}
|
catch (Exception ex)
|
{
|
MainPage.Log($"set red color error : {ex.Message}");
|
}
|
}
|
}
|
[Newtonsoft.Json.JsonIgnore]
|
public byte greenColor
|
{
|
get
|
{
|
try
|
{
|
return Convert.ToByte(color.Substring(3, 3));
|
}
|
catch (Exception ex)
|
{
|
MainPage.Log($"Get green color error : {ex.Message}");
|
return 0;
|
}
|
}
|
set
|
{
|
try
|
{
|
dicPropert["color"] = dicPropert["color"].ToString().Remove(3, 3).Insert(3, value.ToString().PadLeft(3, '0'));
|
}
|
catch (Exception ex)
|
{
|
MainPage.Log($"set green color error : {ex.Message}");
|
}
|
}
|
}
|
[Newtonsoft.Json.JsonIgnore]
|
public byte blueColor
|
{
|
get
|
{
|
try
|
{
|
return Convert.ToByte(color.Substring(6, 3));
|
}
|
catch (Exception ex)
|
{
|
MainPage.Log($"Get blue color error : {ex.Message}");
|
return 0;
|
}
|
}
|
set
|
{
|
try
|
{
|
dicPropert["color"] = dicPropert["color"].ToString().Remove(6, 3).Insert(6, value.ToString().PadLeft(3, '0'));
|
}
|
catch (Exception ex)
|
{
|
MainPage.Log($"set blue color error : {ex.Message}");
|
}
|
}
|
}
|
|
|
/// <summary>
|
/// 拼接、获取A协议操作数据
|
/// </summary>
|
public override JObject GetSendJObject(CommandType_A command )
|
{
|
var sendJob = new JObject();
|
if (command == CommandType_A.write)
|
{
|
|
sendJob = new JObject { { "vendor_code", vendor_code }, { "Command", command.ToString() }, { "Type", "device" } };
|
JObject data = null;
|
switch (functionType)
|
{
|
case FunctionType.Relay:
|
data = new JObject { { "sid", sid }, { "switch", on_off } };
|
break;
|
case FunctionType.Dimmer:
|
data = new JObject { { "sid", sid }, { "brightness", brightness } };
|
break;
|
case FunctionType.RGB:
|
data = new JObject { { "sid", sid }, { "brightness", brightness }, { "color", color } };
|
break;
|
}
|
sendJob.Add("objects", data);
|
}
|
else if (command == CommandType_A.read)
|
{
|
sendJob = new JObject { { "vendor_code", vendor_code }, { "Command", command.ToString() }, { "Type", "device" } };
|
var data = new JObject {{ "sid", sid } };
|
sendJob.Add("objects", data);
|
}
|
return sendJob;
|
}
|
}
|
}
|