From cda76b6930793db30ad3ef360a8a8ddeea175f04 Mon Sep 17 00:00:00 2001 From: Ryan Wagoner Date: Thu, 1 Nov 2018 22:45:59 -0400 Subject: [PATCH] - Add override zone type for web service --- OmniLinkBridge/Global.cs | 4 +- OmniLinkBridge/Modules/WebServiceModule.cs | 25 +--------- OmniLinkBridge/OmniLinkBridge.csproj | 2 + OmniLinkBridge/OmniLinkBridge.ini | 2 + OmniLinkBridge/Settings.cs | 29 +++++++---- OmniLinkBridge/WebService/DeviceType.cs | 18 +++++++ .../WebService/MappingExtensions.cs | 30 ++++++++++++ OmniLinkBridge/WebService/OmniLinkService.cs | 48 ++++--------------- OmniLinkBridge/WebService/OverrideZone.cs | 13 +++++ 9 files changed, 97 insertions(+), 74 deletions(-) create mode 100644 OmniLinkBridge/WebService/DeviceType.cs create mode 100644 OmniLinkBridge/WebService/OverrideZone.cs diff --git a/OmniLinkBridge/Global.cs b/OmniLinkBridge/Global.cs index bf5a4ca..58ad32e 100644 --- a/OmniLinkBridge/Global.cs +++ b/OmniLinkBridge/Global.cs @@ -1,4 +1,3 @@ -using OmniLinkBridge.MQTT; using System.Collections.Concurrent; using System.Collections.Generic; using System.Net.Mail; @@ -40,6 +39,7 @@ namespace OmniLinkBridge // Web Service public static bool webapi_enabled; public static int webapi_port; + public static ConcurrentDictionary webapi_override_zone; public static string webapi_subscriptions_file; // MQTT @@ -53,7 +53,7 @@ namespace OmniLinkBridge public static string mqtt_discovery_name_prefix; public static HashSet mqtt_discovery_ignore_zones; public static HashSet mqtt_discovery_ignore_units; - public static ConcurrentDictionary mqtt_discovery_override_zone; + public static ConcurrentDictionary mqtt_discovery_override_zone; // Notifications public static bool notify_area; diff --git a/OmniLinkBridge/Modules/WebServiceModule.cs b/OmniLinkBridge/Modules/WebServiceModule.cs index 52f9ab9..7f7042d 100644 --- a/OmniLinkBridge/Modules/WebServiceModule.cs +++ b/OmniLinkBridge/Modules/WebServiceModule.cs @@ -79,30 +79,7 @@ namespace OmniLinkBridge return; } - switch (e.Zone.ZoneType) - { - case enuZoneType.EntryExit: - case enuZoneType.X2EntryDelay: - case enuZoneType.X4EntryDelay: - case enuZoneType.Perimeter: - case enuZoneType.Tamper: - case enuZoneType.Auxiliary: - WebNotification.Send("contact", JsonConvert.SerializeObject(e.Zone.ToContract())); - break; - case enuZoneType.AwayInt: - case enuZoneType.NightInt: - WebNotification.Send("motion", JsonConvert.SerializeObject(e.Zone.ToContract())); - break; - case enuZoneType.Water: - WebNotification.Send("water", JsonConvert.SerializeObject(e.Zone.ToContract())); - break; - case enuZoneType.Fire: - WebNotification.Send("smoke", JsonConvert.SerializeObject(e.Zone.ToContract())); - break; - case enuZoneType.Gas: - WebNotification.Send("co", JsonConvert.SerializeObject(e.Zone.ToContract())); - break; - } + WebNotification.Send(Enum.GetName(typeof(DeviceType), e.Zone.ToDeviceType()), JsonConvert.SerializeObject(e.Zone.ToContract())); } private void Omnilink_OnUnitStatus(object sender, UnitStatusEventArgs e) diff --git a/OmniLinkBridge/OmniLinkBridge.csproj b/OmniLinkBridge/OmniLinkBridge.csproj index 0e4c77f..43b7675 100644 --- a/OmniLinkBridge/OmniLinkBridge.csproj +++ b/OmniLinkBridge/OmniLinkBridge.csproj @@ -123,6 +123,7 @@ + @@ -131,6 +132,7 @@ + diff --git a/OmniLinkBridge/OmniLinkBridge.ini b/OmniLinkBridge/OmniLinkBridge.ini index bb3cd42..94a4387 100644 --- a/OmniLinkBridge/OmniLinkBridge.ini +++ b/OmniLinkBridge/OmniLinkBridge.ini @@ -28,6 +28,8 @@ mysql_connection = # Can be used for integration with Samsung SmartThings webapi_enabled = yes webapi_port = 8000 +# device_type must be contact, motion, water, smoke, or co +#webapi_override_zone = id=20;device_type=motion # MQTT # Can be used for integration with Home Assistant diff --git a/OmniLinkBridge/Settings.cs b/OmniLinkBridge/Settings.cs index 2ae5d19..b9d46c4 100644 --- a/OmniLinkBridge/Settings.cs +++ b/OmniLinkBridge/Settings.cs @@ -1,5 +1,4 @@ using log4net; -using OmniLinkBridge.MQTT; using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -48,6 +47,7 @@ namespace OmniLinkBridge // Web Service Global.webapi_enabled = ValidateYesNo(settings, "webapi_enabled"); Global.webapi_port = ValidatePort(settings, "webapi_port"); + Global.webapi_override_zone = LoadOverrideZone(settings, "webapi_override_zone"); // MQTT Global.mqtt_enabled = ValidateYesNo(settings, "mqtt_enabled"); @@ -64,7 +64,7 @@ namespace OmniLinkBridge Global.mqtt_discovery_ignore_zones = ValidateRange(settings, "mqtt_discovery_ignore_zones"); Global.mqtt_discovery_ignore_units = ValidateRange(settings, "mqtt_discovery_ignore_units"); - Global.mqtt_discovery_override_zone = LoadOverrideZone(settings, "mqtt_discovery_override_zone"); + Global.mqtt_discovery_override_zone = LoadOverrideZone(settings, "mqtt_discovery_override_zone"); // Notifications Global.notify_area = ValidateYesNo(settings, "notify_area"); @@ -86,11 +86,11 @@ namespace OmniLinkBridge Global.pushover_user = ValidateMultipleStrings(settings, "pushover_user"); } - private static ConcurrentDictionary LoadOverrideZone(NameValueCollection settings, string section) + private static ConcurrentDictionary LoadOverrideZone(NameValueCollection settings, string section) where T : new() { try { - ConcurrentDictionary ret = new ConcurrentDictionary(); + ConcurrentDictionary ret = new ConcurrentDictionary(); if (settings[section] == null) return ret; @@ -106,13 +106,24 @@ namespace OmniLinkBridge if (!attributes.ContainsKey("id") || !Int32.TryParse(attributes["id"], out int attrib_id)) throw new Exception("Missing or invalid id attribute"); - if (!attributes.ContainsKey("device_class") || !Enum.TryParse(attributes["device_class"], out BinarySensor.DeviceClass attrib_device_class)) - throw new Exception("Missing or invalid device_class attribute"); + T override_zone = new T(); - ret.TryAdd(attrib_id, new OverrideZone() + if (((object)override_zone) is WebAPI.OverrideZone webapi_zone) { - device_class = attrib_device_class, - }); + if (!attributes.ContainsKey("device_type") || !Enum.TryParse(attributes["device_type"], out WebAPI.DeviceType attrib_device_type)) + throw new Exception("Missing or invalid device_type attribute"); + + webapi_zone.device_type = attrib_device_type; + } + else if (((object)override_zone) is MQTT.OverrideZone mqtt_zone) + { + if (!attributes.ContainsKey("device_class") || !Enum.TryParse(attributes["device_class"], out MQTT.BinarySensor.DeviceClass attrib_device_class)) + throw new Exception("Missing or invalid device_class attribute"); + + mqtt_zone.device_class = attrib_device_class; + } + + ret.TryAdd(attrib_id, override_zone); } return ret; diff --git a/OmniLinkBridge/WebService/DeviceType.cs b/OmniLinkBridge/WebService/DeviceType.cs new file mode 100644 index 0000000..96074d9 --- /dev/null +++ b/OmniLinkBridge/WebService/DeviceType.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OmniLinkBridge.WebAPI +{ + public enum DeviceType + { + unknown, + contact, + motion, + water, + smoke, + co + } +} diff --git a/OmniLinkBridge/WebService/MappingExtensions.cs b/OmniLinkBridge/WebService/MappingExtensions.cs index 299522a..90f3d2d 100644 --- a/OmniLinkBridge/WebService/MappingExtensions.cs +++ b/OmniLinkBridge/WebService/MappingExtensions.cs @@ -98,5 +98,35 @@ namespace OmniLinkBridge.WebAPI return ret; } + + public static DeviceType ToDeviceType(this clsZone zone) + { + Global.webapi_override_zone.TryGetValue(zone.Number, out OverrideZone override_zone); + + if (override_zone != null) + return override_zone.device_type; + + switch (zone.ZoneType) + { + case enuZoneType.EntryExit: + case enuZoneType.X2EntryDelay: + case enuZoneType.X4EntryDelay: + case enuZoneType.Perimeter: + case enuZoneType.Tamper: + case enuZoneType.Auxiliary: + return DeviceType.contact; + case enuZoneType.AwayInt: + case enuZoneType.NightInt: + return DeviceType.motion; + case enuZoneType.Water: + return DeviceType.water; + case enuZoneType.Fire: + return DeviceType.smoke; + case enuZoneType.Gas: + return DeviceType.co; + default: + return DeviceType.unknown; + } + } } } diff --git a/OmniLinkBridge/WebService/OmniLinkService.cs b/OmniLinkBridge/WebService/OmniLinkService.cs index c9a528d..4fa0d90 100644 --- a/OmniLinkBridge/WebService/OmniLinkService.cs +++ b/OmniLinkBridge/WebService/OmniLinkService.cs @@ -1,5 +1,6 @@ using HAI_Shared; using log4net; +using OmniLinkBridge.WebAPI; using System; using System.Collections.Generic; using System.Reflection; @@ -53,12 +54,7 @@ namespace OmniLinkBridge.WebAPI { clsZone zone = WebServiceModule.OmniLink.Controller.Zones[i]; - if ((zone.ZoneType == enuZoneType.EntryExit || - zone.ZoneType == enuZoneType.X2EntryDelay || - zone.ZoneType == enuZoneType.X4EntryDelay || - zone.ZoneType == enuZoneType.Perimeter || - zone.ZoneType == enuZoneType.Tamper || - zone.ZoneType == enuZoneType.Auxiliary) && zone.DefaultProperties == false) + if (zone.DefaultProperties == false && zone.ToDeviceType() == DeviceType.contact) names.Add(new NameContract() { id = i, name = zone.Name }); } return names; @@ -73,8 +69,7 @@ namespace OmniLinkBridge.WebAPI { clsZone zone = WebServiceModule.OmniLink.Controller.Zones[i]; - if ((zone.ZoneType == enuZoneType.AwayInt || - zone.ZoneType == enuZoneType.NightInt) && zone.DefaultProperties == false) + if (zone.DefaultProperties == false && zone.ToDeviceType() == DeviceType.motion) names.Add(new NameContract() { id = i, name = zone.Name }); } return names; @@ -89,7 +84,7 @@ namespace OmniLinkBridge.WebAPI { clsZone zone = WebServiceModule.OmniLink.Controller.Zones[i]; - if (zone.ZoneType == enuZoneType.Water && zone.DefaultProperties == false) + if (zone.DefaultProperties == false && zone.ToDeviceType() == DeviceType.water) names.Add(new NameContract() { id = i, name = zone.Name }); } return names; @@ -104,7 +99,7 @@ namespace OmniLinkBridge.WebAPI { clsZone zone = WebServiceModule.OmniLink.Controller.Zones[i]; - if (zone.ZoneType == enuZoneType.Fire && zone.DefaultProperties == false) + if (zone.DefaultProperties == false && zone.ToDeviceType() == DeviceType.smoke) names.Add(new NameContract() { id = i, name = zone.Name }); } return names; @@ -119,7 +114,7 @@ namespace OmniLinkBridge.WebAPI { clsZone zone = WebServiceModule.OmniLink.Controller.Zones[i]; - if (zone.ZoneType == enuZoneType.Gas && zone.DefaultProperties == false) + if (zone.DefaultProperties == false && zone.ToDeviceType() == DeviceType.co) names.Add(new NameContract() { id = i, name = zone.Name }); } return names; @@ -134,7 +129,7 @@ namespace OmniLinkBridge.WebAPI { clsZone zone = WebServiceModule.OmniLink.Controller.Zones[i]; - if (zone.IsTemperatureZone() && zone.DefaultProperties == false) + if (zone.DefaultProperties == false && zone.IsTemperatureZone()) names.Add(new NameContract() { id = i, name = zone.Name }); } return names; @@ -152,33 +147,8 @@ namespace OmniLinkBridge.WebAPI } else { - switch (WebServiceModule.OmniLink.Controller.Zones[id].ZoneType) - { - case enuZoneType.EntryExit: - case enuZoneType.X2EntryDelay: - case enuZoneType.X4EntryDelay: - case enuZoneType.Perimeter: - case enuZoneType.Tamper: - case enuZoneType.Auxiliary: - ctx.OutgoingResponse.Headers.Add("type", "contact"); - break; - case enuZoneType.AwayInt: - case enuZoneType.NightInt: - ctx.OutgoingResponse.Headers.Add("type", "motion"); - break; - case enuZoneType.Water: - ctx.OutgoingResponse.Headers.Add("type", "water"); - break; - case enuZoneType.Fire: - ctx.OutgoingResponse.Headers.Add("type", "smoke"); - break; - case enuZoneType.Gas: - ctx.OutgoingResponse.Headers.Add("type", "co"); - break; - default: - ctx.OutgoingResponse.Headers.Add("type", "unknown"); - break; - } + ctx.OutgoingResponse.Headers.Add("type", Enum.GetName(typeof(DeviceType), + WebServiceModule.OmniLink.Controller.Zones[id].ToDeviceType())); } return WebServiceModule.OmniLink.Controller.Zones[id].ToContract(); diff --git a/OmniLinkBridge/WebService/OverrideZone.cs b/OmniLinkBridge/WebService/OverrideZone.cs new file mode 100644 index 0000000..b0c1e52 --- /dev/null +++ b/OmniLinkBridge/WebService/OverrideZone.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OmniLinkBridge.WebAPI +{ + public class OverrideZone + { + public DeviceType device_type { get; set; } + } +}