typescriptifying

This commit is contained in:
Wolfgang Hottgenroth
2018-01-09 16:36:14 +01:00
parent 9e39d74084
commit 8d85314fc9
21 changed files with 372 additions and 182 deletions

View File

@ -1,15 +0,0 @@
let AItem = require('./AItem')
class AHomematicItem extends AItem {
constructor(floor, room, item, hmId) {
super(floor, room, item);
this.hmId = hmId;
this.homegearTopicPre = 'homegear/instance1';
this.actionTopicPre = `${this.homegearTopicPre}/set/${this.hmId}`;
this.deviceTopicPre = `${this.homegearTopicPre}/plain/${this.hmId}`;
}
}
module.exports = AHomematicItem;

17
src/AHomematicItem.ts Normal file
View File

@ -0,0 +1,17 @@
import { AItem } from './AItem'
export abstract class AHomematicItem extends AItem {
protected deviceTopicPre: string;
protected actionTopicPre: string;
protected homegearTopicPre: string;
protected hmId: number;
constructor(floor: string, room: string, item: string, hmId: number) {
super(floor, room, item);
this.hmId = hmId;
this.homegearTopicPre = 'homegear/instance1';
this.actionTopicPre = `${this.homegearTopicPre}/set/${this.hmId}`;
this.deviceTopicPre = `${this.homegearTopicPre}/plain/${this.hmId}`;
}
}

View File

@ -1,23 +0,0 @@
let mqtt = require('./mqttHandler');
let logger = require('./log');
class AItem {
constructor(floor, room, item) {
this.floor = floor;
this.room = room;
this.item = item;
this.itemId = `${this.floor}.${this.room}.${this.item}`;
this.topicFirstPart = `dispatcher_ng/items/${this.floor}/${this.room}/${this.item}`;
}
start() {
mqtt.register(this.subscribeTopics, (topic, payload) => {
logger.info(`item ${this.itemId}: ${topic}, ${payload}`)
this.processMessage(topic, payload);
});
}
}
module.exports = AItem;

31
src/AItem.ts Normal file
View File

@ -0,0 +1,31 @@
import * as logger from './log'
import { mqttHandler } from './MqttDispatcher'
export abstract class AItem {
protected topicFirstPart: string;
private itemId: string;
private item: string;
private room: string;
private floor: string
protected subscribeTopics: string[]
constructor(floor: string, room: string, item: string) {
this.floor = floor
this.room = room
this.item = item
this.itemId = `${this.floor}.${this.room}.${this.item}`
this.topicFirstPart = `dispatcher_ng/items/${this.floor}/${this.room}/${this.item}`
}
abstract processMessage(topic: string, payload: string) : void
start() : void {
mqttHandler.register(this.subscribeTopics, (topic: string, payload: string) : void => {
logger.info(`item ${this.itemId}: ${topic}, ${payload}`)
this.processMessage(topic, payload)
})
}
}

View File

@ -1,51 +0,0 @@
let AItem = require('./AItem')
let logger = require('./log')
let mqtt = require('./mqttHandler');
class DimmerAdaptor extends AItem {
constructor(floor, room, item) {
super(floor, room, item);
this.actionStateTopic = `${this.topicFirstPart}/state`;
this.actionBrightTopic = `${this.topicFirstPart}/bright`;
this.inTopic = `${this.topicFirstPart}/dimmerIn`;
this.subscribeTopics = [ this.inTopic ];
this.state = 'OFF';
this.bright = 100;
this.brightDirection = -1;
}
processMessage(topic, payload) {
switch (topic) {
case this.inTopic:
switch (payload) {
case 'SHORT':
if (this.state == 'OFF') {
this.state = 'ON';
} else {
this.state = 'OFF';
}
mqtt.send(this.actionStateTopic, this.state, true);
break;
case 'LONG_HOLD':
this.bright += (5 * this.brightDirection);
if (this.bright > 100) {
this.bright = 100;
}
if (this.bright < 0) {
this.bright = 0;
}
mqtt.send(this.actionBrightTopic, this.bright, true);
break;
case 'LONG_END':
this.brightDirection = this.brightDirection * -1;
break;
}
break;
}
}
}
module.exports = DimmerAdaptor;

