sinricpro 2.5.1 → 2.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/npm-publish.yml +32 -32
- package/CHANGELOG.md +6 -0
- package/README.md +15 -15
- package/lib/cbhandler.js +202 -199
- package/lib/contorllers/mode_controller.js +21 -0
- package/lib/contorllers/range_value_controller.js +36 -32
- package/lib/contorllers/speaker_controller.js +2 -9
- package/lib/contorllers/toggle_state_controller.js +18 -17
- package/lib/events/event_handler.js +3 -1
- package/lib/sinrcpro.js +127 -121
- package/package.json +37 -38
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
|
|
2
|
-
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
|
|
3
|
-
|
|
4
|
-
name: Node.js Package
|
|
5
|
-
|
|
6
|
-
on:
|
|
7
|
-
release:
|
|
8
|
-
types: [created]
|
|
9
|
-
|
|
10
|
-
jobs:
|
|
11
|
-
build:
|
|
12
|
-
runs-on: ubuntu-latest
|
|
13
|
-
steps:
|
|
14
|
-
- uses: actions/checkout@v2
|
|
15
|
-
- uses: actions/setup-node@v2
|
|
16
|
-
with:
|
|
17
|
-
node-version: 16
|
|
18
|
-
- run: npm ci
|
|
19
|
-
|
|
20
|
-
publish-npm:
|
|
21
|
-
needs: build
|
|
22
|
-
runs-on: ubuntu-latest
|
|
23
|
-
steps:
|
|
24
|
-
- uses: actions/checkout@v2
|
|
25
|
-
- uses: actions/setup-node@v2
|
|
26
|
-
with:
|
|
27
|
-
node-version: 16
|
|
28
|
-
registry-url: https://registry.npmjs.org/
|
|
29
|
-
- run: npm ci
|
|
30
|
-
- run: npm publish
|
|
31
|
-
env:
|
|
32
|
-
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
|
|
1
|
+
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
|
|
2
|
+
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
|
|
3
|
+
|
|
4
|
+
name: Node.js Package
|
|
5
|
+
|
|
6
|
+
on:
|
|
7
|
+
release:
|
|
8
|
+
types: [created]
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
build:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v2
|
|
15
|
+
- uses: actions/setup-node@v2
|
|
16
|
+
with:
|
|
17
|
+
node-version: 16
|
|
18
|
+
- run: npm ci
|
|
19
|
+
|
|
20
|
+
publish-npm:
|
|
21
|
+
needs: build
|
|
22
|
+
runs-on: ubuntu-latest
|
|
23
|
+
steps:
|
|
24
|
+
- uses: actions/checkout@v2
|
|
25
|
+
- uses: actions/setup-node@v2
|
|
26
|
+
with:
|
|
27
|
+
node-version: 16
|
|
28
|
+
registry-url: https://registry.npmjs.org/
|
|
29
|
+
- run: npm ci
|
|
30
|
+
- run: npm publish
|
|
31
|
+
env:
|
|
32
|
+
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
|
package/CHANGELOG.md
ADDED
package/README.md
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
# SinricPro (NodeJs-SDK)
|
|
2
|
-
|
|
3
|
-
[](https://www.npmjs.com/package/sinricpro)
|
|
4
|
-
[](https://www.npmjs.com/package/sinricpro)
|
|
5
|
-
[](https://www.npmjs.com/package/sinricpro)
|
|
6
|
-
[](https://www.npmjs.com/package/sinricpro)
|
|
7
|
-
|
|
8
|
-
### Installation
|
|
9
|
-
|
|
10
|
-
```
|
|
11
|
-
npm install sinricpro
|
|
12
|
-
```
|
|
13
|
-
|
|
14
|
-
### Examples
|
|
15
|
-
Check the examples [here](https://github.com/sinricpro/nodejs-sdk/tree/master/examples)
|
|
1
|
+
# SinricPro (NodeJs-SDK)
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/sinricpro)
|
|
4
|
+
[](https://www.npmjs.com/package/sinricpro)
|
|
5
|
+
[](https://www.npmjs.com/package/sinricpro)
|
|
6
|
+
[](https://www.npmjs.com/package/sinricpro)
|
|
7
|
+
|
|
8
|
+
### Installation
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
npm install sinricpro
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
### Examples
|
|
15
|
+
Check the examples [here](https://github.com/sinricpro/nodejs-sdk/tree/master/examples)
|
package/lib/cbhandler.js
CHANGED
|
@@ -1,199 +1,202 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2019 Sinric. All rights reserved.
|
|
3
|
-
* Licensed under Creative Commons Attribution-Share Alike (CC BY-SA)
|
|
4
|
-
*
|
|
5
|
-
* This file is part of the Sinric Pro (https://github.com/sinricpro/)
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
const { getSignature, verifySignature } = require('./signature');
|
|
9
|
-
const { powerState, powerLevel, powerLevelAdjust } = require('./contorllers/power_controller');
|
|
10
|
-
const { Brightness, BrightnessAdjust } = require('./contorllers/brightness_controller');
|
|
11
|
-
const { Color } = require('./contorllers/color_controller');
|
|
12
|
-
const { ColorTemperature } = require('./contorllers/color_temperature');
|
|
13
|
-
const { ThermoStatMode, ThermoStatTemperature, Lock } = require('./contorllers/sensors_controller');
|
|
14
|
-
const { RangeValue, RangeValueAdjust } = require('./contorllers/range_value_controller');
|
|
15
|
-
const { Volume, VolumeAdjust, Mute } = require('./contorllers/tv_controller');
|
|
16
|
-
const { ChangeChannel, SkipChannel, Media } = require('./contorllers/tv_controller');
|
|
17
|
-
const { Input } = require('./contorllers/tv_controller');
|
|
18
|
-
const { Bands,
|
|
19
|
-
const { AdjustBands } = require('./contorllers/speaker_controller');
|
|
20
|
-
const { ToggleState } = require('./contorllers/toggle_state_controller');
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
})
|
|
199
|
-
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2019 Sinric. All rights reserved.
|
|
3
|
+
* Licensed under Creative Commons Attribution-Share Alike (CC BY-SA)
|
|
4
|
+
*
|
|
5
|
+
* This file is part of the Sinric Pro (https://github.com/sinricpro/)
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const { getSignature, verifySignature } = require('./signature');
|
|
9
|
+
const { powerState, powerLevel, powerLevelAdjust } = require('./contorllers/power_controller');
|
|
10
|
+
const { Brightness, BrightnessAdjust } = require('./contorllers/brightness_controller');
|
|
11
|
+
const { Color } = require('./contorllers/color_controller');
|
|
12
|
+
const { ColorTemperature } = require('./contorllers/color_temperature');
|
|
13
|
+
const { ThermoStatMode, ThermoStatTemperature, Lock } = require('./contorllers/sensors_controller');
|
|
14
|
+
const { RangeValue, RangeValueAdjust } = require('./contorllers/range_value_controller');
|
|
15
|
+
const { Volume, VolumeAdjust, Mute } = require('./contorllers/tv_controller');
|
|
16
|
+
const { ChangeChannel, SkipChannel, Media } = require('./contorllers/tv_controller');
|
|
17
|
+
const { Input } = require('./contorllers/tv_controller');
|
|
18
|
+
const { Bands, BandReset } = require('./contorllers/speaker_controller');
|
|
19
|
+
const { AdjustBands } = require('./contorllers/speaker_controller');
|
|
20
|
+
const { ToggleState } = require('./contorllers/toggle_state_controller');
|
|
21
|
+
const { Mode } = require('./contorllers/mode_controller');
|
|
22
|
+
|
|
23
|
+
const jsonResponse = (client, defaultPayload, payloadValue, instanceId) => {
|
|
24
|
+
const header = {
|
|
25
|
+
payloadVersion: 2,
|
|
26
|
+
signatureVersion: 1,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
let payload = {
|
|
30
|
+
action: defaultPayload.action,
|
|
31
|
+
clientId: defaultPayload.clientId,
|
|
32
|
+
createdAt: Math.floor(new Date() / 1000),
|
|
33
|
+
deviceId: defaultPayload.deviceId,
|
|
34
|
+
message: 'OK',
|
|
35
|
+
replyToken: defaultPayload.replyToken,
|
|
36
|
+
success: true,
|
|
37
|
+
type: 'response',
|
|
38
|
+
value: payloadValue,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
if(instanceId) payload.instanceId = instanceId;
|
|
42
|
+
|
|
43
|
+
const signature = {
|
|
44
|
+
HMAC: getSignature(client, JSON.stringify(payload)),
|
|
45
|
+
};
|
|
46
|
+
return { header, payload, signature };
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const handlePayload = (client, payload, signature, callbacks) => verifySignature(client, payload, signature)
|
|
50
|
+
.then((action) => {
|
|
51
|
+
/* eslint no-else-return: ["error", {allowElseIf: true}] */
|
|
52
|
+
if (action === 'setPowerState') {
|
|
53
|
+
return powerState(payload, callbacks)
|
|
54
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
55
|
+
.catch((err) => {
|
|
56
|
+
if (process.env.SR_DEBUG) console.log(err.message);
|
|
57
|
+
});
|
|
58
|
+
} else if (action === 'setPowerLevel') {
|
|
59
|
+
return powerLevel(payload, callbacks)
|
|
60
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
61
|
+
.catch((err) => {
|
|
62
|
+
if (process.env.SR_DEBUG) console.log(err.message);
|
|
63
|
+
});
|
|
64
|
+
} else if (action === 'adjustPowerLevel') {
|
|
65
|
+
return powerLevelAdjust(payload, callbacks)
|
|
66
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
67
|
+
.catch((err) => {
|
|
68
|
+
if (process.env.SR_DEBUG) console.log(err.message);
|
|
69
|
+
});
|
|
70
|
+
} else if (action === 'setBrightness') {
|
|
71
|
+
return Brightness(payload, callbacks)
|
|
72
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
73
|
+
.catch((err) => {
|
|
74
|
+
if (process.env.SR_DEBUG) console.log(err.message);
|
|
75
|
+
});
|
|
76
|
+
} else if (action === 'adjustBrightness') {
|
|
77
|
+
return BrightnessAdjust(payload, callbacks)
|
|
78
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
79
|
+
.catch((err) => {
|
|
80
|
+
if (process.env.SR_DEBUG) console.log(err.message);
|
|
81
|
+
});
|
|
82
|
+
} else if (action === 'setColor') {
|
|
83
|
+
return Color(payload, callbacks)
|
|
84
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
85
|
+
.catch((err) => {
|
|
86
|
+
if (process.env.SR_DEBUG) console.log(err.message);
|
|
87
|
+
});
|
|
88
|
+
} else if (action === 'setColorTemperature') {
|
|
89
|
+
return ColorTemperature(payload, callbacks)
|
|
90
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
91
|
+
.catch((err) => {
|
|
92
|
+
if (process.env.SR_DEBUG) console.log(err.message);
|
|
93
|
+
});
|
|
94
|
+
} else if (action === 'setThermostatMode') {
|
|
95
|
+
return ThermoStatMode(payload, callbacks)
|
|
96
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
97
|
+
.catch((err) => {
|
|
98
|
+
if (process.env.SR_DEBUG) console.log(err.message);
|
|
99
|
+
});
|
|
100
|
+
} else if (action === 'targetTemperature') {
|
|
101
|
+
return ThermoStatTemperature(payload, callbacks)
|
|
102
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
103
|
+
.catch((err) => {
|
|
104
|
+
if (process.env.SR_DEBUG) console.log(err.message);
|
|
105
|
+
});
|
|
106
|
+
} else if (action === 'setRangeValue') {
|
|
107
|
+
return RangeValue(payload, callbacks)
|
|
108
|
+
.then(([value, instanceId]) => jsonResponse(client, payload, value, instanceId))
|
|
109
|
+
.catch((err) => {
|
|
110
|
+
if (process.env.SR_DEBUG) console.log(err.message);
|
|
111
|
+
});
|
|
112
|
+
} else if (action === 'adjustRangeValue') {
|
|
113
|
+
return RangeValueAdjust(payload, callbacks)
|
|
114
|
+
.then(([value, instanceId]) => jsonResponse(client, payload, value, instanceId))
|
|
115
|
+
.catch((err) => {
|
|
116
|
+
if (process.env.SR_DEBUG) console.log(err.message);
|
|
117
|
+
});
|
|
118
|
+
} else if (action === 'setVolume') {
|
|
119
|
+
return Volume(payload, callbacks)
|
|
120
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
121
|
+
.catch((err) => {
|
|
122
|
+
if (process.env.SR_DEBUG) console.log(err.message);
|
|
123
|
+
});
|
|
124
|
+
} else if (action === 'adjustVolume') {
|
|
125
|
+
return VolumeAdjust(payload, callbacks)
|
|
126
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
127
|
+
.catch((err) => {
|
|
128
|
+
if (process.env.SR_DEBUG) console.log(err.message);
|
|
129
|
+
});
|
|
130
|
+
} else if (action === 'selectInput') {
|
|
131
|
+
return Input(payload, callbacks)
|
|
132
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
133
|
+
.catch((err) => {
|
|
134
|
+
if (process.env.SR_DEBUG) console.log(err.message);
|
|
135
|
+
});
|
|
136
|
+
} else if (action === 'mediaControl') {
|
|
137
|
+
return Media(payload, callbacks)
|
|
138
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
139
|
+
.catch((err) => {
|
|
140
|
+
if (process.env.SR_DEBUG) console.log(err.message);
|
|
141
|
+
});
|
|
142
|
+
} else if (action === 'changeChannel') {
|
|
143
|
+
return ChangeChannel(payload, callbacks)
|
|
144
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
145
|
+
.catch((err) => {
|
|
146
|
+
if (process.env.SR_DEBUG) console.log(err.message);
|
|
147
|
+
});
|
|
148
|
+
} else if (action === 'skipChannels') {
|
|
149
|
+
return SkipChannel(payload, callbacks)
|
|
150
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
151
|
+
.catch((err) => {
|
|
152
|
+
if (process.env.SR_DEBUG) console.log(err.message);
|
|
153
|
+
});
|
|
154
|
+
} else if (action === 'setBands') {
|
|
155
|
+
return Bands(payload, callbacks)
|
|
156
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
157
|
+
.catch((err) => {
|
|
158
|
+
if (process.env.SR_DEBUG) console.log(err.message);
|
|
159
|
+
});
|
|
160
|
+
} else if (action === 'adjustBands') {
|
|
161
|
+
return AdjustBands(payload, callbacks)
|
|
162
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
163
|
+
.catch((err) => {
|
|
164
|
+
if (process.env.SR_DEBUG) console.log(err.message);
|
|
165
|
+
});
|
|
166
|
+
} else if (action === 'resetBands') {
|
|
167
|
+
return BandReset(payload, callbacks)
|
|
168
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
169
|
+
.catch((err) => {
|
|
170
|
+
if (process.env.SR_DEBUG) console.log(err.message);
|
|
171
|
+
});
|
|
172
|
+
} else if (action === 'setMode') {
|
|
173
|
+
return Mode(payload, callbacks)
|
|
174
|
+
.then(([value, instanceId]) => jsonResponse(client, payload, value, instanceId))
|
|
175
|
+
.catch((err) => {
|
|
176
|
+
if (process.env.SR_DEBUG) console.log(err.message);
|
|
177
|
+
});
|
|
178
|
+
} else if (action === 'setLockState') {
|
|
179
|
+
return Lock(payload, callbacks)
|
|
180
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
181
|
+
.catch((err) => {
|
|
182
|
+
if (process.env.SR_DEBUG) console.log(err.message);
|
|
183
|
+
});
|
|
184
|
+
} else if (action === 'setMute') {
|
|
185
|
+
return Mute(payload, callbacks)
|
|
186
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
187
|
+
.catch((err) => {
|
|
188
|
+
if (process.env.SR_DEBUG) console.log(err.message);
|
|
189
|
+
});
|
|
190
|
+
} else if (action === 'setToggleState') {
|
|
191
|
+
return ToggleState(payload, callbacks)
|
|
192
|
+
.then(([value, instanceId]) => jsonResponse(client, payload, value, instanceId))
|
|
193
|
+
.catch((err) => {
|
|
194
|
+
if (process.env.SR_DEBUG) console.log(err.message);
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
return {};
|
|
198
|
+
})
|
|
199
|
+
.catch((err) => {
|
|
200
|
+
console.error(err.message);
|
|
201
|
+
});
|
|
202
|
+
module.exports = handlePayload;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2019 Sinric. All rights reserved.
|
|
3
|
+
* Licensed under Creative Commons Attribution-Share Alike (CC BY-SA)
|
|
4
|
+
*
|
|
5
|
+
* This file is part of the Sinric Pro (https://github.com/sinricpro/)
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const sinricData = require('../storage');
|
|
9
|
+
|
|
10
|
+
const Mode = (payload, callbacks) => {
|
|
11
|
+
const resp = callbacks.setMode(payload.deviceId, payload.value.mode, payload.instanceId);
|
|
12
|
+
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
if (resp) resolve([payload.value, payload.instanceId]);
|
|
15
|
+
else reject(new Error('Mode undefined'));
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
module.exports = {
|
|
20
|
+
Mode
|
|
21
|
+
};
|
|
@@ -1,32 +1,36 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const resp = callbacks.setRangeValue(payload.deviceId, sinricProData.rangeValue);
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2019 Sinric. All rights reserved.
|
|
3
|
+
* Licensed under Creative Commons Attribution-Share Alike (CC BY-SA)
|
|
4
|
+
*
|
|
5
|
+
* This file is part of the Sinric Pro (https://github.com/sinricpro/)
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const sinricProData = require('../storage');
|
|
9
|
+
|
|
10
|
+
const RangeValue = (payload, callbacks) => {
|
|
11
|
+
sinricProData.rangeValue = payload.value.rangeValue;
|
|
12
|
+
|
|
13
|
+
const resp = callbacks.setRangeValue(payload.deviceId, sinricProData.rangeValue, payload.instanceId);
|
|
14
|
+
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
16
|
+
if (payload.value.rangeValue !== undefined && resp) resolve([payload.value, payload.instanceId]);
|
|
17
|
+
else reject(new Error('RangeValue is undefined'));
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const RangeValueAdjust = (payload, callbacks) => {
|
|
22
|
+
sinricProData.rangeValue += payload.value.rangeValueDelta;
|
|
23
|
+
|
|
24
|
+
if (sinricProData.rangeValue < 0) sinricProData.rangeValue = 0;
|
|
25
|
+
else if (sinricProData.rangeValue > 100) sinricProData.rangeValue = 100;
|
|
26
|
+
|
|
27
|
+
const resp = callbacks.adjustRangeValue(payload.deviceId, sinricProData.rangeValue, payload.instanceId);
|
|
28
|
+
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
if (payload.value.rangeValueDelta && resp) resolve([{ rangeValue: sinricProData.rangeValue }, payload.instanceId]);
|
|
31
|
+
else reject(new Error('RangeValue undefined'));
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
module.exports = { RangeValue, RangeValueAdjust };
|
|
@@ -18,14 +18,6 @@ const Bands = (payload, callbacks) => {
|
|
|
18
18
|
});
|
|
19
19
|
};
|
|
20
20
|
|
|
21
|
-
const Mode = (payload, callbacks) => {
|
|
22
|
-
const resp = callbacks.setMode(payload.deviceId, payload.value.mode);
|
|
23
|
-
return new Promise((resolve, reject) => {
|
|
24
|
-
if (resp) resolve(payload.value);
|
|
25
|
-
else reject(new Error('Mode undefined'));
|
|
26
|
-
});
|
|
27
|
-
};
|
|
28
|
-
|
|
29
21
|
const BandReset = (payload, callbacks) => {
|
|
30
22
|
const resp = callbacks.resetBands(payload.deviceId, payload.value);
|
|
31
23
|
return new Promise((resolve, reject) => {
|
|
@@ -45,6 +37,7 @@ const AdjustBands = (payload, callbacks) => {
|
|
|
45
37
|
else reject(new Error('Rest Bands Failed'));
|
|
46
38
|
});
|
|
47
39
|
};
|
|
40
|
+
|
|
48
41
|
module.exports = {
|
|
49
|
-
Bands,
|
|
42
|
+
Bands, BandReset, AdjustBands,
|
|
50
43
|
};
|
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2019 Sinric. All rights reserved.
|
|
3
|
-
* Licensed under Creative Commons Attribution-Share Alike (CC BY-SA)
|
|
4
|
-
*
|
|
5
|
-
* This file is part of the Sinric Pro (https://github.com/sinricpro/)
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
const ToggleState = (payload, callbacks) => {
|
|
9
|
-
const resp = callbacks.setToggleState(payload.deviceId,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2019 Sinric. All rights reserved.
|
|
3
|
+
* Licensed under Creative Commons Attribution-Share Alike (CC BY-SA)
|
|
4
|
+
*
|
|
5
|
+
* This file is part of the Sinric Pro (https://github.com/sinricpro/)
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const ToggleState = (payload, callbacks) => {
|
|
9
|
+
const resp = callbacks.setToggleState(payload.deviceId, payload.value.state, payload.instanceId);
|
|
10
|
+
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
if (resp) resolve([payload.value, payload.instanceId]);
|
|
13
|
+
else reject(new Error('Toggle State Error'));
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
module.exports = {
|
|
18
|
+
ToggleState
|
|
18
19
|
};
|
|
@@ -86,7 +86,9 @@ const raiseEvent = async (client, eventName, deviceId, value) => {
|
|
|
86
86
|
if (actionArr.includes(eventName)) {
|
|
87
87
|
try {
|
|
88
88
|
await burstyLimiter.consume('queue');
|
|
89
|
-
|
|
89
|
+
const request = JSON.stringify(getJson(client, eventName, deviceId, value));
|
|
90
|
+
if (process.env.SR_DEBUG) console.log('>> %j', request);
|
|
91
|
+
client.send(request);
|
|
90
92
|
} catch (rlRejected) {
|
|
91
93
|
console.log('Too many events!');
|
|
92
94
|
}
|
package/lib/sinrcpro.js
CHANGED
|
@@ -1,121 +1,127 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2019 Sinric. All rights reserved.
|
|
3
|
-
* Licensed under Creative Commons Attribution-Share Alike (CC BY-SA)
|
|
4
|
-
*
|
|
5
|
-
* This file is part of the Sinric Pro (https://github.com/sinricpro/)
|
|
6
|
-
*
|
|
7
|
-
*/
|
|
8
|
-
const Websocket = require("ws");
|
|
9
|
-
const rx = require("rxjs");
|
|
10
|
-
|
|
11
|
-
const callbackHandler = require('./cbhandler');
|
|
12
|
-
const { raiseEvent, eventNames } = require('./events/event_handler');
|
|
13
|
-
const SinricProUdp = require('./sinricproudp');
|
|
14
|
-
const sdkVersion = require('./../package.json').version;
|
|
15
|
-
|
|
16
|
-
class SinricPro extends Websocket {
|
|
17
|
-
constructor(appkey, deviceids, secretKey, restoreStates) {
|
|
18
|
-
super("ws://ws.sinric.pro", {
|
|
19
|
-
headers: {
|
|
20
|
-
appkey,
|
|
21
|
-
deviceids: deviceids.join(';'),
|
|
22
|
-
platform: 'nodejs',
|
|
23
|
-
sdkversion: sdkVersion,
|
|
24
|
-
restoredevicestates: restoreStates,
|
|
25
|
-
},
|
|
26
|
-
});
|
|
27
|
-
this._secretKey = secretKey;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
get getSecretKey() {
|
|
31
|
-
return this._secretKey;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
set setSecretKey(key) {
|
|
35
|
-
this._secretKey = key;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const startSinricPro = (client, callbacks) => {
|
|
40
|
-
client.on('open', () => {
|
|
41
|
-
console.log('Connected to Sinric Pro');
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
client.on("message", (data) => {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
console.log(
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
client.on(
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
.
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
.
|
|
99
|
-
observer.
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2019 Sinric. All rights reserved.
|
|
3
|
+
* Licensed under Creative Commons Attribution-Share Alike (CC BY-SA)
|
|
4
|
+
*
|
|
5
|
+
* This file is part of the Sinric Pro (https://github.com/sinricpro/)
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
const Websocket = require("ws");
|
|
9
|
+
const rx = require("rxjs");
|
|
10
|
+
|
|
11
|
+
const callbackHandler = require('./cbhandler');
|
|
12
|
+
const { raiseEvent, eventNames } = require('./events/event_handler');
|
|
13
|
+
const SinricProUdp = require('./sinricproudp');
|
|
14
|
+
const sdkVersion = require('./../package.json').version;
|
|
15
|
+
|
|
16
|
+
class SinricPro extends Websocket {
|
|
17
|
+
constructor(appkey, deviceids, secretKey, restoreStates) {
|
|
18
|
+
super("ws://ws.sinric.pro", {
|
|
19
|
+
headers: {
|
|
20
|
+
appkey,
|
|
21
|
+
deviceids: deviceids.join(';'),
|
|
22
|
+
platform: 'nodejs',
|
|
23
|
+
sdkversion: sdkVersion,
|
|
24
|
+
restoredevicestates: restoreStates,
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
this._secretKey = secretKey;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
get getSecretKey() {
|
|
31
|
+
return this._secretKey;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
set setSecretKey(key) {
|
|
35
|
+
this._secretKey = key;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const startSinricPro = (client, callbacks) => {
|
|
40
|
+
client.on('open', () => {
|
|
41
|
+
console.log('Connected to Sinric Pro');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
client.on("message", (data) => {
|
|
45
|
+
if (process.env.SR_DEBUG) console.log('<< %s', data.toString());
|
|
46
|
+
|
|
47
|
+
const parsedData = JSON.parse(data);
|
|
48
|
+
|
|
49
|
+
if (parsedData.timestamp) {
|
|
50
|
+
if (Math.floor(new Date() / 1000) - parsedData.timestamp > 60000) {
|
|
51
|
+
if (process.env.SR_DEBUG) console.log("Timestamp is not in sync");
|
|
52
|
+
} else if (process.env.SR_DEBUG) console.log("TimeStamp is in sync");
|
|
53
|
+
} else {
|
|
54
|
+
const response = callbackHandler(
|
|
55
|
+
client,
|
|
56
|
+
parsedData.payload,
|
|
57
|
+
parsedData.signature.HMAC,
|
|
58
|
+
callbacks,
|
|
59
|
+
);
|
|
60
|
+
response
|
|
61
|
+
.then((res) => {
|
|
62
|
+
const reqResponse = JSON.stringify(res);
|
|
63
|
+
if (process.env.SR_DEBUG) console.log('>> %s', reqResponse);
|
|
64
|
+
client.send(reqResponse);
|
|
65
|
+
})
|
|
66
|
+
.catch((err) => {
|
|
67
|
+
console.log(err.message);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
client.on("close", () => {
|
|
73
|
+
if (callbacks.onDisconnect) {
|
|
74
|
+
callbacks.onDisconnect();
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const startSinricProObservable = (client, callbacks) => new rx.Observable((observer) => {
|
|
80
|
+
client.on('open', () => {
|
|
81
|
+
console.log('Connected to Sinric Pro');
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
client.on("message", (data) => {
|
|
85
|
+
const parsedData = JSON.parse(data);
|
|
86
|
+
if (parsedData.timestamp) {
|
|
87
|
+
if (Math.floor(new Date() / 1000) - parsedData.timestamp > 60000) {
|
|
88
|
+
if (process.env.SR_DEBUG) console.log("Timestamp is not in sync");
|
|
89
|
+
} else if (process.env.SR_DEBUG) console.log("TimeStamp is in sync");
|
|
90
|
+
} else {
|
|
91
|
+
const response = callbackHandler(
|
|
92
|
+
client,
|
|
93
|
+
parsedData.payload,
|
|
94
|
+
parsedData.signature.HMAC,
|
|
95
|
+
callbacks,
|
|
96
|
+
);
|
|
97
|
+
response
|
|
98
|
+
.then((res) => {
|
|
99
|
+
observer.next(res);
|
|
100
|
+
const reqResponse = JSON.stringify(res);
|
|
101
|
+
if (process.env.SR_DEBUG) console.log('>> %s', reqResponse);
|
|
102
|
+
client.send(reqResponse);
|
|
103
|
+
})
|
|
104
|
+
.catch((err) => {
|
|
105
|
+
observer.error(err.message);
|
|
106
|
+
console.log(err.message);
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
client.on("close", () => {
|
|
112
|
+
observer.complete();
|
|
113
|
+
if (callbacks.onDisconnect) {
|
|
114
|
+
callbacks.onDisconnect();
|
|
115
|
+
}
|
|
116
|
+
console.log("Disconnected");
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
module.exports = {
|
|
121
|
+
SinricPro,
|
|
122
|
+
startSinricPro,
|
|
123
|
+
startSinricProObservable,
|
|
124
|
+
raiseEvent,
|
|
125
|
+
eventNames,
|
|
126
|
+
SinricProUdp,
|
|
127
|
+
};
|
package/package.json
CHANGED
|
@@ -1,38 +1,37 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "sinricpro",
|
|
3
|
-
"version": "2.
|
|
4
|
-
"description": "Sinric Pro Nodejs SDK",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"repository": {
|
|
7
|
-
"type": "git",
|
|
8
|
-
"url": "git+https://github.com/sinricpro/nodejs-sdk.git"
|
|
9
|
-
},
|
|
10
|
-
"author": "
|
|
11
|
-
"license": "ISC",
|
|
12
|
-
"bugs": {
|
|
13
|
-
"url": "https://github.com/sinricpro/nodejs-sdk/issues"
|
|
14
|
-
},
|
|
15
|
-
"homepage": "https://github.com/sinricpro/nodejs-sdk#readme",
|
|
16
|
-
"dependencies": {
|
|
17
|
-
"dgram": "^1.0.1",
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
"eslint": "^
|
|
31
|
-
"eslint-config-
|
|
32
|
-
"eslint-
|
|
33
|
-
"eslint-plugin-
|
|
34
|
-
"eslint-plugin-
|
|
35
|
-
"eslint-plugin-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "sinricpro",
|
|
3
|
+
"version": "2.6.1",
|
|
4
|
+
"description": "Sinric Pro Nodejs SDK",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/sinricpro/nodejs-sdk.git"
|
|
9
|
+
},
|
|
10
|
+
"author": "SinricPro",
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/sinricpro/nodejs-sdk/issues"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://github.com/sinricpro/nodejs-sdk#readme",
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"dgram": "^1.0.1",
|
|
18
|
+
"process": "^0.11.10",
|
|
19
|
+
"rate-limiter-flexible": "^2.3.6",
|
|
20
|
+
"rxjs": "^6.6.3",
|
|
21
|
+
"uuid": "^3.3.3",
|
|
22
|
+
"ws": "^8.5.0"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"sinricpro",
|
|
26
|
+
"sinric"
|
|
27
|
+
],
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"eslint": "^8.9.0",
|
|
30
|
+
"eslint-config-airbnb-base": "^14.0.0",
|
|
31
|
+
"eslint-config-standard": "^14.1.0",
|
|
32
|
+
"eslint-plugin-import": "^2.18.2",
|
|
33
|
+
"eslint-plugin-node": "^10.0.0",
|
|
34
|
+
"eslint-plugin-promise": "^4.2.1",
|
|
35
|
+
"eslint-plugin-standard": "^4.0.1"
|
|
36
|
+
}
|
|
37
|
+
}
|