Skip to content

Commit

Permalink
Further split code
Browse files Browse the repository at this point in the history
  • Loading branch information
santam85 committed Jan 27, 2023
1 parent 09bd075 commit 784c59c
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 170 deletions.
84 changes: 84 additions & 0 deletions warema-bridge/srv/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import {stickUsb} from "./stick";

const mqtt = require("mqtt");
const log = require("./logger");
const {devices} = require("./register-device");

const mqttServer = process.env.MQTT_SERVER || 'mqtt://localhost'

var client = mqtt.connect(mqttServer,
{
username: process.env.MQTT_USER,
password: process.env.MQTT_PASSWORD,
will: {
topic: 'warema/bridge/state',
payload: 'offline',
retain: true
}
}
)

client.on('connect', function () {
log.info('Connected to MQTT')

client.subscribe([
'warema/+/set',
'warema/+/set_position',
'warema/+/set_tilt',
'homeassistant/status'
]);
})

client.on('error', function (error) {
log.error('MQTT Error: ' + error.toString())
})

client.on('message', function (topic, message) {
let [scope, device, command] = topic.split('/');
message = message.toString();

log.debug('Received message on topic')
log.debug('scope: ' + scope + ', device: ' + device + ', command: ' + command)
log.debug('message: ' + message)

if (scope === 'homeassistant' && command === 'status') {
if (message === 'online') {
log.info('Home Assistant is online');
}
return;
}

//scope === 'warema'
switch (command) {
case 'set':
switch (message) {
case 'CLOSE':
log.debug('Closing ' + device);
stickUsb.vnBlindSetPosition(device, 100)
client.publish('warema/' + device + '/state', 'closing');
break;
case 'OPEN':
log.debug('Opening ' + device);
stickUsb.vnBlindSetPosition(device, 0);
client.publish('warema/' + device + '/state', 'opening');
break;
case 'STOP':
log.debug('Stopping ' + device);
stickUsb.vnBlindStop(device);
break;
}
break;
case 'set_position':
log.debug('Setting ' + device + ' to ' + message + '%, angle ' + devices[device].angle);
stickUsb.vnBlindSetPosition(device, parseInt(message), parseInt(devices[device]['angle']))
break;
case 'set_tilt':
log.debug('Setting ' + device + ' to ' + message + '°, position ' + devices[device].position);
stickUsb.vnBlindSetPosition(device, parseInt(devices[device]['position']), parseInt(message))
break;
default:
log.info('Unrecognised command from HA')
}
});

export {client};
170 changes: 0 additions & 170 deletions warema-bridge/srv/index.js
Original file line number Diff line number Diff line change
@@ -1,173 +1,3 @@
const warema = require('./warema-wms-venetian-blinds');
const {registerDevice, devices} = require('./register-device');
const log = require('./logger');
const mqtt = require('mqtt');

process.on('SIGINT', function () {
process.exit(0);
});

const mqttServer = process.env.MQTT_SERVER || 'mqtt://localhost'
const forceDevices = process.env.FORCE_DEVICES ? process.env.FORCE_DEVICES.split(',') : [];
const pollingInterval = process.env.POLLING_INTERVAL || 30000;
const movingInterval = process.env.MOVING_INTERVAL || 1000;

const settingsPar = {
wmsChannel: process.env.WMS_CHANNEL || 17,
wmsKey: process.env.WMS_KEY || '00112233445566778899AABBCCDDEEFF',
wmsPanid: process.env.WMS_PAN_ID || 'FFFF',
wmsSerialPort: process.env.WMS_SERIAL_PORT || '/dev/ttyUSB0',
};

