-
Notifications
You must be signed in to change notification settings - Fork 0
/
BCM0002.js
51 lines (43 loc) · 1.75 KB
/
BCM0002.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
const mqtt = require('mqtt');
//const fs = require('fs');
const mqttIP = 'mqtts://10.10.13.21:8883'; //LAN is mqtts://192.168.0.121:8883
const topic = 'cmtk/cmtkv1/port1/pd';
const options = {
clientId: 'DIH',
username: 'user',
password: 'Balluff#1',
rejectUnauthorized: false //ca is self signed so it can't be used within this script.
//ca: fs.readFileSync('ca.crt') //when 'rejectUnauthorized' is set to false, ca file may not be included at all
};
const init = (myscada) => {
//Connecting mqtt broker
const client = mqtt.connect(mqttIP, options);
client.on('connect', function () {
//console.log('connected');
client.subscribe(topic, {qos: 1}); //single topic
});
client.on('error', (err) => {
//console.log('Connection error: ', err);
});
client.on('message', function (topic, message, packet) {
const messageJSON = JSON.parse(message);
// Extract the relevant sensor data from the JSON
const temperature = messageJSON['Contact Temperature Contact Temperature'];
const ambientPressure = messageJSON['Ambient Pressure Ambient Pressure'];
const humidity = messageJSON['Humidity Humidity'];
//Write tags for tag group cmtk
const options = {};
options['name'] = 'BCM0002';
options['values'] = {};
options['values']['temperature'] = temperature;
options['values']['ambientpressure'] = ambientPressure;
options['values']['humidity'] = humidity;
myscada.writeTags(options, (err, data) => {
if (err) {
//write error
}
});
exports.messageBCM0002 = messageJSON;
});
};
exports.init = init;