using System; using System.Collections.Generic; using Shared; using Newtonsoft; namespace SuperGateWay { [System.Serializable] public class SuperGateWayCommon : Common { public List functions = new List { }; public Dictionary keyValuePairs = new Dictionary { }; public void Add (string key, object value) { if (key == null) { return; } if (keyValuePairs.ContainsKey (key)) { keyValuePairs [key] = value; } else { keyValuePairs.Add (key, value); } } public int GetIntValueByKey (string key, int defaultValue) { try { object o; keyValuePairs.TryGetValue (key, out o); if (o == null) { return defaultValue; } else { return int.Parse (o.ToString ()); } } catch { return defaultValue; } } public string GetStringValueByKey (string key, string defaultValue) { try { object o; keyValuePairs.TryGetValue (key, out o); if (o == null) { return defaultValue; } else { return o.ToString (); } } catch { return defaultValue; } } } [System.Serializable] public class Scene { /// /// 功能类型 /// public string module = "scene"; /// /// 1 true /// 0 false /// //public int enable; /// /// 文件类型 4bit 驱动类型12bit 设备识别号 16bit /// public string sid; [Newtonsoft.Json.JsonIgnore] public uint uid { get { return (uint)(Convert.ToUInt64 (sid, 16) >> 32); } } public string name; public List> objects = new List> { }; [Newtonsoft.Json.JsonIgnore] /// /// 场景的背景图 /// public string BackgoundImagePath { get { if (!BackgroundImageFilePaths.Exists ((obj) => obj.Key == sid)) { return "Scene/s20.png"; } else { return BackgroundImageFilePaths.Find ((obj) => obj.Key == sid).Value; } } } /// /// 获取 /// /// The new uid. public static string NewSid { get { uint max = 0; bool d = false; foreach (var scene in GateWay.Instance.Scenes) { var number = scene.uid & 0x0000FFFF; if (max < number) { max = number; } d = true; } var zoneID = (max & 0x0000FF00) >> 8; //区号从1开始 if (zoneID == 0) { zoneID = 1; } var sceneID = max & 0x000000FF; if (31 <= sceneID) { zoneID++; sceneID = 0; } else { if (d) { sceneID++; } } max = zoneID * 256 + sceneID; return ((((ulong)(0x20000000 | 0x00010000 | max)) << 32) | 0x00000000).ToString ("X"); } } [Newtonsoft.Json.JsonIgnore] public List deviceList = new List { }; static readonly string sceneBackgroundImagePath = "SuperGateWay_Scene_BackgroundImage_FilePaths"; static List> backgroundImageFilePaths; static List> BackgroundImageFilePaths { get { if (backgroundImageFilePaths == null) { backgroundImageFilePaths = Newtonsoft.Json.JsonConvert.DeserializeObject>> (System.Text.Encoding.UTF8.GetString (Shared.IO.FileUtils.ReadFile (sceneBackgroundImagePath))); if (backgroundImageFilePaths == null) { backgroundImageFilePaths = new List> { }; } } return backgroundImageFilePaths; } } public static void Add (string sid, string filePath) { BackgroundImageFilePaths.RemoveAll ((obj) => obj.Key == sid); BackgroundImageFilePaths.Add (new KeyValuePair (sid, filePath)); Shared.IO.FileUtils.WriteFileByBytes (sceneBackgroundImagePath, System.Text.Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (BackgroundImageFilePaths))); } public void SidToDevice() { try { if (objects == null) { return; } foreach (var o in objects) { var tempSid = o ["sid"]?.ToString (); if (tempSid == null) { continue; } var value = o ["value"]?.ToString (); if (value == null) { value = "0"; } //判断SID是否有效的,如果不是有效的当前这个场景数据就不处理 Convert.ToUInt64 (tempSid, 16); var sidUlong = Convert.ToUInt64 (tempSid, 16); var subnetId = (byte)((sidUlong >> 40) & 0xFF); var deviceId = (byte)((sidUlong >> 32) & 0xFF); var targetType = (byte)((sidUlong >> 24) & 0xFF); var property = (byte)((sidUlong >> 16) & 0xFF); var targetNumber = (uint)((sidUlong >> 0) & 0xFFFF); switch (targetType) { case 0: break; case 1: break; //空调 case 2: { var common = new SuperGateWayCommon { Type = DeviceType.HVAC, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; var tempCommon = deviceList.Find ((obj) => obj.Type == common.Type && obj.CommonLoopID == common.CommonLoopID); if (tempCommon == null) { deviceList.Add (common); } else { common = tempCommon; } if (property == 0) { common.Add ("Power", byte.Parse (value)); } else if (property == 1) { common.Add ("Mode", byte.Parse (value)); } else if (property == 2) { common.Add ("Speed", byte.Parse (value)); } else if (property == 3) { common.Add ("Temperature", byte.Parse (value)); } else if (property == 4) { common.Add ("Scavenging", byte.Parse (value)); } else if (property == 5) { common.Add ("ambienTtemperature", byte.Parse (value)); } } break; //地热 case 3: { var common = new SuperGateWayCommon { Type = DeviceType.FoolHeat, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; var tempCommon = deviceList.Find ((obj) => obj.Type == common.Type&& obj.CommonLoopID == common.CommonLoopID); if (tempCommon == null) { deviceList.Add (common); } else { common = tempCommon; } if (property == 0) { common.Add ("Power", byte.Parse (value)); } else if (property == 1) { common.Add ("Temperature", byte.Parse (value)); } } break; //新风 case 4: { var common = new SuperGateWayCommon { Type = DeviceType.FreshAir, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; var tempCommon = deviceList.Find ((obj) => obj.Type == common.Type && obj.CommonLoopID == common.CommonLoopID); if (tempCommon == null) { deviceList.Add (common); } else { common = tempCommon; } if (property == 0) { common.Add ("Power", byte.Parse (value)); } else if (property == 1) { common.Add ("Mode", byte.Parse (value)); } else if (property == 2) { common.Add ("Speed", byte.Parse (value)); } } break; //窗帘 case 5: { if (property == 0) { var common = new SuperGateWayCommon { Type = DeviceType.CurtainModel, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; var tempCommon = deviceList.Find ((obj) => obj.Type== common.Type && obj.CommonLoopID == common.CommonLoopID); if (tempCommon == null) { deviceList.Add (common); } else { common = tempCommon; } common.Add ("Power", byte.Parse (value)); } else if (property == 1) { var common = new SuperGateWayCommon { Type = DeviceType.CurtainRoller, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; var tempCommon = deviceList.Find ((obj) => obj.Type == common.Type&& obj.CommonLoopID == common.CommonLoopID); if (tempCommon == null) { deviceList.Add (common); } else { common = tempCommon; } common.Add ("Brightness", byte.Parse (value)); } } break; //灯光,可能包含调光及开关 case 6: { if (property == 0) { var common = new SuperGateWayCommon { Type = DeviceType.LightSwitch, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; var tempCommon = deviceList.Find ((obj) => obj.Type== common.Type && obj.CommonLoopID == common.CommonLoopID); if (tempCommon == null) { deviceList.Add (common); } else { common = tempCommon; } common.Add ("Power", value == "0" ? 0 : 1); } else if (property == 1) { var common = new SuperGateWayCommon { Type = DeviceType.LightDimming, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; var tempCommon = deviceList.Find ((obj) => obj.Type == common.Type&& obj.CommonLoopID == common.CommonLoopID); if (tempCommon == null) { deviceList.Add (common); } else { common = tempCommon; } common.Add ("Brightness", byte.Parse (value)); } } break; //按键 case 7: { if (property == 0) { var common = new SuperGateWayCommon { Type = DeviceType.MechanicalSwitch, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; var tempCommon = deviceList.Find ((obj) => obj.Type== common.Type && obj.CommonLoopID == common.CommonLoopID); if (tempCommon == null) { deviceList.Add (common); } else { common = tempCommon; } common.Add ("Power", value == "0" ? 0 : 1); } else if (property == 1) { var common = new SuperGateWayCommon { Type = DeviceType.AutomaticSwitch, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; var tempCommon = deviceList.Find ((obj) => obj.Type== common.Type && obj.CommonLoopID == common.CommonLoopID); if (tempCommon == null) { deviceList.Add (common); } else { common = tempCommon; } common.Add ("Power", byte.Parse (value)); } } break; case 8: { var common = new SuperGateWayCommon { Type = DeviceType.MusicModel, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; var tempCommon = deviceList.Find ((obj) => obj.Type== common.Type && obj.CommonLoopID == common.CommonLoopID); if (tempCommon == null) { deviceList.Add (common); } else { common = tempCommon; } if (property == 0) { common.Add ("Status", byte.Parse (value)); } else if (property == 1) { common.Add ("Volume", byte.Parse (value)); } else if (property == 3) { common.Add ("Source", byte.Parse (value)); } else if (property == 5) { common.Add ("Mode", byte.Parse (value)); } } break; //传感器 case 9: { var common = new SuperGateWayCommon { Type = DeviceType.Sensor, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; var tempCommon = deviceList.Find ((obj) => obj.Type== common.Type && obj.CommonLoopID == common.CommonLoopID); if (tempCommon == null) { deviceList.Add (common); } else { common = tempCommon; } if (property == 0) { common.Add ("Temperature", byte.Parse (value)); } else if (property == 1) { common.Add ("Brightness", byte.Parse (value)); } } break; //门锁 case 10: { var common = new SuperGateWayCommon { Type = DeviceType.DoorLock, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; var tempCommon = deviceList.Find ((obj) => obj.Type == common.Type && obj.CommonLoopID == common.CommonLoopID); if (tempCommon == null) { deviceList.Add (common); } else { common = tempCommon; } common.Add ("Power", value == "0" ? 0 : 1); } break; case 11: { } break; case 12: break; case 13: break; //干接点 case 14: { var common = new SuperGateWayCommon { Type = DeviceType.DryContact, SubnetID = subnetId, DeviceID = deviceId, LoopID = (byte)targetNumber }; var tempCommon = deviceList.Find ((obj) => obj.Type == common.Type&& obj.CommonLoopID == common.CommonLoopID); if (tempCommon == null) { deviceList.Add (common); } else { common = tempCommon; } common.Add ("Power", value == "0" ? 0 : 1); } break; case 15: break; case 16: break; } } } catch { } } } [System.Serializable] public class Function { public string sid; public Dictionary attr; } }