- Add override zone type for web service

This commit is contained in:
Ryan Wagoner 2018-11-01 22:45:59 -04:00
parent c9e5063ddd
commit cda76b6930
9 changed files with 97 additions and 74 deletions

View file

@ -1,4 +1,3 @@
using OmniLinkBridge.MQTT;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net.Mail; using System.Net.Mail;
@ -40,6 +39,7 @@ namespace OmniLinkBridge
// Web Service // Web Service
public static bool webapi_enabled; public static bool webapi_enabled;
public static int webapi_port; public static int webapi_port;
public static ConcurrentDictionary<int, WebAPI.OverrideZone> webapi_override_zone;
public static string webapi_subscriptions_file; public static string webapi_subscriptions_file;
// MQTT // MQTT
@ -53,7 +53,7 @@ namespace OmniLinkBridge
public static string mqtt_discovery_name_prefix; public static string mqtt_discovery_name_prefix;
public static HashSet<int> mqtt_discovery_ignore_zones; public static HashSet<int> mqtt_discovery_ignore_zones;
public static HashSet<int> mqtt_discovery_ignore_units; public static HashSet<int> mqtt_discovery_ignore_units;
public static ConcurrentDictionary<int, OverrideZone> mqtt_discovery_override_zone; public static ConcurrentDictionary<int, MQTT.OverrideZone> mqtt_discovery_override_zone;
// Notifications // Notifications
public static bool notify_area; public static bool notify_area;

View file

@ -79,30 +79,7 @@ namespace OmniLinkBridge
return; return;
} }
switch (e.Zone.ZoneType) WebNotification.Send(Enum.GetName(typeof(DeviceType), e.Zone.ToDeviceType()), JsonConvert.SerializeObject(e.Zone.ToContract()));
{
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;
}
} }
private void Omnilink_OnUnitStatus(object sender, UnitStatusEventArgs e) private void Omnilink_OnUnitStatus(object sender, UnitStatusEventArgs e)

View file

@ -123,6 +123,7 @@
</Compile> </Compile>
<Compile Include="Settings.cs" /> <Compile Include="Settings.cs" />
<Compile Include="WebService\MappingExtensions.cs" /> <Compile Include="WebService\MappingExtensions.cs" />
<Compile Include="WebService\OverrideZone.cs" />
<Compile Include="WebService\SubscribeContract.cs" /> <Compile Include="WebService\SubscribeContract.cs" />
<Compile Include="WebService\ThermostatContract.cs" /> <Compile Include="WebService\ThermostatContract.cs" />
<Compile Include="WebService\NameContract.cs" /> <Compile Include="WebService\NameContract.cs" />
@ -131,6 +132,7 @@
<Compile Include="WebService\UnitContract.cs" /> <Compile Include="WebService\UnitContract.cs" />
<Compile Include="WebService\WebNotification.cs" /> <Compile Include="WebService\WebNotification.cs" />
<Compile Include="Modules\WebServiceModule.cs" /> <Compile Include="Modules\WebServiceModule.cs" />
<Compile Include="WebService\DeviceType.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="App.config"> <None Include="App.config">

View file

@ -28,6 +28,8 @@ mysql_connection =
# Can be used for integration with Samsung SmartThings # Can be used for integration with Samsung SmartThings
webapi_enabled = yes webapi_enabled = yes
webapi_port = 8000 webapi_port = 8000
# device_type must be contact, motion, water, smoke, or co
#webapi_override_zone = id=20;device_type=motion
# MQTT # MQTT
# Can be used for integration with Home Assistant # Can be used for integration with Home Assistant

View file

