using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;
using HDL_ON;
using Shared;
namespace HDL_ON.Entity
{
///
/// 功能属性
/// 属性字段解析:attri :属性内容,value 属性的值,max 最大值 min 最小值
///
public class Trait
{
///
/// 属性名称
///
public string name;
List _value_key;
///
/// 属性的值列表
///
[Newtonsoft.Json.JsonIgnore]
public List value_key {
get
{
return _value_key;
}
set
{
_value_key = value;
}
}
///
/// 最大值
///
public int max;
///
/// 最小值
///
public int min;
///
/// 数据类型
///
public string data_type = "";
///
/// 当前值
///
public object value = new object();
//----app自定义
///
/// 属性值单位
///
string _uintString;
[Newtonsoft.Json.JsonIgnore]
public string uintString
{
get
{
if(!string.IsNullOrEmpty( _uintString))
{
return _uintString;
}
var us = "";
switch(name)
{
case "temperature":
us = "°C";
break;
case "percent":
case "brightness":
us = "%";
break;
}
return us;
}
}
///
/// 属性名称显示文本
/// 中英文显示
///
[Newtonsoft.Json.JsonIgnore]
public string name_text
{
get
{
string text = "";
switch (name)
{
case "on_off":
text = Language.StringByID(StringId.OnOff);
break;
case "brightness":
text = Language.StringByID(StringId.Brightness);
break;
case "color":
text = Language.StringByID(StringId.ColorValue);
break;
case "mode":
text = Language.StringByID(StringId.Mode);
break;
case "fan":
text = Language.StringByID(StringId.FanSpeed);
break;
case "temperature":
text = Language.StringByID(StringId.Temp);
break;
case "delay":
text = Language.StringByID(StringId.Delay);
break;
case "cct":
text = "CCT";
break;
//case "fade_time":
case "percent":
text = Language.StringByID(StringId.PercentAdjustment);
break;
//case "lock":
//case "ico":
//case "swing":
//case "set_ point":
//case "pm25":
//case "volume":
//case "vol_step":
//case "source":
//case "treble":
//case "bass":
//case "playlist":
//case "song_name":
//case "current_status":
//case "enable":
//case "lux":
//case "adjust_value":
//case "range":
//case "humidity":
//case "type":
//case "state":
//case "sensitivity":
//case "pm25value":
}
return text;
}
}
///
/// 获取当前属性的显示文本
/// 中英文显示
///
public string GetCurValueText()
{
return GetValueText(value.ToString());
}
///
/// 获取指定属性的显示文本
/// 中英文显示
///
public string GetValueText(string value)
{
string text = "";
value = value.Replace("{}", "");
switch (name)
{
case "on_off":
text = value == "on" ? Language.StringByID(StringId.On) : Language.StringByID(StringId.OFF);
break;
case "temperature":
case "brightness":
case "percent":
if (value == "")
{
this.value = this.min;
text = this.min.ToString();
}
else
{
text = value;
}
break;
//case "color":
case "mode":
switch (value)
{
//----空调
case "auto":
text = Language.StringByID(StringId.Auto);
break;
case "cool":
text = Language.StringByID(StringId.Cool);
break;
case "heat":
text = Language.StringByID(StringId.Heat);
break;
case "dry":
text = Language.StringByID(StringId.Dry);
break;
case "fan":
text = Language.StringByID(StringId.AirSupply);
break;
//-----地热
case "day":
text = Language.StringByID(StringId.Day);
break;
case "night":
text = Language.StringByID(StringId.Night);
break;
case "away":
text = Language.StringByID(StringId.Away);
break;
case "normal":
text = Language.StringByID(StringId.Normal);
break;
case "timer":
text = Language.StringByID(StringId.Timer);
break;
default:
if (this.value_key.Contains("cool"))
{
this.value = "cool";
text = Language.StringByID(StringId.Cool);
}
else if (this.value_key.Contains("day"))
{
this.value = "day";
text = Language.StringByID(StringId.Day);
}
else
{
foreach (var v in this.value_key)
{
this.value = v;
text = GetCurValueText();
}
}
break;
}
break;
case "fan":
switch (value)
{
case "high":
text = Language.StringByID(StringId.HighWindSpeed);
break;
case "medium":
text = Language.StringByID(StringId.MiddleWindSpeed);
break;
case "low":
text = Language.StringByID(StringId.LowWindSpeed);
break;
case "auto":
text = Language.StringByID(StringId.Auto);
break;
default:
if (this.value_key.Contains("low"))
{
this.value = "low";
text = Language.StringByID(StringId.LowWindSpeed);
}
else
{
foreach (var v in this.value_key)
{
this.value = v;
text = GetCurValueText();
}
}
break;
}
break;
//case "delay":
//case "cct":
//case "fade_time":
//case "lock":
//case "ico":
//case "swing":
//case "set_ point":
//case "pm25":
//case "volume":
//case "vol_step":
//case "source":
//case "treble":
//case "bass":
//case "playlist":
//case "song_name":
//case "current_status":
//case "enable":
//case "lux":
//case "adjust_value":
//case "range":
//case "humidity":
//case "type":
//case "state":
//case "sensitivity":
//case "pm25value":
case "high":
text = Language.StringByID(StringId.HighWindSpeed);
break;
case "medium":
text = Language.StringByID(StringId.MiddleWindSpeed);
break;
case "low":
text = Language.StringByID(StringId.LowWindSpeed);
break;
case "auto":
text = Language.StringByID(StringId.Auto);
break;
}
return text;
}
}
///
/// 设备功能oid
///
public class FunctionOid
{
public string oid;
public string name;
public string machine_id;
public string net_id;
public string dev_id;
public string channels;
}
public class Function
{
public Function()
{
}
///
/// 数据存储文件名
///
[Newtonsoft.Json.JsonIgnore]
public string savePath
{
get
{
return "FunctionData_" + sid;
}
}
#region base info
///
/// HDL统一协议格式:14bytes
/// 举例: 来源 厂商代码 通讯方式 产品时间戳 产品类别 物模型类 通道号 大小类别
// 1byte 1byte 1byte 4byte 1byte 2byte 2byte 2byte
/// 来源:00 默认原生态系统数据 、01 网关或者其他A设备、02 调试软件、03 APP应用程序、04 第三方网关或者平台
/// 厂商代码:01 HDL
/// 通讯方式:01 HDL Bus、02 Zigbee、03 KNX、04 Z-Wave
/// 产品时间戳:4bytes 以2020年1月1日算出的时间戳0.1s为单位
/// 产品类别:01 调光器、02 继电器、03 干接点模块、04 传感器、05 面板
/// 物模型类型:
/// 01 开关类:01 开关、02 插座、03
/// 02 照明: 01 开关、02 调光、03 色温、04 LED
/// 03 遮阳: 01 窗帘电机、02 百叶窗、03 开合帘、04 卷帘
/// 04 恒温器:01 空调、02 地暖、03 毛细空调
/// 05 新风
/// 06 影音
/// 07 音乐
/// 08 能源
/// 09 安防
/// 大类别 1bytes (预留)
/// 小类别 1byte (预留)
///
public string sid = "0301011234567801012301230123";
///
/// A协议功能的特性
/// 如:是AC功能:特性:on_off,mode,fan,temperature
/// attri
///
public List function = new List();
///
/// 房间ID列表
/// 该功能添加到到房间列表
///
public List roomIdList = new List();
///
/// bus协议数据格式
/// 使用A协议控制时,改属性为空
///
public BusData bus_Data;
///
/// 延时
///
public int delay = 0;
///
/// 延时显示的文本
///
[Newtonsoft.Json.JsonIgnore]
public string delayText
{
get
{
string text = "";
switch (delay)
{
case 0:
text = Language.StringByID(StringId.NoDelay);
break;
case 30:
text = "30s";
break;
case 60:
text = "1min";
break;
case 120:
text = "2min";
break;
case 300:
text = "5min";
break;
}
return text;
}
}
#endregion
///
/// 功能类别
/// 如:空调类、灯光类、窗帘类
///
public FunctionCategory functionCategory
{
get
{
try
{
var _functionCategoryString = sid.Substring(16, 2);
var _functionCategory = Convert.ToInt32(_functionCategoryString, 16);
return (FunctionCategory)Enum.ToObject(typeof(FunctionCategory), _functionCategory);
}
catch (Exception ex)
{
MainPage.Log($"get FunctionCategory error : {ex.Message}");
return FunctionCategory.UnKown;
}
}
}
public FunctionType functionType
{
get
{
var _functionTypeString = sid.Substring(16, 4);
return (FunctionType)Enum.ToObject(typeof(FunctionType), Convert.ToInt32(_functionTypeString, 16));
}
}
///
/// 备注
///
public string name;
///
/// 最后控制的一次状态
///
[Newtonsoft.Json.JsonIgnore]
public string lastState = "";
Trait _trait_on_off;
[Newtonsoft.Json.JsonIgnore]
public Trait trait_on_off
{
get
{
if (_trait_on_off == null)
{
_trait_on_off = function.Find((obj) => obj.name == "on_off");
//找不到属性需要声明一个,防止报错闪退
if (_trait_on_off == null)
{
_trait_on_off = new Trait()
{
name = "on_off",
value_key = new List { "on", "off" },
max = 1,
min = 0,
};
}
_trait_on_off.value = "on";
}
return _trait_on_off;
}
//set
//{
// _trait_on_off = value;
//}
}
///
/// 是否收藏
///
public bool collection = false;
///
/// 使用次数
///
public double usageCount = 0;
///
/// 使用频率
///
public double usageFrequency {
get
{
return usageCount / 7;
}
}
///
/// 固定的序号
///
public int fixedSerialNumber = int.MaxValue;
public string GetBusId ()
{
string busId = "";
if (bus_Data != null)
{
busId = bus_Data.SubnetID + "_" + bus_Data.DeviceID + "_" + bus_Data.LoopID;
}
return busId;
}
///
/// 获取设备添加到房间的房间名称
///
///
public string GetRoomListName()
{
string roomNameList = "";
foreach(var roomId in roomIdList)
{
var findRoom = DB_ResidenceData.rooms.Find(obj => obj.sid == roomId);
if (findRoom == null)
continue;
if(roomNameList != "")
{
roomNameList += ",";
}
roomNameList += findRoom.floorName + findRoom.name;
}
if (roomNameList == "" && functionType == FunctionType.Scene)
{
roomNameList = Language.StringByID(StringId.WholeHouseScene);
}
return roomNameList;
}
///
/// 保存功能数据
///
public void SaveFunctionData()
{
var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
FileUtils.WriteFileByBytes("FunctionData_" + this.sid, ssd);
MainPage.Log($"Save FunctionData {this.functionType} : {this.sid}");
}
///
/// 更新时间
///
public DateTime refreshTime = DateTime.MinValue;
}
}