-
Notifications
You must be signed in to change notification settings - Fork 3
/
connector.js
59 lines (54 loc) · 1.88 KB
/
connector.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
'use strict';
/**
* ST Schema connector
*/
const {SchemaConnector, DeviceErrorTypes} = require('st-schema')
const deviceStates = { switch: 'off', level: 100}
const connector = new SchemaConnector()
.enableEventLogging(2)
.discoveryHandler((accessToken, response) => {
const d = response.addDevice('external-device-1', 'Test Dimmer', 'c2c-dimmer')
d.manufacturerName('STS');
d.modelName('Dimmer 1');
})
.stateRefreshHandler((accessToken, response) => {
response.addDevice('external-device-1', [
{
component: 'main',
capability: 'st.switch',
attribute: 'switch',
value: deviceStates.switch
},
{
component: 'main',
capability: 'st.switchLevel',
attribute: 'level',
value: deviceStates.level
}
])
})
.commandHandler((accessToken, response, devices) => {
for (const device of devices) {
const deviceResponse = response.addDevice(device.externalDeviceId);
for (const cmd of device.commands) {
const state = {
component: cmd.component,
capability: cmd.capability
};
if (cmd.capability === 'st.switchLevel' && cmd.command === 'setLevel') {
state.attribute = 'level';
state.value = deviceStates.level = cmd.arguments[0];
deviceResponse.addState(state);
} else if (cmd.capability === 'st.switch') {
state.attribute = 'switch';
state.value = deviceStates.switch = cmd.command === 'on' ? 'on' : 'off';
deviceResponse.addState(state);
} else {
deviceResponse.setError(
`Command '${cmd.command} of capability '${cmd.capability}' not supported`,
DeviceErrorTypes.CAPABILITY_NOT_SUPPORTED)
}
}
}
});
module.exports = connector