-
Notifications
You must be signed in to change notification settings - Fork 83
/
index.js
143 lines (117 loc) · 4.25 KB
/
index.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
"use strict";
var Service, Characteristic, HomebridgeAPI;
const { HomebridgeDummyVersion } = require('./package.json');
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
HomebridgeAPI = homebridge;
homebridge.registerAccessory("homebridge-dummy", "DummySwitch", DummySwitch);
}
function DummySwitch(log, config) {
this.log = log;
this.name = config.name;
this.stateful = config.stateful;
this.dimmer = config.dimmer;
this.brightness = config.brightness;
this.brightnessStorageKey = this.name + "Brightness";
this.reverse = config.reverse;
this.time = config.time ? config.time : 1000;
this.resettable = config.resettable;
this.timer = null;
this.random = config.random;
this.disableLogging = config.disableLogging;
if (this.dimmer) {
this._service = new Service.Lightbulb(this.name);
this.modelString = "Dummy Dimmer";
} else {
this._service = new Service.Switch(this.name);
this.modelString = "Dummy Switch";
}
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, 'Homebridge')
.setCharacteristic(Characteristic.Model, this.modelString)
.setCharacteristic(Characteristic.FirmwareRevision, HomebridgeDummyVersion)
.setCharacteristic(Characteristic.SerialNumber, 'Dummy-' + this.name.replace(/\s/g, '-'));
this.cacheDirectory = HomebridgeAPI.user.persistPath();
this.storage = require('node-persist');
this.storage.initSync({dir:this.cacheDirectory, forgiveParseErrors: true});
this._service.getCharacteristic(Characteristic.On)
.on('set', this._setOn.bind(this));
if (this.dimmer) {
this._service.getCharacteristic(Characteristic.Brightness)
.on('get', this._getBrightness.bind(this))
.on('set', this._setBrightness.bind(this));
}
if (this.reverse) this._service.setCharacteristic(Characteristic.On, true);
if (this.stateful) {
var cachedState = this.storage.getItemSync(this.name);
if((cachedState === undefined) || (cachedState === false)) {
this._service.setCharacteristic(Characteristic.On, false);
} else {
this._service.setCharacteristic(Characteristic.On, true);
}
}
if (this.dimmer) {
var cachedBrightness = this.storage.getItemSync(this.brightnessStorageKey);
if ((cachedBrightness == undefined) || cachedBrightness == 0) {
this._service.setCharacteristic(Characteristic.On, false);
this._service.setCharacteristic(Characteristic.Brightness, 0);
} else {
this._service.setCharacteristic(Characteristic.On, true);
this._service.setCharacteristic(Characteristic.Brightness, cachedBrightness);
}
}
}
DummySwitch.prototype.getServices = function() {
return [this.informationService, this._service];
}
function randomize(time) {
return Math.floor(Math.random() * (time + 1));
}
DummySwitch.prototype._getBrightness = function(callback) {
if ( ! this.disableLogging ) {
this.log("Getting " + "brightness: " + this.brightness);
}
callback(null, this.brightness);
}
DummySwitch.prototype._setBrightness = function(brightness, callback) {
if ( ! this.disableLogging ) {
var msg = "Setting brightness: " + brightness
this.log(msg);
}
this.brightness = brightness;
this.storage.setItemSync(this.brightnessStorageKey, brightness);
callback();
}
DummySwitch.prototype._setOn = function(on, callback) {
var delay = this.random ? randomize(this.time) : this.time;
var msg = "Setting switch to " + on
if (this.random && !this.stateful) {
if (on && !this.reverse || !on && this.reverse) {
msg = msg + " (random delay " + delay + "ms)"
}
}
if( ! this.disableLogging ) {
this.log(msg);
}
if (on && !this.reverse && !this.stateful) {
if (this.resettable) {
clearTimeout(this.timer);
}
this.timer = setTimeout(function() {
this._service.setCharacteristic(Characteristic.On, false);
}.bind(this), delay);
} else if (!on && this.reverse && !this.stateful) {
if (this.resettable) {
clearTimeout(this.timer);
}
this.timer = setTimeout(function() {
this._service.setCharacteristic(Characteristic.On, true);
}.bind(this), delay);
}
if (this.stateful) {
this.storage.setItemSync(this.name, on);
}
callback();
}