sinricpro 2.2.8 → 2.5.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 -0
- package/README.md +15 -191
- package/lib/cbhandler.js +199 -192
- package/lib/contorllers/range_value_controller.js +32 -32
- package/lib/contorllers/toggle_state_controller.js +18 -0
- package/lib/contorllers/tv_controller.js +1 -1
- package/lib/sinrcpro.js +121 -122
- package/lib/storage.js +1 -1
- package/notes-to-aruna.txt +6 -0
- package/package.json +38 -42
|
@@ -0,0 +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}}
|
package/README.md
CHANGED
|
@@ -1,191 +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
|
-
###
|
|
15
|
-
|
|
16
|
-
- [websockets](https://www.npmjs.com/package/ws)
|
|
17
|
-
|
|
18
|
-
- [crypto](https://nodejs.org/api/crypto.html)
|
|
19
|
-
|
|
20
|
-
- [uuid](https://www.npmjs.com/package/uuid)
|
|
21
|
-
|
|
22
|
-
- [dgram]()
|
|
23
|
-
|
|
24
|
-
- [rxjs](https://www.npmjs.com/package/rxjs)
|
|
25
|
-
|
|
26
|
-
### Basic example
|
|
27
|
-
|
|
28
|
-
```javascript 1.8
|
|
29
|
-
const {
|
|
30
|
-
SinricPro,
|
|
31
|
-
SinricProActions,
|
|
32
|
-
raiseEvent,
|
|
33
|
-
eventNames,
|
|
34
|
-
SinricProUdp
|
|
35
|
-
} = require("../index"); // Use require('sinricpro'); if you are using NPM
|
|
36
|
-
|
|
37
|
-
const appKey = ""; // d89f1***-****-****-****-************
|
|
38
|
-
const secretKey = ""; // f44d1d31-1c19-****-****-9bc96c34b5bb-d19f42dd-****-****-****-************
|
|
39
|
-
const device1 = ""; // 5d7e7d96069e275ea9******
|
|
40
|
-
const device2 = ""; // 5d80ac5713fa175e99******
|
|
41
|
-
const deviceId = [device1, device2];
|
|
42
|
-
|
|
43
|
-
function setPowerState(deviceid, data) {
|
|
44
|
-
console.log(deviceid, data);
|
|
45
|
-
return true;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const callbacks = {
|
|
49
|
-
setPowerState
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
const sinricpro = new SinricPro(appKey, deviceId, secretKey, true);
|
|
53
|
-
|
|
54
|
-
SinricProActions(sinricpro, callbacks);
|
|
55
|
-
|
|
56
|
-
setInterval(() => {
|
|
57
|
-
raiseEvent(sinricpro, eventNames.powerState, device1, { state: "On" });
|
|
58
|
-
}, 2000);
|
|
59
|
-
|
|
60
|
-
// https://github.com/sinricpro/nodejs-sdk/blob/master/examples/simple-example/simple-example.js
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
### Advanced example
|
|
64
|
-
|
|
65
|
-
#### Add a credential file into your root folder (credential.js)
|
|
66
|
-
|
|
67
|
-
```javascript 1.8
|
|
68
|
-
const credential = {
|
|
69
|
-
appkey: "",
|
|
70
|
-
secretKey: ""
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const deviceIds = {
|
|
74
|
-
deviceId1: "",
|
|
75
|
-
deviceId2: "",
|
|
76
|
-
deviceId3: ""
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
// No need to edit the code below
|
|
80
|
-
|
|
81
|
-
let deviceIdT = [];
|
|
82
|
-
|
|
83
|
-
for (let key in deviceIds) {
|
|
84
|
-
if (deviceIds.hasOwnProperty(key)) {
|
|
85
|
-
deviceIdT.push(deviceIds[key]);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
module.exports = {
|
|
90
|
-
appKey: credential.appkey,
|
|
91
|
-
secretKey: credential.secretKey,
|
|
92
|
-
deviceId: deviceIdT
|
|
93
|
-
};
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
#### Example (app.js)
|
|
97
|
-
|
|
98
|
-
```javascript
|
|
99
|
-
const { appKey, deviceId, secretKey } = require("../credentials");
|
|
100
|
-
const {
|
|
101
|
-
SinricPro,
|
|
102
|
-
SinricProActions,
|
|
103
|
-
raiseEvent,
|
|
104
|
-
eventNames,
|
|
105
|
-
SinricProUdp
|
|
106
|
-
} = require("../index");
|
|
107
|
-
|
|
108
|
-
function setPowerState(deviceid, data) {
|
|
109
|
-
console.log(deviceid, data);
|
|
110
|
-
return true;
|
|
111
|
-
}
|
|
112
|
-
function setPowerLevel(deviceid, data) {
|
|
113
|
-
console.log(deviceid, data);
|
|
114
|
-
return true;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function adjustPowerLevel(deviceid, data) {
|
|
118
|
-
console.log(deviceid, data);
|
|
119
|
-
return true;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
function setColor(deviceid, data) {
|
|
123
|
-
console.log(deviceid, data);
|
|
124
|
-
return true;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
function setRangeValue(deviceid, data) {
|
|
128
|
-
console.log(deviceid, data);
|
|
129
|
-
return true;
|
|
130
|
-
}
|
|
131
|
-
function setLockState(deviceid, data) {
|
|
132
|
-
console.log(deviceid, data);
|
|
133
|
-
return true;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
function setBrightness(deviceid, data) {
|
|
137
|
-
console.log(deviceid, data);
|
|
138
|
-
return true;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
function setVolume(deviceid, data) {
|
|
142
|
-
console.log(deviceid, data);
|
|
143
|
-
return true;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function adjustVolume(deviceid, data) {
|
|
147
|
-
console.log(deviceid, data);
|
|
148
|
-
return true;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
function setMute(deviceid, data) {
|
|
152
|
-
console.log(deviceid, data);
|
|
153
|
-
return true;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const callbacks = {
|
|
157
|
-
setPowerState,
|
|
158
|
-
setPowerLevel,
|
|
159
|
-
adjustPowerLevel,
|
|
160
|
-
setColor,
|
|
161
|
-
setRangeValue,
|
|
162
|
-
setLockState,
|
|
163
|
-
setBrightness,
|
|
164
|
-
setVolume,
|
|
165
|
-
adjustVolume,
|
|
166
|
-
setMute
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
const sinricpro = new SinricPro(appKey, deviceId, secretKey, true);
|
|
170
|
-
SinricProActions(sinricpro, callbacks);
|
|
171
|
-
|
|
172
|
-
setInterval(() => {
|
|
173
|
-
// raiseEvent(sinricpro, eventNames.powerState, 'deviceId', { state: 'On' });
|
|
174
|
-
// raiseEvent(sinricpro, eventNames.setBrightness, 'deviceId', { brightness: 44 });
|
|
175
|
-
// raiseEvent(sinricpro, eventNames.powerLevel, 'deviceId', { powerLevel: 44 });
|
|
176
|
-
// raiseEvent(sinricpro, eventNames.color, 'DeviceId', { color: { b: 0, g: 0, r: 0 } });
|
|
177
|
-
// raiseEvent(sinricpro, eventNames.colorTemperature, 'Deviceid', { colorTemperature: 8 });
|
|
178
|
-
// raiseEvent(sinricpro, eventNames.doorBell, 'DeviceId', { state: 'pressed' });
|
|
179
|
-
// raiseEvent(sinricpro, eventNames.thermostatMode, 'DeviceId', { thermostatMode: 'AUTO' });
|
|
180
|
-
// raiseEvent(sinricpro, eventNames.rangvalue, 'DeviceId', { rangvalue: 3 });
|
|
181
|
-
// raiseEvent(sinricpro, eventNames.motion, 'DeviceId', { state: 'detected' });
|
|
182
|
-
// raiseEvent(sinricpro, eventNames.contact, 'DeviceId', { state: 'closed' });
|
|
183
|
-
// raiseEvent(sinricpro, eventNames.setVolume, 'DeviceId', { volume: 29 });
|
|
184
|
-
// raiseEvent(sinricpro, eventNames.selectInput, 'DeviceId', { input: 'HDMI' });
|
|
185
|
-
// raiseEvent(sinricpro, eventNames.media, 'DeviceId', { control: 'FastForward' });
|
|
186
|
-
// raiseEvent(sinricpro, eventNames.channel, 'DeviceId', { channel: { name: 'HBO' } });
|
|
187
|
-
// raiseEvent(sinricpro, eventNames.mode, 'DeviceId', { mode: 'MOVIE' });
|
|
188
|
-
// raiseEvent(sinricpro, eventNames.lock, 'DeviceId', { state: 'LOCKED' });
|
|
189
|
-
// raiseEvent(sinricpro, eventNames.mute, 'DeviceId', { mute: true });
|
|
190
|
-
}, 2000);
|
|
191
|
-
```
|
|
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,192 +1,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, Mode, BandReset } = require('./contorllers/speaker_controller');
|
|
19
|
-
const { AdjustBands } = require('./contorllers/speaker_controller');
|
|
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
|
-
|
|
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
|
-
|
|
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, Mode, BandReset } = require('./contorllers/speaker_controller');
|
|
19
|
+
const { AdjustBands } = require('./contorllers/speaker_controller');
|
|
20
|
+
const { ToggleState } = require('./contorllers/toggle_state_controller');
|
|
21
|
+
|
|
22
|
+
const jsonResponse = (client, defaultPayload, dValue) => {
|
|
23
|
+
const header = {
|
|
24
|
+
payloadVersion: 2,
|
|
25
|
+
signatureVersion: 1,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const payload = {
|
|
29
|
+
action: defaultPayload.action,
|
|
30
|
+
clientId: defaultPayload.clientId,
|
|
31
|
+
createdAt: Math.floor(new Date() / 1000),
|
|
32
|
+
deviceId: defaultPayload.deviceId,
|
|
33
|
+
message: 'OK',
|
|
34
|
+
replyToken: defaultPayload.replyToken,
|
|
35
|
+
success: true,
|
|
36
|
+
type: 'response',
|
|
37
|
+
value: dValue,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const signature = {
|
|
41
|
+
HMAC: getSignature(client, JSON.stringify(payload)),
|
|
42
|
+
};
|
|
43
|
+
return { header, payload, signature };
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const handlePayload = (client, payload, signature, callbacks) => verifySignature(client, payload, signature)
|
|
47
|
+
.then((action) => {
|
|
48
|
+
/* eslint no-else-return: ["error", {allowElseIf: true}] */
|
|
49
|
+
if (action === 'setPowerState') {
|
|
50
|
+
return powerState(payload, callbacks)
|
|
51
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
52
|
+
.catch((err) => {
|
|
53
|
+
console.log(err.message);
|
|
54
|
+
});
|
|
55
|
+
} else if (action === 'setPowerLevel') {
|
|
56
|
+
return powerLevel(payload, callbacks)
|
|
57
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
58
|
+
.catch((err) => {
|
|
59
|
+
console.log(err.message);
|
|
60
|
+
});
|
|
61
|
+
} else if (action === 'adjustPowerLevel') {
|
|
62
|
+
return powerLevelAdjust(payload, callbacks)
|
|
63
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
64
|
+
.catch((err) => {
|
|
65
|
+
console.log(err.message);
|
|
66
|
+
});
|
|
67
|
+
} else if (action === 'setBrightness') {
|
|
68
|
+
return Brightness(payload, callbacks)
|
|
69
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
70
|
+
.catch((err) => {
|
|
71
|
+
console.log(err.message);
|
|
72
|
+
});
|
|
73
|
+
} else if (action === 'adjustBrightness') {
|
|
74
|
+
return BrightnessAdjust(payload, callbacks)
|
|
75
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
76
|
+
.catch((err) => {
|
|
77
|
+
console.log(err.message);
|
|
78
|
+
});
|
|
79
|
+
} else if (action === 'setColor') {
|
|
80
|
+
return Color(payload, callbacks)
|
|
81
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
82
|
+
.catch((err) => {
|
|
83
|
+
console.log(err.message);
|
|
84
|
+
});
|
|
85
|
+
} else if (action === 'setColorTemperature') {
|
|
86
|
+
return ColorTemperature(payload, callbacks)
|
|
87
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
88
|
+
.catch((err) => {
|
|
89
|
+
console.log(err.message);
|
|
90
|
+
});
|
|
91
|
+
} else if (action === 'setThermostatMode') {
|
|
92
|
+
return ThermoStatMode(payload, callbacks)
|
|
93
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
94
|
+
.catch((err) => {
|
|
95
|
+
console.log(err.message);
|
|
96
|
+
});
|
|
97
|
+
} else if (action === 'targetTemperature') {
|
|
98
|
+
return ThermoStatTemperature(payload, callbacks)
|
|
99
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
100
|
+
.catch((err) => {
|
|
101
|
+
console.log(err.message);
|
|
102
|
+
});
|
|
103
|
+
} else if (action === 'setRangeValue') {
|
|
104
|
+
return RangeValue(payload, callbacks)
|
|
105
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
106
|
+
.catch((err) => {
|
|
107
|
+
console.log(err.message);
|
|
108
|
+
});
|
|
109
|
+
} else if (action === 'adjustRangeValue') {
|
|
110
|
+
return RangeValueAdjust(payload, callbacks)
|
|
111
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
112
|
+
.catch((err) => {
|
|
113
|
+
console.log(err.message);
|
|
114
|
+
});
|
|
115
|
+
} else if (action === 'setVolume') {
|
|
116
|
+
return Volume(payload, callbacks)
|
|
117
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
118
|
+
.catch((err) => {
|
|
119
|
+
console.log(err.message);
|
|
120
|
+
});
|
|
121
|
+
} else if (action === 'adjustVolume') {
|
|
122
|
+
return VolumeAdjust(payload, callbacks)
|
|
123
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
124
|
+
.catch((err) => {
|
|
125
|
+
console.log(err.message);
|
|
126
|
+
});
|
|
127
|
+
} else if (action === 'selectInput') {
|
|
128
|
+
return Input(payload, callbacks)
|
|
129
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
130
|
+
.catch((err) => {
|
|
131
|
+
console.log(err.message);
|
|
132
|
+
});
|
|
133
|
+
} else if (action === 'mediaControl') {
|
|
134
|
+
return Media(payload, callbacks)
|
|
135
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
136
|
+
.catch((err) => {
|
|
137
|
+
console.log(err.message);
|
|
138
|
+
});
|
|
139
|
+
} else if (action === 'changeChannel') {
|
|
140
|
+
return ChangeChannel(payload, callbacks)
|
|
141
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
142
|
+
.catch((err) => {
|
|
143
|
+
console.log(err.message);
|
|
144
|
+
});
|
|
145
|
+
} else if (action === 'skipChannels') {
|
|
146
|
+
return SkipChannel(payload, callbacks)
|
|
147
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
148
|
+
.catch((err) => {
|
|
149
|
+
console.log(err.message);
|
|
150
|
+
});
|
|
151
|
+
} else if (action === 'setBands') {
|
|
152
|
+
return Bands(payload, callbacks)
|
|
153
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
154
|
+
.catch((err) => {
|
|
155
|
+
console.log(err.message);
|
|
156
|
+
});
|
|
157
|
+
} else if (action === 'adjustBands') {
|
|
158
|
+
return AdjustBands(payload, callbacks)
|
|
159
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
160
|
+
.catch((err) => {
|
|
161
|
+
console.log(err.message);
|
|
162
|
+
});
|
|
163
|
+
} else if (action === 'resetBands') {
|
|
164
|
+
return BandReset(payload, callbacks)
|
|
165
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
166
|
+
.catch((err) => {
|
|
167
|
+
console.log(err.message);
|
|
168
|
+
});
|
|
169
|
+
} else if (action === 'setMode') {
|
|
170
|
+
return Mode(payload, callbacks)
|
|
171
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
172
|
+
.catch((err) => {
|
|
173
|
+
console.log(err.message);
|
|
174
|
+
});
|
|
175
|
+
} else if (action === 'setLockState') {
|
|
176
|
+
return Lock(payload, callbacks)
|
|
177
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
178
|
+
.catch((err) => {
|
|
179
|
+
console.log(err.message);
|
|
180
|
+
});
|
|
181
|
+
} else if (action === 'setMute') {
|
|
182
|
+
return Mute(payload, callbacks)
|
|
183
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
184
|
+
.catch((err) => {
|
|
185
|
+
console.log(err.message);
|
|
186
|
+
});
|
|
187
|
+
} else if (action === 'setToggleState') {
|
|
188
|
+
return ToggleState(payload, callbacks)
|
|
189
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
190
|
+
.catch((err) => {
|
|
191
|
+
console.log(err.message);
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
return {};
|
|
195
|
+
})
|
|
196
|
+
.catch((err) => {
|
|
197
|
+
console.log(err.message);
|
|
198
|
+
});
|
|
199
|
+
module.exports = handlePayload;
|
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
/*
|
|
3
|
-
* Copyright (c) 2019 Sinric. All rights reserved.
|
|
4
|
-
* Licensed under Creative Commons Attribution-Share Alike (CC BY-SA)
|
|
5
|
-
*
|
|
6
|
-
* This file is part of the Sinric Pro (https://github.com/sinricpro/)
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
const sinricProData = require('../storage');
|
|
10
|
-
|
|
11
|
-
const RangeValue = (payload, callbacks) => {
|
|
12
|
-
sinricProData.rangeValue = payload.value.rangeValue;
|
|
13
|
-
const resp = callbacks.setRangeValue(payload.deviceId, sinricProData.rangeValue);
|
|
14
|
-
return new Promise((resolve, reject) => {
|
|
15
|
-
if (payload.value.rangeValue && resp) resolve(payload.value);
|
|
16
|
-
else reject(new Error('RangeValue is undefined'));
|
|
17
|
-
});
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const RangeValueAdjust = (payload, callbacks) => {
|
|
21
|
-
sinricProData.rangeValue += payload.value.rangeValueDelta;
|
|
22
|
-
if (sinricProData.rangeValue < 0) sinricProData.rangeValue = 0;
|
|
23
|
-
else if (sinricProData.rangeValue > 100) sinricProData.rangeValue = 100;
|
|
24
|
-
const resp = callbacks.adjustRangeValue(payload.deviceId, sinricProData.rangeValue);
|
|
25
|
-
return new Promise((resolve, reject) => {
|
|
26
|
-
if (payload.value.rangeValueDelta && resp) resolve({ rangeValue: sinricProData.rangeValue });
|
|
27
|
-
else reject(new Error('RangeValue undefined'));
|
|
28
|
-
});
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
module.exports = { RangeValue, RangeValueAdjust };
|
|
1
|
+
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2019 Sinric. All rights reserved.
|
|
4
|
+
* Licensed under Creative Commons Attribution-Share Alike (CC BY-SA)
|
|
5
|
+
*
|
|
6
|
+
* This file is part of the Sinric Pro (https://github.com/sinricpro/)
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const sinricProData = require('../storage');
|
|
10
|
+
|
|
11
|
+
const RangeValue = (payload, callbacks) => {
|
|
12
|
+
sinricProData.rangeValue = payload.value.rangeValue;
|
|
13
|
+
const resp = callbacks.setRangeValue(payload.deviceId, sinricProData.rangeValue);
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
if (payload.value.rangeValue !== undefined && resp) resolve(payload.value);
|
|
16
|
+
else reject(new Error('RangeValue is undefined'));
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const RangeValueAdjust = (payload, callbacks) => {
|
|
21
|
+
sinricProData.rangeValue += payload.value.rangeValueDelta;
|
|
22
|
+
if (sinricProData.rangeValue < 0) sinricProData.rangeValue = 0;
|
|
23
|
+
else if (sinricProData.rangeValue > 100) sinricProData.rangeValue = 100;
|
|
24
|
+
const resp = callbacks.adjustRangeValue(payload.deviceId, sinricProData.rangeValue);
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
if (payload.value.rangeValueDelta && resp) resolve({ rangeValue: sinricProData.rangeValue });
|
|
27
|
+
else reject(new Error('RangeValue undefined'));
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
module.exports = { RangeValue, RangeValueAdjust };
|
|
@@ -0,0 +1,18 @@
|
|
|
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, { instanceId: payload.instanceId, value: payload.value.state });
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
if (resp) resolve(payload);
|
|
12
|
+
else reject(new Error('Toggle State Error'));
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
module.exports = {
|
|
17
|
+
ToggleState
|
|
18
|
+
};
|
|
@@ -55,7 +55,7 @@ const Media = (payload, callbacks) => {
|
|
|
55
55
|
};
|
|
56
56
|
|
|
57
57
|
const ChangeChannel = (payload, callbacks) => {
|
|
58
|
-
const channel = payload.value.channel.name
|
|
58
|
+
const channel = payload.value.channel.name || payload.value.channel.number;
|
|
59
59
|
const resp = callbacks.changeChannel(payload.deviceId, channel);
|
|
60
60
|
return new Promise((resolve, reject) => {
|
|
61
61
|
if (channel && resp) resolve(payload.value);
|
package/lib/sinrcpro.js
CHANGED
|
@@ -1,122 +1,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(
|
|
12
|
-
const { raiseEvent, eventNames } = require(
|
|
13
|
-
const SinricProUdp = require(
|
|
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
|
-
parsedData.
|
|
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
|
-
parsedData.
|
|
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
|
-
};
|
|
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
|
+
const parsedData = JSON.parse(data);
|
|
46
|
+
|
|
47
|
+
if (parsedData.timestamp) {
|
|
48
|
+
if (Math.floor(new Date() / 1000) - parsedData.timestamp > 60000) {
|
|
49
|
+
console.log("Timestamp is not in sync");
|
|
50
|
+
} else console.log("TimeStamp is in sync");
|
|
51
|
+
} else {
|
|
52
|
+
const response = callbackHandler(
|
|
53
|
+
client,
|
|
54
|
+
parsedData.payload,
|
|
55
|
+
parsedData.signature.HMAC,
|
|
56
|
+
callbacks,
|
|
57
|
+
);
|
|
58
|
+
response
|
|
59
|
+
.then((res) => {
|
|
60
|
+
client.send(JSON.stringify(res));
|
|
61
|
+
})
|
|
62
|
+
.catch((err) => {
|
|
63
|
+
console.log(err.message);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
client.on("close", () => {
|
|
69
|
+
if (callbacks.onDisconnect) {
|
|
70
|
+
callbacks.onDisconnect();
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const startSinricProObservable = (client, callbacks) => new rx.Observable((observer) => {
|
|
76
|
+
client.on('open', () => {
|
|
77
|
+
console.log('Connected');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
client.on("message", (data) => {
|
|
81
|
+
const parsedData = JSON.parse(data);
|
|
82
|
+
if (parsedData.timestamp) {
|
|
83
|
+
if (Math.floor(new Date() / 1000) - parsedData.timestamp > 60000) {
|
|
84
|
+
console.log("Timestamp is not in sync");
|
|
85
|
+
} else console.log("TimeStamp is in sync");
|
|
86
|
+
} else {
|
|
87
|
+
const response = callbackHandler(
|
|
88
|
+
client,
|
|
89
|
+
parsedData.payload,
|
|
90
|
+
parsedData.signature.HMAC,
|
|
91
|
+
callbacks,
|
|
92
|
+
);
|
|
93
|
+
response
|
|
94
|
+
.then((res) => {
|
|
95
|
+
observer.next(res);
|
|
96
|
+
client.send(JSON.stringify(res));
|
|
97
|
+
})
|
|
98
|
+
.catch((err) => {
|
|
99
|
+
observer.error(err.message);
|
|
100
|
+
console.log(err.message);
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
client.on("close", () => {
|
|
106
|
+
observer.complete();
|
|
107
|
+
if (callbacks.onDisconnect) {
|
|
108
|
+
callbacks.onDisconnect();
|
|
109
|
+
}
|
|
110
|
+
console.log("Disconnected");
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
module.exports = {
|
|
115
|
+
SinricPro,
|
|
116
|
+
startSinricPro,
|
|
117
|
+
startSinricProObservable,
|
|
118
|
+
raiseEvent,
|
|
119
|
+
eventNames,
|
|
120
|
+
SinricProUdp,
|
|
121
|
+
};
|
package/lib/storage.js
CHANGED
package/package.json
CHANGED
|
@@ -1,42 +1,38 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "sinricpro",
|
|
3
|
-
"version": "2.
|
|
4
|
-
"description": "Sinric Pro Nodejs SDK",
|
|
5
|
-
"main": "index.js",
|
|
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
|
-
"eslint": "^
|
|
35
|
-
"eslint-
|
|
36
|
-
"eslint-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
"eslint-plugin-promise": "^4.2.1",
|
|
40
|
-
"eslint-plugin-standard": "^4.0.1"
|
|
41
|
-
}
|
|
42
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "sinricpro",
|
|
3
|
+
"version": "2.5.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": "Dhanush",
|
|
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
|
+
"leaky-bucket": "^4.1.4",
|
|
19
|
+
"process": "^0.11.10",
|
|
20
|
+
"rate-limiter-flexible": "^2.3.6",
|
|
21
|
+
"rxjs": "^6.6.3",
|
|
22
|
+
"uuid": "^3.3.3",
|
|
23
|
+
"ws": "^8.5.0"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"sinricpro",
|
|
27
|
+
"sinric"
|
|
28
|
+
],
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"eslint": "^8.9.0",
|
|
31
|
+
"eslint-config-airbnb-base": "^14.0.0",
|
|
32
|
+
"eslint-config-standard": "^14.1.0",
|
|
33
|
+
"eslint-plugin-import": "^2.18.2",
|
|
34
|
+
"eslint-plugin-node": "^10.0.0",
|
|
35
|
+
"eslint-plugin-promise": "^4.2.1",
|
|
36
|
+
"eslint-plugin-standard": "^4.0.1"
|
|
37
|
+
}
|
|
38
|
+
}
|