This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (82 loc) · 2.97 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
"use strict";
const WIFI370 = require('wifi370-js-api');
const NpmAutoUpdate = require('npm-auto-update');
let Service, Characteristic, UUIDGen;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
homebridge.registerAccessory("homebridge-wifi370-led-controller", "wifi370", Wifi370Accessory);
};
function Wifi370Accessory (log, config) {
this.log = log;
this.autoUpdate = config["autoupdate"]!==null;
this.host = config["host"];
this.name = config["name"];
this.npmAutoUpdate = new NpmAutoUpdate(log);
if(this.autoUpdate) this.updatePackage();
this.verifyConfig();
this.ledController = WIFI370(config["controller"], this.host, 5577);
this.lightService = new Service.Lightbulb(this.name);
this.infoService = new Service.AccessoryInformation();
this.uuid = UUIDGen.generate(this.name);
}
Wifi370Accessory.prototype.updatePackage = function () {
this.npmAutoUpdate.checkForUpdate((error, result) => {
if(result) {
this.npmAutoUpdate.updatePackage((error, result) => {
});
}
});
};
Wifi370Accessory.prototype.verifyConfig = function () {
if(!this.host || !this.name) {
this.log.error("Please define name and host in config.json");
}
};
Wifi370Accessory.prototype.getServices = function () {
this.lightService
.getCharacteristic(Characteristic.On)
.on('set', (value, callback) => {
if (value) {
this.ledController.setOn(callback);
} else {
this.ledController.setOff(callback);
}
})
.on('get', (callback) => {
this.ledController.getOn(callback);
});
this.lightService
.addCharacteristic(Characteristic.Brightness)
.on('set', (value, callback) => {
this.ledController.setBrightness(value, callback);
})
.on('get', (callback) => {
this.ledController.getBrightness(callback);
});
this.lightService
.addCharacteristic(Characteristic.Hue)
.on('set', (value, callback) => {
this.ledController.setHue(value, callback);
})
.on('get', (callback) => {
this.ledController.getHue(callback);
});
this.lightService
.addCharacteristic(Characteristic.Saturation)
.on('set', (value, callback) => {
this.ledController.setSaturation(value, callback);
})
.on('get', (callback) => {
this.ledController.getSaturation(callback);
});
this.infoService
.setCharacteristic(Characteristic.Manufacturer, "wifi370")
.setCharacteristic(Characteristic.Model, this.host)
.setCharacteristic(Characteristic.SerialNumber, this.lightService.UUID);
let services = [];
services.push(this.lightService);
services.push(this.infoService);
return services;
};