forked from Supereg/homebridge-http-temperature-sensor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
188 lines (153 loc) · 6.46 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
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
// ISC License
// Original work Copyright (c) 2017, Andreas Bauer
// Modified work Copyright 2018, Sander van Woensel
"use strict";
// -----------------------------------------------------------------------------
// Module variables
// -----------------------------------------------------------------------------
let Service, Characteristic, api;
// -----------------------------------------------------------------------------
// Constants
// -----------------------------------------------------------------------------
const configParser = require("homebridge-http-base").configParser;
const http = require("homebridge-http-base").http;
const PullTimer = require("homebridge-http-base").PullTimer;
const PACKAGE_JSON = require('./package.json');
const MANUFACTURER = PACKAGE_JSON.author.name;
const SERIAL_NUMBER = '001';
const MODEL = PACKAGE_JSON.name;
const FIRMWARE_REVISION = PACKAGE_JSON.version;
const MIN_LUX_VALUE = 0.0;
const MAX_LUX_VALUE = Math.pow(2, 16) - 1.0; // Default BH1750 max 16bit lux value.
// -----------------------------------------------------------------------------
// Exports
// -----------------------------------------------------------------------------
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
api = homebridge;
homebridge.registerAccessory(MODEL, "HttpAmbientLightSensor", HttpAmbientLightSensor);
};
// -----------------------------------------------------------------------------
// Module public functions
// -----------------------------------------------------------------------------
function HttpAmbientLightSensor(log, config) {
this.log = log;
this.name = config.name;
this.minSensorValue = config.minValue || MIN_LUX_VALUE;
this.maxSensorValue = config.maxValue || MAX_LUX_VALUE;
if (config.getUrl) {
try {
this.getUrl = configParser.parseUrlProperty(config.getUrl);
} catch (error) {
this.log.error("Error occurred while parsing 'getUrl': " + error.message);
this.log.error("Aborting...");
return;
}
}
else {
this.log.error("Property 'getUrl' is required!");
this.log.error("Aborting...");
return;
}
if(config.identifyUrl) {
try {
this.identifyUrl = configParser.parseUrlProperty(config.identifyUrl);
} catch (error) {
this.log.error("Error occurred while parsing 'identifyUrl': " + error.message);
this.log.error("Aborting...");
return;
}
}
this.homebridgeService = new Service.LightSensor(this.name);
this.homebridgeService.getCharacteristic(Characteristic.CurrentAmbientLightLevel)
.setProps({
minValue: this.minSensorValue,
maxValue: this.maxSensorValue
})
.on("get", this.getSensorValue.bind(this));
if (config.pullInterval) {
this.pullTimer = new PullTimer(log, config.pullInterval, this.getSensorValue.bind(this), value => {
this.homebridgeService.setCharacteristic(Characteristic.CurrentAmbientLightLevel, value);
});
this.pullTimer.start();
}
// Register notification server.
api.on('didFinishLaunching', function() {
// Check if notificationRegistration is set and user specified notificationID.
// if not 'notificationRegistration' is probably not installed on the system.
if (global.notificationRegistration && typeof global.notificationRegistration === "function" &&
config.notificationID) {
try {
global.notificationRegistration(config.notificationID, this.handleNotification.bind(this), config.notificationPassword);
} catch (error) {
// notificationID is already taken.
}
}
}.bind(this));
}
HttpAmbientLightSensor.prototype = {
identify: function (callback) {
this.log.info("Identify requested");
if (this.identifyUrl) {
http.httpRequest(this.identifyUrl, (error, response, body) => {
if (error) {
this.log.error("identify() failed: %s", error.message);
callback(error);
}
else if (response.statusCode !== 200) {
this.log.error("identify() returned http error: %s", response.statusCode);
callback(new Error("Got http error code " + response.statusCode));
}
else {
callback(null);
}
});
}
else {
callback(null);
}
},
getServices: function () {
const informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, MANUFACTURER)
.setCharacteristic(Characteristic.Model, MODEL)
.setCharacteristic(Characteristic.SerialNumber, SERIAL_NUMBER)
.setCharacteristic(Characteristic.FirmwareRevision, FIRMWARE_REVISION);
return [informationService, this.homebridgeService];
},
handleNotification: function(body) {
const value = body.value;
let characteristic;
switch (body.characteristic) {
case "CurrentAmbientLightLevel":
characteristic = Characteristic.CurrentAmbientLightLevel;
break;
default:
this.log.warn("Encountered unknown characteristic handling notification: " + body.characteristic);
return;
}
this.log.debug("Update received from device: " + body.characteristic + ": " + body.value);
this.homebridgeService.setCharacteristic(characteristic, value);
},
getSensorValue: function (callback) {
http.httpRequest(this.getUrl, (error, response, body) => {
if (this.pullTimer)
this.pullTimer.resetTimer();
if (error) {
this.log.error("getSensorValue() failed: %s", error.message);
callback(error);
}
else if (response.statusCode !== 200) {
this.log.error("getSensorValue() returned http error: %s", response.statusCode);
callback(new Error("Got http error code " + response.statusCode));
}
else {
const sensorValue = parseFloat(body);
this.log.info("Get sensor value: %s", sensorValue);
callback(null, sensorValue);
}
});
},
};