node-red-contrib-alice 2.2.5 → 2.3.3

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,107 +1,106 @@
1
- module.exports = function(RED) {
2
- // ************** ON/OFF *******************
3
- function AliceSensor(config){
4
- RED.nodes.createNode(this,config);
5
- const device = RED.nodes.getNode(config.device);
6
- device.setMaxListeners(device.getMaxListeners() + 1); // увеличиваем лимит для event
7
- const id =JSON.parse(JSON.stringify(this.id));
8
- const name = config.name;
9
- const stype = config.stype;
10
- const reportable = true;
11
- const retrievable = true;
12
- const unit = config.unit;
13
- const instance = config.instance;
14
- let initState = false;
15
- // this.value;
16
- let curentState= {
17
- type:stype,
18
- state:{
19
- instance: instance,
20
- value: 0
21
- }
22
- };
23
-
24
- this.status({fill:"red",shape:"dot",text:"offline"});
25
-
26
- this.init = ()=>{
27
- this.debug("Starting sensor initilization ...");
28
- let sensor = {
29
- type: stype,
30
- reportable: reportable,
31
- retrievable: retrievable,
32
- parameters: {
33
- instance: instance,
34
- unit: unit
35
- }
36
- };
37
-
38
- device.setSensor(id,sensor)
39
- .then(res=>{
40
- this.debug("Sensor initilization - success!");
41
- this.status({fill:"green",shape:"dot",text:"online"});
42
- initState = true;
43
- })
44
- .catch(err=>{
45
- this.error("Error on create sensor: " +err.message);
46
- this.status({fill:"red",shape:"dot",text:"error"});
1
+ "use strict";
2
+ module.exports = (RED) => {
3
+ function AliceSensor(config) {
4
+ RED.nodes.createNode(this, config);
5
+ const device = RED.nodes.getNode(config.device);
6
+ device.setMaxListeners(device.getMaxListeners() + 1);
7
+ const id = JSON.parse(JSON.stringify(this.id));
8
+ const stype = config.stype;
9
+ const unit = config.unit;
10
+ const instance = config.instance;
11
+ const curentState = {
12
+ type: stype,
13
+ state: {
14
+ instance: instance,
15
+ value: 0
16
+ }
17
+ };
18
+ let lastUpdateTime = 0;
19
+ const UPDATE_INTERVAL = 10 * 60 * 1000;
20
+ this.status({ fill: "red", shape: "dot", text: "offline" });
21
+ const init = () => {
22
+ this.debug("Starting sensor initilization ...");
23
+ const sensor = {
24
+ type: stype,
25
+ reportable: true,
26
+ retrievable: true,
27
+ parameters: {
28
+ instance: instance,
29
+ unit: unit
30
+ }
31
+ };
32
+ device.setSensor(id, sensor)
33
+ .then(() => {
34
+ this.debug("Sensor initilization - success!");
35
+ this.status({ fill: "green", shape: "dot", text: "online" });
36
+ })
37
+ .catch(err => {
38
+ this.error("Error on create sensor: " + err.message);
39
+ this.status({ fill: "red", shape: "dot", text: "error" });
40
+ });
41
+ };
42
+ if (device.initState)
43
+ init();
44
+ device.on("online", () => {
45
+ init();
47
46
  });
48
- };
49
-
50
- // Проверяем сам девайс уже инициирован
51
- if (device.initState) this.init();
52
-
53
- device.on("online",()=>{
54
- this.init();
55
- });
56
-
57
- device.on("offline",()=>{
58
- this.status({fill:"red",shape:"dot",text:"offline"});
59
- });
60
-
61
- this.on('input', (msg, send, done)=>{
62
- if (typeof msg.payload != 'number'){
63
- this.error("Wrong type! msg.payload must be number.");
64
- if (done) {done();}
65
- return;
66
- };
67
- if (unit == 'unit.temperature.celsius' || unit == 'unit.ampere'){
68
- msg.payload = +msg.payload.toFixed(1);
69
- }else {
70
- msg.payload = +msg.payload.toFixed(0);
71
- };
72
- if (curentState.state.value == msg.payload){
73
- this.debug("Value not changed. Cancel update");
74
- if (done) {done();}
75
- return;
76
- };
77
- curentState.state.value = msg.payload;
78
- device.updateSensorState(id,curentState)
79
- .then(ref=>{
80
- this.status({fill:"green",shape:"dot",text: msg.payload});
81
- if (done) {done();}
82
- })
83
- .catch(err=>{
84
- this.error("Error on update sensor state: " +err.message);
85
- this.status({fill:"red",shape:"dot",text:"Error"});
86
- if (done) {done();}
87
- })
88
- });
89
-
90
- this.on('close', function(removed, done) {
91
- if (removed) {
92
- device.delSensor(id)
93
- .then(res=>{
94
- done()
95
- })
96
- .catch(err=>{
97
- this.error("Error on delete property: " +err.message);
98
- done();
99
- })
100
- }else{
101
- device.setMaxListeners(device.getMaxListeners() - 1);
102
- done();
103
- }
104
- });
105
- }
106
- RED.nodes.registerType("Sensor",AliceSensor);
107
- };
47
+ device.on("offline", () => {
48
+ this.status({ fill: "red", shape: "dot", text: "offline" });
49
+ });
50
+ this.on('input', (msg, _send, done) => {
51
+ if (typeof msg.payload != 'number') {
52
+ this.error("Wrong type! msg.payload must be number.");
53
+ if (done) {
54
+ done();
55
+ }
56
+ return;
57
+ }
58
+ if (unit == 'unit.temperature.celsius' || unit == 'unit.ampere' || unit == 'unit.pressure.bar') {
59
+ msg.payload = +msg.payload.toFixed(1);
60
+ }
61
+ else {
62
+ msg.payload = +msg.payload.toFixed(0);
63
+ }
64
+ const timeSinceLastUpdate = Date.now() - lastUpdateTime;
65
+ if (curentState.state.value == msg.payload && timeSinceLastUpdate < UPDATE_INTERVAL) {
66
+ this.debug("Value not changed. Cancel update");
67
+ if (done) {
68
+ done();
69
+ }
70
+ return;
71
+ }
72
+ curentState.state.value = msg.payload;
73
+ lastUpdateTime = Date.now();
74
+ device.updateSensorState(id, curentState)
75
+ .then(() => {
76
+ this.status({ fill: "green", shape: "dot", text: String(msg.payload) });
77
+ if (done) {
78
+ done();
79
+ }
80
+ })
81
+ .catch(err => {
82
+ this.error("Error on update sensor state: " + err.message);
83
+ this.status({ fill: "red", shape: "dot", text: "Error" });
84
+ if (done) {
85
+ done();
86
+ }
87
+ });
88
+ });
89
+ this.on('close', (removed, done) => {
90
+ if (removed) {
91
+ device.delSensor(id)
92
+ .then(() => { done(); })
93
+ .catch(err => {
94
+ this.error("Error on delete property: " + err.message);
95
+ done();
96
+ });
97
+ }
98
+ else {
99
+ device.setMaxListeners(device.getMaxListeners() - 1);
100
+ done();
101
+ }
102
+ });
103
+ }
104
+ RED.nodes.registerType("Sensor", AliceSensor);
105
+ };
106
+ //# sourceMappingURL=alice-sensor.js.map
@@ -1,127 +1,120 @@
1
- module.exports = function(RED) {
2
- // ************** Toggle *******************
3
- function AliceToggle(config){
4
- RED.nodes.createNode(this,config);
5
- this.device = RED.nodes.getNode(config.device);
6
- this.device.setMaxListeners(this.device.getMaxListeners() + 1); // увеличиваем лимит для event
7
- this.name = config.name;
8
- this.ctype = 'devices.capabilities.toggle';
9
- this.instance = config.instance;
10
- this.response = config.response;
11
- this.initState = false;
12
- this.value = false;
13
-
14
- if (config.response === undefined){
15
- this.response = true;
16
- }
17
-
18
- this.status({fill:"red",shape:"dot",text:"offline"});
19
-
20
- this.init = ()=>{
21
- this.debug("Starting capability initilization ...");
22
- let capab = {
23
- type: this.ctype,
24
- retrievable: true,
25
- reportable: true,
26
- parameters: {
27
- instance: this.instance,
1
+ "use strict";
2
+ module.exports = (RED) => {
3
+ function AliceToggle(config) {
4
+ RED.nodes.createNode(this, config);
5
+ const device = RED.nodes.getNode(config.device);
6
+ device.setMaxListeners(device.getMaxListeners() + 1);
7
+ const ctype = 'devices.capabilities.toggle';
8
+ const instance = config.instance || '';
9
+ let response = config.response;
10
+ let value = false;
11
+ if (config.response === undefined) {
12
+ response = true;
28
13
  }
29
- };
30
- this.device.setCapability(this.id,capab)
31
- .then(res=>{
32
- this.initState = true;
33
- // this.value = capab.state.value;
34
- this.debug("Capability initilization - success!");
35
- this.status({fill:"green",shape:"dot",text:"online"});
36
- })
37
- .catch(err=>{
38
- this.error("Error on create capability: " + err.message);
39
- this.status({fill:"red",shape:"dot",text:"error"});
14
+ this.status({ fill: "red", shape: "dot", text: "offline" });
15
+ const init = () => {
16
+ this.debug("Starting capability initilization ...");
17
+ const capab = {
18
+ type: ctype,
19
+ retrievable: true,
20
+ reportable: true,
21
+ parameters: {
22
+ instance: instance,
23
+ }
24
+ };
25
+ device.setCapability(this.id, capab)
26
+ .then(() => {
27
+ this.debug("Capability initilization - success!");
28
+ this.status({ fill: "green", shape: "dot", text: "online" });
29
+ })
30
+ .catch(err => {
31
+ this.error("Error on create capability: " + err.message);
32
+ this.status({ fill: "red", shape: "dot", text: "error" });
33
+ });
34
+ };
35
+ if (device.initState)
36
+ init();
37
+ device.on("online", () => {
38
+ init();
40
39
  });
41
- };
42
-
43
- // Проверяем сам девайс уже инициирован
44
- if (this.device.initState) this.init();
45
-
46
- this.device.on("online",()=>{
47
- this.init();
48
- });
49
-
50
- this.device.on("offline",()=>{
51
- this.status({fill:"red",shape:"dot",text:"offline"});
52
- });
53
-
54
- this.device.on(this.id,(val)=>{
55
- this.debug("Received a new value from Yandex...");
56
- this.send({
57
- payload: val
58
- });
59
- let state= {
60
- type:this.ctype,
61
- state:{
62
- instance: this.instance,
63
- value: val
64
- }
65
- };
66
- if (this.response){
67
- this.debug("Automatic confirmation is true, sending confirmation to Yandex ...");
68
- this.device.updateCapabState(this.id,state)
69
- .then (res=>{
70
- this.value = val;
71
- this.status({fill:"green",shape:"dot",text:val});
72
- })
73
- .catch(err=>{
74
- this.error("Error on update capability state: " + err.message);
75
- this.status({fill:"red",shape:"dot",text:"Error"});
76
- })
77
- };
78
- })
79
-
80
- this.on('input', (msg, send, done)=>{
81
- if (typeof msg.payload != 'boolean'){
82
- this.error("Wrong type! msg.payload must be boolean.");
83
- if (done) {done();}
84
- return;
85
- };
86
- if (msg.payload === this.value){
87
- this.debug("Value not changed. Cancel update");
88
- if (done) {done();}
89
- return;
90
- };
91
- let state= {
92
- type:this.ctype,
93
- state:{
94
- instance: this.instance,
95
- value: msg.payload
96
- }
97
- };
98
- this.device.updateCapabState(this.id,state)
99
- .then(ref=>{
100
- this.value = msg.payload;
101
- this.status({fill:"green",shape:"dot",text:msg.payload.toString()});
102
- if (done) {done();}
103
- })
104
- .catch(err=>{
105
- this.error("Error on update capability state: " + err.message);
106
- this.status({fill:"red",shape:"dot",text:"Error"});
107
- if (done) {done();}
108
- })
109
- });
110
-
111
- this.on('close', (removed, done)=>{
112
- if (removed) {
113
- this.device.delCapability(this.id)
114
- .then(res=>{
115
- done()
116
- })
117
- .catch(err=>{
118
- this.error("Error on delete capability: " + err.message);
119
- done();
120
- })
121
- }else{
122
- done();
123
- }
124
- });
125
- }
126
- RED.nodes.registerType("Toggle",AliceToggle);
127
- };
40
+ device.on("offline", () => {
41
+ this.status({ fill: "red", shape: "dot", text: "offline" });
42
+ });
43
+ device.on(this.id, (val) => {
44
+ this.debug("Received a new value from Yandex...");
45
+ this.send({ payload: val });
46
+ const state = {
47
+ type: ctype,
48
+ state: {
49
+ instance: instance,
50
+ value: val
51
+ }
52
+ };
53
+ if (response) {
54
+ this.debug("Automatic confirmation is true, sending confirmation to Yandex ...");
55
+ device.updateCapabState(this.id, state)
56
+ .then(() => {
57
+ value = val;
58
+ this.status({ fill: "green", shape: "dot", text: String(val) });
59
+ })
60
+ .catch(err => {
61
+ this.error("Error on update capability state: " + err.message);
62
+ this.status({ fill: "red", shape: "dot", text: "Error" });
63
+ });
64
+ }
65
+ });
66
+ this.on('input', (msg, _send, done) => {
67
+ if (typeof msg.payload != 'boolean') {
68
+ this.error("Wrong type! msg.payload must be boolean.");
69
+ if (done) {
70
+ done();
71
+ }
72
+ return;
73
+ }
74
+ if (msg.payload === value) {
75
+ this.debug("Value not changed. Cancel update");
76
+ if (done) {
77
+ done();
78
+ }
79
+ return;
80
+ }
81
+ const state = {
82
+ type: ctype,
83
+ state: {
84
+ instance: instance,
85
+ value: msg.payload
86
+ }
87
+ };
88
+ device.updateCapabState(this.id, state)
89
+ .then(() => {
90
+ value = msg.payload;
91
+ this.status({ fill: "green", shape: "dot", text: String(msg.payload) });
92
+ if (done) {
93
+ done();
94
+ }
95
+ })
96
+ .catch(err => {
97
+ this.error("Error on update capability state: " + err.message);
98
+ this.status({ fill: "red", shape: "dot", text: "Error" });
99
+ if (done) {
100
+ done();
101
+ }
102
+ });
103
+ });
104
+ this.on('close', (removed, done) => {
105
+ if (removed) {
106
+ device.delCapability(this.id)
107
+ .then(() => { done(); })
108
+ .catch(err => {
109
+ this.error("Error on delete capability: " + err.message);
110
+ done();
111
+ });
112
+ }
113
+ else {
114
+ done();
115
+ }
116
+ });
117
+ }
118
+ RED.nodes.registerType("Toggle", AliceToggle);
119
+ };
120
+ //# sourceMappingURL=alice-togle.js.map