sinricpro 2.12.1 → 3.0.0

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.
Files changed (142) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +132 -15
  3. package/dist/capabilities/AirQualitySensor.js +23 -0
  4. package/dist/capabilities/AirQualitySensor.js.map +1 -0
  5. package/dist/capabilities/BrightnessController.js +76 -0
  6. package/dist/capabilities/BrightnessController.js.map +1 -0
  7. package/dist/capabilities/CameraController.js +29 -0
  8. package/dist/capabilities/CameraController.js.map +1 -0
  9. package/dist/capabilities/ChannelController.js +52 -0
  10. package/dist/capabilities/ChannelController.js.map +1 -0
  11. package/dist/capabilities/ColorController.js +53 -0
  12. package/dist/capabilities/ColorController.js.map +1 -0
  13. package/dist/capabilities/ColorTemperatureController.js +53 -0
  14. package/dist/capabilities/ColorTemperatureController.js.map +1 -0
  15. package/dist/capabilities/ContactSensor.js +23 -0
  16. package/dist/capabilities/ContactSensor.js.map +1 -0
  17. package/dist/capabilities/Doorbell.js +23 -0
  18. package/dist/capabilities/Doorbell.js.map +1 -0
  19. package/dist/capabilities/EqualizerController.js +103 -0
  20. package/dist/capabilities/EqualizerController.js.map +1 -0
  21. package/dist/capabilities/InputController.js +51 -0
  22. package/dist/capabilities/InputController.js.map +1 -0
  23. package/dist/capabilities/KeypadController.js +23 -0
  24. package/dist/capabilities/KeypadController.js.map +1 -0
  25. package/dist/capabilities/LockController.js +40 -0
  26. package/dist/capabilities/LockController.js.map +1 -0
  27. package/dist/capabilities/MediaController.js +40 -0
  28. package/dist/capabilities/MediaController.js.map +1 -0
  29. package/dist/capabilities/ModeController.js +51 -0
  30. package/dist/capabilities/ModeController.js.map +1 -0
  31. package/dist/capabilities/MotionSensor.js +23 -0
  32. package/dist/capabilities/MotionSensor.js.map +1 -0
  33. package/dist/capabilities/MuteController.js +51 -0
  34. package/dist/capabilities/MuteController.js.map +1 -0
  35. package/dist/capabilities/OpenCloseController.js +40 -0
  36. package/dist/capabilities/OpenCloseController.js.map +1 -0
  37. package/dist/capabilities/PercentageController.js +52 -0
  38. package/dist/capabilities/PercentageController.js.map +1 -0
  39. package/dist/capabilities/PowerLevelController.js +52 -0
  40. package/dist/capabilities/PowerLevelController.js.map +1 -0
  41. package/dist/capabilities/PowerSensor.js +23 -0
  42. package/dist/capabilities/PowerSensor.js.map +1 -0
  43. package/dist/capabilities/PowerStateController.js +53 -0
  44. package/dist/capabilities/PowerStateController.js.map +1 -0
  45. package/dist/capabilities/PushNotification.js +23 -0
  46. package/dist/capabilities/PushNotification.js.map +1 -0
  47. package/dist/capabilities/RangeController.js +52 -0
  48. package/dist/capabilities/RangeController.js.map +1 -0
  49. package/dist/capabilities/SettingController.js +33 -0
  50. package/dist/capabilities/SettingController.js.map +1 -0
  51. package/dist/capabilities/StartStopController.js +51 -0
  52. package/dist/capabilities/StartStopController.js.map +1 -0
  53. package/dist/capabilities/TemperatureSensor.js +29 -0
  54. package/dist/capabilities/TemperatureSensor.js.map +1 -0
  55. package/dist/capabilities/ThermostatController.js +70 -0
  56. package/dist/capabilities/ThermostatController.js.map +1 -0
  57. package/dist/capabilities/ToggleController.js +51 -0
  58. package/dist/capabilities/ToggleController.js.map +1 -0
  59. package/dist/capabilities/VolumeController.js +74 -0
  60. package/dist/capabilities/VolumeController.js.map +1 -0
  61. package/dist/capabilities/index.js +56 -0
  62. package/dist/capabilities/index.js.map +1 -0
  63. package/dist/core/EventLimiter.js +50 -0
  64. package/dist/core/EventLimiter.js.map +1 -0
  65. package/dist/core/MessageQueue.js +28 -0
  66. package/dist/core/MessageQueue.js.map +1 -0
  67. package/dist/core/Signature.js +68 -0
  68. package/dist/core/Signature.js.map +1 -0
  69. package/dist/core/SinricPro.js +332 -0
  70. package/dist/core/SinricPro.js.map +1 -0
  71. package/dist/core/SinricProDevice.js +82 -0
  72. package/dist/core/SinricProDevice.js.map +1 -0
  73. package/dist/core/WebSocketClient.js +156 -0
  74. package/dist/core/WebSocketClient.js.map +1 -0
  75. package/dist/core/types.js +30 -0
  76. package/dist/core/types.js.map +1 -0
  77. package/dist/devices/SinricProAirQualitySensor.js +29 -0
  78. package/dist/devices/SinricProAirQualitySensor.js.map +1 -0
  79. package/dist/devices/SinricProBlinds.js +29 -0
  80. package/dist/devices/SinricProBlinds.js.map +1 -0
  81. package/dist/devices/SinricProCamera.js +29 -0
  82. package/dist/devices/SinricProCamera.js.map +1 -0
  83. package/dist/devices/SinricProContactSensor.js +28 -0
  84. package/dist/devices/SinricProContactSensor.js.map +1 -0
  85. package/dist/devices/SinricProDimSwitch.js +29 -0
  86. package/dist/devices/SinricProDimSwitch.js.map +1 -0
  87. package/dist/devices/SinricProDoorbell.js +29 -0
  88. package/dist/devices/SinricProDoorbell.js.map +1 -0
  89. package/dist/devices/SinricProFan.js +29 -0
  90. package/dist/devices/SinricProFan.js.map +1 -0
  91. package/dist/devices/SinricProFanUS.js +29 -0
  92. package/dist/devices/SinricProFanUS.js.map +1 -0
  93. package/dist/devices/SinricProGarageDoor.js +28 -0
  94. package/dist/devices/SinricProGarageDoor.js.map +1 -0
  95. package/dist/devices/SinricProLight.js +30 -0
  96. package/dist/devices/SinricProLight.js.map +1 -0
  97. package/dist/devices/SinricProLock.js +28 -0
  98. package/dist/devices/SinricProLock.js.map +1 -0
  99. package/dist/devices/SinricProMotionSensor.js +28 -0
  100. package/dist/devices/SinricProMotionSensor.js.map +1 -0
  101. package/dist/devices/SinricProPowerSensor.js +28 -0
  102. package/dist/devices/SinricProPowerSensor.js.map +1 -0
  103. package/dist/devices/SinricProSpeaker.js +33 -0
  104. package/dist/devices/SinricProSpeaker.js.map +1 -0
  105. package/dist/devices/SinricProSwitch.js +24 -0
  106. package/dist/devices/SinricProSwitch.js.map +1 -0
  107. package/dist/devices/SinricProTV.js +33 -0
  108. package/dist/devices/SinricProTV.js.map +1 -0
  109. package/dist/devices/SinricProThermostat.js +30 -0
  110. package/dist/devices/SinricProThermostat.js.map +1 -0
  111. package/dist/devices/SinricProWindowAC.js +31 -0
  112. package/dist/devices/SinricProWindowAC.js.map +1 -0
  113. package/dist/devices/index.js +44 -0
  114. package/dist/devices/index.js.map +1 -0
  115. package/dist/index.js +43 -0
  116. package/dist/index.js.map +1 -0
  117. package/dist/utils/SinricProSdkLogger.js +42 -0
  118. package/dist/utils/SinricProSdkLogger.js.map +1 -0
  119. package/package.json +78 -38
  120. package/.github/workflows/npm-publish.yml +0 -32
  121. package/.nvmrc +0 -1
  122. package/.prettierrc +0 -5
  123. package/CHANGELOG.md +0 -45
  124. package/index.js +0 -1
  125. package/lib/cbhandler.js +0 -182
  126. package/lib/contorllers/brightness_controller.js +0 -40
  127. package/lib/contorllers/camera_stream_controller.js +0 -39
  128. package/lib/contorllers/color_controller.js +0 -23
  129. package/lib/contorllers/color_temperature.js +0 -23
  130. package/lib/contorllers/mode_controller.js +0 -22
  131. package/lib/contorllers/power_controller.js +0 -53
  132. package/lib/contorllers/range_value_controller.js +0 -38
  133. package/lib/contorllers/sensors_controller.js +0 -54
  134. package/lib/contorllers/speaker_controller.js +0 -51
  135. package/lib/contorllers/toggle_state_controller.js +0 -21
  136. package/lib/contorllers/tv_controller.js +0 -93
  137. package/lib/errors/errors.js +0 -8
  138. package/lib/events/event_handler.js +0 -108
  139. package/lib/signature.js +0 -26
  140. package/lib/sinrcpro.js +0 -129
  141. package/lib/sinricproudp.js +0 -60
  142. package/lib/storage.js +0 -31
