thing-it-device-enocean-ip
Version:
[thing-it-node] Device Plugin for EnOcean IP products.
159 lines (128 loc) • 4.55 kB
JavaScript
const moment = require('moment')
const express = require('express')
var bodyParser = require('body-parser');
const app = express()
app.use(bodyParser.json());
// 0180CAB7 Vorfuehrung-RWM F6-05-02
// 01A8E96D Vorfuehrung-FG F6-10-00
// 01A0355D Neu2Kanal D2-01-11/F6-03-01
// 019D6252 Neu1K D2-01-01/F6-03-01
// 050BF691 NeuJA D2-01-01/F6-03-01
// 01923B84 Vorfuehrung-FK D5-00-01
// 05065B85 SEnSE_0 F6-02-01
// 002FD579 Vorfuehrung-WS-1 F6-02-01
// 050BFAD9 Vorfuehrung-DIM-2 D2-01-03/F6-03-01
// 002FD5BB Vorfuehrung-WS-3-4 F6-02-01
// 002FD652 Vorfuehrung-WS-2-RJ F6-02-01
// EEP
//
// D2-01-11 Electronic Switches and Dimmers with Energy Management, Type 0x11
// D2-01-03 Electronic Switches and Dimmers with Energy Management, Type 0x11
// D2-01-01 Electronic Switches and Dimmers with Energy Management, Type 0x01
// D5-00-01 Single Input Contact
// F6-02-01 Rocker Switch 2 Rocker, Light and Blind Control - Application Style 1
// F6-03-01 Rocker Switch 4 Rocker, Light and Blind Control - Application Style 1
// F6-05-02 Liquid Leakage Sensor
// F6-10-00 Window Handle
const outlet = {
configuration: {deviceId: '01A0355D', friendlyId: 'Power Outlet Lounge'}, state: {switch: false},
setState: function (functions) {
console.log('Set state to', functions[0].value);
this.switch = functions[0].value === 'on';
writeState(this, functions);
}
};
const smartSwitch = {
configuration: {deviceId: '002FD652', friendlyId: 'Vorfuehrung-WS-2-RJ'}, state: {},
setState: function (functions) {
}
};
const motionSensor = {
configuration: {deviceId: '05065B85'},
state: {occupied: false, lastMotionTimestamp: null, ticksPerMinute: 0}
};
const thermokon = {
configuration: {deviceId: '5'},
state: { state: {functions: [{key: 'temperature', value: 22}, {key: 'setpoint', value: 22}]} },
}
const micropelt = {
configuration: {},
state: { valve: 1, batteryLow: false, actuatorObstructed: false},
setState(functions) {
console.log('Set setpoint to', functions[1].value);
thermokon.state.state.functions[1].value = functions[1].value;
writeState(thermokon, functions);
}
}
const temperatureSensor = {configuration: {deviceId: '4'}, state: {temperature: 20}};
const devices = {
'01A0355D': outlet,
'002FD652': smartSwitch,
'05065B85': motionSensor,
'4': temperatureSensor,
'5': thermokon,
'6': micropelt
};
let stream;
function writeState(device, functions) {
stream.write(JSON.stringify({telegram: {deviceId: device.configuration.deviceId, friendlyId: device.configuration.friendlyId,
functions: functions
}}) + '\r\n\r\n');
}
// Returns the state of the Gateway
app.get('/', (req, res) => {
res.send({
header : {
httpStatus : 200,
content : "devices",
gateway : "DC-GW/EO-IP v0.99.43",
timestamp : "2001-01-27T17:59:33.958+0100"
},
devices : []
});
});
// Returns the state of EnOcean Device
app.get('/devices/:device/state', (req, res) => {
res.send(devices[req.params.device].state);
});
app.put('/devices/:device/state', (req, res) => {
if (!devices[req.params.device]) {
res.status(500).send('Device [' + req.params.device + '] does not exist.');
}
devices[req.params.device].setState(req.body.state.functions);
});
app.get('/devices/stream', (req, res) => {
console.log('Device Stream initialized.');
res.setHeader('Content-Type', 'text/plain; charset=UTF-8');
res.setHeader('Transfer-Encoding', 'chunked');
stream = res;
var toggleSwitch = false;
setTimeout(function() {
const device = devices['002FD652']
writeState(device,[ {
key: 'buttonA0',
channel: 0,
value: toggleSwitch ? 'pressed' : 'released'
}, {
key: 'buttonB0',
channel: 1,
value: toggleSwitch ? 'released' : 'pressed'
} ]);
}, 5000);
var lastState = false;
setInterval(function() {
const device = devices['05065B85'];
const state = Math.random() > 0.2 ? 'true' : 'false'; // 80% probability
if (state !== lastState) {
lastState = state;
console.log('Write state', state);
writeState(device, [{
key: 'motionDetected',
channel: 0,
value: state
}]);
}
}, 2000);
});
app.listen(3335, () => console.log('Enocean Gateway Simulator listening on port 3335.')
);