node-red-contrib-alice 2.1.2 → 2.1.4

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.
@@ -168,6 +168,7 @@ module.exports = function(RED) {
168
168
  }
169
169
  break;
170
170
  case 'number':
171
+ value = Math.round(value);
171
172
  if (value>=this.temperature_min && value<=this.temperature_max){
172
173
  state.value = value;
173
174
  state.instance = 'temperature_k';
@@ -42,42 +42,67 @@ module.exports = function(RED) {
42
42
  this.initState = true;
43
43
  };
44
44
  // функция обновления информации об устройстве
45
- this._updateDeviceInfo= (now)=>{
46
- let data;
47
- if (deviceconfig === null || (deviceconfig.capabilities.length==0 && deviceconfig.properties.length==0)){
48
- data = '';
49
- }else{
50
- data = JSON.stringify(deviceconfig);
51
- };
52
- if (now){
53
- this.debug("Updating Device info ...");
54
- service.send2gate('$me/device/state/'+this.id, data ,true);
45
+ this._updateDeviceInfo= _=>{
46
+ let now = false;
47
+
48
+ if (deviceconfig.capabilities.length==0 && deviceconfig.properties.length==0){
49
+ this.debug("DELETE Device config from gateway ...");
50
+ /// отправка по http
51
+ const option = {
52
+ timeout: 5000,
53
+ method: 'POST',
54
+ url: 'https://api.nodered-home.ru/gtw/device/config',
55
+ headers: {
56
+ 'content-type': 'application/json',
57
+ 'Authorization': "Bearer "+service.getToken()
58
+ },
59
+ data: {
60
+ id: this.id,
61
+ config: deviceconfig
62
+ }
63
+ };
64
+ axios.request(option)
65
+ .then(res=>{
66
+ this.trace("Device config deleted on gateway successfully");
67
+ })
68
+ .catch(error=>{
69
+ this.debug("Error when delete device config on gateway: "+error.message);
70
+ });
55
71
  return;
56
72
  };
73
+
57
74
  if (!updating){
58
75
  updating = true;
59
76
  setTimeout(() => {
60
- this.debug("Updating Device info ...");
77
+ this.debug("Updating Device config ...");
61
78
  updating = false;
62
- let nData = JSON.stringify(deviceconfig);
63
- if (deviceconfig === null){ nData = ''};
64
- service.send2gate('$me/device/state/'+this.id, nData ,true);
65
- }, 300);
79
+ const option = {
80
+ timeout: 5000,
81
+ method: 'POST',
82
+ url: 'https://api.nodered-home.ru/gtw/device/config',
83
+ headers: {
84
+ 'content-type': 'application/json',
85
+ 'Authorization': "Bearer "+service.getToken()
86
+ },
87
+ data: {
88
+ id: this.id,
89
+ config: deviceconfig
90
+ }
91
+ };
92
+ axios.request(option)
93
+ .then(res=>{
94
+ this.trace("Device config updated successfully");
95
+ })
96
+ .catch(error=>{
97
+ this.debug("Error when update device config: "+error.message);
98
+ });
99
+ }, 1000);
66
100
  }
67
101
  };
68
102
  // функция обновления состояния устройства (умений и сенсоров)
69
103
  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
- if (states === null){
77
- return;
78
- };
79
104
  const option = {
80
- timeout: 4000,
105
+ timeout: 5000,
81
106
  method: 'POST',
82
107
  url: 'https://api.nodered-home.ru/gtw/device/state',
83
108
  headers: {
@@ -85,6 +110,7 @@ module.exports = function(RED) {
85
110
  'Authorization': "Bearer "+service.getToken()
86
111
  },
87
112
  data: {
113
+ id: this.id,
88
114
  event: event,
89
115
  state: states
90
116
  }
@@ -94,7 +120,7 @@ module.exports = function(RED) {
94
120
  this.trace("Device state updated successfully");
95
121
  })
96
122
  .catch(error=>{
97
- this.error("Error when update device state: "+error.message);
123
+ this.debug("Error when update device state: "+error.message);
98
124
  })
99
125
  };
100
126
  // отправка эвентов
@@ -221,6 +247,7 @@ module.exports = function(RED) {
221
247
  if (stateindex>-1){
222
248
  states.properties.splice(stateindex, 1);
223
249
  };
250
+ this._updateDeviceState();
224
251
  resolve(true);
225
252
  })
226
253
  };
@@ -256,15 +283,17 @@ module.exports = function(RED) {
256
283
  this.on('close', (removed, done)=>{
257
284
  this.emit('offline');
258
285
  if (removed){
259
- deviceconfig = null;
260
- states = null;
286
+ deviceconfig.capabilities = [];
287
+ deviceconfig.properties = [];
288
+ states.capabilities = [];
289
+ states.properties = [];
261
290
  this._updateDeviceState();
262
- this._updateDeviceInfo(true);
291
+ this._updateDeviceInfo();
263
292
  };
264
293
  setTimeout(()=>{
265
294
  // this.emit('offline');
266
295
  done();
267
- },300)
296
+ },500)
268
297
  });
269
298
  };
270
299
  RED.nodes.registerType("alice-device",AliceDevice);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-alice",
3
- "version": "2.1.2",
3
+ "version": "2.1.4",
4
4
  "description": "",
5
5
  "main": "alice.js",
6
6
  "scripts": {
@@ -11,7 +11,7 @@
11
11
  "url": "git+https://github.com/efa2000/node-red-contrib-alice.git"
12
12
  },
13
13
  "engines": {
14
- "node": ">=12.0.0"
14
+ "node": ">=14.0.0"
15
15
  },
16
16
  "keywords": [
17
17
  "node-red",