Created
October 27, 2022 09:02
-
-
Save iotux/83e22749f28f11e4412990403eefb498 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
"use strict"; | |
const programName = "tibHexDumper"; | |
const programPid = process.pid; | |
const format= require('date-fns/format'); | |
const mqtt = require("mqtt"); | |
const fs = require("fs"); | |
const yaml = require("yamljs"); | |
//const { StringDecoder } = require('node:string_decoder'); | |
const configFile = "./config.yaml"; | |
// Load broker and topics preferences from config file | |
const C = yaml.load(configFile); | |
function now() { | |
let now = new Date(); | |
// Get rid of AM/PM | |
//return now.toLocaleTimeString().split(' ')[0]; | |
return format(now, "yyyy-MM-dd'T'HH").toString() | |
} | |
let pulse = { | |
init: function () { | |
pulse.debug = true; | |
if (C.mqttBroker === null) { | |
console.log("\nBroker IP address or hostname missing"); | |
console.log("Edit your \"config.yaml\" file\n"); | |
process.exit(0); | |
} | |
pulse.broker = C.mqttBroker + ":" + C.brokerPort; | |
pulse.mqttOptions = { | |
userName: C.userName, password: C.password, | |
will: { | |
topic: C.pubNotice, payload: C.willMessage, | |
} | |
}; | |
pulse.client = mqtt.connect("mqtt://" + pulse.broker, pulse.mqttOptions); | |
pulse.client.on("error", function (err) { | |
if (err.errno === "ENOTFOUND") { | |
console.log("\nNot connectd to broker"); | |
console.log("Check your \"config.yaml\" file\n") | |
process.exit(0); | |
} else | |
console.log("Client error: ", err); | |
}); | |
pulse.client.on("connect", function () { | |
pulse.client.subscribe(C.topic, function (err) { | |
if (err) { console.log("Subscription error"); } | |
}); | |
}); | |
if (!fs.existsSync('./hex')) { | |
fs.mkdirSync('./hex',0x0744); | |
} | |
}, // init() | |
run: function () { | |
pulse.client.on("message", function (topic, message) { | |
if (topic === "tibber") { | |
let buf = Buffer.from(message); | |
// JSON data | |
if (buf[0] === 0x7b) { // 0x7b, 123, "{" = Pulse status | |
} | |
// Raw buffer meter data | |
else if (buf[0] === 0x7e) { // 0x7e, 126, "~" | |
let hex = buf.toString('hex'); | |
// Check for valid data | |
if (buf.length === buf[2] + 2) { | |
if (buf[2] === 0x27) { // 0x27,39 | |
//fs.writeFileSync("./hex/list1-" + now() + ".hex", hex); | |
} | |
else if (buf[2] === 0x79) { // 0x79, 121 / 0x9b, 155 | |
//fs.writeFileSync("./hex/list2-" + now() + ".hex", hex); | |
} | |
else if (buf[2] === 0x9b) { // S0x9b, 155 | |
fs.writeFileSync("./hex/list3-" + now() + ".hex", hex); | |
} | |
} // End valid data | |
} // End raw buffer meter data | |
} // topic === "tibber" | |
}); // client.on(message) | |
} // run () | |
}; // pulse() | |
pulse.init(); | |
pulse.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment