forked from GladysAssistant/Gladys
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add internal "created device" events
Fixes GladysAssistant#911
- Loading branch information
Showing
13 changed files
with
267 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const get = require('get-value'); | ||
|
||
const logger = require('../../utils/logger'); | ||
const { EVENTS } = require('../../utils/constants'); | ||
|
||
const FUNC_BY_EVENT = { | ||
[EVENTS.DEVICE.CREATE]: 'onNewDevice', | ||
[EVENTS.DEVICE.UPDATE]: 'onUpdateDevice', | ||
[EVENTS.DEVICE.DELETE]: 'onDeleteDevice', | ||
}; | ||
|
||
/** | ||
* @description Notify a device is created. | ||
* @param {Object} device - Created device. | ||
* @param {string} event - Event to send. | ||
* @example | ||
* device.notify({ service_id: 'a810b8db-6d04-4697-bed3-c4b72c996279'}, 'device.create'); | ||
*/ | ||
function notify(device, event) { | ||
logger.debug(`Notify device ${device.selector} creation`); | ||
|
||
const serviceFuncName = FUNC_BY_EVENT[event]; | ||
|
||
if (!serviceFuncName) { | ||
logger.warn(`Event ${event} not handled to notify device changes`); | ||
} else { | ||
// send global event | ||
this.eventManager.emit(event, device); | ||
|
||
// notify concerned service | ||
const service = this.serviceManager.getServiceById(device.service_id); | ||
if (service === null) { | ||
logger.warn(`Service ${device.service_id} was not found.`); | ||
} else if (typeof get(service, `device.${serviceFuncName}`) !== 'function') { | ||
logger.info(`Function device.${serviceFuncName} in service ${service.name} does not exist.`); | ||
} else { | ||
service.device[serviceFuncName](device); | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
notify, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* @public | ||
* @description Get service by its id. | ||
* @param {string} id - Id of the service to get. | ||
* @returns {Object} Return the service or null if not present. | ||
* @example | ||
* service.getServiceById('99dc10bb-14ab-49dc-bd11-f724e98fc97c'); | ||
*/ | ||
function getServiceById(id) { | ||
return this.stateManager.get('serviceById', id); | ||
} | ||
|
||
module.exports = { | ||
getServiceById, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.