-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathtypes.js
70 lines (60 loc) · 1.61 KB
/
types.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
/**
* Private variables and functions
*/
var types = {};
var validateTimers = function (timers) {
/*
"timers": [{
"enabled": true or false,
"type": "once or repeat"
"at": "ISO format when type is once, Cron job format when type is repeat",
"do": {
"switch": "on/off"
}
}]
*/
if (! Array.isArray(timers)) return false;
return timers.every(function (timer) {
return timer &&
('enabled' in timer) && (typeof timer.enabled === 'boolean') &&
timer.type && (timer.type === 'once' || timer.type === 'repeat') &&
timer.at && (typeof timer.at === 'string') &&
timer.do && (typeof timer.do === 'object');
});
};
/**
* Exports
*/
module.exports = exports = function (req) {
var type = req.deviceid.substr(0, 2);
if (! (type in types)) {
return true;
}
return types[type](req.params);
};
exports.addType = function (type, validate) {
if (typeof type !== 'string' || typeof validate !== 'function') {
return;
}
types[type] = validate;
};
// Switch
exports.addType('01', function (params) {
if (params.timers) {
return validateTimers(params.timers);
}
return ('switch' in params) && (/^on|off$/.test(params['switch']));
});
// Light
exports.addType('02', function (params) {
if (params.timers) {
return validateTimers(params.timers);
}
return ('light' in params) && (/^on|off$/.test(params['light']));
});
// Temperature and humidity sensor
exports.addType('03', function (params) {
return ('humidity' in params) && ('temperature' in params) &&
(! isNaN(Number(params['humidity']))) &&
(! isNaN(Number(params['temperature'])));
});