56
src/DimmerAdaptor.ts Normal file
View File

@ -0,0 +1,56 @@
import { AItem } from './AItem'
import * as logger from './log'
import { mqttHandler } from './MqttDispatcher'
export class DimmerAdaptor extends AItem {
private brightDirection: number
private bright: number
private state: string
private inTopic: string
private actionBrightTopic: string
private actionStateTopic: string
constructor(floor: string, room: string, item: string) {
super(floor, room, item)
this.actionStateTopic = `${this.topicFirstPart}/state`
this.actionBrightTopic = `${this.topicFirstPart}/bright`
this.inTopic = `${this.topicFirstPart}/dimmerIn`
this.subscribeTopics = [ this.inTopic ]
this.state = 'OFF'
this.bright = 100
this.brightDirection = -1
}
processMessage(topic: string, payload: string) {
switch (topic) {
case this.inTopic:
switch (payload) {
case 'SHORT':
if (this.state == 'OFF') {
this.state = 'ON'
} else {
this.state = 'OFF'
}
mqttHandler.send(this.actionStateTopic, this.state, true)
break
case 'LONG_HOLD':
this.bright += (5 * this.brightDirection)
if (this.bright > 100) {
this.bright = 100
}
if (this.bright < 0) {
this.bright = 0
}
mqttHandler.send(this.actionBrightTopic, this.bright.toString(), true)
break
case 'LONG_END':
this.brightDirection = this.brightDirection * -1
break
}
break
}
}
}

View File

@ -1,31 +0,0 @@
let logger = require('./log');
let mqtt = require('./mqttHandler');
class HomematicFourButtonSingleItem {
constructor(name, actionTopic) {
this.name = name;
this.actionTopic = actionTopic;
}
processMessage(topic, payload) {
switch(topic) {
case 'PRESS_SHORT':
mqtt.send(this.actionTopic, 'SHORT', true);
break;
case 'PRESS_LONG':
case 'PRESS_CONT':
mqtt.send(this.actionTopic, 'LONG_HOLD', true);
break;
case 'PRESS_LONG_RELEASE':
mqtt.send(this.actionTopic, 'LONG_END', true);
break;
default:
logger.warn(`HM4BSI: no handling available for ${topic}`);
break;
}
}
}
module.exports = HomematicFourButtonSingleItem;

View File

@ -1,31 +0,0 @@
let logger = require('./log');
let mqtt = require('./mqttHandler');
let AHomematicItem = require('./AHomematicItem')
class HomematicFourButtonThing extends AHomematicItem {
constructor(floor, room, item, hmId, itemObjs) {
super(floor, room, item, hmId);
this.itemObjs = itemObjs;
if (this.itemObjs.length != 4) {
throw new Error('itemObjs for HomematicFourButtonThing must have four elements');
}
this.subscribeTopics = [
`${this.deviceTopicPre}/#`
];
}
processMessage(topic, payload) {
logger.info(`HM4B: ${topic}, ${payload}`);
let buttonRelatedPart = topic.substring(this.deviceTopicPre.length+1);
let buttonIdx = parseInt(buttonRelatedPart.substring(0, buttonRelatedPart.indexOf('/')));
if (buttonIdx >= 1 && buttonIdx <= 4) {
this.itemObjs[buttonIdx-1].processMessage(buttonRelatedPart.substring(buttonRelatedPart.indexOf('/')+1), payload);
} else {
logger.warn(`HM4B: no handling available for ${topic}`);
}
}
}
module.exports = HomematicFourButtonThing;

View File

