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()
{
}
[Newtonsoft.Json.JsonIgnore]
public Trait trait_brightness;
///
/// 亮度值
///
[Newtonsoft.Json.JsonIgnore]
public int brightness
{
get
{
if (trait_brightness == null)
{
trait_brightness = function.Find((obj) => obj.attri == "brightness");
//找不到属性需要声明一个,防止报错闪退
if (trait_brightness == null)
{
trait_brightness = new Trait()
{
attri = "brightness",
value = new List { "up", "down" },
max = 100,
min = 0,
};
}
trait_brightness.curValues = trait_brightness.min;
}
return Convert.ToInt32(trait_brightness.curValues);
}
set
{
try
{
if (trait_brightness == null)
{
trait_brightness = function.Find((obj) => obj.attri == "brightness");
//找不到属性需要声明一个,防止报错闪退
if (trait_brightness == null)
{
trait_brightness = new Trait()
{
attri = "brightness",
value = new List { "up", "down" },
max = 100,
min = 0,
};
}
trait_brightness.curValues = trait_brightness.min;
}
trait_brightness.curValues = value;
MainPage.Log($"brightness 数据刷新{value}.");
}
catch
{
MainPage.Log("brightness 数据刷新失败.");
}
}
}
[Newtonsoft.Json.JsonIgnore]
public Trait trait_color;
///
/// RGB颜色
/// 255255255
///
[Newtonsoft.Json.JsonIgnore]
public int color
{
get
{
if (trait_color == null)
{
trait_color = function.Find((obj) => obj.attri == "color");
//找不到属性需要声明一个,防止报错闪退
if (trait_color == null)
{
trait_color = new Trait()
{
attri = "color",
value = new List { "FFFFFF" },
max = 0xFFFFFF,
min = 0x000000,
};
}
trait_color.curValues = trait_color.min;
}
return Convert.ToInt32(trait_color.curValues);
}
set
{
try
{
trait_color.curValues = value;
}
catch
{
MainPage.Log("color 数据刷新失败.");
}
}
}
[Newtonsoft.Json.JsonIgnore]
public int redColor
{
get {
try
{
return color >> 16;
}
catch (Exception ex)
{
MainPage.Log($"Get red color error : {ex.Message}");
return 0;
}
}
set
{
try
{
var rc = value << 16;
color = rc + (color & 0xFFFF);
}
catch (Exception ex)
{
MainPage.Log($"set red color error : {ex.Message}");
}
}
}
[Newtonsoft.Json.JsonIgnore]
public int greenColor
{
get
{
try
{
return (color & 0xFFFF) >> 8;
}
catch (Exception ex)
{
MainPage.Log($"Get green color error : {ex.Message}");
return 0;
}
}
set
{
try
{
var gc = value << 8;
color = gc + (color & 0xFF00FF);
}
catch (Exception ex)
{
MainPage.Log($"set green color error : {ex.Message}");
}
}
}
[Newtonsoft.Json.JsonIgnore]
public int blueColor
{
get
{
try
{
return color & 0xFF;
}
catch (Exception ex)
{
MainPage.Log($"Get blue color error : {ex.Message}");
return 0;
}
}
set
{
try
{
color = value + (color & 0xFFFF00);
}
catch (Exception ex)
{
MainPage.Log($"set blue color error : {ex.Message}");
}
}
}
///
/// 拼接、获取A协议操作数据
///
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 = new JObject { { "sid", sid } };
sendJob.Add("objects", data);
List controlData = new List();
switch (functionType)
{
case FunctionType.Relay:
controlData.Add(new ControlData()
{
name = "on_off",
data_type = "Bool",
value = on_off
});
break;
case FunctionType.Dimmer:
controlData.Add(new ControlData()
{
name = "on_off",
data_type = "Bool",
value = on_off
});
controlData.Add(new ControlData()
{
name = "brightness",
data_type = "int",
value = brightness.ToString(),
});
break;
case FunctionType.RGB:
controlData.Add(new ControlData()
{
name = "on_off",
data_type = "Bool",
value = on_off
});
controlData.Add(new ControlData()
{
name = "brightness",
data_type = "int",
value = brightness.ToString(),
});
controlData.Add(new ControlData()
{
name = "color",
data_type = "int",
value = color.ToString(),
});
break;
}
AProtocolEntity ape = new AProtocolEntity()
{
command = command.ToString(),
vendor_code = vendor_code,
type = "device_sid",
};
ape.ControlFunction(sid, controlData);
sendJob = JObject.FromObject(ape);
}
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;
}
}
}