Cache.js
1.32 KB
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
'use strict';
const model = require('@peter-murray/hue-bridge-model').model
, LightIdPlaceHolder = require('../placeholders/LightIdPlaceholder')
;
const LIGHT_ID_PLACEHOLDER = new LightIdPlaceHolder();
/**
* @typedef {import('@peter-murray/hue-bridge-model').model.Light} Light
*
* @type {Cache}
*/
module.exports = class Cache {
constructor(data) {
this.data = data;
this._lights = {};
}
/**
* Obtains a known light from the cache
* @param id {Number | String} The id for the light.
* @return {Light}
*/
getLight(id) {
const lightId = LIGHT_ID_PLACEHOLDER.getValue({id: id});
let light = this._lights[lightId];
if (!light) {
let lightData = this.data.lights[lightId];
if (lightData) {
light = model.createFromBridge('light', lightId, lightData);
this._lights[lightId] = light;
}
}
return light;
}
/**
* Get the modelid for the bridge.
* @return {String} The model number fo the bridge.
*/
get modelid() {
// BSB001 is the first generation bridge, BSB002 is the current generation one that can support entertainment API
return this.data.config.modelid;
}
/**
* Get the API version for the bridge.
* @return {String} The Api Version for the bridge.
*/
get apiversion() {
return this.data.config.apiversion;
}
};