forked from timroemisch/mqtt-s7-connector
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathattribute.js
232 lines (188 loc) · 5.36 KB
/
attribute.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
let sf = require("./service_functions.js");
module.exports = class attribute {
constructor(plc, mqtt, name, type, mqtt_device_topic) {
this.plc_handler = plc;
this.mqtt_handler = mqtt;
this.last_update = 0;
this.last_value = null;
this.update_interval = 0;
this.plc_address = null;
this.plc_set_address = null;
// if true, the attribute is allowed to publish update msg to mqtt
this.publish_to_mqtt = true;
// if true, the attribute is allowed to write updates to the plc
// and allowed to subscribe to the '/set' topic
this.write_to_s7 = true;
// plc type e.g. X, BYTE
this.type = type;
// TODO
this.round_value = true;
// attribute name (last part in topic)
this.name = name;
// works only with booleans
// send inverted value to mqtt
this.boolean_inverted = false;
// full topic
this.full_mqtt_topic = mqtt_device_topic + "/" + this.name;
// optional write back changes from plc to set_plc
this.write_back = false;
// only subscribe if attribute is allowed to write to plc
if (this.write_to_s7) {
this.mqtt_handler.subscribe(this.full_mqtt_topic + "/set");
sf.debug("-- Subscribe to topic: '" + this.full_mqtt_topic + "/set'");
}
this.subscribePlcUpdates();
}
// every attribute as to add itself to the plc_handler
// so that it can be updated from the plc
subscribePlcUpdates() {
if (this.plc_address) {
this.plc_handler.addItems(this.full_mqtt_topic);
}
}
set_RW(data) {
data = data.toLowerCase();
// data is always going to be read from the plc,
// but with 'w' the state won't be sent over mqtt
// with 'r' enabled it isn't possible to write into the plc
switch (data) {
case "r":
this.write_to_s7 = false;
this.publish_to_mqtt = true;
// unsubscribe if already subscribed
sf.debug("-- Unsubscribe from topic: '" + this.full_mqtt_topic + "/set'");
this.mqtt_handler.unsubscribe(this.full_mqtt_topic + "/set");
break;
case "w":
this.write_to_s7 = true;
this.publish_to_mqtt = false;
break;
case "i":
this.write_to_s7 = true;
this.publish_to_mqtt = false;
break;
case "rw":
case "wr":
this.write_to_s7 = true;
this.publish_to_mqtt = true;
break;
default:
sf.debug("couldn't set rw-mode '" + data + "' on attribute '" + this.name + "'");
sf.debug("it can be either 'r', 'w' or 'rw'")
}
}
rec_s7_data(data) {
if (this.publish_to_mqtt) {
// round all floating point values up to 3 decimal places
if (this.type === "REAL" && this.round_value) {
data = Math.round(data * 1000) / 1000;
}
if (this.type === "X" && this.boolean_inverted) {
data = !data;
}
const now = Date.now();
// if time has passed, then updated if the update_interval is set
let should_update = ((now - this.last_update) > this.update_interval) &&
this.update_interval !== 0;
// last_value / last_update update
if (data !== this.last_value && this.update_interval === 0) {
should_update = true;
}
// send mqtt msg if necessary
if (should_update) {
this.last_value = data;
this.last_update = now;
this.mqtt_handler.publish(this.full_mqtt_topic, data.toString(), {
retain: false
});
if (this.write_back) {
if (data === this.last_set_data) {
// Ourselves triggered this change. Skipping and reset.
this.last_set_data = undefined;
} else {
// Writing back the change to the S7 input.
this.write_to_plc(data, (error) => {
if (error) {
sf.debug("Error while writing back: " + error);
}
});
}
}
}
}
}
rec_mqtt_data(data, cb) {
// type check
let msg = this.formatMessage(data, this.type);
// no error in formatting
if (msg[0] === 0) {
this.write_to_plc(msg[1], cb);
} else {
if (cb) cb("Incorrect formating");
}
}
write_to_plc(data, cb) {
this.last_set_data = data;
// write to plc
this.plc_handler.writeItems(this.full_mqtt_topic + "/set", data, (error) => {
if (error) {
sf.warning("Error while writing to PLC !")
}
if (cb) cb(error);
});
}
//
// format message
// according to type
//
// @param msg String mqtt message
// type String PLC type (X/BYTE/REAL/INT)
//
// @return Array[0] Error code, 0="OK", -1="type not found", -2="cant format type"
// Array[1] formatted variable
//
formatMessage(msg, type, noDebugOut = false) {
let write;
switch (type) {
case "X":
if (msg === "true") write = true;
else if (msg === "false") write = false;
else {
if (noDebugOut)
sf.debug("can´t format incoming message '" + msg + "' -> skipping it");
return [-2];
}
break;
case "BYTE":
write = parseInt(msg);
if (isNaN(write)) {
if (noDebugOut)
sf.debug("can´t format incoming message '" + msg + "' -> skipping it");
return [-2];
}
break;
case "REAL":
write = parseFloat(msg);
if (isNaN(write)) {
if (noDebugOut)
sf.debug("can´t format incoming message '" + msg + "' -> skipping it");
return [-2];
}
break;
case "DINT":
case "INT":
write = parseInt(msg, 10);
if (isNaN(write)) {
if (noDebugOut)
sf.debug("can't format incoming message '" + msg + "' -> skipping it");
return [-2];
}
break;
default:
if (noDebugOut)
sf.debug("can´t format incoming message '" + msg + "' -> skipping it");
return [-1];
}
return [0, write];
}
}