sinricpro 2.2.5 → 2.3.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.
@@ -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://img.shields.io/npm/v/sinricpro)](https://www.npmjs.com/package/sinricpro)
4
- [![](https://img.shields.io/npm/dependency-version/sinricpro/dev/eslint)](https://www.npmjs.com/package/sinricpro)
5
- [![](https://img.shields.io/bundlephobia/min/sinricpro)](https://www.npmjs.com/package/sinricpro)
6
- [![](https://img.shields.io/github/languages/code-size/sinricpro/nodejs-sdk)](https://www.npmjs.com/package/sinricpro)
7
-
8
- ### Installation
9
-
10
- ```
11
- npm install sinricpro
12
- ```
13
-
14
- ### Dependencies
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://img.shields.io/npm/v/sinricpro)](https://www.npmjs.com/package/sinricpro)
4
+ [![](https://img.shields.io/npm/dependency-version/sinricpro/dev/eslint)](https://www.npmjs.com/package/sinricpro)
5
+ [![](https://img.shields.io/bundlephobia/min/sinricpro)](https://www.npmjs.com/package/sinricpro)
6
+ [![](https://img.shields.io/github/languages/code-size/sinricpro/nodejs-sdk)](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,10 +10,10 @@ 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
- const { ChangeChannel, SkipChannel, Media, } = require('./contorllers/tv_controller');
16
+ const { ChangeChannel, SkipChannel, Media } = require('./contorllers/tv_controller');
17
17
  const { Input } = require('./contorllers/tv_controller');
18
18
  const { Bands, Mode, BandReset } = require('./contorllers/speaker_controller');
19
19
  const { AdjustBands } = require('./contorllers/speaker_controller');
@@ -87,13 +87,18 @@ const handlePayload = (client, payload, signature, callbacks) => verifySignature
87
87
  .catch((err) => {
88
88
  console.log(err.message);
89
89
  });
90
- }
91
- else if (action === 'setThermostatMode') {
90
+ } else if (action === 'setThermostatMode') {
92
91
  return ThermoStatMode(payload, callbacks)
93
92
  .then((value) => jsonResponse(client, payload, value))
94
93
  .catch((err) => {
95
94
  console.log(err.message);
96
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
+ });
97
102
  } else if (action === 'setRangeValue') {
98
103
  return RangeValue(payload, callbacks)
99
104
  .then((value) => jsonResponse(client, payload, value))
@@ -178,7 +183,7 @@ const handlePayload = (client, payload, signature, callbacks) => verifySignature
178
183
  .catch((err) => {
179
184
  console.log(err.message);
180
185
  });
181
- }
186
+ }
182
187
  return {};
183
188
  })
184
189
  .catch((err) => {
@@ -11,7 +11,7 @@ const sinricProData = require('../storage');
11
11
  const ColorTemperature = (payload, callbacks) => {
12
12
  sinricProData.colorTemperature = payload.value.colorTemperature;
13
13
  const resp = callbacks.setColorTemperature(payload.deviceId, payload.value.colorTemperature);
14
-
14
+
15
15
  return new Promise((resolve, reject) => {
16
16
  if (payload.value.colorTemperature && resp) resolve(payload.value);
17
17
  else reject(new Error('color temperature is undefined'));
@@ -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,9 +55,10 @@ const Media = (payload, callbacks) => {
55
55
  };
56
56
 
57
57
  const ChangeChannel = (payload, callbacks) => {
58
- const resp = callbacks.changeChannel(payload.deviceId, payload.value.channel.name);
58
+ const channel = payload.value.channel.name || payload.value.channel.number;
59
+ const resp = callbacks.changeChannel(payload.deviceId, channel);
59
60
  return new Promise((resolve, reject) => {
60
- if (payload.value.channel.name && resp) resolve(payload.value);
61
+ if (channel && resp) resolve(payload.value);
61
62
  else reject(new Error('Channel name undefined'));
62
63
  });
63
64
  };
@@ -7,10 +7,10 @@
7
7
  */
8
8
 
9
9
  const uuidv4 = require('uuid/v4');
10
+ const { RateLimiterMemory, BurstyRateLimiter } = require('rate-limiter-flexible');
10
11
  const { getSignature } = require('../signature');
11
- const {RateLimiterMemory, BurstyRateLimiter} = require('rate-limiter-flexible');
12
12
 
13
- /*
13
+ /*
14
14
  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
15
15
  * !! !!
16
16
  * !! WARNING: DON'T TOUCH ! !!
@@ -31,7 +31,7 @@ const burstyLimiter = new BurstyRateLimiter(
31
31
  keyPrefix: 'burst',
32
32
  points: 10,
33
33
  duration: 60,
34
- })
34
+ }),
35
35
  );
36
36
 
37
37
  // @TODO resteBands
@@ -83,15 +83,14 @@ const getJson = (client, eventName, deviceId, value) => {
83
83
  };
84
84
 
85
85
  const raiseEvent = async (client, eventName, deviceId, value) => {
86
- if (actionArr.includes(eventName)) {
86
+ if (actionArr.includes(eventName)) {
87
87
  try {
88
88
  await burstyLimiter.consume('queue');
89
89
  client.send(JSON.stringify(getJson(client, eventName, deviceId, value)));
90
90
  } catch (rlRejected) {
91
- console.log(`Too many events!`);
91
+ console.log('Too many events!');
92
92
  }
93
- }
94
- else {
93
+ } else {
95
94
  console.log('Invalid Event');
96
95
  }
97
96
  };
package/lib/sinrcpro.js CHANGED
@@ -5,23 +5,22 @@
5
5
  * This file is part of the Sinric Pro (https://github.com/sinricpro/)
6
6
  *
7
7
  */
8
- const Websocket = require('ws');
9
- const rx = require('rxjs');
8
+ const Websocket = require("ws");
9
+ const rx = require("rxjs");
10
10
 
11
11
  const callbackHandler = require('./cbhandler');
12
12
  const { raiseEvent, eventNames } = require('./events/event_handler');
13
13
  const SinricProUdp = require('./sinricproudp');
14
-
15
- const VERSION = '2.2.4';
14
+ const sdkVersion = require('./../package.json').version;
16
15
 
17
16
  class SinricPro extends Websocket {
18
17
  constructor(appkey, deviceids, secretKey, restoreStates) {
19
- super('ws://ws.sinric.pro', {
18
+ super("ws://ws.sinric.pro", {
20
19
  headers: {
21
20
  appkey,
22
21
  deviceids: deviceids.join(';'),
23
22
  platform: 'nodejs',
24
- version: VERSION,
23
+ sdkversion: sdkVersion,
25
24
  restoredevicestates: restoreStates,
26
25
  },
27
26
  });
@@ -37,17 +36,18 @@ class SinricPro extends Websocket {
37
36
  }
38
37
  }
39
38
 
40
- const SinricProActions = (client, callbacks) => {
39
+ const startSinricPro = (client, callbacks) => {
41
40
  client.on('open', () => {
42
41
  console.log('Connected to Sinric Pro');
43
42
  });
44
43
 
45
- client.on('message', (data) => {
44
+ client.on("message", (data) => {
46
45
  const parsedData = JSON.parse(data);
47
-
46
+
48
47
  if (parsedData.timestamp) {
49
- if (Math.floor(new Date() / 1000) - parsedData.timestamp > 60000) console.log('Timestamp is not in sync');
50
- else console.log('TimeStamp is in sync');
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
51
  } else {
52
52
  const response = callbackHandler(
53
53
  client,
@@ -64,18 +64,25 @@ const SinricProActions = (client, callbacks) => {
64
64
  });
65
65
  }
66
66
  });
67
+
68
+ client.on("close", () => {
69
+ if (callbacks.onDisconnect) {
70
+ callbacks.onDisconnect();
71
+ }
72
+ });
67
73
  };
68
74
 
69
- const SinricProActionsObservable = (client, callbacks) => new rx.Observable((observer) => {
75
+ const startSinricProObservable = (client, callbacks) => new rx.Observable((observer) => {
70
76
  client.on('open', () => {
71
77
  console.log('Connected');
72
78
  });
73
79
 
74
- client.on('message', (data) => {
80
+ client.on("message", (data) => {
75
81
  const parsedData = JSON.parse(data);
76
82
  if (parsedData.timestamp) {
77
- if (Math.floor(new Date() / 1000) - parsedData.timestamp > 60000) console.log('Timestamp is not in sync');
78
- else console.log('TimeStamp is in sync');
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");
79
86
  } else {
80
87
  const response = callbackHandler(
81
88
  client,
@@ -95,16 +102,19 @@ const SinricProActionsObservable = (client, callbacks) => new rx.Observable((obs
95
102
  }
96
103
  });
97
104
 
98
- client.on('close', () => {
105
+ client.on("close", () => {
99
106
  observer.complete();
100
- console.log('Disconnected');
107
+ if (callbacks.onDisconnect) {
108
+ callbacks.onDisconnect();
109
+ }
110
+ console.log("Disconnected");
101
111
  });
102
112
  });
103
113
 
104
114
  module.exports = {
105
115
  SinricPro,
106
- SinricProActions,
107
- SinricProActionsObservable,
116
+ startSinricPro,
117
+ startSinricProObservable,
108
118
  raiseEvent,
109
119
  eventNames,
110
120
  SinricProUdp,
package/lib/storage.js CHANGED
@@ -13,7 +13,10 @@ let data = {
13
13
  b: 0,
14
14
  },
15
15
  lockState: '',
16
- thermostat: '',
16
+ thermostat: {
17
+ mode: '',
18
+ temperature: 0,
19
+ },
17
20
  rangeValue: 0,
18
21
  volume: 0,
19
22
  colorTemperature: 0,
package/package.json CHANGED
@@ -1,42 +1,38 @@
1
- {
2
- "name": "sinricpro",
3
- "version": "2.2.5",
4
- "description": "Sinric Pro Nodejs SDK",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1",
8
- "lint": "eslint --fix ./"
9
- },
10
- "repository": {
11
- "type": "git",
12
- "url": "git+https://github.com/sinricpro/nodejs-sdk.git"
13
- },
14
- "author": "Dhanush",
15
- "license": "ISC",
16
- "bugs": {
17
- "url": "https://github.com/sinricpro/nodejs-sdk/issues"
18
- },
19
- "homepage": "https://github.com/sinricpro/nodejs-sdk#readme",
20
- "dependencies": {
21
- "dgram": "^1.0.1",
22
- "leaky-bucket": "^3.0.4",
23
- "process": "^0.11.10",
24
- "rate-limiter-flexible": "^2.2.1",
25
- "rxjs": "^6.6.3",
26
- "uuid": "^3.3.3",
27
- "ws": "^7.4.2"
28
- },
29
- "keywords": [
30
- "sinricpro",
31
- "sinric"
32
- ],
33
- "devDependencies": {
34
- "eslint": "^6.8.0",
35
- "eslint-config-airbnb-base": "^14.0.0",
36
- "eslint-config-standard": "^14.1.0",
37
- "eslint-plugin-import": "^2.18.2",
38
- "eslint-plugin-node": "^10.0.0",
39
- "eslint-plugin-promise": "^4.2.1",
40
- "eslint-plugin-standard": "^4.0.1"
41
- }
42
- }
1
+ {
2
+ "name": "sinricpro",
3
+ "version": "2.3.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": "^6.8.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
+ }