feat: HA event handling + emitting

This commit is contained in:
NGPixel
2020-04-19 20:26:26 -04:00
committed by Nicolas Giard
parent 6a00a5dbce
commit bd4263ecb2
14 changed files with 109 additions and 33 deletions

View File

@@ -95,7 +95,7 @@ module.exports = {
* @param {Array} keys Array of keys to save
* @returns Promise
*/
async saveToDb(keys) {
async saveToDb(keys, propagate = true) {
try {
for (let key of keys) {
let value = _.get(WIKI.config, key, null)
@@ -107,6 +107,9 @@ module.exports = {
await WIKI.models.settings.query().insert({ key, value })
}
}
if (propagate) {
WIKI.events.outbound.emit('reloadConfig')
}
} catch (err) {
WIKI.logger.error(`Failed to save configuration to DB: ${err.message}`)
return false
@@ -119,5 +122,15 @@ module.exports = {
*/
async applyFlags() {
WIKI.models.knex.client.config.debug = WIKI.config.flags.sqllog
},
/**
* Subscribe to HA propagation events
*/
subscribeToEvents() {
WIKI.events.inbound.on('reloadConfig', async () => {
await WIKI.configSvc.loadFromDb()
await WIKI.configSvc.applyFlags()
})
}
}