- Add MQTT messages for controller disconnect and last will

This commit is contained in:
Ryan Wagoner 2019-11-22 20:41:44 -05:00
parent a891363539
commit 3e4a53fa13
6 changed files with 123 additions and 62 deletions

View file

@ -1,6 +1,6 @@
using OmniLinkBridge.Notifications; using log4net;
using OmniLinkBridge.Notifications;
using OmniLinkBridge.OmniLink; using OmniLinkBridge.OmniLink;
using log4net;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
@ -215,7 +215,7 @@ namespace OmniLinkBridge.Modules
if (e.Zone.IsTemperatureZone()) if (e.Zone.IsTemperatureZone())
log.Debug("ZoneStatus " + e.ID + " " + e.Zone.Name + ", Temp: " + e.Zone.TempText()); log.Debug("ZoneStatus " + e.ID + " " + e.Zone.Name + ", Temp: " + e.Zone.TempText());
else else
log.Debug("ZoneStatus" + e.ID + " " + e.Zone.Name + ", Status: " + e.Zone.StatusText()); log.Debug("ZoneStatus " + e.ID + " " + e.Zone.Name + ", Status: " + e.Zone.StatusText());
} }
} }

View file

@ -1,18 +1,17 @@
using HAI_Shared; using HAI_Shared;
using OmniLinkBridge.OmniLink;
using log4net; using log4net;
using MQTTnet; using MQTTnet;
using MQTTnet.Client; using MQTTnet.Client;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using Newtonsoft.Json;
using MQTTnet.Extensions.ManagedClient; using MQTTnet.Extensions.ManagedClient;
using OmniLinkBridge.MQTT;
using MQTTnet.Protocol; using MQTTnet.Protocol;
using System.Text.RegularExpressions; using Newtonsoft.Json;
using OmniLinkBridge.MQTT;
using OmniLinkBridge.OmniLink;
using System;
using System.Reflection;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
namespace OmniLinkBridge.Modules namespace OmniLinkBridge.Modules
{ {
@ -34,6 +33,7 @@ namespace OmniLinkBridge.Modules
{ {
OmniLink = omni; OmniLink = omni;
OmniLink.OnConnect += OmniLink_OnConnect; OmniLink.OnConnect += OmniLink_OnConnect;
OmniLink.OnDisconnect += OmniLink_OnDisconnect;
OmniLink.OnAreaStatus += Omnilink_OnAreaStatus; OmniLink.OnAreaStatus += Omnilink_OnAreaStatus;
OmniLink.OnZoneStatus += Omnilink_OnZoneStatus; OmniLink.OnZoneStatus += Omnilink_OnZoneStatus;
OmniLink.OnUnitStatus += Omnilink_OnUnitStatus; OmniLink.OnUnitStatus += Omnilink_OnUnitStatus;
@ -42,8 +42,17 @@ namespace OmniLinkBridge.Modules
public void Startup() public void Startup()
{ {
MqttApplicationMessage lastwill = new MqttApplicationMessage()
{
Topic = $"{Global.mqtt_prefix}/status",
Payload = Encoding.UTF8.GetBytes("offline"),
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce,
Retain = true
};
MqttClientOptionsBuilder options = new MqttClientOptionsBuilder() MqttClientOptionsBuilder options = new MqttClientOptionsBuilder()
.WithTcpServer(Global.mqtt_server); .WithTcpServer(Global.mqtt_server)
.WithWillMessage(lastwill);
if (!string.IsNullOrEmpty(Global.mqtt_username)) if (!string.IsNullOrEmpty(Global.mqtt_username))
options = options options = options
@ -92,6 +101,7 @@ namespace OmniLinkBridge.Modules
// Wait until shutdown // Wait until shutdown
trigger.WaitOne(); trigger.WaitOne();
log.Debug("Publishing controller offline");
MqttClient.PublishAsync($"{Global.mqtt_prefix}/status", "offline", MqttQualityOfServiceLevel.AtMostOnce, true); MqttClient.PublishAsync($"{Global.mqtt_prefix}/status", "offline", MqttQualityOfServiceLevel.AtMostOnce, true);
} }
@ -302,6 +312,14 @@ namespace OmniLinkBridge.Modules
ControllerConnected = true; ControllerConnected = true;
} }
private void OmniLink_OnDisconnect(object sender, EventArgs e)
{
ControllerConnected = false;
log.Debug("Publishing controller offline");
MqttClient.PublishAsync($"{Global.mqtt_prefix}/status", "offline", MqttQualityOfServiceLevel.AtMostOnce, true);
}
private void PublishConfig() private void PublishConfig()
{ {
PublishAreas(); PublishAreas();
@ -310,6 +328,7 @@ namespace OmniLinkBridge.Modules
PublishThermostats(); PublishThermostats();
PublishButtons(); PublishButtons();
log.Debug("Publishing controller online");
MqttClient.PublishAsync($"{Global.mqtt_prefix}/status", "online", MqttQualityOfServiceLevel.AtMostOnce, true); MqttClient.PublishAsync($"{Global.mqtt_prefix}/status", "online", MqttQualityOfServiceLevel.AtMostOnce, true);
} }

View file

@ -25,6 +25,7 @@ namespace OmniLinkBridge.Modules
// Events // Events
public event EventHandler<EventArgs> OnConnect; public event EventHandler<EventArgs> OnConnect;
public event EventHandler<EventArgs> OnDisconnect;
public event EventHandler<AreaStatusEventArgs> OnAreaStatus; public event EventHandler<AreaStatusEventArgs> OnAreaStatus;
public event EventHandler<ZoneStatusEventArgs> OnZoneStatus; public event EventHandler<ZoneStatusEventArgs> OnZoneStatus;
public event EventHandler<ThermostatStatusEventArgs> OnThermostatStatus; public event EventHandler<ThermostatStatusEventArgs> OnThermostatStatus;
@ -138,6 +139,7 @@ namespace OmniLinkBridge.Modules
break; break;
case enuOmniLinkCommStatus.Disconnected: case enuOmniLinkCommStatus.Disconnected:
log.Info("CONNECTION STATUS: Disconnected"); log.Info("CONNECTION STATUS: Disconnected");
OnDisconnect?.Invoke(this, new EventArgs());
break; break;
case enuOmniLinkCommStatus.InterruptedFunctionCall: case enuOmniLinkCommStatus.InterruptedFunctionCall:
if (Global.running) if (Global.running)

View file

@ -1,5 +1,4 @@
using HAI_Shared; using HAI_Shared;
using OmniLinkBridge.OmniLink;
using log4net; using log4net;
using System; using System;
using System.Reflection; using System.Reflection;

View file

@ -1,9 +1,8 @@
using HAI_Shared; using log4net;
using Newtonsoft.Json;
using OmniLinkBridge.Modules; using OmniLinkBridge.Modules;
using OmniLinkBridge.OmniLink; using OmniLinkBridge.OmniLink;
using OmniLinkBridge.WebAPI; using OmniLinkBridge.WebAPI;
using log4net;
using Newtonsoft.Json;
using System; using System;
using System.Reflection; using System.Reflection;
using System.ServiceModel; using System.ServiceModel;

134
README.md
View file

@ -21,68 +21,78 @@ You can use docker to build an image from git or download the [binary here](http
- Always sent for area alarms and critical system events - Always sent for area alarms and critical system events
- Optionally enable for area status changes and console messages - Optionally enable for area status changes and console messages
## Docker (preferred) ## Docker Hub (preferred)
1. Configure at a minimum the controller IP and encryptions keys. The web service port must be 8000.
```
mkdir /opt/omnilink-bridge
curl https://raw.githubusercontent.com/excaliburpartners/OmniLinkBridge/master/OmniLinkBridge/OmniLinkBridge.ini -o /opt/omnilink-bridge/OmniLinkBridge.ini
vim /opt/omnilink-bridge/OmniLinkBridge.ini
```
2. Start docker container
```
docker run -d --name="omnilink-bridge" -v /opt/omnilink-bridge:/config -v /etc/localtime:/etc/localtime:ro --net=host --restart always excaliburpartners/omnilink-bridge
```
3. Verify connectivity by looking at logs
```
docker logs omnilink-bridge
```
## Docker (for developers)
1. Clone git repo and build docker image 1. Clone git repo and build docker image
- git clone https://github.com/excaliburpartners/OmniLinkBridge.git ```
- cd OmniLinkBridge git clone https://github.com/excaliburpartners/OmniLinkBridge.git
- docker build --tag="omnilink-bridge" . cd OmniLinkBridge
docker build --tag="omnilink-bridge" .
```
2. Configure at a minimum the controller IP and encryptions keys. The web service port must be 8000 unless the Dockerfile is changed. 2. Configure at a minimum the controller IP and encryptions keys. The web service port must be 8000 unless the Dockerfile is changed.
- mkdir /opt/omnilink-bridge ```
- cp OmniLinkBridge/OmniLinkBridge.ini /opt/omnilink-bridge mkdir /opt/omnilink-bridge
- vim /opt/omnilink-bridge/OmniLinkBridge.ini cp OmniLinkBridge/OmniLinkBridge.ini /opt/omnilink-bridge
vim /opt/omnilink-bridge/OmniLinkBridge.ini
```
3. Start docker container 3. Start docker container
- docker run -d --name="omnilink-bridge" -v /opt/omnilink-bridge:/config -v /etc/localtime:/etc/localtime:ro --net=host --restart always omnilink-bridge ```
docker run -d --name="omnilink-bridge" -v /opt/omnilink-bridge:/config -v /etc/localtime:/etc/localtime:ro --net=host --restart always omnilink-bridge
```
4. Verify connectivity by looking at logs 4. Verify connectivity by looking at logs
- docker container logs omnilink-bridge ```
docker logs omnilink-bridge
```
## Installation Windows ## Installation Windows
1. Copy files to your desired location like C:\OmniLinkBridge 1. Copy files to your desired location like C:\OmniLinkBridge
2. Edit OmniLinkBridge.ini and define at a minimum the controller IP and encryptions keys 2. Edit OmniLinkBridge.ini and define at a minimum the controller IP and encryptions keys
3. Run OmniLinkBridge.exe to verify connectivity 3. Run OmniLinkBridge.exe to verify connectivity
4. Add Windows service 4. Add Windows service
- sc create OmniLinkBridge binpath=C:\OmniLinkBridge\OmniLinkBridge.exe ```
sc create OmniLinkBridge binpath=C:\OmniLinkBridge\OmniLinkBridge.exe
```
5. Start service 5. Start service
- net start OmniLinkBridge ```
net start OmniLinkBridge
```
## Installation Linux ## Installation Linux
1. Copy files to your desired location like /opt/OmniLinkBridge 1. Copy files to your desired location like /opt/OmniLinkBridge
2. Configure at a minimum the controller IP and encryptions keys 2. Configure at a minimum the controller IP and encryptions keys
- vim OmniLinkBridge.ini ```
vim OmniLinkBridge.ini
```
3. Run as interactive to verify connectivity 3. Run as interactive to verify connectivity
- mono OmniLinkBridge.exe -i ```
mono OmniLinkBridge.exe -i
```
4. Add systemd file and configure paths 4. Add systemd file and configure paths
- cp omnilinkbridge.service /etc/systemd/system/ ```
- vim /etc/systemd/system/omnilinkbridge.service cp omnilinkbridge.service /etc/systemd/system/
- systemctl daemon-reload vim /etc/systemd/system/omnilinkbridge.service
systemctl daemon-reload
```
5. Enable at boot and start service 5. Enable at boot and start service
- systemctl enable omnilinkbridge.service ```
- systemctl start omnilinkbridge.service systemctl enable omnilinkbridge.service
systemctl start omnilinkbridge.service
## MySQL Setup ```
You will want to install the MySQL Community Server, Workbench, and ODBC Connector. The Workbench software provides a graphical interface to administer the MySQL server. The OmniLink Bridge uses ODBC to communicate with the database. The MySQL ODBC Connector library is needed for Windows ODBC to communicate with MySQL.
http://dev.mysql.com/downloads/mysql/
http://dev.mysql.com/downloads/tools/workbench/
http://dev.mysql.com/downloads/connector/odbc/
At this point we need to open MySQL Workbench to create the database (called a schema in the Workbench GUI) for OmniLinkBridge to use.
1. After opening the program double-click on "Local instance MySQL" and enter the password you set in the wizard.
2. On the toolbar click the "Create a new schema" button, provide a name, and click apply.
3. On the left side right-click on the schema you created and click "Set as default schema".
4. In the middle section under Query1 click the open file icon and select the OmniLinkBridge.sql file.
5. Click the Execute lighting bolt to run the query, which will create the tables.
Lastly in OmniLinkBridge.ini set mysql_connection. This should get you up and running. The MySQL Workbench can also be used to view the data that OmniLink Bridge inserts into the tables.
mysql_connection = DRIVER={MySQL ODBC 8.0 Driver};SERVER=localhost;DATABASE=OmniLinkBridge;USER=root;PASSWORD=myPassword;OPTION=3;
## Web Service API
To test the API you can use your browser to view a page or PowerShell (see below) to change a value.
- http://localhost:8000/ListUnits
- http://localhost:8000/GetUnit?id=1
- Invoke-WebRequest -Uri "http://localhost:8000/SetUnit" -Method POST -ContentType "application/json" -Body (convertto-json -InputObject @{"id"=1;"value"=100}) -UseBasicParsing
## MQTT ## MQTT
This module will also publish discovery topics for Home Assistant to auto configure devices. This module will also publish discovery topics for Home Assistant to auto configure devices.
@ -173,8 +183,39 @@ PUB omnilink/buttonX/command
string ON string ON
``` ```
## Web Service API
To test the API you can use your browser to view a page or PowerShell (see below) to change a value.
- http://localhost:8000/ListUnits
- http://localhost:8000/GetUnit?id=1
```
Invoke-WebRequest -Uri "http://localhost:8000/SetUnit" -Method POST -ContentType "application/json" -Body (convertto-json -InputObject @{"id"=1;"value"=100}) -UseBasicParsing
```
## MySQL Setup
You will want to install the MySQL Community Server, Workbench, and ODBC Connector. The Workbench software provides a graphical interface to administer the MySQL server. The OmniLink Bridge uses ODBC to communicate with the database. The MySQL ODBC Connector library is needed for Windows ODBC to communicate with MySQL.
http://dev.mysql.com/downloads/mysql/
http://dev.mysql.com/downloads/tools/workbench/
http://dev.mysql.com/downloads/connector/odbc/
At this point we need to open MySQL Workbench to create the database (called a schema in the Workbench GUI) for OmniLinkBridge to use.
1. After opening the program double-click on "Local instance MySQL" and enter the password you set in the wizard.
2. On the toolbar click the "Create a new schema" button, provide a name, and click apply.
3. On the left side right-click on the schema you created and click "Set as default schema".
4. In the middle section under Query1 click the open file icon and select the OmniLinkBridge.sql file.
5. Click the Execute lighting bolt to run the query, which will create the tables.
Lastly in OmniLinkBridge.ini set mysql_connection. This should get you up and running. The MySQL Workbench can also be used to view the data that OmniLink Bridge inserts into the tables.
```
mysql_connection = DRIVER={MySQL ODBC 8.0 Driver};SERVER=localhost;DATABASE=OmniLinkBridge;USER=root;PASSWORD=myPassword;OPTION=3;
```
## Change Log ## Change Log
Version 1.1.4 - 2019-11-06 Version 1.1.4 - 2019-11-22
- Utilize controller temperature format - Utilize controller temperature format
- Don't publish invalid thermostat temperatures - Don't publish invalid thermostat temperatures
- Always enable first area to support Omni LTe and Omni IIe - Always enable first area to support Omni LTe and Omni IIe
@ -182,6 +223,7 @@ Version 1.1.4 - 2019-11-06
- Fix missing last area, zone, unit, thermostat, and button - Fix missing last area, zone, unit, thermostat, and button
- Fix compatibility with Home Assistant 0.95.4 MQTT extra keys - Fix compatibility with Home Assistant 0.95.4 MQTT extra keys
- Add Home Assistant MQTT device registry support - Add Home Assistant MQTT device registry support
- Add MQTT messages for controller disconnect and last will
Version 1.1.3 - 2019-02-10 Version 1.1.3 - 2019-02-10
- Publish config when reconnecting to MQTT - Publish config when reconnecting to MQTT