using System; using System.Collections.Generic; using Shared; using Newtonsoft; namespace SuperGateWay { [System.Serializable] public class SuperCommon { public byte SubnetID { get { return (byte)((uid >> 8) & 0xFF); } } public byte DeviceID { get { return (byte)((uid >> 0) & 0xFF); } } /// /// 高级网关 server /// 场景 scene /// 逻辑 logic /// 空调 air_conditioner /// 窗帘 curtain /// 开关 relay /// 调光 dimmer /// 面板 pannel /// 传感器 sensor /// 音乐 music /// 混合 multi /// public string module; public string name; /// /// 文件类型 4bit 驱动类型12bit 设备识别号 16bit /// public uint uid; public List> objects = new List> { }; public virtual string FileName { get { return $"SuperGateWay_{typeof (SuperCommon).Name}_{uid.ToString ("X")}"; } } public static uint MakeDeviceNumber (byte subnetId, byte deviceId) { return (((uint)subnetId) << 8) | deviceId; } /// /// 生成Uid /// /// 反馈生成的UID /// 文件类型 /// 驱动类型 /// 设备识别号 public static uint MakeUid (uint fileType, uint qudongType, uint devieNumber) { return ((fileType << 28) & 0xF0000000) | ((qudongType << 16) & 0x0FFF0000) | (devieNumber & 0x0000FFFF); } /// /// 生成Oid /// /// 反馈生成的Oida /// 目标类型 /// 属性 /// 目标号 public static uint MakeOid (uint targetType, uint property, uint targetNumber) { return ((targetType << 24) & 0xFF000000) | ((property << 16) & 0x00FF0000) | (targetNumber & 0x0000FFFF); } public static ulong MakeSid (uint uid, uint oid) { return (((ulong)uid) << 32) | oid; } public static SuperGateWayCommon GetBySid (string sid) { SuperGateWayCommon common = null; try { var sidUlong = Convert.ToUInt64 (sid, 16); var subnetId = (byte)((sidUlong >> 40) & 0xFF); var deviceId = (byte)((sidUlong >> 32) & 0xFF); var deviceType = (byte)((sidUlong >> 24) & 0xFF); var property = (byte)((sidUlong >> 16) & 0xFF); var targetNumber = (uint)((sidUlong >> 0) & 0xFFFF); switch (deviceType) { case 0: break; case 1: break; //空调 case 2: common = new SuperGateWayCommon { Type = DeviceType.HVAC, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; if (common.keyValuePairs.Count == 0) { common.Add ("Power", 0); } break; //地热 case 3: { common = new SuperGateWayCommon { Type = DeviceType.FoolHeat, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; if (common.keyValuePairs.Count == 0) { common.Add ("Power", 0); } } break; //新风 case 4: { common = new SuperGateWayCommon { Type = DeviceType.FreshAir, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; if (common.keyValuePairs.Count == 0) { common.Add ("Power", 0); } } break; //窗帘 case 5: if (property == 0) { common = new SuperGateWayCommon { Type = DeviceType.CurtainModel, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; if (common.keyValuePairs.Count == 0) { common.Add ("Power", 0); } } else if (property == 1) { common = new SuperGateWayCommon { Type = DeviceType.CurtainRoller, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; if (common.keyValuePairs.Count == 0) { common.Add ("Brightness", 0); } } break; //灯光,可能包含调光及开关 case 6: if (property == 0) { common = new SuperGateWayCommon { Type = DeviceType.LightSwitch, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; if (common.keyValuePairs.Count == 0) { common.Add ("Power", 0); } } else if (property == 1) { common = new SuperGateWayCommon { Type = DeviceType.LightDimming, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; if (common.keyValuePairs.Count == 0) { common.Add ("Brightness", 0); } } break; //按键 case 7: if (property == 0) { common = new SuperGateWayCommon { Type = DeviceType.MechanicalSwitch, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; if (common.keyValuePairs.Count == 0) { common.Add ("Power", 0); } } else if (property == 1) { common = new SuperGateWayCommon { Type = DeviceType.AutomaticSwitch, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; if (common.keyValuePairs.Count == 0) { common.Add ("Power", 1); } } break; //音乐 case 8: { common = new SuperGateWayCommon { Type = DeviceType.MusicModel, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; if (common.keyValuePairs.Count == 0) { common.Add ("Status", 2); } } break; //传感器 case 9: { common = new SuperGateWayCommon { Type = DeviceType.Sensor, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; if (common.keyValuePairs.Count == 0) { common.Add ("Brightness", 30); } } break; //门锁 case 10: { common = new SuperGateWayCommon { Type = DeviceType.DoorLock, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; if (common.keyValuePairs.Count == 0) { common.Add ("Power", 0); } } break; case 11: break; case 12: break; case 13: break; //干接点 case 14: { common = new SuperGateWayCommon { Type = DeviceType.DryContact, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; if (common.keyValuePairs.Count == 0) { common.Add ("Power", 0); } } break; case 15: break; case 16: break; } } catch { } return common; } public static Dictionary CommonToSidValue (Common common) { Dictionary dictionary = null; if (common is HVAC) { var device = common as HVAC; { var uid = MakeUid (1, 1, MakeDeviceNumber (common.SubnetID, common.DeviceID)); var oid = MakeOid (2, 0, common.LoopID); dictionary = new Dictionary { ["sid"] = MakeSid (uid, oid).ToString ("X"), ["value"] = device.Power, }; } { var uid = MakeUid (1, 1, MakeDeviceNumber (common.SubnetID, common.DeviceID)); var oid = MakeOid (2, 1, common.LoopID); dictionary = new Dictionary { ["sid"] = MakeSid (uid, oid).ToString ("X"), ["value"] = device.SetMode, }; } { var uid = MakeUid (1, 1, MakeDeviceNumber (common.SubnetID, common.DeviceID)); var oid = MakeOid (2, 2, common.LoopID); dictionary = new Dictionary { ["sid"] = MakeSid (uid, oid).ToString ("X"), ["value"] = device.SetFanSpeed, }; } { var uid = MakeUid (1, 1, MakeDeviceNumber (common.SubnetID, common.DeviceID)); var oid = MakeOid (2, 3, common.LoopID); dictionary = new Dictionary { ["sid"] = MakeSid (uid, oid).ToString ("X"), ["value"] = device.SetTemperature, }; } } else if (common is LightDimming) { var device = common as LightDimming; var uid = MakeUid (1, 1, MakeDeviceNumber (common.SubnetID, common.DeviceID)); var oid = MakeOid (6, 1, common.LoopID); dictionary = new Dictionary { ["sid"] = MakeSid (uid, oid).ToString ("X"), ["value"] = device.CurrentBrightness }; } else if (common is LightSwitch) { var device = common as LightSwitch; var uid = MakeUid (1, 1, MakeDeviceNumber (common.SubnetID, common.DeviceID)); var oid = MakeOid (6, 0, common.LoopID); dictionary = new Dictionary { ["sid"] = MakeSid (uid, oid).ToString ("X"), ["value"] = device.CurrentBrightness }; } else if (common is CurtainModel) { var device = common as CurtainModel; var uid = MakeUid (1, 1, MakeDeviceNumber (common.SubnetID, common.DeviceID)); var oid = MakeOid (5, 0, common.LoopID); dictionary = new Dictionary { ["sid"] = MakeSid (uid, oid).ToString ("X"), ["value"] = device.Status, }; } return dictionary; } public static SuperGateWayCommon GetCommon (string sid, GateWay gateWay) { SuperGateWayCommon common = new SuperGateWayCommon (); try { var sidUlong = Convert.ToUInt64 (sid, 16); var subnetId = (byte)((sidUlong >> 40) & 0xFF); var deviceId = (byte)((sidUlong >> 32) & 0xFF); var deviceType = (byte)((sidUlong >> 24) & 0xFF); var property = (byte)((sidUlong >> 16) & 0xFF); var targetNumber = (uint)((sidUlong >> 0) & 0xFFFF); common.SubnetID = subnetId; common.DeviceID = deviceId; common.LoopID = (byte)targetNumber; switch (deviceType) { case 0: break; case 1: break; //空调 case 2: { var device = gateWay.Commons.Find ((c) => c.Type == DeviceType.HVAC && c.CommonLoopID == common.CommonLoopID); if (device == null) { common.Type = DeviceType.HVAC; if (common.keyValuePairs.Count == 0) { common.Add ("Power", 0); } } else { common = device; } } break; //地热 case 3: { var device = gateWay.Commons.Find ((c) => c.Type == DeviceType.FoolHeat && c.CommonLoopID == common.CommonLoopID); if (device == null) { common.Type = DeviceType.FoolHeat; if (common.keyValuePairs.Count == 0) { common.Add ("Power", 0); } } else { common = device; } } break; //新风 case 4: { var device = gateWay.Commons.Find ((c) => c.Type == DeviceType.FreshAir && c.CommonLoopID == common.CommonLoopID); if (device == null) { common.Type = DeviceType.FreshAir; if (common.keyValuePairs.Count == 0) { common.Add ("Power", 0); } } else { common = device; } } break; //窗帘 case 5: { if (property == 0) { var device = gateWay.Commons.Find ((c) => c.Type == DeviceType.CurtainModel && c.CommonLoopID == common.CommonLoopID); if (device == null) { common.Type = DeviceType.CurtainModel; if (common.keyValuePairs.Count == 0) { common.Add ("Power", 0); } } else { common = device; } } else if (property == 1) { var device = gateWay.Commons.Find ((c) => c.Type == DeviceType.CurtainRoller && c.CommonLoopID == common.CommonLoopID); if (device == null) { common.Type = DeviceType.CurtainRoller; if (common.keyValuePairs.Count == 0) { common.Add ("Brightness", 0); } } else { common = device; } } } break; //灯光,可能包含调光及开关 case 6: { if (property == 0) { var device = gateWay.Commons.Find ((c) => c.Type == DeviceType.LightSwitch && c.CommonLoopID == common.CommonLoopID); if (device == null) { common.Type = DeviceType.LightSwitch; if (common.keyValuePairs.Count == 0) { common.Add ("Power", 0); } } else { common = device; } } else if (property == 1) { var device = gateWay.Commons.Find ((c) => c.Type == DeviceType.LightDimming && c.CommonLoopID == common.CommonLoopID); if (device == null) { common.Type = DeviceType.LightDimming; if (common.keyValuePairs.Count == 0) { common.Add ("Brightness", 0); } } else { common = device; } } } break; //按键 case 7: { if (property == 0) { var device = gateWay.Commons.Find ((c) => c.Type == DeviceType.MechanicalSwitch && c.CommonLoopID == common.CommonLoopID); if (device == null) { common.Type = DeviceType.MechanicalSwitch; if (common.keyValuePairs.Count == 0) { common.Add ("Power", 0); } } else { common = device; } } else if (property == 1) { var device = gateWay.Commons.Find ((c) => c.Type == DeviceType.AutomaticSwitch && c.CommonLoopID == common.CommonLoopID); if (device == null) { common.Type = DeviceType.AutomaticSwitch; if (common.keyValuePairs.Count == 0) { common.Add ("Power", 1); } } else { common = device; } } } break; //音乐 case 8: { var device = gateWay.Commons.Find ((c) => c.Type == DeviceType.MusicModel && c.CommonLoopID == common.CommonLoopID); if (device == null) { common.Type = DeviceType.MusicModel; if (common.keyValuePairs.Count == 0) { common.Add ("Status", 2); } } else { common = device; } } break; //传感器 case 9: { var device = gateWay.Commons.Find ((c) => c.Type == DeviceType.Sensor && c.CommonLoopID == common.CommonLoopID); if (device == null) { common.Type = DeviceType.Sensor; if (common.keyValuePairs.Count == 0) { common.Add ("Brightness", 30); } } else { common = device; } } break; //门锁 case 10: { var device = gateWay.Commons.Find ((c) => c.Type == DeviceType.DoorLock && c.CommonLoopID == common.CommonLoopID); if (device == null) { common.Type = DeviceType.DoorLock; if (common.keyValuePairs.Count == 0) { common.Add ("Power", 0); } } else { common = device; } } break; case 11: break; case 12: break; case 13: break; //干接点 case 14: { var device = gateWay.Commons.Find ((c) => c.Type == DeviceType.DryContact && c.CommonLoopID == common.CommonLoopID); if (device == null) { common.Type = DeviceType.DryContact; if (common.keyValuePairs.Count == 0) { common.Add ("Power", 0); } } else { common = device; } } break; case 15: break; case 16: break; } } catch { } return common; } } }