@ -0,0 +1,58 @@
import * as logger from './log'
import { mqttHandler } from './MqttDispatcher'
import { AHomematicItem } from './AHomematicItem'
export class HomematicFourButtonSingleItem {
private actionTopic: string
constructor(actionTopic: string) {
this.actionTopic = actionTopic
}
processMessage(topic: string, payload: string) {
switch(topic) {
case 'PRESS_SHORT':
mqttHandler.send(this.actionTopic, 'SHORT', true)
break
case 'PRESS_LONG':
case 'PRESS_CONT':
mqttHandler.send(this.actionTopic, 'LONG_HOLD', true)
break
case 'PRESS_LONG_RELEASE':
mqttHandler.send(this.actionTopic, 'LONG_END', true)
break
default:
logger.warn(`HM4BSI: no handling available for ${topic}`)
break
}
}
}
export class HomematicFourButtonThing extends AHomematicItem {
private itemObjs: HomematicFourButtonSingleItem[]
constructor(floor: string, room: string, item: string, hmId: number, itemObjs: HomematicFourButtonSingleItem[]) {
super(floor, room, item, hmId)
this.itemObjs = itemObjs
if (this.itemObjs.length != 4) {
throw new Error('itemObjs for HomematicFourButtonThing must have four elements')
}
this.subscribeTopics = [
`${this.deviceTopicPre}/#`
]
}
processMessage(topic: string, payload: string) {
logger.info(`HM4B: ${topic}, ${payload}`)
let buttonRelatedPart = topic.substring(this.deviceTopicPre.length+1)
let buttonIdx = parseInt(buttonRelatedPart.substring(0, buttonRelatedPart.indexOf('/')))
if (buttonIdx >= 1 && buttonIdx <= 4) {
this.itemObjs[buttonIdx-1].processMessage(buttonRelatedPart.substring(buttonRelatedPart.indexOf('/')+1), payload)
} else {
logger.warn(`HM4B: no handling available for ${topic}`)
}
}
}

40
src/M433SwitchItem.ts Normal file
View File

@ -0,0 +1,40 @@
import * as logger from './log'
import { mqttHandler } from './MqttDispatcher'
import { AItem } from './AItem'
export class M433SwitchItem extends AItem {
private offCode: string
private onCode: string
private oldState: string|undefined
private state: string
private actionTopic: string
private stateFeedbackTopic: string
private stateTopic: string
constructor(floor: string, room: string, item: string, onCode: string, offCode: string) {
super(floor, room, item)
this.stateTopic = `${this.topicFirstPart}/state`
this.subscribeTopics = [this.stateTopic]
this.stateFeedbackTopic = `${this.topicFirstPart}/state/feedback`
this.actionTopic = 'IoT/Mqtt433Gateway/Message'
this.state = 'OFF'
this.oldState = undefined
this.onCode = onCode
this.offCode = offCode
}
processMessage(topic: string, payload: string) {
this.state = payload;
mqttHandler.send(this.stateFeedbackTopic, this.state);
if (this.state != this.oldState) {
if (this.state == 'ON') {
mqttHandler.send(this.actionTopic, this.onCode);
} else {
mqttHandler.send(this.actionTopic, this.offCode);
}
this.oldState = this.state;
}
}
}

View File

@ -32,17 +32,19 @@ class MqttHandler {
}
this.topicHandlers = []
logger.info("MqttHandler constructed")
}
register(topics: string[], cb: TopicCallbackFunc) : void {
topics.forEach((topic) => {
this.topicHandlers.push({topic: topic, callback: cb})
logger.info(`additional callback registered for ${topic}`)
logger.info(`Callback registered for ${topic}`)
})
}
exec() : void {
logger.info(`connecting to ${this.mqttBrokerUrl}`)
logger.info(`Connecting to ${this.mqttBrokerUrl}`)
this.mqttClient = Mqtt.connect(this.mqttBrokerUrl, this.mqttOptions)
this.mqttClient.on('error', (err) => {
@ -55,12 +57,12 @@ class MqttHandler {
this.mqttClient.subscribe(topicHandler.topic)
logger.info(`${topicHandler.topic} subscribed`)
})
logger.info('mqtt connection established')
logger.info('MQTT connection established')
})
this.mqttClient.on('message', (topic: string, payload: Buffer, packet : Mqtt.IPublishPacket): void => {
if (! packet.retain) {
let payloadStr : string = payload.toString('UTF-8')
logger.info(`message received on topic ${topic}: ${payload}`)
logger.info(`Message received on topic ${topic}: ${payload}`)
this.processMessage(topic, payloadStr)
}
})

