typescriptifying completed

This commit is contained in:
Wolfgang Hottgenroth
2018-01-09 17:40:11 +01:00
parent 8d85314fc9
commit 3a8dd1b577
13 changed files with 315 additions and 127 deletions

View File

@ -1,25 +1,23 @@
let AItem = require('./AItem')
let logger = require('./log')
let mqtt = require('./mqttHandler');
class Forwarder extends AItem {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const AItem_1 = require("./AItem");
const MqttDispatcher_1 = require("./MqttDispatcher");
class Forwarder extends AItem_1.AItem {
constructor(floor, room, item, topicLastPart, targetTopics) {
super(floor, room, item);
this.inTopic = `${this.topicFirstPart}/${topicLastPart}`;
this.subscribeTopics = [ this.inTopic ];
this.subscribeTopics = [this.inTopic];
this.targetTopics = targetTopics;
}
processMessage(topic, payload) {
switch (topic) {
case this.inTopic:
this.targetTopics.forEach((targetTopic) => {
mqtt.send(targetTopic, payload, true);
MqttDispatcher_1.mqttHandler.send(targetTopic, payload, true);
});
break;
}
}
}
module.exports = Forwarder;
exports.Forwarder = Forwarder;
//# sourceMappingURL=Forwarder.js.map

View File

@ -1,9 +1,9 @@
let logger = require('./log');
let mqtt = require('./mqttHandler');
let AHomematicItem = require('./AHomematicItem')
class HomematicDimmerItem extends AHomematicItem {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const logger = require("./log");
const MqttDispatcher_1 = require("./MqttDispatcher");
const AHomematicItem_1 = require("./AHomematicItem");
class HomematicDimmerItem extends AHomematicItem_1.AHomematicItem {
constructor(floor, room, item, hmId) {
super(floor, room, item, hmId);
this.stateTopic = `${this.topicFirstPart}/state`;
@ -26,29 +26,28 @@ class HomematicDimmerItem extends AHomematicItem {
this.bright = 0.0;
this.oldBright = undefined;
}
dimmerAction() {
if ((this.state != this.oldState) || (this.bright != this.oldBright)) {
if ((this.state != this.oldState) || (this.bright != this.oldBright)) {
if (this.state == 'ON') {
mqtt.send(this.actionTopic, `${this.bright / 100.0}`);
} else {
mqtt.send(this.actionTopic, '0');
MqttDispatcher_1.mqttHandler.send(this.actionTopic, `${this.bright / 100.0}`);
}
else {
MqttDispatcher_1.mqttHandler.send(this.actionTopic, '0');
}
this.oldState = this.state;
this.oldBright = this.bright;
}
}
processMessage(topic, payload) {
switch (topic) {
case this.stateTopic:
this.state = payload;
mqtt.send(this.stateFeedbackTopic, this.state);
MqttDispatcher_1.mqttHandler.send(this.stateFeedbackTopic, this.state);
this.dimmerAction();
break;
case this.brightTopic:
this.bright = parseFloat(payload);
mqtt.send(this.brightFeedbackTopic, `${this.bright}`);
MqttDispatcher_1.mqttHandler.send(this.brightFeedbackTopic, `${this.bright}`);
this.dimmerAction();
break;
case this.errorOverheatTopic:
@ -61,6 +60,5 @@ class HomematicDimmerItem extends AHomematicItem {
}
}
}
module.exports = HomematicDimmerItem;
exports.HomematicDimmerItem = HomematicDimmerItem;
//# sourceMappingURL=HomematicDimmerItem.js.map

View File

@ -1,9 +1,8 @@
let logger = require('./log');
let mqtt = require('./mqttHandler');
let AHomematicItem = require('./AHomematicItem')
class HomematicSwitchItem extends AHomematicItem {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const MqttDispatcher_1 = require("./MqttDispatcher");
const AHomematicItem_1 = require("./AHomematicItem");
class HomematicSwitchItem extends AHomematicItem_1.AHomematicItem {
constructor(floor, room, item, hmId) {
super(floor, room, item, hmId);
this.stateTopic = `${this.topicFirstPart}/state`;
@ -17,18 +16,17 @@ class HomematicSwitchItem extends AHomematicItem {
this.state = 'OFF';
this.oldState = undefined;
}
processMessage(topic, payload) {
switch (topic) {
case this.stateTopic:
this.state = payload;
mqtt.send(this.stateFeedbackTopic, this.state);
if (this.state != this.oldState) {
MqttDispatcher_1.mqttHandler.send(this.stateFeedbackTopic, this.state);
if (this.state != this.oldState) {
if (this.state == 'ON') {
mqtt.send(this.actionTopic, 'true');
} else {
mqtt.send(this.actionTopic, 'false');
MqttDispatcher_1.mqttHandler.send(this.actionTopic, 'true');
}
else {
MqttDispatcher_1.mqttHandler.send(this.actionTopic, 'false');
}
this.oldState = this.state;
}
@ -36,15 +34,15 @@ class HomematicSwitchItem extends AHomematicItem {
case this.deviceFeedbackTopic:
if (payload == 'true') {
this.state = 'ON';
} else {
}
else {
this.state = 'OFF';
}
this.oldState = this.state;
mqtt.send(this.stateFeedbackTopic, this.state);
MqttDispatcher_1.mqttHandler.send(this.stateFeedbackTopic, this.state);
break;
}
}
}
module.exports = HomematicSwitchItem;
exports.HomematicSwitchItem = HomematicSwitchItem;
//# sourceMappingURL=HomematicSwitchItem.js.map

View File

@ -1,19 +1,18 @@
let AItem = require('./AItem')
let logger = require('./log')
let mqtt = require('./mqttHandler');
class TimerAdaptor extends AItem {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const AItem_1 = require("./AItem");
const logger = require("./log");
const MqttDispatcher_1 = require("./MqttDispatcher");
class TimerAdaptor extends AItem_1.AItem {
constructor(floor, room, item, delay) {
super(floor, room, item);
this.actionStateTopic = `${this.topicFirstPart}/state`;
this.inTopic = `${this.topicFirstPart}/timerIn`;
this.subscribeTopics = [ this.inTopic ];
this.subscribeTopics = [this.inTopic];
this.state = 'OFF';
this.delay = delay;
this.timer = null;
}
processMessage(topic, payload) {
switch (topic) {
case this.inTopic:
@ -23,28 +22,29 @@ class TimerAdaptor extends AItem {
clearTimeout(this.timer);
}
this.state = 'ON';
mqtt.send(this.actionStateTopic, this.state, true);
MqttDispatcher_1.mqttHandler.send(this.actionStateTopic, this.state, true);
this.timer = setTimeout(() => {
logger.info(`timer ${this.itemId} elapsed`);
this.state = 'OFF';
mqtt.send(this.actionStateTopic, this.state, true);
MqttDispatcher_1.mqttHandler.send(this.actionStateTopic, this.state, true);
}, (this.delay * 1000));
break;
case 'LONG_END':
clearTimeout(this.timer);
if (this.timer != null) {
clearTimeout(this.timer);
}
if (this.state == 'OFF') {
this.state = 'ON';
} else {
}
else {
this.state = 'OFF';
}
mqtt.send(this.actionStateTopic, this.state, true);
MqttDispatcher_1.mqttHandler.send(this.actionStateTopic, this.state, true);
break;
}
}
break;
}
}
}
module.exports = TimerAdaptor;
exports.TimerAdaptor = TimerAdaptor;
//# sourceMappingURL=TimerAdaptor.js.map

15
dist/main.js vendored
View File

@ -5,6 +5,10 @@ const MqttDispatcher_1 = require("./MqttDispatcher");
const M433SwitchItem_1 = require("./M433SwitchItem");
const HomematicFourButtonThing_1 = require("./HomematicFourButtonThing");
const DimmerAdaptor_1 = require("./DimmerAdaptor");
const TimerAdaptor_1 = require("./TimerAdaptor");
const HomematicDimmerItem_1 = require("./HomematicDimmerItem");
const HomematicSwitchItem_1 = require("./HomematicSwitchItem");
const Forwarder_1 = require("./Forwarder");
logger.info("Dispatcher starting");
let aquariumLight = new M433SwitchItem_1.M433SwitchItem('1st', 'Anna', 'AquariumLight', '14665044 24 1', '14665041 24 1');
aquariumLight.start();
@ -19,6 +23,17 @@ let testFourButton = new HomematicFourButtonThing_1.HomematicFourButtonThing('Gn
testFourButton.start();
let testDimmerAdaptor = new DimmerAdaptor_1.DimmerAdaptor('Gnd', 'Hallway', 'Testlight');
testDimmerAdaptor.start();
let testTimerAdaptor = new TimerAdaptor_1.TimerAdaptor('Gnd', 'Hallway', 'DeskLight', 10);
testTimerAdaptor.start();
let testLight = new HomematicDimmerItem_1.HomematicDimmerItem('Gnd', 'Hallway', 'Testlight', 8);
testLight.start();
let testLight2 = new HomematicSwitchItem_1.HomematicSwitchItem('Gnd', 'Hallway', 'Testlight2', 5);
testLight2.start();
let testForwarder = new Forwarder_1.Forwarder('Gnd', 'Hallway', 'TestForwarder', 'state', [
'dispatcher_ng/items/Gnd/Hallway/Testlight2/state',
'dispatcher_ng/items/Gnd/Hallway/DeskLight/state'
]);
testForwarder.start();
MqttDispatcher_1.mqttHandler.exec();
logger.info("Dispatcher running");
//# sourceMappingURL=main.js.map

View File

@ -4,7 +4,7 @@ import { mqttHandler } from './MqttDispatcher'
export abstract class AItem {
protected topicFirstPart: string;
private itemId: string;
protected itemId: string;
private item: string;
private room: string;
private floor: string

View File

@ -22,7 +22,7 @@ export class DimmerAdaptor extends AItem {
this.brightDirection = -1
}
processMessage(topic: string, payload: string) {
processMessage(topic: string, payload: string) : void {
switch (topic) {
case this.inTopic:
switch (payload) {

26
src/Forwarder.ts Normal file
View File

@ -0,0 +1,26 @@
import { AItem } from './AItem'
import * as logger from './log'
import { mqttHandler } from './MqttDispatcher'
export class Forwarder extends AItem {
private targetTopics: string[]
private inTopic: string
constructor(floor: string, room: string, item: string, topicLastPart: string, targetTopics: string[]) {
super(floor, room, item)
this.inTopic = `${this.topicFirstPart}/${topicLastPart}`
this.subscribeTopics = [ this.inTopic ]
this.targetTopics = targetTopics
}
processMessage(topic: string, payload: string) : void {
switch (topic) {
case this.inTopic:
this.targetTopics.forEach((targetTopic) => {
mqttHandler.send(targetTopic, payload, true)
})
break
}
}
}

View File

@ -0,0 +1,76 @@
import * as logger from './log'
import { mqttHandler } from './MqttDispatcher'
import { AHomematicItem } from './AHomematicItem'
export class HomematicDimmerItem extends AHomematicItem {
private oldBright: number|undefined
private bright: number
private oldState: string|undefined
private state: string
private errorReducedTopic: string
private errorOverloadTopic: string
private errorOverheatTopic: string
private actionTopic: string
private brightFeedbackTopic: string
private stateFeedbackTopic: string
private brightTopic: string
private stateTopic: string
constructor(floor: string, room: string, item: string, hmId: number) {
super(floor, room, item, hmId)
this.stateTopic = `${this.topicFirstPart}/state`
this.brightTopic = `${this.topicFirstPart}/bright`
this.stateFeedbackTopic = `${this.topicFirstPart}/state/feedback`
this.brightFeedbackTopic = `${this.topicFirstPart}/bright/feedback`
this.actionTopic = `${this.actionTopicPre}/1/LEVEL`
this.errorOverheatTopic = `${this.deviceTopicPre}/1/ERROR_OVERHEAT`
this.errorOverloadTopic = `${this.deviceTopicPre}/1/ERROR_OVERLOAD`
this.errorReducedTopic = `${this.deviceTopicPre}/1/ERROR_REDUCED`
this.subscribeTopics = [
this.stateTopic,
this.brightTopic,
this.errorOverheatTopic,
this.errorOverloadTopic,
this.errorReducedTopic
]
this.state = 'OFF'
this.oldState = undefined
this.bright = 0.0
this.oldBright = undefined
}
dimmerAction() : void {
if ((this.state != this.oldState) || (this.bright != this.oldBright)) {
if (this.state == 'ON') {
mqttHandler.send(this.actionTopic, `${this.bright / 100.0}`)
} else {
mqttHandler.send(this.actionTopic, '0')
}
this.oldState = this.state
this.oldBright = this.bright
}
}
processMessage(topic: string, payload: string) : void {
switch (topic) {
case this.stateTopic:
this.state = payload
mqttHandler.send(this.stateFeedbackTopic, this.state)
this.dimmerAction()
break
case this.brightTopic:
this.bright = parseFloat(payload)
mqttHandler.send(this.brightFeedbackTopic, `${this.bright}`)
this.dimmerAction()
break
case this.errorOverheatTopic:
case this.errorOverloadTopic:
case this.errorReducedTopic:
if (payload != 'false') {
logger.warn(`Homematic dimmer ${this.hmId}, ${this.itemId} reports: ${topic}: ${payload}`)
}
break
}
}
}

View File

@ -0,0 +1,54 @@
import * as logger from './log'
import { mqttHandler } from './MqttDispatcher'
import { AHomematicItem } from './AHomematicItem'
export class HomematicSwitchItem extends AHomematicItem {
private oldState: string|undefined
private state: string
private actionTopic: string
private deviceFeedbackTopic: string
private stateFeedbackTopic: string
private stateTopic: string
constructor(floor: string, room: string, item: string, hmId: number) {
super(floor, room, item, hmId)
this.stateTopic = `${this.topicFirstPart}/state`
this.stateFeedbackTopic = `${this.topicFirstPart}/state/feedback`
this.deviceFeedbackTopic = `${this.deviceTopicPre}/1/STATE`
this.actionTopic = `${this.actionTopicPre}/1/STATE`
this.subscribeTopics = [
this.stateTopic,
this.deviceFeedbackTopic
]
this.state = 'OFF'
this.oldState = undefined
}
processMessage(topic: string, payload: string) : void {
switch (topic) {
case this.stateTopic:
this.state = payload
mqttHandler.send(this.stateFeedbackTopic, this.state)
if (this.state != this.oldState) {
if (this.state == 'ON') {
mqttHandler.send(this.actionTopic, 'true')
} else {
mqttHandler.send(this.actionTopic, 'false')
}
this.oldState = this.state
}
break
case this.deviceFeedbackTopic:
if (payload == 'true') {
this.state = 'ON'
} else {
this.state = 'OFF'
}
this.oldState = this.state
mqttHandler.send(this.stateFeedbackTopic, this.state)
break
}
}
}

56
src/TimerAdaptor.ts Normal file
View File

@ -0,0 +1,56 @@
import { AItem } from './AItem'
import * as logger from './log'
import { mqttHandler } from './MqttDispatcher'
export class TimerAdaptor extends AItem {
private timer: NodeJS.Timer|null
private delay: number
private state: string
private inTopic: string
private actionStateTopic: string
constructor(floor: string, room: string, item: string, delay: number) {
super(floor, room, item)
this.actionStateTopic = `${this.topicFirstPart}/state`
this.inTopic = `${this.topicFirstPart}/timerIn`
this.subscribeTopics = [ this.inTopic ]
this.state = 'OFF'
this.delay = delay
this.timer = null
}
processMessage(topic: string, payload: string) : void {
switch (topic) {
case this.inTopic:
switch (payload) {
case 'SHORT':
if ((this.state == 'ON') && (this.timer != null)) {
clearTimeout(this.timer)
}
this.state = 'ON'
mqttHandler.send(this.actionStateTopic, this.state, true)
this.timer = setTimeout(() => {
logger.info(`timer ${this.itemId} elapsed`)
this.state = 'OFF'
mqttHandler.send(this.actionStateTopic, this.state, true)
}, (this.delay * 1000))
break
case 'LONG_END':
if (this.timer != null) {
clearTimeout(this.timer)
}
if (this.state == 'OFF') {
this.state = 'ON'
} else {
this.state = 'OFF'
}
mqttHandler.send(this.actionStateTopic, this.state, true)
break
}
break
}
}
}

View File

@ -1,53 +0,0 @@
let logger = require('./log');
let mqtt = require('./mqttHandler');
let HomematicDimmerItemClass = require('./HomematicDimmerItem');
let HomematicSwitchItemClass = require('./HomematicSwitchItem');
let M433SwitchItem = require('./M433SwitchItem');
let HomematicFourButtonThing = require('./HomematicFourButtonThing');
let HomematicFourButtonSingleItem = require('./HomematicFourButtonSingleItem');
let DimmerAdaptor = require('./DimmerAdaptor');
let TimerAdaptor = require('./TimerAdaptor');
let Forwarder = require('./Forwarder');
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();
let testFourButton = new HomematicFourButtonThing('Gnd', 'Hallway', 'TestButton', 9, [
new HomematicFourButtonSingleItem('button1', 'dispatcher_ng/items/Gnd/Hallway/Testlight/dimmerIn'),
new HomematicFourButtonSingleItem('button2', 'dispatcher_ng/items/Gnd/Hallway/DeskLight/timerIn'),
new HomematicFourButtonSingleItem('button3', 'test/button/3'),
new HomematicFourButtonSingleItem('button4', 'test/button/4')
]);
testFourButton.start();
let testDimmerAdaptor = new DimmerAdaptor('Gnd', 'Hallway', 'Testlight');
testDimmerAdaptor.start();
let testTimerAdaptor = new TimerAdaptor('Gnd', 'Hallway', 'DeskLight', 10);
testTimerAdaptor.start();
let testLight = new HomematicDimmerItemClass('Gnd', 'Hallway', 'Testlight', 8);
testLight.start();
let testLight2 = new HomematicSwitchItemClass('Gnd', 'Hallway', 'Testlight2', 5);
testLight2.start();
let testForwarder = new Forwarder('Gnd', 'Hallway', 'TestForwarder', 'state', [
'dispatcher_ng/items/Gnd/Hallway/Testlight2/state',
'dispatcher_ng/items/Gnd/Hallway/DeskLight/state'
]);
testForwarder.start();
mqtt.start();

View File

@ -5,26 +5,46 @@ import { mqttHandler } from './MqttDispatcher'
import { M433SwitchItem } from './M433SwitchItem'
import { HomematicFourButtonThing, HomematicFourButtonSingleItem } from './HomematicFourButtonThing'
import { DimmerAdaptor } from './DimmerAdaptor'
import { TimerAdaptor } from './TimerAdaptor'
import { HomematicDimmerItem } from './HomematicDimmerItem'
import { HomematicSwitchItem } from './HomematicSwitchItem'
import { Forwarder } from './Forwarder'
logger.info("Dispatcher starting")
let aquariumLight = new M433SwitchItem('1st', 'Anna', 'AquariumLight', '14665044 24 1', '14665041 24 1');
aquariumLight.start();
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();
let deskLight = new M433SwitchItem('Gnd', 'Hallway', 'DeskLight', '83221 24 1', '83220 24 1')
deskLight.start()
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();
])
testFourButton.start()
let testDimmerAdaptor = new DimmerAdaptor('Gnd', 'Hallway', 'Testlight');
testDimmerAdaptor.start();
let testDimmerAdaptor = new DimmerAdaptor('Gnd', 'Hallway', 'Testlight')
testDimmerAdaptor.start()
let testTimerAdaptor = new TimerAdaptor('Gnd', 'Hallway', 'DeskLight', 10)
testTimerAdaptor.start()
let testLight = new HomematicDimmerItem('Gnd', 'Hallway', 'Testlight', 8)
testLight.start()
let testLight2 = new HomematicSwitchItem('Gnd', 'Hallway', 'Testlight2', 5)
testLight2.start()
let testForwarder = new Forwarder('Gnd', 'Hallway', 'TestForwarder', 'state', [
'dispatcher_ng/items/Gnd/Hallway/Testlight2/state',
'dispatcher_ng/items/Gnd/Hallway/DeskLight/state'
])
testForwarder.start()