-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenhab.js
160 lines (151 loc) · 4.25 KB
/
openhab.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
const
log = require('loglevel').getLogger('openhab'),
request = require('request'),
HttpStatus = require('http-status-codes');
utils = require('./utils'),
format = utils.format;
class OpenHab {
constructor(config, sitemap) {
this.config = config;
this.sitemap = sitemap;
this.openHabRestUri = this.config.openHabRestUri;
}
executeRequest({
itemNode,
value
}) {
return new Promise((resolve, reject) => {
if (value === undefined) {
this.getItemValue(itemNode, resolve, reject);
} else if (typeof itemNode == 'object') {
let commandValue = this.findCommandValue(value, itemNode.values, itemNode.mappings);
if (commandValue) {
this.sendItemCommand(itemNode.command, commandValue.toUpperCase(), resolve, reject);
} else {
reject(format('Unsupported command value %s', value));
}
} else {
this.setItemValue(itemNode, value, resolve, reject);
}
});
}
findCommandValue(value, possibleValues, mappings) {
if (possibleValues) {
for (let possibleValue of possibleValues) {
if (possibleValue == value) {
return this.getCommandValue(value, mappings);
}
}
}
return null;
}
getCommandValue(commandValue, mappings) {
let mappedCommandValue = mappings ? mappings[commandValue] : null;
return mappedCommandValue ? mappedCommandValue : commandValue;
}
sendItemCommand(item, value, resolve, reject) {
log.debug('Sending OpenHab item %s command %s', item, value);
let httpOptions = {
uri: format('%s/items/%s', this.openHabRestUri, item),
method: 'POST',
headers: {
'Content-Type': 'text/plain',
'Accept': 'application/json;charset=UTF-8',
'Accept-Charset': 'UTF-8'
},
body: value.toString()
};
request(httpOptions, (err, res, body) => {
let errorResult = err || (res.statusCode != HttpStatus.ACCEPTED ? body : null);
if (errorResult) {
reject(errorResult);
} else {
resolve({
item,
value,
updated: true,
req: httpOptions,
res: res
});
}
});
}
getItem(item, resolve, reject) {
log.debug('Getting OpenHab item %s', item);
let httpOptions = {
uri: format('%s/items/%s', this.openHabRestUri, item),
method: 'GET',
headers: {
'Accept': 'application/json;charset=UTF-8',
'Accept-Charset': 'UTF-8'
}
};
request(httpOptions, (err, res, body) => {
let errorResult = err || (res.statusCode != HttpStatus.OK ? body : null);
if (errorResult) {
reject(errorResult);
} else {
resolve({
item,
value: JSON.parse(body),
updated: false,
req: httpOptions,
res: res
});
}
});
}
getItemValue(item, resolve, reject) {
log.debug('Getting OpenHab item %s value', item);
let httpOptions = {
uri: format('%s/items/%s/state', this.openHabRestUri, item),
method: 'GET',
headers: {
'Accept': 'text/plain;charset=UTF-8',
'Accept-Charset': 'UTF-8'
}
};
request(httpOptions, (err, res, body) => {
let errorResult = err || (res.statusCode != HttpStatus.OK ? body : null);
if (errorResult) {
reject(errorResult);
} else {
resolve({
item,
value: body,
updated: false,
req: httpOptions,
res: res
});
}
});
}
setItemValue(item, value, resolve, reject) {
log.debug('Setting OpenHab item %s value %s', item, value);
let httpOptions = {
uri: format('%s/items/%s/state', this.openHabRestUri, item),
method: 'PUT',
headers: {
'Content-Type': 'text/plain',
'Accept': 'application/json;charset=UTF-8',
'Accept-Charset': 'UTF-8'
},
body: value.toString()
};
request(httpOptions, (err, res, body) => {
let errorResult = err || (res.statusCode != HttpStatus.OK ? body : null);
if (errorResult) {
reject(errorResult);
} else {
resolve({
item,
value,
updated: true,
req: httpOptions,
res: res
});
}
});
}
}
module.exports = OpenHab;