View File

@ -1,6 +1,5 @@
import * as fs from 'fs'
import * as cmdargs from 'command-line-args'
import * as logger from './log'
@ -15,5 +14,6 @@ export let dict : any
export function readConfig() {
let options = cmdargs(OPTION_DEFINITIONS)
dict = JSON.parse(fs.readFileSync(options.config, "utf8"))
logger.info(JSON.stringify(dict))
}
readConfig()

View File

@ -1,9 +0,0 @@
const SimpleNodeLogger = require('simple-node-logger');
let opts = {
logFilePath:'mylogfile.log',
timestampFormat:'YYYY-MM-DD HH:mm:ss.SSS'
};
let log = SimpleNodeLogger.createSimpleLogger(opts);
module.exports = log

View File

@ -1,34 +0,0 @@
let logger = require('./log');
let mqtt = require('./mqttHandler');
let AItem = require('./AItem');
class M433SwitchItem extends AItem {
constructor(floor, room, item, onCode, offCode) {
super(floor, room, item);
this.stateTopic = `${this.topicFirstPart}/state`;
this.subscribeTopics = [this.stateTopic];
this.stateFeedbackTopic = `${this.topicFirstPart}/state/feedback`;
this.actionTopic = 'IoT/Mqtt433Gateway/Message';
this.state = 'OFF';
this.oldState = undefined;
this.onCode = onCode;
this.offCode = offCode;
}
processMessage(topic, payload) {
this.state = payload;
mqtt.send(this.stateFeedbackTopic, this.state);
if (this.state != this.oldState) {
if (this.state == 'ON') {
mqtt.send(this.actionTopic, this.onCode);
} else {
mqtt.send(this.actionTopic, this.offCode);
}
this.oldState = this.state;
}
}
}
module.exports = M433SwitchItem;

View File

@ -1,28 +1,34 @@
import * as logger from './log'
import * as config from './config'
import * as logger from './log'
import { mqttHandler } from './MqttDispatcher'
// import { mqttHandler } from './MqttDispatcher'
import { M433SwitchItem } from './M433SwitchItem'
import { HomematicFourButtonThing, HomematicFourButtonSingleItem } from './HomematicFourButtonThing'
import { DimmerAdaptor } from './DimmerAdaptor'
config.readConfig()
logger.info("Dispatcher starting")
let aquariumLight = new M433SwitchItem('1st', 'Anna', 'AquariumLight', '14665044 24 1', '14665041 24 1');
aquariumLight.start();
let deskLight = new M433SwitchItem('Gnd', 'Hallway', 'DeskLight', '83221 24 1', '83220 24 1');
deskLight.start();
class Dispatcher {
constructor() {
logger.info("Dispatcher starting")
}
let testFourButton = new HomematicFourButtonThing('Gnd', 'Hallway', 'TestButton', 9, [
new HomematicFourButtonSingleItem('dispatcher_ng/items/Gnd/Hallway/Testlight/dimmerIn'),
new HomematicFourButtonSingleItem('dispatcher_ng/items/Gnd/Hallway/DeskLight/timerIn'),
new HomematicFourButtonSingleItem('test/button/3'),
new HomematicFourButtonSingleItem('test/button/4')
]);
testFourButton.start();
exec() : void {
logger.info("Hello world")
let testDimmerAdaptor = new DimmerAdaptor('Gnd', 'Hallway', 'Testlight');
testDimmerAdaptor.start();
// mqttHandler.exec()
}
}
const dispatcher = new Dispatcher()
dispatcher.exec()
mqttHandler.exec()
logger.info("Dispatcher running")