sinricpro 2.8.1 → 2.10.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/.prettierrc ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "tabWidth": 2,
3
+ "useTabs": false,
4
+ "printWidth": 120
5
+ }
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, ThermoStatTemperature, Lock } = require('./contorllers/sensors_controller');
13
+ const { ThermoStatMode, ThermoStatTemperature, Lock, AdjustTargetTemperature } = 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');
@@ -19,7 +19,7 @@ const { Bands, BandReset } = require('./contorllers/speaker_controller');
19
19
  const { AdjustBands } = require('./contorllers/speaker_controller');
20
20
  const { ToggleState } = require('./contorllers/toggle_state_controller');
21
21
  const { Mode } = require('./contorllers/mode_controller');
22
- const { WebRTC } = require('./contorllers/webrtc_controller');
22
+ const { CameraStreamController } = require('./contorllers/camera_stream_controller');
23
23
  const { UndefinedCallbackError, InvalidResponseError } = require('./errors/errors');
24
24
 
25
25
  const jsonResponse = (client, defaultPayload, payloadValue, instanceId) => {
@@ -45,6 +45,7 @@ const jsonResponse = (client, defaultPayload, payloadValue, instanceId) => {
45
45
  const signature = {
46
46
  HMAC: getSignature(client, JSON.stringify(payload)),
47
47
  };
48
+
48
49
  return { header, payload, signature };
49
50
  };
50
51
 
@@ -153,16 +154,29 @@ const handlePayload = (client, payload, signature, callbacks) => verifySignature
153
154
  return ToggleState(payload, callbacks)
154
155
  .then(([value, instanceId]) => jsonResponse(client, payload, value, instanceId))
155
156
  .catch(handleError);
156
- } else if (action === 'webrtcOffer') {
157
- return WebRTC(payload, callbacks)
158
- .then(([value, answer]) => jsonResponse(client, payload, { answer: answer } ))
159
- .catch((err) => {
160
- if (process.env.SR_DEBUG) console.log(err.message);
161
- });
157
+ } else if (action === 'getWebRTCAnswer') {
158
+ return CameraStreamController(payload, callbacks)
159
+ .then((answer) => jsonResponse(client, payload, { answer }))
160
+ .catch(handleError);
161
+ } else if (action === 'getCameraStreamUrl') {
162
+ return CameraStreamController(payload, callbacks)
163
+ .then((url) => jsonResponse(client, payload, { url }))
164
+ .catch(handleError);
165
+ } else if (action === 'getWebRTCOffer') {
166
+ return CameraStreamController(payload, callbacks)
167
+ .then((offer) => jsonResponse(client, payload, { offer }))
168
+ .catch(handleError);
169
+ } else if (action === 'adjustTargetTemperature') {
170
+ return AdjustTargetTemperature(payload, callbacks)
171
+ .then((resp) => {
172
+ const { temperature } = resp;
173
+ return jsonResponse(client, payload, temperature);
174
+ })
175
+ .catch(handleError);
162
176
  }
163
177
  return {};
164
178
  })
165
179
  .catch((err) => {
166
- console.error(err.message);
180
+ console.error(err);
167
181
  });
168
182
  module.exports = handlePayload;
@@ -0,0 +1,39 @@
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
+ const { UndefinedCallbackError, InvalidResponseError } = require("../errors/errors");
8
+
9
+ const CameraStreamController = async (payload, callbacks) => {
10
+ if (payload.action === "getWebRTCAnswer" && callbacks.getWebRTCAnswer) {
11
+ const data = await callbacks.getWebRTCAnswer(payload.deviceId, payload.value.offer);
12
+
13
+ if (data.answer) {
14
+ return data.answer;
15
+ }
16
+ throw new InvalidResponseError(" error");
17
+ }
18
+ if (payload.action === "getWebRTCOffer" && callbacks.getWebRTCOffer) {
19
+ const data = await callbacks.getWebRTCOffer(payload.deviceId);
20
+
21
+ if (data.offer) {
22
+ return data.offer;
23
+ }
24
+ throw new InvalidResponseError(" error");
25
+ }
26
+ else if (payload.action === "getCameraStreamUrl" && callbacks.getCameraStreamUrl) {
27
+ const data = await callbacks.getCameraStreamUrl(payload.deviceId, payload.value.protocol);
28
+ if (data.url) {
29
+ return data.url;
30
+ }
31
+ throw new InvalidResponseError("getCameraStreamUrl error");
32
+ } else {
33
+ throw new UndefinedCallbackError("getCameraStreamUrl callback is undefined.");
34
+ }
35
+ };
36
+
37
+ module.exports = {
38
+ CameraStreamController,
39
+ };
@@ -31,6 +31,16 @@ const ThermoStatTemperature = async (payload, callbacks) => {
31
31
  }
