-
Notifications
You must be signed in to change notification settings - Fork 41
/
google-mgmt.js
183 lines (159 loc) · 7.22 KB
/
google-mgmt.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* node-red-contrib-google-smarthome
* Copyright (C) 2024 Michael Jacobsen and others.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
module.exports = function (RED) {
"use strict";
/******************************************************************************************************************
*
*
*/
class MgmtNode {
constructor(config) {
RED.nodes.createNode(this, config);
this.client = config.client;
/** @type {GoogleSmartHomeNode} */
this.clientConn = RED.nodes.getNode(this.client);
if (!this.clientConn) {
this.error(RED._("googlemanagement.errors.missing-config"));
this.status({ fill: "red", shape: "dot", text: "Missing config" });
return;
} else if (typeof this.clientConn.register !== 'function') {
this.error(RED._("googlemanagement.errors.missing-bridge"));
this.status({ fill: "red", shape: "dot", text: "Missing SmartHome" });
return;
}
this.set_state_type = config.set_state_type || 'filtered_by_id';
this.clientConn.register(this, 'mgmt', config.name);
this.status({ fill: "yellow", shape: "dot", text: "Ready" });
this.on('input', this.onInput);
this.on('close', this.onClose);
}
_debug(msg) {
if (this.clientConn.enabledebug) {
console.log(msg)
} else {
RED.log.debug(msg);
}
}
/******************************************************************************************************************
* called when state is updated from Google Assistant
*
*/
updated(data) { // this must be defined before the call to clientConn.register()
const node = this;
node._debug("MgmtNode(updated): data = " + JSON.stringify(data));
let msg = {
topic: "management",
payload: data
};
node.send(msg);
}
/**
* respond to inputs from NodeRED
*
* @param {object} msg - The incoming message
* @param {Function} send - Function to send outgoing messages
* @param {Function} done - Function to inform the runtime that this node has finished its operation
*/
onInput(msg, send, done) {
const node = this;
node._debug("MgmtNode(input)");
let topicArr = (msg.topic || '').split(node.topicDelim);
let topic = topicArr[topicArr.length - 1]; // get last part of topic
const topic_upper = topic.toUpperCase();
node._debug("MgmtNode(input): topic = " + topic);
try {
if (topic_upper === 'RESTART_SERVER') {
node._debug("MgmtNode(input): RESTART_SERVER");
this.clientConn.app.Restart(RED.httpNode || RED.httpAdmin, RED.server);
} else if (topic_upper === 'REPORT_STATE') {
node._debug("MgmtNode(input): REPORT_STATE");
this.clientConn.app.ReportAllStates();
} else if (topic_upper === 'REQUEST_SYNC') {
node._debug("MgmtNode(input): REQUEST_SYNC");
this.clientConn.app.RequestSync();
} else if (topic_upper === 'GET_STATE' || topic_upper === 'GETSTATE') {
let onlyPersistent = ['filtered_by_id', 'filtered_by_name'].includes(node.set_state_type );
let useNames = ['all_by_name', 'filtered_by_name'].includes(node.set_state_type );
let deviceIds = undefined;
if (typeof msg.payload === 'boolean') {
onlyPersistent = msg.payload;
} else if (typeof msg.payload === 'string') {
deviceIds = [msg.payload];
} else if (Array.isArray(msg.payload)) {
deviceIds = msg.payload;
} else if (typeof msg.payload === 'object') {
if (typeof msg.payload.onlyPersistent === 'boolean') {
onlyPersistent = msg.payload.onlyPersistent;
}
if (typeof msg.payload.useNames === 'boolean') {
useNames = msg.payload.useNames;
}
if (typeof msg.payload.devices === 'string') {
deviceIds = [msg.payload.devices];
} else if (Array.isArray(msg.payload.devices)) {
deviceIds = msg.payload.devices;
}
}
let states = this.clientConn.app.devices.getStates(deviceIds, onlyPersistent, useNames);
if (states) {
send({
topic: topic,
payload: states
});
}
} else if (topic_upper === 'SET_STATE' || topic_upper === 'SETSTATE') {
if (typeof msg.payload === 'object') {
this.clientConn.app.devices.setStates(msg.payload);
}
}
done();
} catch (err) {
done(err);
}
}
/**
* Called by the runtime when this node is being removed or restarted
*
* @param {boolean} removed - true if the is being removed, false on restart
* @param {Function} done - Function to inform the runtime that this node has finished its operation
*/
onClose(removed, done) {
if (removed) {
// this node has been deleted
this.clientConn.remove(this, 'mgmt');
} else {
// this node is being restarted
this.clientConn.deregister(this, 'mgmt');
}
done();
}
sendSetState() {
if (this.set_state_type === 'no_nodes') return;
let onlyPersistent = ['filtered_by_id', 'filtered_by_name'].includes(this.set_state_type);
let useNames = ['all_by_name', 'filtered_by_name'].includes(this.set_state_type);
let states = this.clientConn.app.devices.getStates(undefined, onlyPersistent, useNames);
if (states) {
this.send({
topic: 'set_state',
payload: states
});
}
}
}
RED.nodes.registerType("google-mgmt", MgmtNode);
}