This commit is contained in:
Wolfgang Hottgenroth
2018-01-01 21:15:06 +01:00
commit c00520e67b
7 changed files with 155 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
node_modules
*.map

23
package.json Normal file
View File

@ -0,0 +1,23 @@
{
"name": "dispatcher_ng",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+ssh://git@gitlab.com/wolutator/dispatcher_ng.git"
},
"author": "Wolfgang Hottgenroth <woho@hottis.de>",
"license": "ISC",
"bugs": {
"url": "https://gitlab.com/wolutator/dispatcher_ng/issues"
},
"homepage": "https://gitlab.com/wolutator/dispatcher_ng#README",
"dependencies": {
"mqtt": "^2.15.0",
"simple-node-logger": "^0.93.33"
}
}

41
src/genericItem.js Normal file
View File

@ -0,0 +1,41 @@
let logger = require('./log');
let mqtt = require('./mqttHandler');
class GenericItem {
constructor(itemId) {
this.itemId = itemId;
this.stateTopic = `dispatcher_ng/items/${this.itemId}/state`;
this.brightTopic = `dispatcher_ng/items/${this.itemId}/bright`;
this.stateFeedbackTopic = `dispatcher_ng/items/${this.itemId}/state/feedback`;
this.brightFeedbackTopic = `dispatcher_ng/items/${this.itemId}/bright/feedback`;
this.actionTopic = `dispatcher_ng/items/${this.itemId}/action`;
this.state = 'OFF';
this.oldState = undefined;
this.bright = 0;
this.oldBright = undefined;
mqtt.register([this.stateTopic, this.brightTopic], (topic, payload) => {
logger.info(`item ${this.itemId}: ${topic}, ${payload}`)
if (topic == this.stateTopic) {
this.state = payload;
mqtt.send(this.stateFeedbackTopic, this.state);
} else if (topic == this.brightTopic) {
this.bright = payload;
mqtt.send(this.brightFeedbackTopic, this.bright);
}
logger.info(this.state != this.oldState);
logger.info(this.bright != this.oldBright);
if ((this.state != this.oldState) || (this.bright != this.oldBright)) {
mqtt.send(this.actionTopic, `${this.state} ${this.bright}`);
this.oldState = this.state;
this.oldBright = this.bright;
}
});
}
}
module.exports = GenericItem;

9
src/item1.js Normal file
View File

@ -0,0 +1,9 @@
let logger = require('./log');
let mqtt = require('./mqttHandler');
mqtt.register(['dispatcher_ng/items/1/set', 'dispatcher_ng/items/1/bright'], (topic, payload) => {
logger.info(`item 1: ${topic}, ${payload}`)
mqtt.send('dispatcher_ng/items/1/feedback', 'bla');
});

9
src/log.js Normal file
View File

@ -0,0 +1,9 @@
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

12
src/main.js Normal file
View File

@ -0,0 +1,12 @@
let logger = require('./log');
let mqtt = require('./mqttHandler');
logger.info("Hello world!");
require('./item1');
let genericItemClass = require('./genericItem');
let item2 = new genericItemClass(2);
mqtt.start();

59
src/mqttHandler.js Normal file
View File

@ -0,0 +1,59 @@
let logger = require('./log')
logger.info('mqttHandler executed')
var mqtt = require('mqtt');
var client = undefined;
var topicCallbacks = {};
function start() {
client = mqtt.connect('mqtt://172.16.2.16');
client.on('error', (err) => {
logger.error(`Error in mqttHandler: ${err}`)
});
client.on('connect', () => {
client.publish('dispatcher_ng/status', 'dispatcher_ng running');
client.subscribe('dispatcher_ng/cmd');
Object.keys(topicCallbacks).forEach((topic) => {
client.subscribe(topic);
logger.info(`${topic} subscribed`);
});
logger.info('mqtt connection established');
});
client.on('message', (topic, payload) => {
logger.info(`message received on topic ${topic}: ${payload}`);
if (topic in topicCallbacks) {
topicCallbacks[topic].forEach((cb) => { cb(topic, payload) });
}
});
}
function send(topic, payload) {
client.publish(topic, payload);
}
function register(topics, cb) {
if (! (topics instanceof Array)) {
topics = [ topics ];
}
topics.forEach((topic) => {
if (topic in topicCallbacks) {
topicCallbacks[topic].push(cb);
logger.info(`additional callback registered for ${topic}`);
} else {
topicCallbacks[topic] = [ cb ];
logger.info(`first callback registered for ${topic}`);
}
})
}
module.exports = {
start,
send,
register
};