-
Notifications
You must be signed in to change notification settings - Fork 1
/
light_library.js
95 lines (71 loc) · 1.85 KB
/
light_library.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
var Light = require('./light.js');
var _ = require('lodash/core');
function LightLibrary() {
var self = this;
self.lights = {};
self.newLight = function(id) {
var light = new Light(id);
self.lights[id] = light;
return self.lights[id];
}
self.setLightOnline = function(id) {
var light = self.getOrCreate(id);
light.setOnline();
}
self.setLightOffline = function(id) {
var light = self.getOrCreate(id);
light.setOffline();
}
self.lightIsOnline = function(id) {
var light = self._getLight(id);
return light ? light.isOnline() : false;
}
self.setFade = function(id, fade) {
var light = self.getOrCreate(id);
light.setFade(fade);
}
self.getFade = function(id) {
var light = self._getLight(id);
return light ? light.getFade() : false;
}
self.getLightPower = function (lightId) {
var light = self._getLight(lightId);
return light ? light.getPower() : true;
}
self.setLightPowerOn = function (lightId) {
self._setLightPower(lightId, true);
}
self.setLightPowerOff = function (lightId) {
self._setLightPower(lightId, false);
}
self._setLightPower = function (lightId, powerState) {
var light = self.getOrCreate(lightId);
light.setPower(powerState);
}
self.getLightPower = function (lightId) {
var light = self._getLight(lightId);
return light ? light.getPower() : false;
}
self.getOrCreate = function(id) {
var light = self._getLight(id);
return light ? light : self.newLight(id);
}
self._getLight = function(id) {
if(
typeof self.lights[id] != 'undefined'
) {
return self.lights[id];
}
return false;
}
self._setLight = function(light) {
console.log(light);
if(typeof self.lights[light.id] != 'undefined') {
var existing = self._getLight(light.id);
var light = _.merge(existing, light);
console.log(light);
}
this.lights[light.id] = light;
}
}
module.exports = LightLibrary;