-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.js
92 lines (81 loc) · 2.93 KB
/
app.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"use strict";
const Homey = require('homey');
const brokerMQTT = require("./broker.js");
const actionsMQTT = require("./actions.js");
const triggerMQTT = require("./triggers.js");
class MQTTApp extends Homey.App {
/*
Initialize the Owntracks app. Register all variables,
Connect to the broker when the broker is used.
Register triggers, actions and conditions
*/
async onInit() {
this.logmodule = require("./logmodule.js");
this.broker = new brokerMQTT(this);
this.triggers = new triggerMQTT(this);
this.actions = new actionsMQTT(this);
this.broker.updateRef(this);
}
changedSettings(body) {
this.logmodule.writelog("changedSettings called");
this.logmodule.writelog("topics:" + this.broker.getTopicArray().getTopics())
if (this.broker.getTopicArray().getTopics().length > 0) {
this.broker.getConnectedClient().unsubscribe(this.broker.getTopicArray().getTopics());
this.broker.getTopicArray().clearTopicArray();
};
if (this.broker.getConnectedClient() !== null) {
this.broker.getConnectedClient().end(true);
}
this.logmodule.writelog("topics:" + this.broker.getTopicArray().getTopics());
this.broker.clearConnectedClient();
this.triggers.getTriggerArgs();
return true;
}
getLogLines() {
return this.logmodule.getLogLines();
}
/**
* sendMessage - Publish a message on a given topic.
* The MQTT broker used is configured in the MQTT client settings.
* If there is no connection, one is setup to the broker.
*
* @param {type} body JSON object containing:
* mqttTopic: Topic the message should be published on
* mqttMessage: Message payload that is posted on the topic.
* qos: 0, 1 or 2 representing the quality of service to be used.
* retain: true when sending as retained message or false.
* @return {type} returns an error object on failure or true when succesfull
*/
sendMessage(body) {
let response = {
result: -1,
error: ""
}
if (body !== undefined) {
try {
this.broker.sendMessageToTopic(body);
response.result = 0;
return response;
} catch (error) {
this.logmodule.writelog('error', error);
response.error=error;
}
}
return response;
}
subscribeToTopic(topic, callback) {
let response = {
result: -1,
error: ""
}
this.logmodule.writelog('info', 'API: subscribe to topic: ' + topic);
try {
this.broker.subscribeToApiTopic(topic, callback);
response.result=0;
} catch (error) {
response.error = error;
}
return response;
}
}
module.exports = MQTTApp;