32
32
  };
33
33
 
34
+ const AdjustTargetTemperature = async (payload, callbacks) => {
35
+ if (callbacks.adjustTargetTemperature) {
36
+ const resp = await callbacks.adjustTargetTemperature(payload.deviceId, payload.value.temperature);
37
+ if (resp.temperature) return resp;
38
+ throw new InvalidResponseError('Thermostat temperature undefined');
39
+ } else {
40
+ throw new UndefinedCallbackError('adjustTargetTemperature callback is undefined.');
41
+ }
42
+ };
43
+
34
44
  const Lock = async (payload, callbacks) => {
35
45
  if (callbacks.setLockState) {
36
46
  const resp = await callbacks.setLockState(payload.deviceId, payload.value.state);
@@ -41,4 +51,4 @@ const Lock = async (payload, callbacks) => {
41
51
  }
42
52
  };
43
53
 
44
- module.exports = { ThermoStatMode, ThermoStatTemperature, Lock };
54
+ module.exports = { ThermoStatMode, ThermoStatTemperature, Lock, AdjustTargetTemperature };
@@ -1,4 +1,3 @@
1
-
2
1
  /*
3
2
  * Copyright (c) 2019 Sinric. All rights reserved.
4
3
  * Licensed under Creative Commons Attribution-Share Alike (CC BY-SA)
@@ -6,9 +5,9 @@
6
5
  * This file is part of the Sinric Pro (https://github.com/sinricpro/)
7
6
  */
8
7
 
9
- const uuidv4 = require('uuid/v4');
10
- const { RateLimiterMemory, BurstyRateLimiter } = require('rate-limiter-flexible');
11
- const { getSignature } = require('../signature');
8
+ const uuidv4 = require("uuid/v4");
9
+ const { RateLimiterMemory, BurstyRateLimiter } = require("rate-limiter-flexible");
10
+ const { getSignature } = require("../signature");
12
11
 
13
12
  /*
14
13
  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@@ -28,37 +27,47 @@ const burstyLimiter = new BurstyRateLimiter(
28
27
  duration: 60,
29
28
  }),
30
29
  new RateLimiterMemory({
31
- keyPrefix: 'burst',
30
+ keyPrefix: "burst",
32
31
  points: 10,
33
32
  duration: 60,
34
- }),
33
+ })
35
34
  );
36
35
 
37
36
  // @TODO resteBands
38
37
 
39
38
  const eventNames = {
40
- powerState: 'setPowerState',
41
- setBrightness: 'setBrightness',
42
- powerLevel: 'setPowerLevel',
43
- color: 'setColor',
44
- colorTemperature: 'setColorTemperature',
45
- doorBell: 'DoorbellPress',
46
- thermostatMode: 'setThermostatMode',
47
- rangvalue: 'setRangeValue',
48
- motion: 'motion',
49
- contact: 'setContactState',
50
- setVolume: 'setVolume',
51
- selectInput: 'selectInput',
52
- media: 'mediaControl',
53
- channel: 'changeChannel',
54
- mode: 'setMode',
55
- lock: 'setLockState',
56
- mute: 'setMute',
57
- currentTemperature: 'currentTemperature',
58
- pushNotification: 'pushNotification',
39
+ powerState: "setPowerState",
40
+ setBrightness: "setBrightness",
41
+ powerLevel: "setPowerLevel",
42
+ color: "setColor",
43
+ colorTemperature: "setColorTemperature",
44
+ doorBell: "DoorbellPress",
45
+ thermostatMode: "setThermostatMode",
46
+ rangvalue: "setRangeValue",
47
+ motion: "motion",
48
+ contact: "setContactState",
49
+ setVolume: "setVolume",
50
+ selectInput: "selectInput",
51
+ media: "mediaControl",
52
+ channel: "changeChannel",
53
+ mode: "setMode",
54
+ lock: "setLockState",
55
+ mute: "setMute",
56
+ currentTemperature: "currentTemperature",
57
+ pushNotification: "pushNotification",
59
58
  };
60
59
 
61
- const actionArr = ['setPowerState', 'setBrightness', 'setPowerLevel', 'setColor', 'setColorTemperature', 'DoorbellPress', 'currentTemperature', 'pushNotification'];
60
+ const actionArr = [
61
+ "setPowerState",
62
+ "setBrightness",
63
+ "setPowerLevel",
64
+ "setColor",
65
+ "setColorTemperature",
66
+ "DoorbellPress",
67
+ "currentTemperature",
68
+ "pushNotification",
69
+ "setThermostatMode",
70
+ ];
62
71
 
63
72
  const getJson = (client, eventName, deviceId, value) => {
64
73
  const header = {
@@ -68,30 +77,30 @@ const getJson = (client, eventName, deviceId, value) => {
68
77
  const payload = {
69
78
  action: eventName,
70
79
  cause: {
71
- type: 'PHYSICAL_INTERACTION',
80
+ type: "PHYSICAL_INTERACTION",
72
81
  },
73
82
  createdAt: Math.round(new Date() / 1000),
74
83
  deviceId,
75
84
  replyToken: uuidv4(),
76
- type: 'event',
85
+ type: "event",
77
86
  value,
78
87
  };
79
88
  const signature = {
80
89
  HMAC: getSignature(client, JSON.stringify(payload)),
81
90
  };
82
91
 
83
- return ({ header, payload, signature });
92
+ return { header, payload, signature };
84
93
  };
85
94
 
86
95
  const raiseEvent = async (client, eventName, deviceId, value) => {
87
96
  if (actionArr.includes(eventName)) {
88
97
  try {
89
- await burstyLimiter.consume('queue');
98
+ await burstyLimiter.consume("queue");
90
99
  const request = JSON.stringify(getJson(client, eventName, deviceId, value));
91
- if (process.env.SR_DEBUG) console.log('>> %j', request);
100
+ if (process.env.SR_DEBUG) console.log(">> %j", request);
92
101
  client.send(request);
93
102
  } catch (rlRejected) {
94
- console.log('Too many events!');
103
+ console.log("Too many events!");
95
104
  }
96
105
  } else {
97
106
  console.log(`Invalid Event: ${eventName}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sinricpro",
3
- "version": "2.8.1",
3
+ "version": "2.10.1",
4
4
  "description": "Sinric Pro Nodejs SDK",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -1,21 +0,0 @@
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
- const { UndefinedCallbackError, InvalidResponseError } = require('../errors/errors');
8
-
9
- const WebRTC = async (payload, callbacks) => {
10
- if (callbacks.webRtcOffer) {
11
- const data = await callbacks.webRtcOffer(payload.deviceId, payload.value.format, payload.value.value);
12
- if (data) return [payload.value, data.answer];
13
- throw new InvalidResponseError('WebRTC error');
14
- } else {
15
- throw new UndefinedCallbackError('webRtcOffer callback is undefined.');
16
- }
17
- };
18
-
19
- module.exports = {
20
- WebRTC,
21
- };
@@ -1,6 +0,0 @@
1
- Switch to Login module (by typing "how-to-npm" on command line)
2
- Type "npm adduser" to login
3
- Type "npm whoami" to confirm if logged in
4
- Switch back to the package at which you are getting error(my case it was Publish Again)
5
- Type "npm publish"
6
- Done