@ -1,5 +1,4 @@
using log4net; using log4net;
using OmniLinkBridge.MQTT;
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
@ -48,6 +47,7 @@ namespace OmniLinkBridge
// Web Service // Web Service
Global.webapi_enabled = ValidateYesNo(settings, "webapi_enabled"); Global.webapi_enabled = ValidateYesNo(settings, "webapi_enabled");
Global.webapi_port = ValidatePort(settings, "webapi_port"); Global.webapi_port = ValidatePort(settings, "webapi_port");
Global.webapi_override_zone = LoadOverrideZone<WebAPI.OverrideZone>(settings, "webapi_override_zone");
// MQTT // MQTT
Global.mqtt_enabled = ValidateYesNo(settings, "mqtt_enabled"); 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_zones = ValidateRange(settings, "mqtt_discovery_ignore_zones");
Global.mqtt_discovery_ignore_units = ValidateRange(settings, "mqtt_discovery_ignore_units"); 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<MQTT.OverrideZone>(settings, "mqtt_discovery_override_zone");
// Notifications // Notifications
Global.notify_area = ValidateYesNo(settings, "notify_area"); Global.notify_area = ValidateYesNo(settings, "notify_area");
@ -86,11 +86,11 @@ namespace OmniLinkBridge
Global.pushover_user = ValidateMultipleStrings(settings, "pushover_user"); Global.pushover_user = ValidateMultipleStrings(settings, "pushover_user");
} }
private static ConcurrentDictionary<int, OverrideZone> LoadOverrideZone(NameValueCollection settings, string section) private static ConcurrentDictionary<int, T> LoadOverrideZone<T>(NameValueCollection settings, string section) where T : new()
{ {
try try
{ {
ConcurrentDictionary<int, OverrideZone> ret = new ConcurrentDictionary<int, OverrideZone>(); ConcurrentDictionary<int, T> ret = new ConcurrentDictionary<int, T>();
if (settings[section] == null) if (settings[section] == null)
return ret; return ret;
@ -106,13 +106,24 @@ namespace OmniLinkBridge
if (!attributes.ContainsKey("id") || !Int32.TryParse(attributes["id"], out int attrib_id)) if (!attributes.ContainsKey("id") || !Int32.TryParse(attributes["id"], out int attrib_id))
throw new Exception("Missing or invalid id attribute"); throw new Exception("Missing or invalid id attribute");
if (!attributes.ContainsKey("device_class") || !Enum.TryParse(attributes["device_class"], out BinarySensor.DeviceClass attrib_device_class)) T override_zone = new T();
throw new Exception("Missing or invalid device_class attribute");
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; return ret;

View file

@ -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
}
}

View file

@ -98,5 +98,35 @@ namespace OmniLinkBridge.WebAPI
return ret; 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;
}
}
} }
} }

View file

@ -1,5 +1,6 @@
using HAI_Shared; using HAI_Shared;
using log4net; using log4net;
using OmniLinkBridge.WebAPI;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection; using System.Reflection;
@ -53,12 +54,7 @@ namespace OmniLinkBridge.WebAPI
{ {
clsZone zone = WebServiceModule.OmniLink.Controller.Zones[i]; clsZone zone = WebServiceModule.OmniLink.Controller.Zones[i];
if ((zone.ZoneType == enuZoneType.EntryExit || if (zone.DefaultProperties == false && zone.ToDeviceType() == DeviceType.contact)
zone.ZoneType == enuZoneType.X2EntryDelay ||
zone.ZoneType == enuZoneType.X4EntryDelay ||
zone.ZoneType == enuZoneType.Perimeter ||
zone.ZoneType == enuZoneType.Tamper ||
zone.ZoneType == enuZoneType.Auxiliary) && zone.DefaultProperties == false)
names.Add(new NameContract() { id = i, name = zone.Name }); names.Add(new NameContract() { id = i, name = zone.Name });
} }
return names; return names;
@ -73,8 +69,7 @@ namespace OmniLinkBridge.WebAPI
{ {
clsZone zone = WebServiceModule.OmniLink.Controller.Zones[i]; clsZone zone = WebServiceModule.OmniLink.Controller.Zones[i];
if ((zone.ZoneType == enuZoneType.AwayInt || if (zone.DefaultProperties == false && zone.ToDeviceType() == DeviceType.motion)
zone.ZoneType == enuZoneType.NightInt) && zone.DefaultProperties == false)
names.Add(new NameContract() { id = i, name = zone.Name }); names.Add(new NameContract() { id = i, name = zone.Name });
} }
return names; return names;
@ -89,7 +84,7 @@ namespace OmniLinkBridge.WebAPI
{ {
clsZone zone = WebServiceModule.OmniLink.Controller.Zones[i]; 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 }); names.Add(new NameContract() { id = i, name = zone.Name });
} }
return names; return names;
@ -104,7 +99,7 @@ namespace OmniLinkBridge.WebAPI
{ {
clsZone zone = WebServiceModule.OmniLink.Controller.Zones[i]; 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 }); names.Add(new NameContract() { id = i, name = zone.Name });
} }
return names; return names;
@ -119,7 +114,7 @@ namespace OmniLinkBridge.WebAPI
{ {
clsZone zone = WebServiceModule.OmniLink.Controller.Zones[i]; 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 }); names.Add(new NameContract() { id = i, name = zone.Name });
} }
return names; return names;
@ -134,7 +129,7 @@ namespace OmniLinkBridge.WebAPI
{ {
clsZone zone = WebServiceModule.OmniLink.Controller.Zones[i]; 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 }); names.Add(new NameContract() { id = i, name = zone.Name });
} }
return names; return names;
@ -152,33 +147,8 @@ namespace OmniLinkBridge.WebAPI
} }
else else
{ {
switch (WebServiceModule.OmniLink.Controller.Zones[id].ZoneType) ctx.OutgoingResponse.Headers.Add("type", Enum.GetName(typeof(DeviceType),
{ WebServiceModule.OmniLink.Controller.Zones[id].ToDeviceType()));
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;
}
} }
return WebServiceModule.OmniLink.Controller.Zones[id].ToContract(); return WebServiceModule.OmniLink.Controller.Zones[id].ToContract();

View file

@ -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; }
}
}