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,131 +1,127 @@
1
- module.exports = function(RED) {
2
- // ************** ON/OFF *******************
3
- function AliceOnOff(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 ctype = 'devices.capabilities.on_off';
9
- const instance = 'on';
10
- let response = config.response;
11
- let split = config.split;
12
- let initState = false;
13
- let curentState = {
14
- type:ctype,
15
- state:{
16
- instance: instance,
17
- value: false
18
- }
19
- };
20
-
21
- if (config.response === undefined){
22
- response = true;
23
- };
24
- if (config.split === undefined){
25
- split = false;
26
- };
27
-
28
- this.status({fill:"red",shape:"dot",text:"offline"});
29
-
30
- this.init = ()=>{
31
- this.debug("Starting capability initilization ...");
32
- let capab = {
33
- type: ctype,
34
- retrievable: true,
35
- reportable: true,
36
- parameters: {
37
- instance: instance,
38
- split: split
1
+ "use strict";
2
+ module.exports = (RED) => {
3
+ function AliceOnOff(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 ctype = 'devices.capabilities.on_off';
9
+ const instance = 'on';
10
+ let response = config.response;
11
+ let split = config.split;
12
+ let initState = false;
13
+ const curentState = {
14
+ type: ctype,
15
+ state: {
16
+ instance: instance,
17
+ value: false
18
+ }
19
+ };
20
+ if (config.response === undefined) {
21
+ response = true;
39
22
  }
40
- };
41
-
42
- device.setCapability(id,capab)
43
- .then(res=>{
44
- this.debug("Capability initilization - success!");
45
- initState = true;
46
- this.status({fill:"green",shape:"dot",text:"online"});
47
- })
48
- .catch(err=>{
49
- this.error("Error on create capability: " + err.message);
50
- this.status({fill:"red",shape:"dot",text:"error"});
51
- });
52
- device.updateCapabState(id,curentState)
53
- .then (res=>{
54
- this.status({fill:"green",shape:"dot",text:"online"});
55
- })
56
- .catch(err=>{
57
- this.error("Error on update capability state: " + err.message);
58
- this.status({fill:"red",shape:"dot",text:"Error"});
59
- });
60
- };
61
-
62
- // Проверяем сам девайс уже инициирован
63
- if (device.initState) this.init();
64
-
65
- device.on("online",()=>{
66
- this.init();
67
- });
68
-
69
- device.on("offline",()=>{
70
- this.status({fill:"red",shape:"dot",text:"offline"});
71
- });
72
-
73
- device.on(id,(val)=>{
74
- this.send({
75
- payload: val
76
- });
77
- if (response){
78
- curentState.state.value = val;
79
- device.updateCapabState(id,curentState)
80
- .then (res=>{
81
- this.status({fill:"green",shape:"dot",text:val.toString()});
82
- })
83
- .catch(err=>{
84
- this.error("Error on update capability state: " + err.message);
85
- this.status({fill:"red",shape:"dot",text:"Error"});
86
- })
87
- };
88
- })
89
-
90
- this.on('input', (msg, send, done)=>{
91
- if (typeof msg.payload != 'boolean'){
92
- this.error("Wrong type! msg.payload must be boolean.");
93
- if (done) {done();}
94
- return;
95
- };
96
- if (msg.payload === curentState.state.value){
97
- this.debug("Value not changed. Cancel update");
98
- if (done) {done();}
99
- return;
100
- };
101
- curentState.state.value = msg.payload;
102
- device.updateCapabState(id,curentState)
103
- .then(ref=>{
104
- this.status({fill:"green",shape:"dot",text:msg.payload.toString()});
105
- if (done) {done();}
106
- })
107
- .catch(err=>{
108
- this.error("Error on update capability state: " + err.message);
109
- this.status({fill:"red",shape:"dot",text:"Error"});
110
- if (done) {done();}
111
- })
112
- });
113
-
114
- this.on('close', (removed, done)=>{
115
- device.setMaxListeners(device.getMaxListeners() - 1);
116
- if (removed) {
117
- device.delCapability(id)
118
- .then(res=>{
119
- done()
120
- })
121
- .catch(err=>{
122
- this.error("Error on delete capability: " + err.message);
123
- done();
124
- })
125
- };
126
- done();
127
- return;
128
- });
129
- }
130
- RED.nodes.registerType("On_Off",AliceOnOff);
131
- };
23
+ if (config.split === undefined) {
24
+ split = false;
25
+ }
26
+ this.status({ fill: "red", shape: "dot", text: "offline" });
27
+ const init = () => {
28
+ this.debug("Starting capability initilization ...");
29
+ const capab = {
30
+ type: ctype,
31
+ retrievable: true,
32
+ reportable: true,
33
+ parameters: {
34
+ instance: instance,
35
+ split: split
36
+ }
37
+ };
38
+ device.setCapability(id, capab)
39
+ .then(() => {
40
+ this.debug("Capability initilization - success!");
41
+ initState = true;
42
+ this.status({ fill: "green", shape: "dot", text: "online" });
43
+ })
44
+ .catch(err => {
45
+ this.error("Error on create capability: " + err.message);
46
+ this.status({ fill: "red", shape: "dot", text: "error" });
47
+ });
48
+ device.updateCapabState(id, curentState)
49
+ .then(() => {
50
+ this.status({ fill: "green", shape: "dot", text: "online" });
51
+ })
52
+ .catch(err => {
53
+ this.error("Error on update capability state: " + err.message);
54
+ this.status({ fill: "red", shape: "dot", text: "Error" });
55
+ });
56
+ };
57
+ if (device.initState)
58
+ init();
59
+ device.on("online", () => {
60
+ init();
61
+ });
62
+ device.on("offline", () => {
63
+ this.status({ fill: "red", shape: "dot", text: "offline" });
64
+ });
65
+ device.on(id, (val) => {
66
+ this.send({ payload: val });
67
+ if (response) {
68
+ curentState.state.value = val;
69
+ device.updateCapabState(id, curentState)
70
+ .then(() => {
71
+ this.status({ fill: "green", shape: "dot", text: val.toString() });
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
+ this.on('input', (msg, _send, done) => {
80
+ if (typeof msg.payload != 'boolean') {
81
+ this.error("Wrong type! msg.payload must be boolean.");
82
+ if (done) {
83
+ done();
84
+ }
85
+ return;
86
+ }
87
+ if (msg.payload === curentState.state.value) {
88
+ this.debug("Value not changed. Cancel update");
89
+ if (done) {
90
+ done();
91
+ }
92
+ return;
93
+ }
94
+ curentState.state.value = msg.payload;
95
+ device.updateCapabState(id, curentState)
96
+ .then(() => {
97
+ this.status({ fill: "green", shape: "dot", text: String(msg.payload) });
98
+ if (done) {
99
+ done();
100
+ }
101
+ })
102
+ .catch(err => {
103
+ this.error("Error on update capability state: " + err.message);
104
+ this.status({ fill: "red", shape: "dot", text: "Error" });
105
+ if (done) {
106
+ done();
107
+ }
108
+ });
109
+ });
110
+ this.on('close', (removed, done) => {
111
+ device.setMaxListeners(device.getMaxListeners() - 1);
112
+ if (removed) {
113
+ device.delCapability(id)
114
+ .then(() => { done(); })
115
+ .catch(err => {
116
+ this.error("Error on delete capability: " + err.message);
117
+ done();
118
+ });
119
+ }
120
+ else {
121
+ done();
122
+ }
123
+ });
124
+ }
125
+ RED.nodes.registerType("On_Off", AliceOnOff);
126
+ };
127
+ //# sourceMappingURL=alice-onoff.js.map
@@ -1,153 +1,147 @@
1
- module.exports = function(RED) {
2
- // ************** Range *******************
3
- function AliceRange(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.range';
9
- this.retrievable = config.retrievable;
10
- this.instance = config.instance;
11
- this.unit = config.unit;
12
- this.random_access = true;
13
- this.min = parseFloat(config.min);
14
- this.max = parseFloat(config.max);
15
- this.precision = parseFloat(config.precision);
16
- this.response = config.response;
17
- this.initState = false;
18
- this.value = null;
19
-
20
- if (config.response === undefined){
21
- this.response = true;
22
- };
23
- if (typeof this.min != 'number'){this.min = 0};
24
- if (typeof this.max != 'number'){this.max = 100};
25
- if (typeof this.precision != 'number'){this.precision = 1};
26
-
27
- this.status({fill:"red",shape:"dot",text:"offline"});
28
-
29
- this.init = ()=>{
30
- let capab = {
31
- type: this.ctype,
32
- retrievable: this.retrievable,
33
- reportable: true,
34
- parameters: {
35
- instance: this.instance,
36
- unit: this.unit,
37
- random_access: this.random_access,
38
- range: {
39
- min: this.min,
40
- max: this.max,
41
- precision: this.precision
42
- }
1
+ "use strict";
2
+ module.exports = (RED) => {
3
+ function AliceRange(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.range';
8
+ const retrievable = config.retrievable;
9
+ const instance = config.instance || '';
10
+ const unit = config.unit;
11
+ const random_access = true;
12
+ let min = parseFloat(config.min);
13
+ let max = parseFloat(config.max);
14
+ let precision = parseFloat(config.precision);
15
+ let response = config.response;
16
+ let value = null;
17
+ if (config.response === undefined) {
18
+ response = true;
43
19
  }
44
- };
45
- // если unit не пременим к параметру, то нужно удалить
46
- if (this.unit == "unit.number"){
47
- delete capab.parameters.unit;
48
- };
49
-
50
- this.device.setCapability(this.id,capab)
51
- .then(res=>{
52
- this.initState = true;
53
- this.status({fill:"green",shape:"dot",text:"online"});
54
- })
55
- .catch(err=>{
56
- this.error("Error on create capability: " + err.message);
57
- this.status({fill:"red",shape:"dot",text:"error"});
58
- });
59
- };
60
-
61
- // Проверяем сам девайс уже инициирован
62
- if (this.device.initState) this.init();
63
-
64
- this.device.on("online",()=>{
65
- this.init();
66
- });
67
-
68
- this.device.on("offline",()=>{
69
- this.status({fill:"red",shape:"dot",text:"offline"});
70
- });
71
-
72
- this.device.on(this.id,(val, fullstate)=>{
73
- let value = val;
74
- //проверка является ли значение относительным и нужно ли отдавать полное значение
75
- if (fullstate.relative && this.retrievable){
76
- value = this.value + val;
77
- if (val<0 && value<this.min) value=this.min;
78
- if (val>0 && value>this.max) value=this.max;
79
- };
80
- this.send({
81
- payload: value
82
- });
83
- let state= {
84
- type:this.ctype,
85
- state:{
86
- instance: this.instance,
87
- value: value
20
+ if (typeof min != 'number' || isNaN(min)) {
21
+ min = 0;
88
22
  }
89
- };
90
- // если установлено требование немедленно отвечать, отвечаем
91
- if (this.response){
92
- this.device.updateCapabState(this.id,state)
93
- .then (res=>{
94
- this.value = value;
95
- this.status({fill:"green",shape:"dot",text:"online"});
96
- })
97
- .catch(err=>{
98
- this.error("Error on update capability state: " + err.message);
99
- this.status({fill:"red",shape:"dot",text:"Error"});
100
- })
101
- };
102
- })
103
-
104
- this.on('input', (msg, send, done)=>{
105
- const value = msg.payload;
106
- if (typeof value != 'number'){
107
- this.error("Wrong type! msg.payload must be Number.");
108
- if (done) {done();}
109
- return;
110
- }
111
- if (value === this.value){
112
- this.debug("Value not changed. Cancel update");
113
- if (done) {done();}
114
- return;
115
- };
116
- let state= {
117
- type:this.ctype,
118
- state:{
119
- instance: this.instance,
120
- value: value
23
+ if (typeof max != 'number' || isNaN(max)) {
24
+ max = 100;
121
25
  }
122
- };
123
- this.device.updateCapabState(this.id,state)
124
- .then(ref=>{
125
- this.value = value;
126
- this.status({fill:"green",shape:"dot",text:value});
127
- if (done) {done();}
128
- })
129
- .catch(err=>{
130
- this.error("Error on update capability state: " + err.message);
131
- this.status({fill:"red",shape:"dot",text:"Error"});
132
- if (done) {done();}
133
- })
134
- });
135
-
136
- this.on('close', (removed, done)=>{
137
- this.device.setMaxListeners(this.device.getMaxListeners() - 1); // уменьшаем лимит для event
138
- if (removed) {
139
- this.device.delCapability(this.id)
140
- .then(res=>{
141
- done()
142
- })
143
- .catch(err=>{
144
- this.error("Error on delete capability: " + err.message);
145
- done();
146
- })
147
- }else{
148
- done();
149
- }
150
- });
151
- }
152
- RED.nodes.registerType("Range",AliceRange);
153
- };
26
+ if (typeof precision != 'number' || isNaN(precision)) {
27
+ precision = 1;
28
+ }
29
+ this.status({ fill: "red", shape: "dot", text: "offline" });
30
+ const init = () => {
31
+ const parameters = {
32
+ instance: instance,
33
+ unit: unit,
34
+ random_access: random_access,
35
+ range: { min, max, precision }
36
+ };
37
+ if (unit == "unit.number") {
38
+ delete parameters.unit;
39
+ }
40
+ device.setCapability(this.id, {
41
+ type: ctype,
42
+ retrievable: retrievable,
43
+ reportable: true,
44
+ parameters: parameters
45
+ })
46
+ .then(() => {
47
+ this.status({ fill: "green", shape: "dot", text: "online" });
48
+ })
49
+ .catch(err => {
50
+ this.error("Error on create capability: " + err.message);
51
+ this.status({ fill: "red", shape: "dot", text: "error" });
52
+ });
53
+ };
54
+ if (device.initState)
55
+ init();
56
+ device.on("online", () => {
57
+ init();
58
+ });
59
+ device.on("offline", () => {
60
+ this.status({ fill: "red", shape: "dot", text: "offline" });
61
+ });
62
+ device.on(this.id, (val, fullstate) => {
63
+ let newValue = val;
64
+ if (fullstate.relative && retrievable && value !== null) {
65
+ newValue = value + val;
66
+ if (val < 0 && newValue < min)
67
+ newValue = min;
68
+ if (val > 0 && newValue > max)
69
+ newValue = max;
70
+ }
71
+ this.send({ payload: newValue });
72
+ const state = {
73
+ type: ctype,
74
+ state: {
75
+ instance: instance,
76
+ value: newValue
77
+ }
78
+ };
79
+ if (response) {
80
+ device.updateCapabState(this.id, state)
81
+ .then(() => {
82
+ value = newValue;
83
+ this.status({ fill: "green", shape: "dot", text: "online" });
84
+ })
85
+ .catch(err => {
86
+ this.error("Error on update capability state: " + err.message);
87
+ this.status({ fill: "red", shape: "dot", text: "Error" });
88
+ });
89
+ }
90
+ });
91
+ this.on('input', (msg, _send, done) => {
92
+ const newValue = msg.payload;
93
+ if (typeof newValue != 'number') {
94
+ this.error("Wrong type! msg.payload must be Number.");
95
+ if (done) {
96
+ done();
97
+ }
98
+ return;
99
+ }
100
+ if (newValue === value) {
101
+ this.debug("Value not changed. Cancel update");
102
+ if (done) {
103
+ done();
104
+ }
105
+ return;
106
+ }
107
+ const state = {
108
+ type: ctype,
109
+ state: {
110
+ instance: instance,
111
+ value: newValue
112
+ }
113
+ };
114
+ device.updateCapabState(this.id, state)
115
+ .then(() => {
116
+ value = newValue;
117
+ this.status({ fill: "green", shape: "dot", text: String(newValue) });
118
+ if (done) {
119
+ done();
120
+ }
121
+ })
122
+ .catch(err => {
123
+ this.error("Error on update capability state: " + err.message);
124
+ this.status({ fill: "red", shape: "dot", text: "Error" });
125
+ if (done) {
126
+ done();
127
+ }
128
+ });
129
+ });
130
+ this.on('close', (removed, done) => {
131
+ device.setMaxListeners(device.getMaxListeners() - 1);
132
+ if (removed) {
133
+ device.delCapability(this.id)
134
+ .then(() => { done(); })
135
+ .catch(err => {
136
+ this.error("Error on delete capability: " + err.message);
137
+ done();
138
+ });
139
+ }
140
+ else {
141
+ done();
142
+ }
143
+ });
144
+ }
145
+ RED.nodes.registerType("Range", AliceRange);
146
+ };
147
+ //# sourceMappingURL=alice-range.js.map