forked from firsttris/node-red-contrib-homematic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomematic.js
98 lines (88 loc) · 2.98 KB
/
homematic.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
module.exports = function(RED) {
"use strict";
function HomematicConfigNode(n) {
RED.nodes.createNode(this, n);
this.device = n.device;
this.serial = n.serial;
}
RED.nodes.registerType("homematic-config", HomematicConfigNode);
function HomematicCommands(n) {
RED.nodes.createNode(this, n);
this._method = n._method;
this._interface = n._interface;
this._device = n._device;
this.addressConfig = RED.nodes.getNode(this._device);
this._valueKey = n._valueKey;
this._type = n._type;
this._username = n._username;
this._password = n._password;
this._url = n._url;
this._interfaceId = n._interfaceId;
this._value = n._value;
this._topic = n._topic;
this._name = n._name;
var node = this;
function isNotEmptyOrUndefined(field) {
if ((typeof field === "undefined") || (field === "")) {
return false;
} else {
return true;
}
}
this.on('input', function(msg) {
if (isNotEmptyOrUndefined(node._topic)) {
msg.topic = node._topic;
}
var myrequest = {};
myrequest.method = node._method;
var params = {};
if (isNotEmptyOrUndefined(RED.settings.functionGlobalContext) && isNotEmptyOrUndefined(RED.settings.functionGlobalContext["currentSessionid"])) {
params["_session_id_"] = RED.settings.functionGlobalContext["currentSessionid"];
}
if (isNotEmptyOrUndefined(node._interface)) {
params["interface"] = node._interface;
}
console.log(node.addressConfig);
if (isNotEmptyOrUndefined(node.addressConfig) && isNotEmptyOrUndefined(node.addressConfig.serial)) {
params["address"] = node.addressConfig.serial;
}
if (isNotEmptyOrUndefined(node._valueKey)) {
params["valueKey"] = node._valueKey;
}
if (isNotEmptyOrUndefined(node._type)) {
params["type"] = node._type;
}
if (isNotEmptyOrUndefined(node._username)) {
params["username"] = node._username;
}
if (isNotEmptyOrUndefined(node._password)) {
params["password"] = node._password;
}
if (isNotEmptyOrUndefined(node._url)) {
params["url"] = node._url;
}
if (isNotEmptyOrUndefined(node._interfaceId)) {
params["interfaceId"] = node._interfaceId;
}
if (isNotEmptyOrUndefined(msg.payload) && isNotEmptyOrUndefined(msg.payload.value)) {
params["value"] = msg.payload.value;
} else if (isNotEmptyOrUndefined(node._value)) {
params["value"] = node._value;
}
myrequest.params = params;
var headers = {};
msg.payload = myrequest;
// headers["Content-Length"] = msg.length;
headers["Content-Type"] = "application/json";
headers["Accept"] = "application/json";
msg.headers = headers;
console.log("**************************PAYLOAD START********************************");
console.log("Topic: "+msg.topic);
//console.log(msg.headers);
console.log(msg.payload);
console.log("***************************PAYLOAD END*********************************");
node.send(msg);
});
}
RED.nodes.registerType("homematic", HomematicCommands);
}