-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathir-device.js
334 lines (302 loc) · 8.1 KB
/
ir-device.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
'use strict';
const IRProperty = require('./ir-property');
const IrConstants = require('./constants');
const ActionLearn = require('./ir-action-learncode');
const {Constants, Device} = require('gateway-addon');
var DEBUG = false;
function getActionInput(type) {
const ActionInputs = {
[IrConstants.IR_DEVICE_TYPE_THERMOSTAT] : IrConstants.IR_ACTION_INPUT_THERMOSTAT,
[IrConstants.IR_DEVICE_TYPE_DIMMABLE_LIGHT]: IrConstants.IR_ACTION_INPUT_DIMMABLE_LIGHT,
[IrConstants.IR_DEVICE_TYPE_ON_OFF_LIGHT] : IrConstants.IR_ACTION_INPUT_ON_OFF_LIGHT,
[IrConstants.IR_DEVICE_TYPE_ON_OF_SWITCH] : IrConstants.IR_ACTION_INPUT_ON_OF_SWITCH,
};
return ActionInputs[type];
}
/**
* thermostat config
* {
* "mac": "broadlink mac address",
* "type": "thermostat",
* "id": "thermostat01",
* "name": "Living thermostat",
* "ir": {
* "cool": ["min temperature ir code", "min temperature + 1 ir code", ....., "max temperature + 1 ir code"],
* "heat": ["min temperature ir code", "min temperature + 1 ir code", ....., "max temperature + 1 ir code"]
* },
* "cool": {
* "min": 16, //min temperature on cool mode
* "max": 32 //man temperature on cool mode
* },
* "heat": {
* "min": 16, //min temperature on heat mode
* "max": 32 //man temperature on heat mode
* }
* }
*/
/**
* dimbleLight config
* {
* "mac": "broadlink mac address",
* "type": "dimbleLight",
* "id": "dimbleLight01",
* "name": "Living dimbleLight",
* "ir": {
* "on": "light on ir code",
* "off": "light off ir code",
* "levelUp": "brightness up ir code",
* "levelDown": "brightness down ir code"
* },
* "onLevel": 100, // brightness level when send on ir code
* "levelStep": 5 // how much brightness level change when send levelUp/Down ir code
* }
*/
/**
* light config
* {
* "mac": "broadlink mac address",
* "type": "light",
* "id": "light",
* "name": "Living light",
* "ir": {
* "on": "light on ir code",
* "off": "light off ir code"
* }
* }
*/
/**
* switch config
* {
* "mac": "broadlink mac address",
* "type": "switch",
* "id": "switch",
* "name": "Living switch",
* "ir": {
* "on": "switch on ir code",
* "off": "switch off ir code"
* }
* }
*/
class IRDevice extends Device {
constructor(adapter, config) {
super(adapter, config.id);
this.name = config.name;
this.description = config.description;
this.mac = adapter.mac;
this.irtype = config.type;
this.actionsList = new Map();
const actionMetadata = {
description: 'Learn ir codes',
input : getActionInput(config.type),
};
this.addAction(IrConstants.IR_ACTION_LEARN, actionMetadata);
const ir = config.ir ? config.ir : {};
switch (config.type) {
case IrConstants.IR_DEVICE_TYPE_THERMOSTAT:
this._initThermostat(ir, config);
break;
case IrConstants.IR_DEVICE_TYPE_DIMMABLE_LIGHT:
this._initDimbleLight(ir, config);
break;
case IrConstants.IR_DEVICE_TYPE_ON_OFF_LIGHT:
this._initOnOffLight(ir);
break;
case IrConstants.IR_DEVICE_TYPE_ON_OF_SWITCH:
this._initOnOffSwitch(ir);
break;
default:
// do nothing
break;
}
}
_initDimbleLight(ir, config) {
this.type = Constants.THING_TYPE_DIMMABLE_LIGHT;
this._addProperty(
'on', // name
{ // property description
type : 'boolean',
unit : 'percent',
default: false,
onLevel: config.onLevel,
},
{ // ir
on : ir.on,
off: ir.off,
},
'setOnOffValue' // setAttrFromValue
);
this._addProperty(
'level', // name
{ // property description
type : 'number',
default : 0,
levelStep: config.levelStep,
},
{ // ir
levelDown: ir.levelDown,
levelUp : ir.levelUp,
},
'setPowerLevelValue' // setAttrFromValue
);
}
_initOnOffLight(ir) {
this.type = Constants.THING_TYPE_ON_OFF_LIGHT;
this._addProperty(
'on', // name
{ // property description
type : 'boolean',
default: false,
},
{ // ir
on : ir.on,
off: ir.off,
},
'setOnOffValue' // setAttrFromValue
);
}
_initThermostat(ir, config) {
// use default things type
this._addProperty(
'temperature', // name
{ // property description
type : 'number',
unit : 'celsius',
default: 20,
},
{}, // ir
'setTemperatureNumericValue' // setAttrFromValue
);
this._addProperty(
'mode', // name
{ // property description
type : 'string',
modes : ['on', 'cool', 'heat', 'off'],
default: 'off',
cool : {
min: config.cool ? config.cool.min : undefined,
max: config.cool ? config.cool.max : undefined,
},
heat: {
min: config.heat ? config.heat.min : undefined,
max: config.heat ? config.heat.max : undefined,
},
},
{ // ir
off : ir.off,
cool: ir.cool,
heat: ir.heat,
},
'setThermostatModeValue' // setAttrFromValue
);
}
_initOnOffSwitch(ir) {
this.type = Constants.THING_TYPE_ON_OFF_SWITCH;
this._addProperty(
'on', // name
{ // property description
type : 'boolean',
default: 'off',
},
{ // ir
on : ir.on,
off: ir.off,
},
'setOnOffValue' // setAttrFromValue
);
}
_addProperty(name, propertyDescr, ir, setIRCodeFromValue) {
const property = new IRProperty(this, name, propertyDescr, ir, setIRCodeFromValue);
this.properties.set(name, property);
}
asDict() {
const dict = super.asDict();
dict.mac = this.mac;
return dict;
}
sendIRSequence(property, sequence) {
if (DEBUG) console.log('sendIRSequence:', property, sequence);
this.adapter.sendSequence(this, property, sequence);
}
/**
* @method requestAction
* @returns a promise which resolves when the action has been requested.
*/
requestAction(actionId, actionName, input) {
return new Promise((resolve, reject) => {
if (!this.actions.has(actionName)) {
reject(`Action "${actionName}" not found`);
return;
}
const action = new ActionLearn(actionId, this, input);
let actions = this.actionsList.get(actionName);
if (typeof actions === 'undefined') {
actions = new Map();
}
// should we check that actionId is duplicated?
actions.set(actionId, action);
this.actionsList.set(actionName, actions);
this.performAction(action).catch((err) => console.log(err));
resolve();
});
}
/**
* @method performAction
*/
performAction(action) {
return new Promise((resolve, reject) => {
action.performAction()
.then(() => {
action.finish();
resolve();
})
.catch((err) => {
action.fail(err);
reject(err);
});
});
}
actionNotify(action) {
super.actionNotify(action);
console.log(action.status);
}
// we should change?
removeAction(actionId, actionName) {
return new Promise((resolve, reject) => {
if (!this.actions.has(actionName)) {
reject(`Action "${actionName}" not found`);
return;
}
const actions = this.actionsList.get(actionName);
if (typeof actions === 'undefined') {
reject(`Action "${actionName}" have not been requested yet`);
return;
}
const action = actions.get(actionId);
if (typeof action === 'undefined') {
reject(`Action "${actionName}" Id "${actionId}" have not been requested yet`);
return;
}
if (action.isCompleted()) {
actions.delete(actionId);
resolve();
return;
}
this.cancelAction(action)
.then(() => {
actions.delete(actionId);
resolve();
})
.catch((err) => {
console.error(err);
reject(`Action "${actionName}" Id "${actionId}" cannot be canceled: ${err}`);
});
});
}
/**
* @method cancelAction
*/
cancelAction(action) {
return action.cancel();
}
}
module.exports = IRDevice;