-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmelview-unitcommand.js
More file actions
111 lines (95 loc) · 3.71 KB
/
melview-unitcommand.js
File metadata and controls
111 lines (95 loc) · 3.71 KB
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
module.exports = function (RED) {
const request = require('request');
function UnitCommandNode(config) {
RED.nodes.createNode(this, config);
this.melviewConnection = RED.nodes.getNode(config.melviewConnection);
this.building = config.building;
this.unit = config.unit;
this.power = config.power;
this.testrun = config.testrun;
this.mode = config.mode;
this.fanspeed = config.fanspeed;
this.direction = config.direction;
this.temperature = config.temperature;
const node = this;
const melviewConnection = this.melviewConnection;
let validTemperature = true;
function addCommand(command, commands) {
if (typeof command != 'undefined' && command !== '')
commands.push(command);
}
function getTempCommand(temp) {
if(isNaN(temp)) {
node.error("temperature is not a number");
validTemperature = false;
}
const t = parseFloat(temp);
if(t < 16 || t > 31)
{
node.error("Temperature out of range");
validTemperature = false;
}
if (typeof temp == 'undefined')
return undefined;
return 'TS' + temp;
}
function buildCommand(msg) {
let commands = [];
if (config.staticconfiguration) {
addCommand(config.mode, commands);
addCommand(config.fanspeed, commands);
addCommand(config.direction, commands);
addCommand(getTempCommand(config.temperature), commands);
if (config.power) {
commands.push('PW1');
} else {
commands.push('PW0');
}
} else {
addCommand(msg.payload.power, commands);
addCommand(msg.payload.mode, commands);
addCommand(msg.payload.fanspeed, commands);
addCommand(msg.payload.direction, commands);
addCommand(getTempCommand(msg.payload.temperature), commands);
}
return commands;
}
node.on('input', function (msg) {
let commands = buildCommand(msg);
let postData = JSON.stringify({
'unitid': config.unit,
'v': 3,
'commands': commands.join(',')
});
melviewConnection.getAuthCookie(function (authCookie) {
const options = {
'method': 'POST',
'url': `https://${melviewConnection.melviewEndpoint}/api/unitcommand.aspx`,
'headers': {
'Content-Type': 'application/json; charset=utf-8',
'User-Agent': 'request',
'Cookie': authCookie,
'Content-Length': postData.length
},
body: postData
};
if(config.testrun)
{
node.log(`test run command: ${postData}`);
return ;
}
if(validTemperature) {
request(options, function (error, response) {
if (error) node.error(error);
node.log(`sending command: ${postData}`);
node.send(msg);
});
}
else {
node.warn(`Temperature of ${config.temperature} is not valid. Not posting to melview.`)
}
});
});
}
RED.nodes.registerType("unit-command", UnitCommandNode);
};