function callback(err, msg) {
if (err) {
log.error(err);
}
if (msg) {
switch (msg.topic) {
case 'wms-vb-init-completion':
log.info('Warema init completed')
client.publish('warema/bridge/state', 'online', {retain: true})

stickUsb.setPosUpdInterval(pollingInterval);
stickUsb.setWatchMovingBlindsInterval(movingInterval);

log.info('Scanning...')

stickUsb.scanDevices({autoAssignBlinds: false});
break;
case 'wms-vb-scanned-devices':
log.info('Scanned devices:\n' + JSON.stringify(msg.payload, null, 2));
if (forceDevices && forceDevices.length) {
forceDevices.forEach(deviceString => {
var [snr, type] = deviceString.split(':');

registerDevice({snr: snr, type: type || 25})
})
} else {
msg.payload.devices.forEach(element => registerDevice(element))
}
log.info('Registered devices:\n' + JSON.stringify(stickUsb.vnBlindsList(), null, 2))
break;
case 'wms-vb-rcv-weather-broadcast':
log.silly('Weather broadcast:\n' + JSON.stringify(msg.payload, null, 2))

if (!devices[msg.payload.weather.snr]) {
registerDevice({snr: msg.payload.weather.snr, type: 6});
}

client.publish('warema/' + msg.payload.weather.snr + '/illuminance/state', msg.payload.weather.lumen.toString())
client.publish('warema/' + msg.payload.weather.snr + '/temperature/state', msg.payload.weather.temp.toString())
client.publish('warema/' + msg.payload.weather.snr + '/wind/state', msg.payload.weather.wind.toString())
client.publish('warema/' + msg.payload.weather.snr + '/rain/state', msg.payload.weather.rain ? 'ON' : 'OFF')

break;
case 'wms-vb-blind-position-update':
log.debug('Position update: \n' + JSON.stringify(msg.payload, null, 2))

if (typeof msg.payload.position !== "undefined") {
devices[msg.payload.snr].position = msg.payload.position;
client.publish('warema/' + msg.payload.snr + '/position', '' + msg.payload.position)

if (msg.payload.moving === false) {
if (msg.payload.position === 0)
client.publish('warema/' + msg.payload.snr + '/state', 'open');
else if (msg.payload.position === 100)
client.publish('warema/' + msg.payload.snr + '/state', 'closed');
else
client.publish('warema/' + msg.payload.snr + '/state', 'stopped');
}
}
if (typeof msg.payload.tilt !== "undefined") {
devices[msg.payload.snr].tilt = msg.payload.tilt;
client.publish('warema/' + msg.payload.snr + '/tilt', '' + msg.payload.angle)
}
break;
default:
log.info('UNKNOWN MESSAGE: ' + JSON.stringify(msg, null, 2));
}
}
}

var client = mqtt.connect(mqttServer,
{
username: process.env.MQTT_USER,
password: process.env.MQTT_PASSWORD,
will: {
topic: 'warema/bridge/state',
payload: 'offline',
retain: true
}
}
)

client.on('connect', function () {
log.info('Connected to MQTT')

client.subscribe([
'warema/+/set',
'warema/+/set_position',
'warema/+/set_tilt',
'homeassistant/status'
]);
})

client.on('error', function (error) {
log.error('MQTT Error: ' + error.toString())
})

client.on('message', function (topic, message) {
let [scope, device, command] = topic.split('/');
message = message.toString();

log.debug('Received message on topic')
log.debug('scope: ' + scope + ', device: ' + device + ', command: ' + command)
log.debug('message: ' + message)

if (scope === 'homeassistant' && command === 'status') {
if (message === 'online') {
log.info('Home Assistant is online');
}
return;
}

//scope === 'warema'
switch (command) {
case 'set':
switch (message) {
case 'CLOSE':
log.debug('Closing ' + device);
stickUsb.vnBlindSetPosition(device, 100)
client.publish('warema/' + device + '/state', 'closing');
break;
case 'OPEN':
log.debug('Opening ' + device);
stickUsb.vnBlindSetPosition(device, 0);
client.publish('warema/' + device + '/state', 'opening');
break;
case 'STOP':
log.debug('Stopping ' + device);
stickUsb.vnBlindStop(device);
break;
}
break;
case 'set_position':
log.debug('Setting ' + device + ' to ' + message + '%, angle ' + devices[device].angle);
stickUsb.vnBlindSetPosition(device, parseInt(message), parseInt(devices[device]['angle']))
break;
case 'set_tilt':
log.debug('Setting ' + device + ' to ' + message + '°, position ' + devices[device].position);
stickUsb.vnBlindSetPosition(device, parseInt(devices[device]['position']), parseInt(message))
break;
default:
log.info('Unrecognised command from HA')
}
});

