/
messagehandling.js
70 lines (59 loc) · 2.1 KB
/
messagehandling.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"use strict";
const { ManagerApi } = require('homey');
class handlingMQTT {
constructor(app) {
this.logmodule = app.logmodule;
this.triggers = app.triggers;
this.Homey = app.homey;
}
/**
* receiveMessage - Handling of received messages.
* This gets called as soon as the client receives a message published
* on a subscribed topic
*
* @param {type} topic topic where message was published on
* @param {type} message payload of the received message
* @param {type} args list of arguments that are part of the trigger FlowCardTrigger
* @param {type} state state arguments of the FlowCardTrigger
* @return {type} no return value
*/
receiveMessage(topic, message, args, state) {
var validJSON = true;
const ref=this;
this.logmodule.writelog('info', "received '" + message.toString() + "' on '" + topic + "'");
let tokens = {
message: message.toString(),
topic: topic
}
let triggerstate = {
triggerTopic: topic,
}
ref.triggers.getEventMQTT().trigger(tokens, triggerstate, null).catch( function(e) {
ref.logmodule.writelog('error', "Error occured: " +e);
})
this.logmodule.writelog('info', "Trigger generic card for " + topic);
this.dispatchToRealtimeApi(topic, message);
}
dispatchToRealtimeApi(topic, message) {
var messageToSend;
this.logmodule.writelog('debug', "send message to listeners via realtime api");
this.logmodule.writelog('debug', topic + ": " + message.toString());
try {
messageToSend = JSON.parse(message.toString());
} catch (err) {
messageToSend = message.toString();
}
//ManagerApi.realtime(topic, messageToSend);
this.Homey.api.realtime(topic, messageToSend);
}
/**
* updateRef - updates references to other classes
*
* @param {type} app reference to the main app instance
* @return {type} no return value
*/
updateRef(app) {
this.triggers = app.triggers;
}
}
module.exports = handlingMQTT;