forked from nzfarmer1/mqtt2opcua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt2opcua.js
227 lines (183 loc) · 7.69 KB
/
mqtt2opcua.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
"use strict";
var opcua = require("node-opcua"),
mqtt = require('mqtt'),
Events = require('events').EventEmitter,
Qlobber = require('qlobber').Qlobber;
var _debug = false;
function debug(s) {
if (_debug) {
console.log(s);
}
}
function Matcher(handlers) {
Events.call(this);
this.matcher = new Qlobber({
separator: '/',
wildcard_one: '+',
wildcard_some: '#'
});
if (handlers.hasOwnProperty('_events')) {
this._events = handlers._events;
}
}
Matcher.prototype = Object.create(Events.prototype);
Matcher.prototype.match = function (topic) {
if (Object.prototype.hasOwnProperty.call(this._events, topic)){
return this._events[topic];
}
var matches = this.matcher.match(topic);
return matches.length ? matches.pop() : null;
};
Matcher.prototype.init = function () {
for (var e in this._events) {
if (Object.prototype.hasOwnProperty.call(this._events, e)) {
this.matcher.add(e, this._events[e]);
}
}
};
Matcher.prototype.hasDefault = function () {
return this._events['#'] !== undefined;
};
var run = function (options) {
_debug = options.debug || false;
// Forward and backward handlers - see below for example
var fhandlers = new Matcher(options.forward),
bhandlers = new Matcher(options.backward);
if (!fhandlers.hasDefault()) {
fhandlers.on("#", function (payload) { // Default backward handler (MQTT -> OPCUA)
return {
dataType: "String",
value: String(payload),
};
});
}
// Default backward handler
if (!bhandlers.hasDefault()) {
bhandlers.on("#", function (variant) { // Default backward handler (OPCUA -> MQTT)
return {
topic:variant.topic,
payload:variant.value
};
});
}
fhandlers.init();
bhandlers.init();
// Let's create an instance of OPCUAServer
var server = new opcua.OPCUAServer({
hostname: options.opcHost || "127.0.0.1",
port: options.opcPort || 4334, // the port of the listening socket of the server
resourcePath: options.opcName || "UA/MQTT Bridge Server", // this path will be added to the endpoint resource name
buildInfo : {
productName: "MQTT2OPCUA Bridge",
buildNumber: "0.1",
buildDate: new Date(2015, 20, 7)
}
});
function post_initialize() {
debug("OPC Server Initialized");
var persist = {}, // New persistent store
nodes = {},
paths = {},
flagged = {};
function onMessage(topic, payload) {
debug("MQTT onMessage: " + topic + " (" + payload.length + " bytes)");
var node,
handler = fhandlers.match(topic);
if (!handler) {
console.error("No handler defined for " + topic);
return;
}
if (nodes.hasOwnProperty(topic)) {
persist[topic] = handler(payload).value; // Note: need to handle null responses
flagged[topic] = false;
return;
}
var sub,
path = topic.split("/"),
top = (path[0] || path[1]); // Cater for paths starting with "/"
node = paths[top] = (paths.hasOwnProperty(top)) ? paths[top] :
server.engine.addressSpace.addFolder("ObjectsFolder", { browseName: top});
for (sub in path) {
if (sub == 0 |!path.hasOwnProperty(sub)) {
continue;
}
top += "/" + path[sub];
if (topic !== top) {
node = paths[top] = (paths.hasOwnProperty(top)) ? paths[top] :
server.engine.addressSpace.addFolder(node, { browseName: path[sub]});
} else {
debug("Creating folders for new topic: " + topic);
persist[topic] = handler(payload).value;
flagged[topic] = false;
try {
nodes[topic] = server.engine.addressSpace.addVariable({
componentOf: node,
browseName: path[sub],
path: topic,
nodeId: "s=" + topic,
dataType: handler(payload).dataType,
value: {
get: function () {
debug("OPCUA Get: " + topic);
return (!flagged[topic]) ?
new opcua.Variant(handler(persist[topic])) :
opcua.StatusCodes.BadCommunicationError;
},
set: function (variant) {
debug("OPCUA Set: " + topic);
try {
variant.topic=topic;
var bhandler = bhandlers.match(topic),
message = bhandler(variant);
if (message.hasOwnProperty("topic") &&
message.hasOwnProperty("payload")) {
if (!options.roundtrip)
persist[topic] = message.payload;
else
flagged[topic] = true;
debug("MQTT Publish: " + message.topic.toString() + " (" + message.payload.toString().substr(0,8) + ")" )
server.mqtt.publish(message.topic.toString(),
message.payload.toString());
}
} catch(e){
console.error(e);
}
return opcua.StatusCodes.Good;
}
}
});
} catch (e) {
console.error(e);
}
}
}
return;
}
server.start(function (err) {
if (err){
console.error(err);
process.exit(1);
}
var endpointUrl = server.endpoints[0].endpointDescriptions()[0].endpointUrl;
console.log("OPC Server is now available at: " + endpointUrl + " ( press CTRL+C to stop)");
var mqttURL = 'mqtt://' + (options.mqttHost || "localhost") + ":" + (options.mqttPort || "1883");
if (!(server.mqtt = mqtt.connect(mqttURL,{ username: options.mqttUsername, password: options.mqttPassword}))) {
console.error("MQTT Unable to connect");
process.exit(1);
}
server.mqtt.on('connect', function () {
console.log("MQTT Connected to: %s\n", mqttURL);
server.mqtt.subscribe(options.topics || ['$SYS/#', '#']);
});
server.mqtt.on('message', function (topic, message) {
onMessage(topic, message);
});
server.mqtt.on('disconnect', function () {
debug("MQTT Disconnected ");
process.exit(1);
});
});
}
server.initialize(post_initialize);
};
module.exports.run = run;