using System; using System.Collections.Generic; using HDL_ON.Common; using HDL_ON.DAL.Server; 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 AttrState { /// /// 属性键名 /// public string key; /// /// 属性的值列表 /// public List value = new List(); /// /// 最大值 /// public int max = 100; /// /// 最小值 /// public int min = 0; /// /// 数据类型 /// public string data_type = ""; /// /// 当前值 /// public string curValue = ""; } /// /// 功能类能满足数据使用要求,子类只是为了方便使用属性 /// public class Function { public Function() { } #region 优化区域 2021-02-25 /// /// 更新的最后时间 /// public string time_stamp = "1615261019796"; public string GeteTime() { try { DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区 return startTime.AddMilliseconds(Convert.ToDouble(time_stamp)).ToString("s"); } catch { return DateTime.MinValue.ToString("s"); } } /// /// 获取属性列表 /// /// public List GetAttributes() { var attrs = new List(); foreach(var attr in attributes) { attrs.Add(attr.key); } return attrs; } /// /// 获取指定属性 /// /// public FunctionAttributes GetAttribute(string key) { var attr = attributes.Find((a) => a.key == key); return attr; } /// /// 获取属性当前状态 /// /// /// public string GetAttrState(string key) { var attrState = attributes.Find((s) => s.key == key.ToString()); if (attrState == null || string.IsNullOrEmpty(attrState.state)) { return "0"; } return attrState.state; } /// /// 设置属性状态 /// /// /// /// 修改结果 public bool SetAttrState(string key, object value) { //var attrState = status.Find((s) => s.key == key.ToString()); var attr = attributes.Find((s) => s.key == key); if (attr == null) { return false; } else { attr.state = value.ToString(); attr.curValue = value.ToString(); } return true; } /// /// icon文件名称 /// public string IconName { get { return spk.Replace(".",""); } } /// /// 版本数据列表 /// public List versions = new List(); /// /// 上一次打开的亮度 /// 灯光私有属性 /// [Newtonsoft.Json.JsonIgnore] public int lastBrightness = 100; /// /// 工作模式对应的工作温度 /// 地热私有属性 /// [Newtonsoft.Json.JsonIgnore] public Dictionary Fh_Mode_Temp = new Dictionary(); #endregion #region base info public string sid = "0301011234567801012301230123"; /// /// 备注 /// public string name; /// /// 设备ID /// 云端负责生成 /// public string deviceId = "0"; /// /// 设备spk /// public string spk = ""; string spk_Prefix; /// /// spk前缀 /// 大类 /// 功能类别 /// 如:空调类、灯光类、窗帘类 /// public string Spk_Prefix { get { if (string.IsNullOrEmpty(spk_Prefix)) { spk_Prefix = spk.Split(".")[0]; } return spk_Prefix; } } /// /// 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 = ""; #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 == FunctionAttributeKey.OnOff); //找不到属性需要声明一个,防止报错闪退 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 = "off"; } } if(_trait_on_off.curValue.ToString() != "on"&& _trait_on_off.curValue.ToString() != "off" && _trait_on_off.curValue.ToString() != "stop") { _trait_on_off.curValue = "off"; } return _trait_on_off; } } /// /// 使用次数 /// public double controlCounter = 0; /// /// 使用频率 /// public double usageFrequency { get { return controlCounter / 7; } } /// /// 固定的序号 /// public int fixedSerialNumber = int.MaxValue; /// /// 收藏功能 /// public void CollectFunction() { var result = ""; new System.Threading.Thread(() => { if (collect) { result = ApiUtlis.Ins.HttpRequest.CollectDevice(deviceId).Code; } else { result = ApiUtlis.Ins.HttpRequest.CancelCollectDevice(deviceId).Code; } //提示错误 if (result != StateCode.SUCCESS) { Application.RunOnMainThread(() => { collect = !collect; IMessageCommon.Current.ShowErrorInfoAlter(result); }); }else { Application.RunOnMainThread(() => { UI.HomePage.RefreshFunctionView(); }); } }) { IsBackground = true }.Start(); } /// /// 编辑功能绑定房间信息 /// public void EditBindRoomInfo(string roomId) { var result = ""; new System.Threading.Thread(() => { if (collect) { result = ApiUtlis.Ins.HttpRequest.BindDeviceToRoom(new List() { deviceId }, new List() { roomId }).Code; } else { result = ApiUtlis.Ins.HttpRequest.UnbindDeviceToRoom(deviceId,roomId); } //提示错误 if (result != StateCode.SUCCESS) { Application.RunOnMainThread(() => { collect = !collect; IMessageCommon.Current.ShowErrorInfoAlter(result); }); } }) { IsBackground = true }.Start(); } 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 == "" ) { roomNameList = Language.StringByID(StringId.WholeZone); } return roomNameList; } /// /// 数据存储文件名 /// [Newtonsoft.Json.JsonIgnore] public string savePath { get { return "FunctionData_" + sid; } } /// /// 保存功能文件 /// public void SaveFunctionFile() { var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this)); FileUtlis.Files.WriteFileByBytes(savePath, ssd); } /// /// 保存房间绑定信息 /// public void UpdataRoomIds() { new System.Threading.Thread(() => { var pack = ApiUtlis.Ins.HttpRequest.UpdataDevcieBindRoomInfo(this); //直接保存本地, SaveFunctionFile(); if (pack.Code == StateCode.SUCCESS) { } else { IMessageCommon.Current.ShowErrorInfoAlter(pack.Code); } }) { IsBackground = true }.Start(); } /// /// 更新功能信息 /// public void UpdataFuncitonInfo() { new System.Threading.Thread(() => { var pm = new HttpServerRequest(); var pack = pm.UpdataDevcieInfo(this); SaveFunctionFile(); if (pack.Code == StateCode.SUCCESS) { } else { IMessageCommon.Current.ShowErrorInfoAlter(pack.Code); } }) { IsBackground = true }.Start(); } /// /// 更新功能名称 /// public void UpdataFunctionName() { new System.Threading.Thread(() => { var pm = new HttpServerRequest(); var pack = pm.UpdataDevcieInfo(this); SaveFunctionFile(); if (pack.Code == StateCode.SUCCESS) { } else { IMessageCommon.Current.ShowErrorInfoAlter(pack.Code); } }) { IsBackground = true }.Start(); } /// /// 转换成场景功能对象 /// /// 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() == "{}") { if (attr.key == FunctionAttributeKey.OnOff) { attr.curValue = "off"; } else { 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 = Utlis.GetTimestamp(); var acd = new AlinkStatusData(); acd.sid = sid; foreach (var dic in commandDictionary) { var aca = new AttributesStatus(); 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 AttributesStatus() { 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 AlinkStatusData { /* mqtt接收的状态数据没有deviceId 接口反馈的数据有 */ /// /// /// public string sid = ""; /// /// 设备ID /// 云端负责生成 /// public string deviceId = "0"; public List status = new List(); } /// /// a协议控制动作数据 /// public class AttributesStatus { /// /// 属性名 /// 列: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 string state = ""; /// /// 当前值 /// public object curValue = new object(); } /// /// 功能属性键名列表 /// public static class FunctionAttributeKey { public const string _null = ""; /// /// 开关 /// 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 WorkMode = "mode_work"; /// /// 风速 /// public const string FanSpeed = "fan"; /// /// 手动风向 /// public const string FanManual = "fan_manual"; /// /// 自动风向 /// public const string FanAuto = "fan_auto"; /// /// 设置温度 /// public const string SetTemp = "set_temp"; /// /// 温度加减 /// public const string SetTempStep = "set_temp_step"; /// /// 温度模式 /// 空调、地热//app自加 /// public const string TempType = "temperature_type"; /// /// 时间标记 /// 地热//app自加 /// public const string TimeFlag = "time_flag"; /// /// 延时 /// public const string Delay = "delay"; /// /// 色温 /// public const string CCT = "cct"; /// /// 百分比 /// public const string Percent = "percent"; /// /// 室内温度 /// public const string RoomTemp = "room_temp"; /// /// value /// public const string Value = "value"; /// /// 状态,传感器 /// public const string Status = "status"; /// /// 电量状态 /// public const string BatteryState = "battery_state"; /// /// 烘干 /// public const string HotDry = "hot_dry"; /// /// 风干 /// public const string WindDry = "wind_dry"; /// /// 消毒 /// public const string Disinfect = "disinfect"; /// /// 上升下降 /// public const string Position = "position"; /// /// 进度 /// public const string PositionPercent = "position_percent"; /// /// 烘干时间 /// public const string HotDryTime = "hot_dry_time"; /// /// 烘干剩余时间 /// public const string HotDryTimeLeft = "hot_dry_time_surplus"; /// /// 风干时间 /// public const string WindDryTime = "wind_dry_time"; /// /// 风干剩余时间 /// public const string WindDryTimeLeft = "wind_dry_time_surplus"; /// /// 消毒时间 /// public const string DisinfectTime = "disinfect_time"; /// /// 消毒剩余时间 /// public const string DisinfectTimeLeft = "disinfect_time_surplus"; /// /// 负离子 /// public const string Anion = "anion"; /// /// 负离子时间 /// public const string AnionTime = "anion_time"; /// /// 负离子剩余时间 /// public const string AnionTimeLeft = "anion_time_surplus"; /// /// 打开等级(风扇) /// public const string OpenLevel = "openLevel"; /// /// 红外按键 /// public const string Key = "key"; /// /// 节能 /// public const string Energy = "energy"; /// /// 湿度 /// public const string Humidity = "humidity"; /// /// 室内温度 /// public const string IndoorTemp = "indoor_temp"; /// /// 室内湿度 /// public const string IndoorHumidity = "indoor_humidity"; /// /// 过滤网剩余量 /// public const string FilterRemain = "filter_remain"; /// /// 过滤网是否超时警告 /// public const string FilterTimeout = "filter_timeout"; #region tuya /// /// 涂鸦水阀开启时间 /// public const string TuyaWaterTime = "countdown_1"; #endregion } /// /// 设备功能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 { try { return Convert.ToByte(loopId, 16); }catch { return 0; } } } public string loopId; } public static class SPK { #region 灯光 /// /// (开关灯) /// 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"; /// /// 灯光spk列表 /// /// public static List LightSpkList() { var spkList = new List(); spkList.Add(LightCCT); spkList.Add(LightDimming); spkList.Add(LightRGB); spkList.Add(LightRGBW); spkList.Add(LightSwitch); return spkList; } #endregion #region 窗帘 /// /// (开关窗帘) /// public const string CurtainSwitch = "curtain.switch"; /// /// (开合帘) /// public const string CurtainTrietex = "curtain.trietex"; /// /// (百叶帘) /// public const string CurtainShades = "curtain.shades"; /// /// (卷帘) /// public const string CurtainRoller = "curtain.roller"; /// /// 窗帘spk列表 /// /// public static List CurtainSpkList() { var spkList = new List(); spkList.Add(CurtainRoller); spkList.Add(CurtainSwitch); spkList.Add(CurtainShades); spkList.Add(CurtainTrietex); return spkList; } #endregion #region 空调 /// /// (空调) /// public const string AcStandard = "ac.standard"; /// /// (红外空调) /// public const string AcIr= "ir.ac"; /// /// 空调spk列表 /// /// public static List AcSpkList() { var spkList = new List(); spkList.Add(AcStandard); spkList.Add(AcIr); return spkList; } #endregion #region 地热 /// /// (地热) /// public const string FloorHeatStandard = "floorHeat.standard"; /// /// 地热spk列表 /// /// public static List FhSpkList() { var spkList = new List(); spkList.Add(FloorHeatStandard); return spkList; } #endregion #region 新风 /// /// (新风) /// public const string AirFreshStandard = "airFresh.standard"; /// /// 新风 ——金茂定制 /// public const string AirFreshJinmao = "airFresh.jinmao"; /// /// 新风spk列表 /// /// public static List AirFreshSpkList() { var spkList = new List(); spkList.Add(AirFreshStandard); spkList.Add(AirFreshJinmao); return spkList; } #endregion #region 能源 /// /// 能源模块 /// public const string EnergyStandard = "energy.standard"; /// /// 能源spk列表 /// /// public static List EnergySpkList() { var spkList = new List(); spkList.Add(EnergyStandard); return spkList; } #endregion #region 音乐 /// /// (音乐) /// public const string MusicStandard = "music.standard"; /// /// 音乐spk列表 /// /// public static List MusicSpkList() { var spkList = new List(); spkList.Add(MusicStandard); return spkList; } #endregion #region 传感器 #region 安防传感器 /// /// (亮度传感器) /// public const string SensorLight = "sensor.light"; /// /// (红外移动传感器) /// public const string SensorPir = "sensor.pir"; /// /// (门窗传感器) /// public const string SensorDoorWindow = "sensor.doorwindow"; /// /// 烟雾传感器 /// public const string SensorSmoke = "sensor.smoke"; /// /// 水浸传感器 /// public const string SensorWater = "sensor.water"; /// /// 燃气传感器 /// public const string SensorGas = "sensor.gas"; /// /// 红外珊栏传感器 /// public const string SensorShanLan = "sensor.shanlan"; /// /// 红外对射传感器 /// public const string SensorDuiShe = "sensor.duishe"; /// /// 超声波传感器 /// public const string SensoruUtrasonic = "sensor.ultrasonic"; /// /// (干接点) /// public const string SensorDryContact = "sensor.dryContact"; /// /// 安防传感器spk列表 /// /// public static List ArmSensorSpkList() { var spkList = new List(); spkList.Add(SensorLight); spkList.Add(SensorPir); spkList.Add(SensorDoorWindow); spkList.Add(SensorSmoke); spkList.Add(SensorWater); spkList.Add(SensorGas); spkList.Add(SensorShanLan); spkList.Add(SensorDuiShe); spkList.Add(SensoruUtrasonic); spkList.Add(SensorDryContact); return spkList; } #endregion #region 环境传感器 /// /// (温度传感器) /// public const string SensorTemperature = "sensor.temperature"; /// /// (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"; /// /// 环境传感器spk列表 /// /// public static List EnvironSpkList() { var spkList = new List(); spkList.Add(SensorTemperature); spkList.Add(SensorPm25); spkList.Add(SensorCO2); spkList.Add(SensorTVOC); spkList.Add(SensorHumidity); return spkList; } #endregion #endregion #region 家电 /// /// 家电、插座 /// public const string ElectricSocket = "electrical.socket"; /// /// 家电、电视 /// public const string ElectricTV = "electrical.tv"; /// /// 红外、电视 /// public const string TvIr = "ir.tv"; /// /// 家电、风扇 /// public const string ElectricFan = "electrical.fan"; /// /// 凉霸 /// public const string ClothesHanger = "electrical.racks"; /// /// 红外遥控器 /// public const string IrLearn = "ir.learn"; #region 涂鸦 /// /// 家电、涂鸦空气净化器 /// public const string ElectricTuyaAirCleaner = "electrical.Q1RsefNf91tIXyyQ"; /// /// 家电、涂鸦电风扇 /// public const string ElectricTuyaFan = "electrical.tyqborgovyzytytz"; /// /// 家电、涂鸦扫地机器人 /// public const string ElectricTuyaWeepRobot = "electrical.ai6HtccKFIw3dxo3"; /// /// 家电、涂鸦水阀 /// public const string ElectricTuyaWaterValve = "electrical.koiGhMKwLf2ZP81g"; #endregion /// /// 家电spk列表 /// /// public static List ElectricalSpkList() { var spkList = new List(); spkList.Add(ElectricSocket); spkList.Add(ElectricTV); spkList.Add(TvIr); spkList.Add(ElectricFan); spkList.Add(ClothesHanger); spkList.Add(IrLearn); spkList.Add(ElectricTuyaAirCleaner); spkList.Add(ElectricTuyaFan); spkList.Add(ElectricTuyaWeepRobot); spkList.Add(ElectricTuyaWaterValve); return spkList; } #endregion #region 设备类 /// /// 红外宝 /// public const string IrModule = "ir.module"; /// /// 设备类spk列表 /// /// public static List DeviceSpkList() { var spkList = new List(); spkList.Add(IrModule); return spkList; } #endregion /// /// 获取第三方设备spk /// /// public static List Get3tySpk(BrandType brandType) { var list = new List(); switch (brandType) { case BrandType.All3tyBrand: case BrandType.Tuya: list.Add(ElectricTuyaAirCleaner); list.Add(ElectricTuyaFan); list.Add(ElectricTuyaWaterValve); list.Add(ElectricTuyaWeepRobot); break; } return list; } /// /// 第三方品牌列表 /// public enum BrandType { All = 0, Hdl = 1, Tuya = 2, All3tyBrand = 999, } } /// /// 功能类别 /// public static class FunctionCategory { /// ///(灯) /// public const string Light = "light"; /// /// (窗帘) /// public const string Curtain = "curtain"; /// /// (空调) /// public const string AC = "ac"; /// /// (地热) /// public const string FloorHeat = "floorHeat"; /// /// (新风) /// public const string AirFresh = "airFresh"; /// /// (音乐) /// public const string Music = "music"; /// /// (传感器) /// public const string Sensor = "sensor"; /// /// (干接点) /// public const string DryContact = "dryContact"; /// /// 家电 /// public const string Electric = "electrical"; /// /// 红外设备 /// public const string IR = "ir"; } public class VersionInfo { public string module = ""; public string version = ""; } }