node-red-contrib-alice 2.0.6 → 2.1.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.
@@ -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,37 @@ module.exports = function(RED) {
62
65
  }, 300);
63
66
  }
64
67
  };
65
- // функция обновления состояния умений и сенсоров
66
- this._updateDeviceState= ()=>{
67
- let data;
68
- if (states === null || (states.capabilities.length==0 && states.properties.length==0)){
69
- data = '';
70
- }else{
71
- data = JSON.stringify(states);
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
+ const option = {
77
+ timeout: 2000,
78
+ method: 'POST',
79
+ url: 'https://api.nodered-home.ru/gtw/device/state',
80
+ headers: {
81
+ 'content-type': 'application/json',
82
+ 'Authorization': "Bearer "+service.getToken()
83
+ },
84
+ data: {
85
+ event: event,
86
+ state: states
87
+ }
88
+ };
89
+ if (states === null){
90
+ return;
72
91
  };
73
- service.send2gate('$me/device/state/'+this.id+'/states', data ,true);
92
+ axios.request(option)
93
+ .then(res=>{
94
+ this.trace("Device state updated successfully");
95
+ })
96
+ .catch(error=>{
97
+ this.error("Error when update device state: "+error.message);
98
+ })
74
99
  };
75
100
  // отправка эвентов
76
101
  this._sendEvent = (event)=>{
@@ -112,7 +137,7 @@ module.exports = function(RED) {
112
137
  sensors[sensorIndex] = sensId; // добавляем новый сенсор в локальный список
113
138
  sensor.id = sensId;
114
139
  deviceconfig.properties.push(sensor);
115
- this._updateDeviceInfo()
140
+ this._updateDeviceInfo();
116
141
  resolve(true);
117
142
  })
118
143
  };
@@ -129,7 +154,11 @@ module.exports = function(RED) {
129
154
  states.capabilities.splice(index, 1);
130
155
  };
131
156
  states.capabilities.push(state);
132
- this._updateDeviceState();
157
+ const currentevent = {
158
+ id: this.id,
159
+ capabilities:[state]
160
+ };
161
+ this._updateDeviceState(currentevent);
133
162
  resolve(true);
134
163
  // reject(new Error("Device not ready"));
135
164
  })
@@ -143,7 +172,11 @@ module.exports = function(RED) {
143
172
  states.properties.splice(index, 1);
144
173
  };
145
174
  states.properties.push(state);
146
- this._updateDeviceState();
175
+ const currentevent = {
176
+ id: this.id,
177
+ properties:[state]
178
+ };
179
+ this._updateDeviceState(currentevent);
147
180
  resolve(true);
148
181
  // reject(new Error("Device not ready"));
149
182
  })
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.6",
3
+ "version": "2.1.1",
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",