-
Notifications
You must be signed in to change notification settings - Fork 0
/
HomebridgeConfig.js
105 lines (86 loc) · 3.49 KB
/
HomebridgeConfig.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
'use strict'
const path = require('path')
const fsExtra = require('fs-extra')
const ip = require('ip')
const { cloneDeep, toUpper } = require('lodash')
const randomMac = require('random-mac')
const homebridgeConfig = require('./homebridge-config.json')
const {
HOMEBRIDGE_PATH,
WRITING_HOMEBRIDGE_CONFIG,
HOMEBRIDGE_PORT,
HOMEBRIDGE_MAC_STORAGE_KEY,
ACCESORY_SWITCH_NAME,
ACCESORY_BUTTON_NAME
} = require('./statics')
class Accesories {
constructor (dpmcPlugin) {
this.plugin = dpmcPlugin
this.tracer = dpmcPlugin.tracer
}
async getPluginConnection () {
const pluginConfig = await this.plugin.config.get()
const pluginApiKeys = await this.plugin.storage.get('apiKeys')
const host = pluginConfig.hostName.length ? pluginConfig.hostName : ip.address()
const secureProtocolSuffix = pluginConfig.sslCert ? 's' : ''
const port = pluginConfig.port
return {
url: `http${secureProtocolSuffix}://${host}:${port}/api/controller/abilities/`,
apiKey: pluginApiKeys.find(apiKey => apiKey.role === 'admin').key
}
}
getAccessoryConfig (accesoryName, ability, pluginConnection) {
return {
accessory: accesoryName,
apiKey: pluginConnection.apiKey,
bridgeUrl: `${pluginConnection.url}${ability._id}`,
servicePackageName: ability.service.package,
serviceName: ability.service.name,
serviceProcessId: ability.service.processId,
abilityName: ability.name,
name: `${ability.service.name} ${ability.name}`
}
}
getSwitchs (abilities, pluginConnection) {
// Abilities with boolean data that have state and action can be considered as homebridge "switches"
const validAbilities = abilities.filter(ability => ability.type === 'boolean' && ability.state === true && ability.action === true)
return validAbilities.map(ability => this.getAccessoryConfig(ACCESORY_SWITCH_NAME, ability, pluginConnection))
}
getButtons (abilities, pluginConnection) {
// Abilities with no defined data that have an action can be considered as Domapic "Buttons", that will be mapped to homebridge "switches". Will return false as state always
const validAbilities = abilities.filter(ability => ability.type === undefined && ability.action === true)
return validAbilities.map(ability => this.getAccessoryConfig(ACCESORY_BUTTON_NAME, ability, pluginConnection))
}
async getAccesories (abilities) {
const pluginConnection = await this.getPluginConnection()
return [
...this.getSwitchs(abilities, pluginConnection),
...this.getButtons(abilities, pluginConnection)
]
}
async getMac () {
let mac
try {
mac = await this.plugin.storage.get(HOMEBRIDGE_MAC_STORAGE_KEY)
} catch (err) {
mac = toUpper(randomMac())
await this.plugin.storage.set(HOMEBRIDGE_MAC_STORAGE_KEY, mac)
}
return mac
}
async write (abilities) {
await this.tracer.info(WRITING_HOMEBRIDGE_CONFIG)
const pluginConfig = await this.plugin.config.get()
const homebridgePath = path.resolve(await this.plugin.storage.getPath(), HOMEBRIDGE_PATH)
this.config = cloneDeep(homebridgeConfig)
this.config.bridge.name = pluginConfig.name
this.config.bridge.username = await this.getMac()
this.config.bridge.port = pluginConfig[HOMEBRIDGE_PORT]
this.config.accessories = await this.getAccesories(abilities)
fsExtra.ensureDirSync(homebridgePath)
await fsExtra.writeJson(path.resolve(homebridgePath, 'config.json'), this.config, {
spaces: 2
})
}
}
module.exports = Accesories