sinricpro 2.11.3 → 2.12.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/CHANGELOG.md CHANGED
@@ -32,3 +32,14 @@
32
32
 
33
33
  ### Features
34
34
  * fix: adjustTargetTemperature response format.
35
+
36
+ ## [2.11.4]
37
+
38
+ ### Features
39
+ * fix: power on, off event
40
+
41
+ ## [2.12.1]
42
+
43
+ ### Features
44
+ * fix: error message when sending an event while connection not established yet
45
+ * feat: add onConnected callback
@@ -58,14 +58,17 @@ const eventNames = {
58
58
  targetTemperature: "targetTemperature"
59
59
  };
60
60
 
61
+ function isValidEventAction(action) {
62
+ return Object.entries(eventNames).find(i => i[1] === action);
63
+ }
61
64
 
62
- const getJson = (client, eventName, deviceId, value) => {
65
+ const getEvent = (client, action, deviceId, value) => {
63
66
  const header = {
64
67
  payloadVersion: 2,
65
68
  signatureVersion: 1,
66
69
  };
67
70
  const payload = {
68
- action: eventName,
71
+ action,
69
72
  cause: {
70
73
  type: "PHYSICAL_INTERACTION",
71
74
  },
@@ -82,18 +85,23 @@ const getJson = (client, eventName, deviceId, value) => {
82
85
  return { header, payload, signature };
83
86
  };
84
87
 
85
- const raiseEvent = async (client, eventName, deviceId, value) => {
86
- if (Object.hasOwn(eventNames, eventName)) {
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)) {
87
95
  try {
88
96
  await burstyLimiter.consume("queue");
89
- const request = JSON.stringify(getJson(client, eventName, deviceId, value));
97
+ const request = JSON.stringify(getEvent(client, action, deviceId, value));
90
98
  if (process.env.SR_DEBUG) console.log(">> %s", request);
91
99
  client.send(request);
92
100
  } catch (rlRejected) {
93
- console.log("Too many events!");
101
+ console.error("Too many events!");
94
102
  }
95
103
  } else {
96
- console.log(`Invalid Event: ${eventName}`);
104
+ console.error(`Invalid Event: ${eventName}`);
97
105
  }
98
106
  };
99
107
 
package/lib/sinrcpro.js CHANGED
@@ -38,7 +38,9 @@ class SinricPro extends Websocket {
38
38
 
39
39
  const startSinricPro = (client, callbacks) => {
40
40
  client.on('open', () => {
41
- console.log('Connected to Sinric Pro');
41
+ if (callbacks.onConnected) {
42
+ callbacks.onConnected();
43
+ }
42
44
  });
43
45
 
44
46
  client.on("message", (data) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sinricpro",
3
- "version": "2.11.3",
3
+ "version": "2.12.1",
4
4
  "description": "Sinric Pro Nodejs SDK",
5
5
  "main": "index.js",
6
6
  "repository": {