Skip to content

Commit

Permalink
Version 1.3 Stabilisierung, falls mehrere Befehle gleichzeitig ankommen
Browse files Browse the repository at this point in the history
  • Loading branch information
Ansgar Schulte committed Sep 27, 2018
1 parent 9dfff04 commit 997a9c1
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 25 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
Ziel dieses Projekt ist es meine HeyTech Rolladen Steuerung in OpenHab2 oder anderen SmartHome Software Lösungen zu integrieren.

## Version

* Version 1.3
* Stabilisierung, falls mehrere Befehle gleichzeitig ankommen
* Version 1.2
* Endpunkte für Klimadaten und Rolladen Öffnungsstatus in Prozent
* Version 1.1
Expand Down
4 changes: 2 additions & 2 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@
"11",
"7",
"8",
"14",
"12",
"13",
"14"
"13"
],
"/rolladengruppe/alleausserkinderzimmerundwohnzimmer/": [
"9",
Expand Down
48 changes: 36 additions & 12 deletions heytech/routes/routes.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
'use strict';





module.exports = function (app) {
const heytech = require('../service/heytech');
const heytechReader = require('../service/heytechReader');


const checkHeytechNotConnected = (callback) => {
if(heytech.isConnected() === true || heytechReader.isConnected()) {
console.log('heytech ist noch connected');
setTimeout(checkHeytechNotConnected.bind(this, callback), 500); /* this checks the flag every 500 milliseconds*/
} else {
callback();
}
};

const config = require('config');

const rolladenConfig = config.get('Heytech.rolladen');
Expand All @@ -15,36 +29,46 @@ module.exports = function (app) {
if (typeof routesConfig === "string") {
const fenster = routesConfig;
app.get(route + ':command', function (req, res) {
heytech.rollershutter(fenster, req.params.command);
res.send('OK');
checkHeytechNotConnected(()=> {
heytech.rollershutter(fenster, req.params.command);
res.send('OK');
});
});
} else if (routesConfig instanceof Array) {
const fensters = routesConfig;
app.get(route + ':command', function (req, res) {
heytech.rollershutters(fensters, req.params.command);
res.send('OK');
checkHeytechNotConnected(()=> {
heytech.rollershutters(fensters, req.params.command);
res.send('OK');
});
});
} else if (routesConfig instanceof Object) {
const fenstersConfig = routesConfig;
app.get(route + ':command', function (req, res) {
heytech.rollershuttersWithTimeout(fenstersConfig.rolladen, fenstersConfig.time, req.params.command);
res.send('OK');
checkHeytechNotConnected(()=> {
heytech.rollershuttersWithTimeout(fenstersConfig.rolladen, fenstersConfig.time, req.params.command);
res.send('OK');
});
});
}
});

console.log('/heytech/klima');
app.get('/heytech/klima', function(req, res) {
heytechReader.klima().then(data => {
res.send(data);
})
checkHeytechNotConnected(()=> {
heytechReader.klima().then(data => {
res.send(data);
})
});
});

console.log('/heytech/oeffnungsprozent');
app.get('/heytech/oeffnungsprozent', function(req, res) {
heytechReader.oeffnungsProzent().then(data => {
res.send(data);
})
checkHeytechNotConnected(()=> {
heytechReader.oeffnungsProzent().then(data => {
res.send(data);
})
});
});

};
28 changes: 22 additions & 6 deletions heytech/service/heytech.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,29 @@ import config from 'config';
const newLine = String.fromCharCode(13);
const serverConfig = config.get('Heytech.lan');

let connectedHeytech = false;
const doTelNetStuff = (telnetCommands) => {


const client = Telnet.client(serverConfig.host + ':' + serverConfig.port);
let connected = false;


client.filter((event) => event instanceof Telnet.Event.Connected)
.subscribe(() => {
connected = true;
telnetCommands(client);
client.disconnect();
connectedHeytech = false;
});
client.connect();
connectedHeytech = true;
};

const freeze = (time) => {
const stop = new Date().getTime() + time;
while(new Date().getTime() < stop);
};
const doCommandForFenster = function (client, fenster, commandStr, pin) {

const doCommandForFenster = function (client, fenster, commandStr, pin, withFreeze = false) {
if(!_.isEmpty(pin)){
console.log('with pin');
client.send('rsc');
Expand All @@ -38,6 +46,10 @@ const doCommandForFenster = function (client, fenster, commandStr, pin) {
client.send(commandStr === 'stop' ? 'off' : commandStr);
client.send(newLine);
client.send(newLine);

if(withFreeze){
freeze(100);
}
};
export const rollershutter = (fenster, commandStr) => {
doTelNetStuff((client) => {
Expand All @@ -48,22 +60,26 @@ export const rollershutter = (fenster, commandStr) => {
export const rollershutters = (fensters, commandStr) => {
doTelNetStuff((client) => {
fensters.forEach(fenster => {
doCommandForFenster(client, fenster, commandStr, serverConfig.pin);
doCommandForFenster(client, fenster, commandStr, serverConfig.pin, fensters.length > 2);
});
});
};

export const rollershuttersWithTimeout = (fensters, downTime, commandStr) => {
doTelNetStuff((client) => {
fensters.forEach(fenster => {
doCommandForFenster(client, fenster, commandStr, serverConfig.pin);
doCommandForFenster(client, fenster, commandStr, serverConfig.pin, fensters.length > 2);
});
});
_.delay(() => {
doTelNetStuff((client) => {
fensters.forEach(fenster => {
doCommandForFenster(client, fenster, 'off', serverConfig.pin);
doCommandForFenster(client, fenster, 'off', serverConfig.pin, fensters.length > 2);
});
});
}, downTime)
};

export const isConnected = () => {
return connectedHeytech;
};
15 changes: 11 additions & 4 deletions heytech/service/heytechReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ const ENDE_SOP = 'ende_sop';
const START_SKD = 'start_skd';
const ENDE_SKD = 'ende_skd';

let connectedHeytechReader = false;

const doTelNetStuff = (telnetCommands) => {

return new Promise((resolve, reject) => {
const client = Telnet.client(serverConfig.host + ':' + serverConfig.port);
let connected = false;


lastStrings = '';

client.filter((event) => event instanceof Telnet.Event.Connected)
.subscribe(() => {
connected = true;
telnetCommands(client);
});

Expand All @@ -44,6 +45,7 @@ const doTelNetStuff = (telnetCommands) => {
// console.log(rolladenStatus);
lastStrings = '';
client.disconnect();
connectedHeytechReader = false;
resolve(rolladenStatus);
} else if (lastStrings.indexOf(START_SKD) >= 0 && lastStrings.indexOf(ENDE_SKD) >= 0) {
// Klima-Daten
Expand All @@ -56,10 +58,11 @@ const doTelNetStuff = (telnetCommands) => {
// console.log(lastStrings);
lastStrings = '';
client.disconnect();
connectedHeytechReader = false;
resolve(klimadaten);
}
});

connectedHeytechReader = true;
client.connect();
});
};
Expand Down Expand Up @@ -88,4 +91,8 @@ export async function oeffnungsProzent() {
return await doTelNetStuff((client) => {
doCommand(client, 'sop', serverConfig.pin);
});
}
}

export const isConnected = () => {
return connectedHeytechReader;
};

0 comments on commit 997a9c1

Please sign in to comment.