var stickUsb = new warema(settingsPar.wmsSerialPort,
settingsPar.wmsChannel,
settingsPar.wmsPanid,
settingsPar.wmsKey,
{},
callback
);
2 changes: 2 additions & 0 deletions warema-bridge/srv/register-device.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const log = require("./logger");
const {client} = require("./client");
const {stickUsb} = require("./stick");

var devices = [];

Expand Down
96 changes: 96 additions & 0 deletions warema-bridge/srv/stick.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const log = require("./logger");
const {registerDevice, devices} = require("./register-device");
const warema = require("./warema-wms-venetian-blinds");
const {client} = require("./client");

const forceDevices = process.env.FORCE_DEVICES ? process.env.FORCE_DEVICES.split(',') : [];
const pollingInterval = process.env.POLLING_INTERVAL || 30000;
const movingInterval = process.env.MOVING_INTERVAL || 1000;

const settingsPar = {
wmsChannel: process.env.WMS_CHANNEL || 17,
wmsKey: process.env.WMS_KEY || '00112233445566778899AABBCCDDEEFF',
wmsPanid: process.env.WMS_PAN_ID || 'FFFF',
wmsSerialPort: process.env.WMS_SERIAL_PORT || '/dev/ttyUSB0',
};

function callback(err, msg) {
if (err) {
log.error(err);
}
if (msg) {
switch (msg.topic) {
case 'wms-vb-init-completion':
log.info('Warema init completed')
client.publish('warema/bridge/state', 'online', {retain: true})

stickUsb.setPosUpdInterval(pollingInterval);
stickUsb.setWatchMovingBlindsInterval(movingInterval);

log.info('Scanning...')

stickUsb.scanDevices({autoAssignBlinds: false});
break;
case 'wms-vb-scanned-devices':
log.info('Scanned devices:\n' + JSON.stringify(msg.payload, null, 2));
if (forceDevices && forceDevices.length) {
forceDevices.forEach(deviceString => {
var [snr, type] = deviceString.split(':');

registerDevice({snr: snr, type: type || 25})
})
} else {
msg.payload.devices.forEach(element => registerDevice(element))
}
log.info('Registered devices:\n' + JSON.stringify(stickUsb.vnBlindsList(), null, 2))
break;
case 'wms-vb-rcv-weather-broadcast':
log.silly('Weather broadcast:\n' + JSON.stringify(msg.payload, null, 2))

if (!devices[msg.payload.weather.snr]) {
registerDevice({snr: msg.payload.weather.snr, type: 6});
}

client.publish('warema/' + msg.payload.weather.snr + '/illuminance/state', msg.payload.weather.lumen.toString())
client.publish('warema/' + msg.payload.weather.snr + '/temperature/state', msg.payload.weather.temp.toString())
client.publish('warema/' + msg.payload.weather.snr + '/wind/state', msg.payload.weather.wind.toString())
client.publish('warema/' + msg.payload.weather.snr + '/rain/state', msg.payload.weather.rain ? 'ON' : 'OFF')

break;
case 'wms-vb-blind-position-update':
log.debug('Position update: \n' + JSON.stringify(msg.payload, null, 2))

if (typeof msg.payload.position !== "undefined") {
devices[msg.payload.snr].position = msg.payload.position;
client.publish('warema/' + msg.payload.snr + '/position', '' + msg.payload.position)

if (msg.payload.moving === false) {
if (msg.payload.position === 0)
client.publish('warema/' + msg.payload.snr + '/state', 'open');
else if (msg.payload.position === 100)
client.publish('warema/' + msg.payload.snr + '/state', 'closed');
else
client.publish('warema/' + msg.payload.snr + '/state', 'stopped');
}
}
if (typeof msg.payload.tilt !== "undefined") {
devices[msg.payload.snr].tilt = msg.payload.tilt;
client.publish('warema/' + msg.payload.snr + '/tilt', '' + msg.payload.angle)
}
break;
default:
log.info('UNKNOWN MESSAGE: ' + JSON.stringify(msg, null, 2));
}
}
}


var stickUsb = new warema(settingsPar.wmsSerialPort,
settingsPar.wmsChannel,
settingsPar.wmsPanid,
settingsPar.wmsKey,
{},
callback
);

export {stickUsb};

0 comments on commit 784c59c

Please sign in to comment.