Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rsyslog refactor #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
.DS_Store
.wink-mqttrc
.node-version
69 changes: 51 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,66 @@
# wink-mqtt
Enable local-control of Rooted Wink Hub running 2.66 firmware with MQTT. Integrates nicely with Home Assistant MQTT lights, switches, sensors, etc. https://home-assistant.io/

The Wink Hub version 2.49 firmware includes NodeJS, but not npm. You will need to run "npm install" on a machine with npm and then copy the files to the Wink with SSH. i.e.
Enable local-control of Rooted Wink Hub running 3.x firmware with MQTT. Integrates nicely with Home Assistant MQTT lights, switches, sensors, etc. https://home-assistant.io/

scp -r ~/wink-mqtt/ root@[wink hub ip address]:/opt/wink-mqtt/

To have wink-mqtt.js application run at start up copy the "mqtt" start up script to /etc/rc.d/init.d/mqtt
## Installation

cp /opt/wink-mqtt/mqtt /etc/rc.d/init.d/mqtt
chmod 755 /etc/rc.d/init.d/mqtt

You will want to also use monit to monitor if the wink-mqtt process is running and restart if it crashes.
The Wink Hub firmware includes NodeJS, but not npm. You will need to do *one* of the following:
* run `npm install` on a machine with npm and then copy the files to the Wink with SSH:

cat /opt/wink-mqtt/monit-mqtt >> /etc/monitrc
```
$ git clone https://github.com/danielolson13/wink-mqtt.git
$ cd wink-mqtt
$ npm install
$ scp -r . root@<wink-hub-ip-address>:/opt/wink-mqtt
```

##How do I root the Wink Hub?
* install from a tarball:

```
[root@flex-dvt ~]# curl -skL https://github.com/danielolson13/wink-mqtt/releases/download/v0.1.0/wink-mqtt-0.1.0.tgz | tar -zxvf - -C /opt
[root@flex-dvt ~]# mv /opt/package /opt/wink-mqtt
```

To have wink-mqtt run at start up, run the installer:

```
[root@flex-dvt ~]# /opt/wink-mqtt/install.sh
```

## Configuration
wink-mqtt looks for a `/opt/wink-mqtt/.wink-mqttrc` config file, which is auto-generated by `install.sh`. It may export the following environment variables (default values shown):

```bash
# The MQTT server URL
export MQTT_SERVER="mqtt://localhost"
# A topic prefix for all wink hub mqtt devices
export WINK_MQTT_TOPIC_BASE="home"
# The full path to `aprontest` command
export WINK_MQTT_APRON_BIN="/usr/sbin/aprontest"
# The number of milliseconds to wait for `aprontest` to complete
export WINK_MQTT_APRON_TIMEOUT="10000"
# The delay between calls to `aprontest` when syncing all devices
export WINK_MQTT_DELAY="100"
# A debounce delay to prevent multiple `aprontest` queries per device
export WINK_MQTT_DEBOUNCE="1000"
```

## How do I root the Wink Hub?
Matt Carrier has a good article on rooting and getting SSH access to the Wink Hub with the UART method https://mattcarrier.com/post/hacking-the-winkhub-part-1/
https://www.exploitee.rs/index.php/Wink_Hub%E2%80%8B%E2%80%8B

##How wink-mqtt works
wink-mqttt uses the "aprontest" binary on the Wink Hub to communicate with the Z-Wave, Zigbee and Lutron radios. Each paired device is given a MasterID. wink-hub subscribes to an MQTT server for 'set' events. Topics are organized by 'home/[MASTERID]/[ATTRIBUTE]/set'
## How wink-mqtt works
wink-mqtt uses the `aprontest` binary on the Wink Hub to communicate with the Z-Wave, Zigbee and Lutron radios. Each paired device is given a MasterID. wink-hub subscribes to an MQTT server for `set` events. Topics are organized by `home/<masterid>/<attribute>/set`

