sinricpro 2.2.6 → 2.4.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 +7 -1
- package/lib/contorllers/range_value_controller.js +32 -32
- package/lib/contorllers/sensors_controller.js +11 -3
- package/lib/contorllers/tv_controller.js +1 -1
- package/lib/sinrcpro.js +121 -123
- package/lib/storage.js +4 -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
|
@@ -10,7 +10,7 @@ const { powerState, powerLevel, powerLevelAdjust } = require('./contorllers/powe
|
|
|
10
10
|
const { Brightness, BrightnessAdjust } = require('./contorllers/brightness_controller');
|
|
11
11
|
const { Color } = require('./contorllers/color_controller');
|
|
12
12
|
const { ColorTemperature } = require('./contorllers/color_temperature');
|
|
13
|
-
const { ThermoStatMode, Lock } = require('./contorllers/sensors_controller');
|
|
13
|
+
const { ThermoStatMode, ThermoStatTemperature, Lock } = require('./contorllers/sensors_controller');
|
|
14
14
|
const { RangeValue, RangeValueAdjust } = require('./contorllers/range_value_controller');
|
|
15
15
|
const { Volume, VolumeAdjust, Mute } = require('./contorllers/tv_controller');
|
|
16
16
|
const { ChangeChannel, SkipChannel, Media } = require('./contorllers/tv_controller');
|
|
@@ -93,6 +93,12 @@ const handlePayload = (client, payload, signature, callbacks) => verifySignature
|
|
|
93
93
|
.catch((err) => {
|
|
94
94
|
console.log(err.message);
|
|
95
95
|
});
|
|
96
|
+
} else if (action === 'targetTemperature') {
|
|
97
|
+
return ThermoStatTemperature(payload, callbacks)
|
|
98
|
+
.then((value) => jsonResponse(client, payload, value))
|
|
99
|
+
.catch((err) => {
|
|
100
|
+
console.log(err.message);
|
|
101
|
+
});
|
|
96
102
|
} else if (action === 'setRangeValue') {
|
|
97
103
|
return RangeValue(payload, callbacks)
|
|
98
104
|
.then((value) => jsonResponse(client, payload, value))
|
|
@@ -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 };
|
|
@@ -9,13 +9,21 @@
|
|
|
9
9
|
const sinricProData = require('../storage');
|
|
10
10
|
|
|
11
11
|
const ThermoStatMode = (payload, callbacks) => {
|
|
12
|
-
sinricProData.thermostat = payload.value.thermostatMode;
|
|
13
|
-
const resp = callbacks.setThermostatMode(payload.deviceId, sinricProData.thermostat);
|
|
12
|
+
sinricProData.thermostat.mode = payload.value.thermostatMode;
|
|
13
|
+
const resp = callbacks.setThermostatMode(payload.deviceId, sinricProData.thermostat.mode);
|
|
14
14
|
return new Promise((resolve, reject) => {
|
|
15
15
|
if (payload.value.thermostatMode && resp) resolve(payload.value);
|
|
16
16
|
else reject(new Error('Thermostat mode undefined'));
|
|
17
17
|
});
|
|
18
18
|
};
|
|
19
|
+
const ThermoStatTemperature = (payload, callbacks) => {
|
|
20
|
+
sinricProData.thermostat.temperature = payload.value.temperature;
|
|
21
|
+
const resp = callbacks.targetTemperature(payload.deviceId, sinricProData.thermostat.temperature);
|
|
22
|
+
return new Promise((resolve, reject) => {
|
|
23
|
+
if (payload.value.temperature && resp) resolve(payload.value);
|
|
24
|
+
else reject(new Error('Thermostat temperature undefined'));
|
|
25
|
+
});
|
|
26
|
+
};
|
|
19
27
|
|
|
20
28
|
const Lock = (payload, callbacks) => {
|
|
21
29
|
const resp = callbacks.setLockState(payload.deviceId, payload.value.state);
|
|
@@ -25,4 +33,4 @@ const Lock = (payload, callbacks) => {
|
|
|
25
33
|
});
|
|
26
34
|
};
|
|
27
35
|
|
|
28
|
-
module.exports = { ThermoStatMode, Lock };
|
|
36
|
+
module.exports = { ThermoStatMode, ThermoStatTemperature, Lock };
|
|
@@ -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,123 +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
|
-
|
|
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
|
-
SinricProUdp,
|
|
123
|
-
};
|
|
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.4.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
|
+
}
|