using System;
using System.Collections.Generic;
using HDL_ON.DriverLayer;
using Shared;
namespace HDL_ON.Entity
{
///
/// 读取服务器空间信息返回到数据包
///
public class DevcieApiPack
{
public List list = new List();
public string totalCount = "0";
public string totalPage = "0";
public string pageNo = "0";
public string pageSize = "0";
}
///
/// 功能类能满足数据使用要求,子类只是为了方便使用属性
///
public class Function
{
public Function()
{
}
#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";
///
/// 备注
///
public string name;
///
/// 设备ID
/// 云端负责生成
///
public string deviceId = "0";
///
/// 设备spk
///
public string spk = "";
///
/// 功能类别
/// 如:空调类、灯光类、窗帘类
///
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));
}
}
///
/// A协议功能的特性
/// 如:是AC功能:特性:on_off,mode,fan,temperature
/// attri
///
public List attributes = new List();
///
/// 房间ID列表
/// 该功能添加到到房间列表
///
public List roomIds = new List();
///
/// bus协议数据格式
/// 使用A协议控制时,改属性为空
///
public BusData bus;
///
/// 是否收藏
///
public bool collect = false;
///
/// 是否在线
///
public bool online = true;
///
/// 云端数据创建的时间
///
public string createTime = "";
///
/// 云端数据修改的最后时间
///
public string modifyTime = "";
///
/// 删除标记
/// 需要删除数据时,标记为:true
/// 由云端删除成功之后,返回数据再清除本地数据
///
public bool DeleteSign = false;
#endregion
///
/// 延时
///
public int delay = 0;
///
/// 最后控制的一次状态
///
[Newtonsoft.Json.JsonIgnore]
public string lastState = "";
FunctionAttributes _trait_on_off;
[Newtonsoft.Json.JsonIgnore]
public FunctionAttributes trait_on_off
{
get
{
if (_trait_on_off == null)
{
_trait_on_off = attributes.Find((obj) => obj.key == "on_off");
//找不到属性需要声明一个,防止报错闪退
if (_trait_on_off == null)
{
_trait_on_off = new FunctionAttributes()
{
key = "on_off",
value = new List { "on", "off" },
max = 1,
min = 0,
};
_trait_on_off.curValue = "on";
}
}
return _trait_on_off;
}
//set
//{
// _trait_on_off = value;
//}
}
///
/// 使用次数
///
public double usageCount = 0;
///
/// 使用频率
///
public double usageFrequency
{
get
{
return usageCount / 7;
}
}
///
/// 固定的序号
///
public int fixedSerialNumber = int.MaxValue;
public string GetBusId()
{
string busId = "";
if (bus != null)
{
busId = bus.SubnetID + "_" + bus.DeviceID + "_" + bus.LoopId;
}
return busId;
}
///
/// 获取设备添加到房间的房间名称
///
///
public string GetRoomListName()
{
string roomNameList = "";
foreach (var roomId in roomIds)
{
var findRoom = SpatialInfo.CurrentSpatial.RoomList.Find(obj => obj.roomId == roomId);
if (findRoom == null)
{
continue;
}
if (roomNameList != "")
{
roomNameList += ",";
}
roomNameList += findRoom.floorName +"-"+ findRoom.roomName;
}
if (roomNameList == "" && functionType == FunctionType.Scene)
{
roomNameList = Language.StringByID(StringId.WholeZone);
}
return roomNameList;
}
///
/// 数据存储文件名
///
[Newtonsoft.Json.JsonIgnore]
public string savePath
{
get
{
return "FunctionData_" + sid;
}
}
///
/// 保存功能数据
///
public void SaveFunctionData(bool upSevser)
{
if (upSevser)
{
var pm = new DAL.Server.HttpServerRequest();
var pack = pm.UpdataDevcieInfo(this);
if (pack.Code == DAL.Server.StateCode.SUCCESS)
{
var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
Common.FileUtlis.Files.WriteFileByBytes(savePath, ssd);
}
else
{
Utlis.ShowTip(Language.StringByID(StringId.EditFunctionInfoFail) + "\r\nCode:" + pack.Code);
}
}
else
{
var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
Common.FileUtlis.Files.WriteFileByBytes(savePath, ssd);
}
}
///
/// 转换成场景功能对象
///
///
public SceneFunction ConvertSceneFunction()
{
var sFunc = new SceneFunction();
foreach (var attr in attributes)
{
switch (attr.key)
{
case FunctionAttributeKey.OnOff:
case FunctionAttributeKey.Brightness:
case FunctionAttributeKey.Mode:
case FunctionAttributeKey.SetTemp:
case FunctionAttributeKey.FanSpeed:
case FunctionAttributeKey.Percent:
//case FunctionAttributeKey.FadeTime:
if (attr.curValue.ToString() == "{}")
{
attr.curValue = "0";
}
if (attr.key == FunctionAttributeKey.SetTemp)
{
double vv = 16;
Double.TryParse(attr.curValue.ToString(), out vv);
sFunc.status.Add(new SceneFunctionStatus() { key = attr.key, value = Convert.ToInt32(vv).ToString() });
}
else
{
sFunc.status.Add(new SceneFunctionStatus() { key = attr.key, value = attr.curValue.ToString() });
}
break;
}
}
sFunc.sid = this.sid;
return sFunc;
}
///
/// 更新时间
///
public DateTime refreshTime = DateTime.MinValue;
///
/// 获取本地控制数据
///
public AlinkFunctionStatusObj GetGatewayAlinkControlData(Dictionary commandDictionary)
{
var sendDataObj = new AlinkFunctionStatusObj();
sendDataObj.id = Control.Ins.msg_id.ToString();
sendDataObj.time_stamp = Control.Ins.Get_TimeStamp();
var acd = new AlinkControlData();
acd.sid = sid;
foreach (var dic in commandDictionary)
{
var aca = new AlinkControlAttributes();
aca.key = dic.Key;
aca.value = dic.Value;
acd.status.Add(aca);
}
sendDataObj.objects.Add(acd);
return sendDataObj;
}
///
/// 获取Api控制数据
///
///
public ApiAlinkControlActionObj GetApiControlData(Dictionary keyValues)
{
ApiAlinkControlActionObj aaao = new ApiAlinkControlActionObj();
aaao.deviceId = this.deviceId;
aaao.spk = this.spk;
aaao.bus = this.bus;
foreach (var kv in keyValues)
{
aaao.attributes.Add(new AlinkControlAttributes()
{
key = kv.Key,
value = kv.Value,
});
}
return aaao;
}
}
///
/// 远程控制
/// api a协议控制动作对象
///
public class ApiAlinkControlActionObj
{
///
/// 设备ID
///
public string deviceId = "0";
///
/// spk
/// 列:light.switch
///
public string spk = "";
public List attributes = new List();
//[Newtonsoft.Json.JsonIgnore]
public BusData bus = new BusData();
}
///
/// A协议控制数据的对象
///
public class AlinkFunctionStatusObj
{
public List objects = new List();
public string time_stamp = "";
public string id = "";
}
///
/// 本地状态读取
/// A协议状态读取格式对象
///
public class AlinkReadFunctionStatusObj
{
public string id = "0";
public List> objects = new List>();
public string time_stamp = "";
}
///
/// A协议控制数据
///
public class AlinkControlData
{
public string sid = "";
public List status = new List();
}
///
/// a协议控制动作数据
///
public class AlinkControlAttributes
{
///
/// 属性名
/// 列:on_off
///
public string key = "";
///
/// 属性值
/// 列:on
///
public string value = "";
}
///
/// 功能属性
/// 属性字段解析:attri :属性内容,value 属性的值,max 最大值 min 最小值
///
[System.Serializable]
public class FunctionAttributes
{
///
/// 属性键名
///
public string key;
///
/// 属性的值列表
///
public List value = new List();
///
/// 最大值
///
public int max = 100;
///
/// 最小值
///
public int min = 0;
///
/// 数据类型
///
public string data_type = "";
///
/// 当前值
///
public object curValue = new object();
}
///
/// 功能属性键名列表
///
public static class FunctionAttributeKey
{
///
/// 开关
///
public const string OnOff = "on_off";
///
/// 亮度
///
public const string Brightness = "brightness";
///
/// 颜色
///
public const string RGB = "rgb";
///
/// 渐变时间
///
public const string FadeTime = "fade_time";
///
/// 模式
///
public const string Mode = "mode";
///
/// 风速
///
public const string FanSpeed = "fan";
///
/// 设置温度
///
public const string SetTemp = "set_temp";
///
/// 延时
///
public const string Delay = "delay";
///
/// 色温
///
public const string CCT = "cct";
///
/// 百分比
///
public const string Percent = "percent";
///
/// 室内温度
///
public const string IndoorTemp = "room_temp";
///
/// value
///
public const string Value = "value";
}
///
/// 设备功能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 BusData
{
public string addresses = "FFFF";
[Newtonsoft.Json.JsonIgnore]
public byte SubnetID
{
get
{
return Convert.ToByte(addresses.Substring(0, 2), 16);
}
}
[Newtonsoft.Json.JsonIgnore]
public byte DeviceID
{
get
{
return Convert.ToByte(addresses.Substring(2, 2), 16);
}
}
public byte LoopId
{
get
{
return Convert.ToByte(loopId, 16);
}
}
public string loopId;
}
public static class SPK
{
///
/// (开关灯)
///
public const string LightSwitch = "light.switch";
///
/// (调光灯)
///
public const string LightDimming = "light.dimming";
///
/// (RGB灯)
///
public const string LightRGB = "light.rgb";
///
/// (RGBW灯)
///
public const string LightRGBW = "light.rgbw";
///
/// (CCT灯)
///
public const string LightCCT = "light.cct";
///
/// (开关窗帘)
///
public const string CurtainSwitch = "curtain.switch";
///
/// (开合帘)
///
public const string CurtainTrietex = "curtain.trietex";
///
/// (百叶帘)
///
public const string CurtainShades = "curtain.shades";
///
/// (卷帘)
///
public const string CurtainRoller = "curtain.roller";
///
/// (空调)
///
public const string AcStandard = "ac.standard";
///
/// (地热)
///
public const string FloorHeatStandard = "floorHeat.standard";
///
/// (新风)
///
public const string AirFreshStandard = "airFresh.standard";
///
/// (音乐)
///
public const string MusicStandard = "music.standard";
///
/// (亮度传感器)
///
public const string SensorLight = "sensor.light";
///
/// (温度传感器)
///
public const string SensorTemperature = "sensor.temperature";
///
/// (红外移动传感器)
///
public const string SensorPir = "sensor.pir";
///
/// (门窗传感器)
///
public const string SensorDoorWindow = "sensor.doorwindow";
///
/// (PM2.5传感器)
///
public const string SensorPm25 = "sensor.pm25";
///
/// co2传感器
///
public const string SensorCO2 = "sensor.co2";
///
/// tvoc传感器
///
public const string SensorTVOC = "sensor.tvoc";
///
/// 湿度传感器
///
public const string SensorHumidity = "sensor.humidity";
///
/// (干接点)
///
public const string DryContact = "dryContact.standard";
///
/// 家电、插座
///
public const string ElectricSocket = "electrical.socket";
///
/// 家电、电视
///
public const string ElectricTV = "electrical.tv";
///
/// 家电、风扇
///
public const string ElectricFan = "electrical.fan";
}
}