Last active
March 31, 2023 09:49
-
-
Save iotux/9210e914e838f0188709b2ef07ebb7c7 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:mm").toString() | |
} | |
function match() { | |
let now = new Date(); | |
return format(now, "yyyy-MM-dd'T'HH:mm:ss").toString().split('T')[1].substr(4,4); | |
} | |
function hex2Dec(str) { | |
return parseInt(str, 16) | |
} | |
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 () { | |
let elements = 0; | |
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').toUpperCase(); | |
let index = hex.indexOf("FF800000") + 8; | |
elements = hex2Dec(hex.substr((index + 2), 2)) | |
if (elements === 18) { | |
fs.writeFileSync("./hex/list-" + now() + ".hex", hex); | |
} else { | |
//console.log('match:', match()); | |
//console.log("./hex/list-" + now() + ".hex"); | |
if( match() == '0:00'|| match() == '0:01' ) { //|| or match() == '0:02) { | |
fs.writeFileSync("./hex/list-" + now() + ".hex", hex); | |
//console.log(hex) | |
} | |
} | |
} // 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