UNPKG

homey-api

Version:
72 lines (61 loc) 2.24 kB
'use strict'; const Manager = require('./Manager'); const Capability = require('./ManagerDevices/Capability'); const Device = require('./ManagerDevices/Device'); const Util = require('../../Util'); class ManagerDevices extends Manager { static CRUD = { ...super.CRUD, Capability, Device, }; scheduleRefresh() { if (this.__refreshTimeout) { clearTimeout(this.__refreshTimeout); } if (this.__pendingRefreshDevicesCall != null) { return; } this.__refreshTimeout = setTimeout(() => { if (this.isConnected()) { this.__debug('Refreshing devices...'); this.__pendingRefreshDevicesCall = this.getDevices({ $cache: false, $updateCache: false }) .then(devices => { const cache = this.__cache['device']; for (const device of Object.values(devices)) { const cachedDevice = cache[device.id]; if (cachedDevice != null) { Object.entries(cachedDevice.__capabilityInstances).forEach(([capabilityId, capabilityInstances]) => { const value = device.capabilitiesObj ? device.capabilitiesObj[capabilityId] != null ? device.capabilitiesObj[capabilityId].value : null : null; const lastUpdated = device.capabilitiesObj ? device.capabilitiesObj[capabilityId] != null ? device.capabilitiesObj[capabilityId].lastUpdated : null : null; for (const capabilityInstance of capabilityInstances) { capabilityInstance.__onCapabilityValue({ capabilityId, value, transactionId: Util.uuid(), transactionTime: lastUpdated, }); } }); } } }) .catch(err => { this.__debug('Failed to refresh devices.', err); }) .finally(() => { this.__pendingRefreshDevicesCall = null; }); } }, 1000); } } module.exports = ManagerDevices;