```
$ mosquitto_pub -t 'home/20/3/set' -v 'TRUE'
```

mosquitto_pub -t 'home/20/3/set' -v 'TRUE'

To view MasterID and attribues you can run ```aprontest -l``` then ```aprontest -l -m20```
To view MasterID and attributes you can run `aprontest -l` then `aprontest -l -m 20`.

wink-mqtt tails the log file(there is probably a much better way) for state change messages and then querys the sqllite3 database "apron.db" for the current values of the devices, those updates are then publised back to the MQTT server 'home/MASTERID/ATTRIBUTE' with their current value.
wink-mqtt receives device status changes from rsyslogd and then queries the apron database via `aprontest` for the current values of the devices. Those updates are then published back to the MQTT topic `home/<masterid>/<attribute>` with their current values.

##Why the Wink Hub?
## Why the Wink Hub?
It's cheap, rootable and has Z-Wave, Zigbee, Lutron, Wifi and Bluetooth radios.

Please feel free to contribute, this is working but by no means a perfect solution.
27 changes: 27 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

set -e

BASE="/opt/wink-mqtt"
RC="$BASE/.wink-mqttrc"
CONF="$BASE/wink-mqtt.conf"

# Enable rsyslog.d
grep -q '$IncludeConfig /etc/rsyslog.d/' /etc/rsyslog.conf || echo '$IncludeConfig /etc/rsyslog.d/' >> /etc/rsyslog.conf

# Link wink-mqtt rsyslog config
mkdir -p /etc/rsyslog.d
ln -sf "$CONF" /etc/rsyslog.d/

# Refine monit local-control process matching
sed -i 's/matching "node"/matching "node.*local_control"/' /etc/monitrc

# Ensure at least the server url is set in config
[ -f "$RC" ] && grep -q 'export MQTT_SERVER' "$RC" || {
echo -n 'Enter mqtt Server url: '
read MQTT_SERVER
echo "export MQTT_SERVER=\"$MQTT_SERVER\"" >> "$RC"
}

# Restart rsyslogd
monit restart rsyslogd
4 changes: 0 additions & 4 deletions monit-mqtt

This file was deleted.

20 changes: 0 additions & 20 deletions mqtt

This file was deleted.

25 changes: 10 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
{
"name": "wink-mqtt",
"version": "0.0.1",
"description": "Node.js example illustrating how to write a RESTful API using the node-restify library.",
"version": "0.1.0",
"description": "Wink Hub MQTT bridge",
"dependencies": {
"always-tail": "^0.2.0",
"forever": "^0.15.1",
"mqtt": "^1.6.3"
"mqtt": "^1.14.1"
},
"engine": "node 0.6.x",
"main": "mqtt.js",
"devDependencies": {},
"bundledDependencies": ["mqtt"],
"engines": {
"node": ">=0.10.45"
},
"main": "wink-mqtt.js",
"repository": {
"type": "git",
"url": "git+https://github.com/danielolson13/wink-mqtt.git"
},
"keywords": [
"MQTT",
"Wink",
"Hub",
"2.19",
"local",
"control",
"nodejs"
"Wink"
],
"author": "Dan Olson",
"license": "MIT",
"bugs": {
"url": "https://github.com/danielolson13/wink-mqtt/issues"
},
"homepage": "https://github.com/danielolson13/wink-mqtt#readme"
"homepage": "https://github.com/danielolson13/wink-mqtt"
}
5 changes: 5 additions & 0 deletions wink-mqtt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module(load="omprog")
# Match device state changes and rsyslogd messages (so wink-mqtt will launch immediately).
if ($programname == 'hub' and $msg contains 'device monitor: state changed in device') or $programname == 'rsyslogd' then
# Rsyslog will start, restart and stop wink-mqtt, feeding it matching log lines via stdin.
action(type="omprog" binary="/opt/wink-mqtt/wink-mqtt.sh")
Loading