forked from rodtoll/isy-js
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
isyscene.js
83 lines (74 loc) · 2.4 KB
/
isyscene.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
var ISYLightDevice = require('./isydevice.js').ISYLightDevice;
var ISYDefs = require("./isydefs.json");
var ISYScene = function(isy, name, address, childDevices) {
this.isy = isy;
this.name = name;
this.address = address;
this.isyType = '';
this.connectionType = 'Insteon Wired';
this.batteryOperated = false;
this.childDevices = childDevices;
this.deviceType = "scene";
this.deviceFriendlyName = "Insteon Scene";
this.lastChanged = new Date();
this.reclalculateState();
};
// Get the current light state
ISYScene.prototype.getCurrentLightState = function() {
for (var i = 0; i < this.childDevices.length; i++) {
var device = this.childDevices[i];
if (device instanceof ISYLightDevice) {
if (device.getCurrentLightState()) {
return true;
}
}
}
return false;
};
// Current light dim state is always calculated
ISYScene.prototype.getCurrentLightDimState = function() {
var lightDeviceCount = 0;
var calculatedDimLevel = 0;
for (var i = 0; i < this.childDevices.length; i++) {
var device = this.childDevices[i];
if (device instanceof ISYLightDevice) {
calculatedDimLevel += device.getCurrentLightDimState();
lightDeviceCount++;
}
}
if (lightDeviceCount > 0) {
return (calculatedDimLevel / lightDeviceCount);
} else {
return 0;
}
};
ISYScene.prototype.reclalculateState = function() {
this.markAsChanged();
return true;
};
ISYScene.prototype.markAsChanged = function() {
this.lastChanged = new Date();
};
ISYScene.prototype.sendLightCommand = function(lightState, resultHandler) {
this.isy.sendRestCommand(this.address, (lightState) ? ISYDefs.cmd.lightOn : ISYDefs.cmd.lightOff, null, resultHandler);
};
ISYScene.prototype.getAreAllLightsInSpecifiedState = function(state) {
for (var i = 0; i < this.childDevices.length; i++) {
var device = this.childDevices[i];
if (device instanceof ISYLightDevice) {
if (device.getCurrentLightState() != state) {
return false;
}
}
}
return true;
};
ISYScene.prototype.isDeviceIncluded = function(device) {
for (var i = 0; i < this.childDevices.length; i++) {
if (this.childDevices[i].address == device.address) {
return true;
}
}
return false;
};
exports.ISYScene = ISYScene;