UNPKG

oee-cli

Version:

Node.js CLI program to calculate and log OEE data obtained from MQTT server to InfluxDB

129 lines (110 loc) 3.2 kB
#!/usr/bin/env node // Log OEE calculations using CLI // Harshad Joshi, 2023 // (c) Bufferstack.IO Analytics Technology LLP, Pune const mqtt = require('mqtt'); const { InfluxDB } = require('influx'); const { program } = require('commander'); if (process.argv.length <= 2) { program.help(); process.exit(); } console.log(' OEE-cli by Harshad Joshi \n'); program .requiredOption('-b, --broker <url>', 'MQTT broker URL') .requiredOption('-u, --username <string>', 'MQTT broker username') .requiredOption('-p, --password <string>', 'MQTT broker password') .requiredOption('-i, --influx-url <url>', 'InfluxDB URL') .requiredOption('-d, --database <string>', 'InfluxDB database') .requiredOption('-t, --topic <string>', 'MQTT topic to subscribe to') .parse(process.argv); const options = program.opts(); const { broker, username, password, influxUrl, database, topic } = options; if (!broker || !username || !password || !influxUrl || !database || !topic) { console.error('All options are required. Please use the --help option for usage information.'); process.exit(1); } // Create a new InfluxDB instance const influx = new InfluxDB({ host: influxUrl, database, port: 8086, username, password, }); // Connect to MQTT broker const client = mqtt.connect(broker, { port: 1883, username, password, }); // Handle MQTT client connection errors client.on('error', (error) => { console.error('MQTT client error:', error); }); // Handle MQTT client reconnects client.on('reconnect', () => { console.log('MQTT client reconnecting...'); }); // Handle MQTT client close client.on('close', () => { console.log('MQTT client disconnected'); }); // Subscribe to MQTT topic client.subscribe(topic, (err) => { if (err) { console.error(`Failed to subscribe to topic ${topic}:`, err); } else { console.log(`Subscribed to topic ${topic}`); } }); // Handle incoming MQTT messages client.on('message', (topic, message) => { const data = JSON.parse(message); // Check if required fields exist in message and are numbers if ( isNaN(data.runTime) || isNaN(data.totalTime) || isNaN(data.targetSpeed) || isNaN(data.totalProduced) || isNaN(data.goodProduced) ) { console.error( `Received message for machine ${topic} is missing or has invalid number fields.` ); return; process.exit(0); } // Calculate OEE values const availability = data.runTime / data.totalTime; const performance = data.totalProduced / (data.runTime * data.targetSpeed); const quality = data.goodProduced / data.totalProduced; const oee = availability * performance * quality; // Write data to InfluxDB influx .writePoints([ { measurement: 'oee', tags: { machine: topic, }, fields: { availability, performance, quality, oee, }, }, ]) .then(() => { console.log( `Successfully wrote OEE data for machine ${topic} to InfluxDB.` ); }) .catch((error) => { console.error( `Failed to write data for machine ${topic} to InfluxDB:`, error ); }); });