@@ -1,22 +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
-
8
- const { UndefinedCallbackError, InvalidResponseError } = require('../errors/errors');
9
-
10
- const Mode = async (payload, callbacks) => {
11
- if (callbacks.setMode) {
12
- const resp = await callbacks.setMode(payload.deviceId, payload.value.mode, payload.instanceId);
13
- if (resp) return [payload.value, payload.instanceId];
14
- throw new InvalidResponseError('Mode undefined');
15
- } else {
16
- throw new UndefinedCallbackError('setMode callback is undefined!');
17
- }
18
- };
19
-
20
- module.exports = {
21
- Mode,
22
- };
@@ -1,53 +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
-
8
- const sinricProData = require('../storage');
9
- const { UndefinedCallbackError, InvalidResponseError } = require('../errors/errors');
10
-
11
- const powerState = async (payload, callbacks) => {
12
- if (callbacks.setPowerState) {
13
- const resp = await callbacks.setPowerState(payload.deviceId, payload.value.state);
14
- if (payload.value.state && resp) {
15
- return payload.value;
16
- }
17
- throw new InvalidResponseError('State is undefined');
18
- } else {
19
- throw new UndefinedCallbackError('setPowerState callback is undefined.');
20
- }
21
- };
22
-
23
- const powerLevel = async (payload, callbacks) => {
24
- sinricProData.powerLevel = payload.value.powerLevel;
25
-
26
- if (callbacks.setPowerLevel) {
27
- const resp = await callbacks.setPowerLevel(payload.deviceId, payload.value.powerLevel);
28
- if (payload.value.powerLevel && resp) {
29
- return payload.value;
30
- }
31
- throw new InvalidResponseError('powerLevel is undefined');
32
- } else {
33
- throw new UndefinedCallbackError('setPowerLevel callback is undefined.');
34
- }
35
- };
36
-
37
- const powerLevelAdjust = async (payload, callbacks) => {
38
- sinricProData.powerLevel += payload.value.powerLevelDelta;
39
-
40
- if (sinricProData.powerLevel < 0) sinricProData.powerLevel = 0;
41
- else if (sinricProData.powerLevel > 100) sinricProData.powerLevel = 100;
42
-
43
- if (callbacks.adjustPowerLevel) {
44
- const resp = await callbacks.adjustPowerLevel(payload.deviceId, sinricProData.powerLevel);
45
-
46
- if (payload.value.powerLevelDelta && resp) return { powerLevel: sinricProData.powerLevel };
47
- throw new InvalidResponseError('PowerLevelDelta is 0 or undefined');
48
- } else {
49
- throw new UndefinedCallbackError('adjustPowerLevel callback is undefined.');
50
- }
51
- };
52
-
53
- module.exports = { powerState, powerLevel, powerLevelAdjust };
@@ -1,38 +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
-
8
- const sinricProData = require('../storage');
9
- const { UndefinedCallbackError, InvalidResponseError } = require('../errors/errors');
10
-
11
- const RangeValue = async (payload, callbacks) => {
12
- sinricProData.rangeValue = payload.value.rangeValue;
13
-
14
- if (callbacks.setRangeValue) {
15
- const resp = await callbacks.setRangeValue(payload.deviceId, sinricProData.rangeValue, payload.instanceId);
16
- if (payload.value.rangeValue !== undefined && resp) return [payload.value, payload.instanceId];
17
- throw new InvalidResponseError('RangeValue is undefined');
18
- } else {
19
- throw new UndefinedCallbackError('setRangeValue callback is undefined.');
20
- }
21
- };
22
-
23
- const RangeValueAdjust = async (payload, callbacks) => {
24
- sinricProData.rangeValue += payload.value.rangeValueDelta;
25
-
26
- if (sinricProData.rangeValue < 0) sinricProData.rangeValue = 0;
27
- else if (sinricProData.rangeValue > 100) sinricProData.rangeValue = 100;
28
-
29
- if (callbacks.adjustRangeValue) {
30
- const resp = await callbacks.adjustRangeValue(payload.deviceId, sinricProData.rangeValue, payload.instanceId);
31
- if (payload.value.rangeValueDelta && resp) return [{ rangeValue: sinricProData.rangeValue }, payload.instanceId];
32
- throw new InvalidResponseError('RangeValue is undefined');
33
- } else {
34
- throw new UndefinedCallbackError('adjustRangeValue callback is undefined.');
35
- }
36
- };
37
-
38
- module.exports = { RangeValue, RangeValueAdjust };
@@ -1,54 +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
-
8
- const sinricProData = require('../storage');
9
- const { UndefinedCallbackError, InvalidResponseError } = require('../errors/errors');
10
-
11
- const ThermoStatMode = async (payload, callbacks) => {
12
- sinricProData.thermostat.mode = payload.value.thermostatMode;
13
-
14
- if (callbacks.setThermostatMode) {
15
- const resp = await callbacks.setThermostatMode(payload.deviceId, sinricProData.thermostat.mode);
16
- if (payload.value.thermostatMode && resp) return payload.value;
17
- throw new InvalidResponseError('Thermostat mode undefined');
18
- } else {
19
- throw new UndefinedCallbackError('setThermostatMode callback is undefined.');
20
- }
21
- };
22
- const ThermoStatTemperature = async (payload, callbacks) => {
23
- sinricProData.thermostat.temperature = payload.value.temperature;
24
-
25
- if (callbacks.targetTemperature) {
26
- const resp = await callbacks.targetTemperature(payload.deviceId, sinricProData.thermostat.temperature);
27
- if (payload.value.temperature && resp) return payload.value;
28
- throw new InvalidResponseError('Thermostat temperature undefined');
29
- } else {
30
- throw new UndefinedCallbackError('targetTemperature callback is undefined.');
31
- }
32
- };
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
-
44
- const Lock = async (payload, callbacks) => {
45
- if (callbacks.setLockState) {
46
- const resp = await callbacks.setLockState(payload.deviceId, payload.value.state);
47
- if (resp) return { state: `${payload.value.state.toUpperCase()}ED` };
48
- throw new InvalidResponseError('Lock state is undefined');
49
- } else {
50
- throw new UndefinedCallbackError('setLockState callback is undefined.');
51
- }
52
- };
53
-
54
- module.exports = { ThermoStatMode, ThermoStatTemperature, Lock, AdjustTargetTemperature };
@@ -1,51 +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
-
8
- const sinricData = require('../storage');
9
- const { UndefinedCallbackError, InvalidResponseError } = require('../errors/errors');
10
-
11
- const Bands = async (payload, callbacks) => {
12
- sinricData.bands[0].level = payload.value.bands[0].level;
13
- sinricData.bands[0].name = payload.value.bands[0].name;
14
-
15
- if (callbacks.setBands) {
16
- const resp = await callbacks.setBands(payload.deviceId, payload.value.bands);
17
- if (resp === true) return payload.value;
18
- throw new InvalidResponseError('Callback failed');
19
- } else {
20
- throw new UndefinedCallbackError('setBands callback is undefined.');
21
- }
22
- };
23
-
24
- const BandReset = async (payload, callbacks) => {
25
- if (callbacks.resetBands) {
26
- const resp = await callbacks.resetBands(payload.deviceId, payload.value);
27
- if (resp === true) return payload.value;
28
- throw new InvalidResponseError('Callback failed');
29
- } else {
30
- throw new UndefinedCallbackError('resetBands callback is undefined.');
31
- }
32
- };
33
-
34
- const AdjustBands = async (payload, callbacks) => {
35
- if (payload.value.bands[0].levelDelta) {
36
- if (sinricData.bands[0].levelDirection === 'UP') sinricData.bands[0].level += payload.value.bands[0].levelDelta;
37
- else if (sinricData.bands[0].levelDirection === 'DOWN') sinricData.bands[0].level -= payload.value.bands[0].levelDelta;
38
- }
39
-
40
- if (callbacks.adjustBands) {
41
- const resp = await callbacks.adjustBands(payload.deviceId, payload.value);
42
- if (resp === true) return [payload.value, { bands: [{ level: sinricData.bands[0].level, name: sinricData.bands[0].name }] }];
43
- throw new InvalidResponseError('Callback failed');
44
- } else {
45
- throw new UndefinedCallbackError('adjustBands callback is undefined.');
46
- }
47
- };
48
-
49
- module.exports = {
50
- Bands, BandReset, AdjustBands,
51
- };
@@ -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 ToggleState = async (payload, callbacks) => {
10
- if (callbacks.setToggleState) {
11
- const resp = await callbacks.setToggleState(payload.deviceId, payload.value.state, payload.instanceId);
12
- if (resp === true) return [payload.value, payload.instanceId];
13
- throw new InvalidResponseError('Callback failed');
14
- } else {
15
- throw new UndefinedCallbackError('setToggleState callback is undefined.');
16
- }
17
- };
18
-
19
- module.exports = {
20
- ToggleState,
21
- };
@@ -1,93 +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
-
8
- const sinricProData = require('../storage');
9
- const { UndefinedCallbackError, InvalidResponseError } = require('../errors/errors');
10
-
11
- const Volume = async (payload, callbacks) => {
12
- sinricProData.volume = payload.value.volume;
13
-
14
- if (callbacks.setVolume) {
15
- const resp = callbacks.setVolume(payload.deviceId, sinricProData.volume);
16
- if (payload.value.volume && resp) return payload.value;
17
- throw new InvalidResponseError('Volume undefined');
18
- } else {
19
- throw new UndefinedCallbackError('setVolume callback is undefined.');
20
- }
21
- };
22
-
23
- const VolumeAdjust = async (payload, callbacks) => {
24
- sinricProData.volume += payload.value.volume;
25
- if (sinricProData.volume > 100) sinricProData.volume = 100;
26
- else if (sinricProData.volume < 0) sinricProData.volume = 0;
27
-
28
- if (callbacks.adjustVolume) {
29
- const resp = await callbacks.setVolume(payload.deviceId, sinricProData.volume);
30
- if (payload.value.volume && resp) return { volume: sinricProData.volume };
31
- throw new InvalidResponseError('Volume undefined');
32
- } else {
33
- throw new UndefinedCallbackError('adjustVolume callback is undefined.');
34
- }
35
- };
36
-
37
- const Mute = async (payload, callbacks) => {
38
- if (callbacks.setMute) {
39
- const resp = await callbacks.setMute(payload.deviceId, payload.value.mute);
40
- if ((payload.value.mute === true || payload.value.mute === false) && resp) return payload.value;
41
- throw new InvalidResponseError('Mute undefined');
42
- } else {
43
- throw new UndefinedCallbackError('setMute callback is undefined.');
44
- }
45
- };
46
-
47
- const Input = async (payload, callbacks) => {
48
- sinricProData.input = payload.value.input;
49
-
50
- if (callbacks.selectInput) {
51
- const resp = await callbacks.selectInput(payload.deviceId, payload.value.input);
52
- if ((payload.value.mute === true || payload.value.mute === false) && resp) return payload.value;
53
- throw new InvalidResponseError('Input undefined');
54
- } else {
55
- throw new UndefinedCallbackError('selectInput callback is undefined.');
56
- }
57
- };
58
-
59
- const Media = async (payload, callbacks) => {
60
- if (callbacks.selectInput) {
61
- const resp = await callbacks.mediaControl(payload.deviceId, payload.value.control);
62
- if (payload.value.control && resp) return payload.value;
63
- throw new InvalidResponseError('Control undefined');
64
- } else {
65
- throw new UndefinedCallbackError('mediaControl callback is undefined.');
66
- }
67
- };
68
-
69
- const ChangeChannel = async (payload, callbacks) => {
70
- const channel = payload.value.channel.name || payload.value.channel.number;
71
-
72
- if (callbacks.changeChannel) {
73
- const resp = callbacks.changeChannel(payload.deviceId, channel);
74
- if (channel && resp) return payload.value;
75
- throw new InvalidResponseError('Channel name undefined');
76
- } else {
77
- throw new UndefinedCallbackError('mediaControl callback is undefined.');
78
- }
79
- };
80
-
81
- const SkipChannel = (payload, callbacks) => {
82
- if (callbacks.changeChannel) {
83
- const resp = callbacks.skipChannels(payload.deviceId, payload.value.channelCount);
84
- if (payload.value.channelCount && resp) return payload.value;
85
- throw new InvalidResponseError('Channel count undefined');
86
- } else {
87
- throw new UndefinedCallbackError('skipChannels callback is undefined.');
88
- }
89
- };
90
-
91
- module.exports = {
92
- Volume, VolumeAdjust, Mute, Input, Media, ChangeChannel, SkipChannel,
93
- };
@@ -1,8 +0,0 @@
1
- /* eslint-disable max-classes-per-file */
2
- // eslint-disable-next-line linebreak-style
3
- class UndefinedCallbackError extends Error { }
4
-
5
- class InvalidResponseError extends Error {}
6
-
7
- module.exports.UndefinedCallbackError = UndefinedCallbackError;
8
- module.exports.InvalidResponseError = InvalidResponseError;
@@ -1,108 +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
-
8
- const uuidv4 = require("uuid/v4");
9
- const { RateLimiterMemory, BurstyRateLimiter } = require("rate-limiter-flexible");
10
- const { getSignature } = require("../signature");
11
-
12
- /*
13
- * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
14
- * !! !!
15
- * !! WARNING: DON'T TOUCH ! !!
16
- * !! ====================== !!
17
- * !! PLEASE DO NOT MODIFY ANY OF THESE SETTINGS HERE !!
18
- * !! THIS IS FOR INTERNAL CONFIGURATION ONLY !!
19
- * !! SINRIC PRO MIGHT NOT WORK IF YOU MODIFY THIS !!
20
- * !! !!
21
- * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
22
- */
23
-
24
- const burstyLimiter = new BurstyRateLimiter(
25
- new RateLimiterMemory({
26
- points: 1,
27
- duration: 60,
28
- }),
29
- new RateLimiterMemory({
30
- keyPrefix: "burst",
31
- points: 10,
32
- duration: 60,
33
- })
34
- );
35
-
36
- // @TODO resteBands
37
-
38
- const eventNames = {
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",
58
- targetTemperature: "targetTemperature"
59
- };
60
-
61
- function isValidEventAction(action) {
62
- return Object.entries(eventNames).find(i => i[1] === action);
63
- }
64
-
65
- const getEvent = (client, action, deviceId, value) => {
66
- const header = {
67
- payloadVersion: 2,
68
- signatureVersion: 1,
69
- };
70
- const payload = {
71
- action,
72
- cause: {
73
- type: "PHYSICAL_INTERACTION",
74
- },
75
- createdAt: Math.round(new Date() / 1000),
76
- deviceId,
77
- replyToken: uuidv4(),
78
- type: "event",
79
- value,
80
- };
81
- const signature = {
82
- HMAC: getSignature(client, JSON.stringify(payload)),
83
- };
84
-
85
- return { header, payload, signature };
86
- };
87
-
88
- const raiseEvent = async (client, action, deviceId, value) => {
89
- if(client.readyState !== 1) {
90
- console.error("Sending aborted! The connection has not been established yet. Try again later!");
91
- return;
92
- }
93
-
94
- if (isValidEventAction(action)) {
95
- try {
96
- await burstyLimiter.consume("queue");
97
- const request = JSON.stringify(getEvent(client, action, deviceId, value));
98
- if (process.env.SR_DEBUG) console.log(">> %s", request);
99
- client.send(request);
100
- } catch (rlRejected) {
101
- console.error("Too many events!");
102
- }
103
- } else {
104
- console.error(`Invalid Event: ${eventName}`);
105
- }
106
- };
107
-
108
- module.exports = { raiseEvent, eventNames };
package/lib/signature.js DELETED
@@ -1,26 +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
-
8
- const crypto = require('crypto');
9
-
10
- const getSignature = (client, message) => crypto
11
- .createHmac('sha256', client.getSecretKey)
12
- .update(message)
13
- .digest('base64');
14
-
15
- const verifySignature = (client, payload, signature) => {
16
- const localSignature = getSignature(client, JSON.stringify(payload));
17
- return new Promise((resolve, reject) => {
18
- if (signature === localSignature) {
19
- resolve(payload.action);
20
- } else {
21
- reject(new Error("Signatures didn't match"));
22
- }
23
- });
24
- };
25
-
26
- module.exports = { getSignature, verifySignature };
package/lib/sinrcpro.js DELETED
@@ -1,129 +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
- */
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("wss://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
- if (callbacks.onConnected) {
42
- callbacks.onConnected();
43
- }
44
- });
45
-
46
- client.on("message", (data) => {
47
- if (process.env.SR_DEBUG) console.log('<< %s', data.toString());
48
-
49
- const parsedData = JSON.parse(data);
50
-
51
- if (parsedData.timestamp) {
52
- if (Math.floor(new Date() / 1000) - parsedData.timestamp > 60000) {
53
- if (process.env.SR_DEBUG) console.log("Timestamp is not in sync");
54
- } else if (process.env.SR_DEBUG) console.log("TimeStamp is in sync");
55
- } else {
56
- const response = callbackHandler(
57
- client,
58
- parsedData.payload,
59
- parsedData.signature.HMAC,
60
- callbacks,
61
- );
62
- response
63
- .then((res) => {
64
- const reqResponse = JSON.stringify(res);
65
- if (process.env.SR_DEBUG) console.log('>> %s', reqResponse);
66
- client.send(reqResponse);
67
- })
68
- .catch((err) => {
69
- console.log(err.message);
70
- });
71
- }
72
- });
73
-
74
- client.on("close", () => {
75
- if (callbacks.onDisconnect) {
76
- callbacks.onDisconnect();
77
- }
78
- });
79
- };
80
-
81
- const startSinricProObservable = (client, callbacks) => new rx.Observable((observer) => {
82
- client.on('open', () => {
83
- console.log('Connected to Sinric Pro');
84
- });
85
-
86
- client.on("message", (data) => {
87
- const parsedData = JSON.parse(data);
88
- if (parsedData.timestamp) {
89
- if (Math.floor(new Date() / 1000) - parsedData.timestamp > 60000) {
90
- if (process.env.SR_DEBUG) console.log("Timestamp is not in sync");
91
- } else if (process.env.SR_DEBUG) console.log("TimeStamp is in sync");
92
- } else {
93
- const response = callbackHandler(
94
- client,
95
- parsedData.payload,
96
- parsedData.signature.HMAC,
97
- callbacks,
98
- );
99
- response
100
- .then((res) => {
101
- observer.next(res);
102
- const reqResponse = JSON.stringify(res);
103
- if (process.env.SR_DEBUG) console.log('>> %s', reqResponse);
104
- client.send(reqResponse);
105
- })
106
- .catch((err) => {
107
- observer.error(err.message);
108
- console.log(err.message);
109
- });
110
- }
111
- });
112
-
113
- client.on("close", () => {
114
- observer.complete();
115
- if (callbacks.onDisconnect) {
116
- callbacks.onDisconnect();
117
- }
118
- console.log("Disconnected");
119
- });
120
- });
121
-
122
- module.exports = {
123
- SinricPro,
124
- startSinricPro,
125
- startSinricProObservable,
126
- raiseEvent,
127
- eventNames,
128
- SinricProUdp,
129
- };
@@ -1,60 +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
-
8
- const SocketUdp = require('dgram');
9
-
10
- const callbackHandler = require('./cbhandler');
11
-
12
- class SinricProUdp {
13
- get getSecretKey() {
14
- return this._secretKey;
15
- }
16
-
17
- constructor(deviceids, secretKey) {
18
- this._multicastAddress = '224.9.9.9';
19
- this._multicastPort = 3333;
20
- this._deviceIds = deviceids;
21
- this._secretKey = secretKey;
22
- this._multisocket = SocketUdp.createSocket({
23
- type: 'udp4',
24
- reuseAddr: true,
25
- });
26
- this._multisocket.bind(this._multicastPort, this._multicastAddress);
27
-
28
- this.begin = (callbacks) => {
29
- this._multisocket.on('listening', () => {
30
- this._multisocket.addMembership(this._multicastAddress);
31
- const address = this._multisocket.address();
32
- console.log(
33
- `Offline support started on : ${address.address}:${this._multicastPort}`,
34
- );
35
- });
36
-
37
- this._multisocket.on('message', (message, rinfo) => {
38
- const response = callbackHandler(
39
- this,
40
- JSON.parse(message).payload,
41
- JSON.parse(message).signature.HMAC,
42
- callbacks,
43
- );
44
- response
45
- .then((res) => {
46
- this._multisocket.send(
47
- JSON.stringify(res),
48
- rinfo.port,
49
- rinfo.address,
50
- );
51
- })
52
- .catch((err) => {
53
- console.log(err.message);
54
- });
55
- });
56
- };
57
- }
58
- }
59
-
60
- module.exports = SinricProUdp;