-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
85 lines (72 loc) · 2.26 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
'use strict'
const request = require('request');
let Service, Characteristic
module.exports = (homebridge) => {
Service = homebridge.hap.Service
Characteristic = homebridge.hap.Characteristic
homebridge.registerAccessory('homebridge-http-dimmer', 'HTTP-DIMMER', DimmerAccessory)
}
class DimmerAccessory {
constructor (log, config) {
this.log = log
this.config = config
this.brightness = 0
this.statusUrl = config.statusUrl
this.onUrl = config.onUrl
this.offUrl = config.offUrl
this.setBrightnessUrl = config.setBrightnessUrl
this.getBrightnessUrl = config.getBrightnessUrl
this.service = new Service.Lightbulb(this.config.name)
}
getServices () {
const informationService = new Service.AccessoryInformation()
.setCharacteristic(Characteristic.Manufacturer, 'illiteratewriter')
.setCharacteristic(Characteristic.Model, 'http-dimmer')
.setCharacteristic(Characteristic.SerialNumber, 'home-http-dimmer')
this.service.getCharacteristic(Characteristic.On)
.on('get', this.getOnCharacteristicHandler.bind(this))
.on('set', this.setOnCharacteristicHandler.bind(this))
this.service.getCharacteristic(Characteristic.Brightness)
.on('get', this.getBrightness.bind(this))
.on('set', this.setBrightness.bind(this));
return [informationService, this.service]
}
getBrightness (callback) {
request(`${this.getBrightnessUrl}`, (err, resp, body) => {
if(err){
this.log(err)
}
callback(null, parseInt(body))
})
}
setBrightness (value, callback) {
this.brightness = value
this.log('setting brightness ', value)
request(`${this.setBrightnessUrl}${value}`, (err, resp, body) => {
callback(null, value)
})
}
setOnCharacteristicHandler (value, callback) {
this.isOn = value
if(value === true){
request(this.onUrl, (err, resp, body) => {
callback(null)
})
} else {
request(this.offUrl, (err, resp, body) => {
callback(null)
})
}
}
getOnCharacteristicHandler (callback) {
request(`${this.statusUrl}`, (err, resp, body) => {
if(body == 0){
this.isOn = false
} else {
this.log()
this.isOn = true
}
callback(null, this.isOn)
})
}
}