-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcc.js
184 lines (167 loc) · 4.93 KB
/
dcc.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
184
const DaikinCloud = require("daikin-controller-cloud");
const options = {
logger: console.log, // optional, logger function used to log details depending on loglevel
logLevel: "debug", // info, debug optional, Loglevel of Library, default 'warn' (logs nothing by default)
communicationTimeout: 10000, // Amount of ms to wait for request and responses before timeout
communicationRetries: 3, // Amount of retries when connection times out
};
let lastUpdated = 0;
let devices;
let tokenSet;
module.exports = function (RED) {
function daikin_brp069c4Node(config) {
const cacheTime = +config.cachetime || 60;
RED.nodes.createNode(this, config);
let node = this;
let daikinCloud;
options.logLevel = config.logLevel;
node.init = async function () {
try {
setNodeStatus({ fill: "gray", shape: "dot", text: "Connecting..." });
if (config.timeout) {
options.communicationTimeout = +config.timeout;
}
if (config.retry) {
options.communicationRetries = +config.retry;
}
if (tokenSet) {
daikinCloud = new DaikinCloud(tokenSet, options);
node.debug("tokenSet found and used");
} else {
const credentials = this.credentials;
if (
credentials &&
credentials.username != null &&
credentials.password != null
) {
daikinCloud = new DaikinCloud(null, options);
tokenSet = await daikinCloud.login(
credentials.username,
credentials.password
);
daikinCloud = new DaikinCloud(tokenSet, options);
} else {
node.error("No credentials provided!");
setNodeStatus({
fill: "red",
shape: "dot",
text: "No credentials provided!",
});
}
}
updateDevices();
setNodeStatus({ fill: "blue", shape: "dot", text: "Waiting..." });
} catch (error) {
setNodeStatus({ fill: "red", shape: "dot", text: error });
node.error(error);
}
};
node.on("input", function (msg, send, done) {
send =
send ||
function () {
node.send.apply(node, arguments);
};
const payload = msg.payload;
const topic = msg.topic;
switch (topic) {
case "get":
updateDevices();
if (devices) {
msg.payload = devices;
msg.lastUpdated = lastUpdated;
node.send(msg);
} else {
node.send(null);
setNodeStatus({
fill: "gray",
shape: "dot",
text: "failed to get devices",
});
}
break;
case "set":
const device = getDeviceBySsid(payload.ssid);
setDeviceData(
device,
payload.managementPoint,
payload.dataPoint,
payload.dataPointPath,
payload.value
);
break;
case "reset":
break;
default:
send(null);
}
if (done) {
done();
}
});
function getDeviceBySsid(ssid) {
const result = devices.find((device) => {
return device.getData("gateway", "ssid").value === ssid;
});
return result ? result : null; // or undefined
}
async function setDeviceData(
device,
managementPoint,
dataPoint,
dataPointPath,
value
) {
try {
await device.setData(managementPoint, dataPoint, dataPointPath, value);
await device.updateData();
setNodeStatus({
fill: "green",
shape: "dot",
text: "Set data succesfully to " + value,
});
} catch (error) {
setNodeStatus({ fill: "red", shape: "dot", text: error });
node.error(error);
}
}
async function updateDevices() {
try {
let timeDiff = (new Date().getTime() - lastUpdated) / 1000;
if (timeDiff >= cacheTime) {
devices = await daikinCloud.getCloudDevices();
lastUpdated = new Date().getTime();
setNodeStatus({
fill: "green",
shape: "dot",
text: "updated devices",
});
} else {
setNodeStatus({
fill: "green",
shape: "dot",
text: "using cached devices",
});
}
} catch (error) {
setNodeStatus({ fill: "red", shape: "dot", text: error });
node.error(error);
}
}
function setNodeStatus({ fill, shape, text }) {
var dDate = new Date();
node.status({
fill: fill,
shape: shape,
text: text + " (" + dDate.toLocaleTimeString() + ")",
});
}
node.init();
}
RED.nodes.registerType("Daikin-Cloud-Controller", daikin_brp069c4Node, {
credentials: {
username: { type: "text" },
password: { type: "password" },
},
});
};