-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (95 loc) · 3.78 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
// HomeKit provides an official Service.Speaker type but Apple didn't bother implamenting it in the Home App
// so I am forced use Service.Lightbulb and rether than volume I use brightness.
const request = require('request')
let Service, Characteristic
module.exports = (homebridge) => {
Service = homebridge.hap.Service
Characteristic = homebridge.hap.Characteristic
homebridge.registerAccessory('homebridge-snapcast', 'Snapcast', DimmerAccessory)
}
class DimmerAccessory {
constructor(log, config) {
this.log = log
this.config = config
this.service = new Service.Lightbulb(this.config.name)
this.id = '00:00:00:00:00:00'
this.muted = true
this.volume = 0
}
getServices() {
const informationService = new Service.AccessoryInformation()
.setCharacteristic(Characteristic.Manufacturer, 'Noel Hibbard')
.setCharacteristic(Characteristic.Model, 'Snapcast')
.setCharacteristic(Characteristic.SerialNumber, 'Snapcast')
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.config.serverUrl, {
method: 'POST', json: { id: 1, jsonrpc: '2.0', method: 'Server.GetStatus' }
}, (err, resp, json) => {
if (err) {
this.log(err)
}
var group
if (this.config.groupnumber) {
group = json.result.server.groups[this.config.groupnumber - 1]
} else {
group = json.result.server.groups.filter(group => group.name == this.config.groupname)[0]
}
group.clients.forEach(client => {
if (client.config.name == this.config.name) {
this.id = client.id
this.volume = client.config.volume.percent
}
})
callback(null, this.volume)
})
}
setBrightness(value, callback) {
request(this.config.serverUrl, {
method: 'POST', json: { id: 1, jsonrpc: '2.0', method: 'Client.SetVolume', params: { id: this.id, volume: { percent: value } } }
}, (err, resp, json) => {
if (err) {
this.log(err)
}
callback(null)
})
}
getOnCharacteristicHandler(callback) {
request(this.config.serverUrl, {
method: 'POST', json: { id: 1, jsonrpc: '2.0', method: 'Server.GetStatus' }
}, (err, resp, json) => {
if (err) {
this.log(err)
}
var group
if (this.config.groupnumber) {
group = json.result.server.groups[this.config.groupnumber - 1]
} else {
group = json.result.server.groups.filter(group => group.name == this.config.groupname)[0]
}
group.clients.forEach(client => {
if (client.config.name == this.config.name) {
this.muted = client.config.volume.muted
}
})
callback(null, !this.muted)
})
}
setOnCharacteristicHandler(value, callback) {
request(this.config.serverUrl, {
method: 'POST', json: { id: 1, jsonrpc: '2.0', method: 'Client.SetVolume', params: { id: this.id, volume: { muted: !value } } }
}, (err, resp, json) => {
if (err) {
this.log(err)
}
callback(null)
})
}
}