forked from timroemisch/mqtt-s7-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.js
148 lines (115 loc) · 3.75 KB
/
device.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
let attribute = require("./attribute.js");
let sf = require('./service_functions.js');
module.exports = class device {
constructor(plc, mqtt, config) {
this.plc_handler = plc;
this.mqtt_handler = mqtt;
this.name = config.name || "unnamed device";
this.config = config;
this.discovery_topic = "homeassistant";
this.discovery_retain = false;
this.type = config.type.toLowerCase();
// device topics
this.mqtt_name = config.mqtt;
this.full_mqtt_topic = config.mqtt_base + "/" + this.mqtt_name;
// store all attribute objects in this array
this.attributes = {};
}
create_attribute(config, required_type, name) {
// create attribute object
let new_attribute = new attribute(
this.plc_handler,
this.mqtt_handler,
name,
required_type,
this.full_mqtt_topic);
// the config could be an object
// or simply an string
if (typeof config == "object") {
new_attribute.plc_address = config.plc;
// optional different set address
if (config.set_plc)
new_attribute.plc_set_address = config.set_plc;
// optional Read Write config
if (config.rw)
new_attribute.set_RW(config.rw);
// optional update_interval
if (config.update_interval)
new_attribute.update_interval = config.update_interval;
// optional inverted, works only with booleans
if (config.inverted)
new_attribute.boolean_inverted = config.inverted;
// optional unit_of_measurement only for homeassistant
if (config.unit_of_measurement)
new_attribute.unit_of_measurement = config.unit_of_measurement;
// optional write back changes from plc to set_plc
if (config.write_back)
new_attribute.write_back = config.write_back;
} else {
new_attribute.plc_address = config;
}
// register the attribute to the plc library
new_attribute.subscribePlcUpdates();
// split the plc adress to get the type
let offset = new_attribute.plc_address.split(',');
let params = offset[1].match(/(\d+|\D+)/g);
let type = params[0];
// check if the type is correct
// and if it isnt then print some infos
if (required_type != "" && type != required_type) {
sf.debug("Wrong datatype '" + type + "' at attribute '" + name + "'");
let numbers = "";
for (var i = 1; i < params.length; i++) {
numbers += params[i];
}
sf.debug("Did you mean " + offset[0] + "," +
required_type + numbers +
" instead of " + new_attribute.plc_address + " ?");
return;
} else {
new_attribute.type = type;
}
sf.debug("- New attribute '" + new_attribute.full_mqtt_topic + "' was created");
// save attribute in array
this.attributes[name] = new_attribute;
}
send_discover_msg(info) {
// create an topic in which the discovery message can be sent
let topic = this.discovery_topic + "/" +
this.type + "/s7-connector/" + this.mqtt_name + "/config";
info.uniq_id = 's7-' + this.mqtt_name;
this.mqtt_handler.publish(topic, JSON.stringify(info), {
retain: this.discovery_retain
});
}
rec_s7_data(attr, data) {
// check if attribute with this name exists
if (this.attributes[attr]) {
// forward all data to attribute
this.attributes[attr].rec_s7_data(data);
}
}
rec_mqtt_data(attr, data, cb) {
// check if attribute with this name exists
if (this.attributes[attr]) {
// forward all data to attribute
this.attributes[attr].rec_mqtt_data(data, cb);
}
}
get_plc_address(attr) {
if (this.attributes[attr] && this.attributes[attr].plc_address) {
return this.attributes[attr].plc_address;
}
return null;
}
get_plc_set_address(attr) {
if (this.attributes[attr]) {
if (this.attributes[attr].plc_set_address) {
return this.attributes[attr].plc_set_address;
} else if (this.attributes[attr].plc_address) {
return this.attributes[attr].plc_address;
}
}
return null;
}
}