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

14
dist/AHomematicItem.js vendored Normal file
View File

@ -0,0 +1,14 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const AItem_1 = require("./AItem");
class AHomematicItem extends AItem_1.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}`;
}
}
exports.AHomematicItem = AHomematicItem;
//# sourceMappingURL=AHomematicItem.js.map

21
dist/AItem.js vendored Normal file
View File

@ -0,0 +1,21 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const logger = require("./log");
const MqttDispatcher_1 = require("./MqttDispatcher");
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() {
MqttDispatcher_1.mqttHandler.register(this.subscribeTopics, (topic, payload) => {
logger.info(`item ${this.itemId}: ${topic}, ${payload}`);
this.processMessage(topic, payload);
});
}
}
exports.AItem = AItem;
//# sourceMappingURL=AItem.js.map

48
dist/DimmerAdaptor.js vendored Normal file
View File

@ -0,0 +1,48 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const AItem_1 = require("./AItem");
const MqttDispatcher_1 = require("./MqttDispatcher");
class DimmerAdaptor extends AItem_1.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';
}
MqttDispatcher_1.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;
}
MqttDispatcher_1.mqttHandler.send(this.actionBrightTopic, this.bright.toString(), true);
break;
case 'LONG_END':
this.brightDirection = this.brightDirection * -1;
break;
}
break;
}
}
}
exports.DimmerAdaptor = DimmerAdaptor;
//# sourceMappingURL=DimmerAdaptor.js.map

53
dist/HomematicFourButtonThing.js vendored Normal file
View File

@ -0,0 +1,53 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const logger = require("./log");
const MqttDispatcher_1 = require("./MqttDispatcher");
const AHomematicItem_1 = require("./AHomematicItem");
class HomematicFourButtonSingleItem {
constructor(actionTopic) {
this.actionTopic = actionTopic;
}
processMessage(topic, payload) {
switch (topic) {
case 'PRESS_SHORT':
MqttDispatcher_1.mqttHandler.send(this.actionTopic, 'SHORT', true);
break;
case 'PRESS_LONG':
case 'PRESS_CONT':
MqttDispatcher_1.mqttHandler.send(this.actionTopic, 'LONG_HOLD', true);
break;
case 'PRESS_LONG_RELEASE':
MqttDispatcher_1.mqttHandler.send(this.actionTopic, 'LONG_END', true);
break;
default:
logger.warn(`HM4BSI: no handling available for ${topic}`);
break;
}
}
}
exports.HomematicFourButtonSingleItem = HomematicFourButtonSingleItem;
class HomematicFourButtonThing extends AHomematicItem_1.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}`);
}
}
}
exports.HomematicFourButtonThing = HomematicFourButtonThing;
//# sourceMappingURL=HomematicFourButtonThing.js.map

32
dist/M433SwitchItem.js vendored Normal file
View File

@ -0,0 +1,32 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const MqttDispatcher_1 = require("./MqttDispatcher");
const AItem_1 = require("./AItem");
class M433SwitchItem extends AItem_1.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;
MqttDispatcher_1.mqttHandler.send(this.stateFeedbackTopic, this.state);
if (this.state != this.oldState) {
if (this.state == 'ON') {
MqttDispatcher_1.mqttHandler.send(this.actionTopic, this.onCode);
}
else {
MqttDispatcher_1.mqttHandler.send(this.actionTopic, this.offCode);
}
this.oldState = this.state;
}
}
}
exports.M433SwitchItem = M433SwitchItem;
//# sourceMappingURL=M433SwitchItem.js.map

View File

@ -17,15 +17,16 @@ class MqttHandler {
this.mqttOptions.rejectUnauthorized = true;
}
this.topicHandlers = [];
logger.info("MqttHandler constructed");
}
register(topics, cb) {
topics.forEach((topic) => {
this.topicHandlers.push({ topic: topic, callback: cb });
logger.info(`additional callback registered for ${topic}`);
logger.info(`Callback registered for ${topic}`);
});
}
exec() {
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) => {
logger.error(`Error in mqttHandler: ${err}`);
@ -37,12 +38,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, payload, packet) => {
if (!packet.retain) {
let payloadStr = payload.toString('UTF-8');
logger.info(`message received on topic ${topic}: ${payload}`);
logger.info(`Message received on topic ${topic}: ${payload}`);
this.processMessage(topic, payloadStr);
}
});

3
dist/config.js vendored
View File

@ -2,7 +2,6 @@
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const cmdargs = require("command-line-args");
const logger = require("./log");
const OPTION_DEFINITIONS = [
{ name: 'verbose', alias: 'v', type: Boolean },
{ name: 'config', alias: 'c', type: String, defaultValue: '~/dispatcher_ng.conf' }
@ -10,7 +9,7 @@ const OPTION_DEFINITIONS = [
function readConfig() {
let options = cmdargs(OPTION_DEFINITIONS);
exports.dict = JSON.parse(fs.readFileSync(options.config, "utf8"));
logger.info(JSON.stringify(exports.dict));
}
exports.readConfig = readConfig;
readConfig();
//# sourceMappingURL=config.js.map

34
dist/main.js vendored
View File

@ -1,18 +1,24 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const logger = require("./log");
const config = require("./config");
// import { mqttHandler } from './MqttDispatcher'
config.readConfig();
class Dispatcher {
constructor() {
logger.info("Dispatcher starting");
}
exec() {
logger.info("Hello world");
// mqttHandler.exec()
}
}
const dispatcher = new Dispatcher();
dispatcher.exec();
const MqttDispatcher_1 = require("./MqttDispatcher");
const M433SwitchItem_1 = require("./M433SwitchItem");
const HomematicFourButtonThing_1 = require("./HomematicFourButtonThing");
const DimmerAdaptor_1 = require("./DimmerAdaptor");
logger.info("Dispatcher starting");
let aquariumLight = new M433SwitchItem_1.M433SwitchItem('1st', 'Anna', 'AquariumLight', '14665044 24 1', '14665041 24 1');
aquariumLight.start();
let deskLight = new M433SwitchItem_1.M433SwitchItem('Gnd', 'Hallway', 'DeskLight', '83221 24 1', '83220 24 1');
deskLight.start();
let testFourButton = new HomematicFourButtonThing_1.HomematicFourButtonThing('Gnd', 'Hallway', 'TestButton', 9, [
new HomematicFourButtonThing_1.HomematicFourButtonSingleItem('dispatcher_ng/items/Gnd/Hallway/Testlight/dimmerIn'),
new HomematicFourButtonThing_1.HomematicFourButtonSingleItem('dispatcher_ng/items/Gnd/Hallway/DeskLight/timerIn'),
new HomematicFourButtonThing_1.HomematicFourButtonSingleItem('test/button/3'),
new HomematicFourButtonThing_1.HomematicFourButtonSingleItem('test/button/4')
]);
testFourButton.start();
let testDimmerAdaptor = new DimmerAdaptor_1.DimmerAdaptor('Gnd', 'Hallway', 'Testlight');
testDimmerAdaptor.start();
MqttDispatcher_1.mqttHandler.exec();
logger.info("Dispatcher running");
//# sourceMappingURL=main.js.map