node-red-contrib-alice 2.0.5 → 2.1.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.
@@ -1,7 +1,10 @@
1
+ const axios = require('axios');
2
+
3
+
1
4
  module.exports = function(RED) {
2
5
  // ***************************** Alice DEVICE ****************************
3
6
  function AliceDevice(config){
4
- var pjson = require('../package.json');
7
+ const pjson = require('../package.json');
5
8
  RED.nodes.createNode(this,config);
6
9
  const service = RED.nodes.getNode(config.service);
7
10
  service.setMaxListeners(service.getMaxListeners() + 1); // увеличиваем лимит для event
@@ -62,15 +65,41 @@ module.exports = function(RED) {
62
65
  }, 300);
63
66
  }
64
67
  };
65
- // функция обновления состояния умений и сенсоров
66
- this._updateDeviceState= ()=>{
67
- let data;
68
+ // функция обновления состояния устройства (умений и сенсоров)
69
+ this._updateDeviceState= (event=null)=>{
70
+ // if (states === null || (states.capabilities.length==0 && states.properties.length==0)){
71
+ // data = '';
72
+ // }else{
73
+ // data = JSON.stringify(states);
74
+ // };
75
+ // service.send2gate('$me/device/state/'+this.id+'/states', data ,true);
76
+ let data = states;
68
77
  if (states === null || (states.capabilities.length==0 && states.properties.length==0)){
69
- data = '';
70
- }else{
71
- data = JSON.stringify(states);
78
+ data = null;
79
+ };
80
+ const option = {
81
+ timeout: 2000,
82
+ method: 'POST',
83
+ url: 'https://api.nodered-home.ru/gtw/device/state',
84
+ headers: {
85
+ 'content-type': 'application/json',
86
+ 'Authorization': "Bearer "+service.getToken()
87
+ },
88
+ data: {
89
+ event: event,
90
+ state: states
91
+ }
92
+ };
93
+ if (states === null || (states.capabilities.length==0 && states.properties.length==0)){
94
+ return;
72
95
  };
73
- service.send2gate('$me/device/state/'+this.id+'/states', data ,true);
96
+ axios.request(option)
97
+ .then(res=>{
98
+ this.trace("Device state updated successfully");
99
+ })
100
+ .catch(error=>{
101
+ this.error("Error when update device state: "+error.message);
102
+ })
74
103
  };
75
104
  // отправка эвентов
76
105
  this._sendEvent = (event)=>{
@@ -112,7 +141,7 @@ module.exports = function(RED) {
112
141
  sensors[sensorIndex] = sensId; // добавляем новый сенсор в локальный список
113
142
  sensor.id = sensId;
114
143
  deviceconfig.properties.push(sensor);
115
- this._updateDeviceInfo()
144
+ this._updateDeviceInfo();
116
145
  resolve(true);
117
146
  })
118
147
  };
@@ -129,7 +158,11 @@ module.exports = function(RED) {
129
158
  states.capabilities.splice(index, 1);
130
159
  };
131
160
  states.capabilities.push(state);
132
- this._updateDeviceState();
161
+ const currentevent = {
162
+ id: this.id,
163
+ capabilities:[state]
164
+ };
165
+ this._updateDeviceState(currentevent);
133
166
  resolve(true);
134
167
  // reject(new Error("Device not ready"));
135
168
  })
@@ -143,7 +176,11 @@ module.exports = function(RED) {
143
176
  states.properties.splice(index, 1);
144
177
  };
145
178
  states.properties.push(state);
146
- this._updateDeviceState();
179
+ const currentevent = {
180
+ id: this.id,
181
+ properties:[state]
182
+ };
183
+ this._updateDeviceState(currentevent);
147
184
  resolve(true);
148
185
  // reject(new Error("Device not ready"));
149
186
  })
package/nodes/alice.js CHANGED
@@ -82,6 +82,10 @@ module.exports = function(RED) {
82
82
  mqttClient.publish(path, data ,{ qos: 0, retain: retain });
83
83
  }
84
84
 
85
+ this.getToken = ()=>{
86
+ return JSON.parse(token).access_token;
87
+ }
88
+
85
89
  };
86
90
  RED.nodes.registerType("alice-service",AliceService,{
87
91
  credentials: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-alice",
3
- "version": "2.0.5",
3
+ "version": "2.1.0",
4
4
  "description": "",
5
5
  "main": "alice.js",
6
6
  "scripts": {
@@ -21,7 +21,7 @@
21
21
  "алиса"
22
22
  ],
23
23
  "node-red": {
24
- "version": ">=2.0.0",
24
+ "version": ">=3.0.0",
25
25
  "nodes": {
26
26
  "alice-service": "./nodes/alice.js",
27
27
  "alice-device": "./nodes/alice-device.js",
@@ -43,6 +43,6 @@
43
43
  "homepage": "https://github.com/efa2000/node-red-contrib-alice#readme",
44
44
  "dependencies": {
45
45
  "axios": "^1.4.0",
46
- "mqtt": "^5.0.3"
46
+ "mqtt": "^4.3.7"
47
47
  }
48
48
  }