Created
October 12, 2022 09:45
-
-
Save iotux/70da06a2881830eb6f806075fa42018b to your computer and use it in GitHub Desktop.
Program for å dumpe output fra Tibber Pulse. Programmet lagres i samme katalog som ElWis
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 = "tibDumper"; | |
const programPid = process.pid; | |
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]; | |
} | |
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"); } | |
}); | |
}); | |
}, // 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'); | |
let j = JSON.stringify(buf); | |
let tmp = JSON.parse(j); | |
tmp['hex'] = hex | |
tmp['length'] = buf.length; | |
let res = { | |
dec: tmp['data'], | |
hex: buf.toString('hex'), | |
length: buf.length | |
} | |
let json = JSON.stringify(res); | |
// Check for valid data | |
if (buf.length === buf[2] + 2) { | |
if (buf[2] === 0x27) { // 0x27,39 | |
//fs.writeFileSync("./list1-" + now() + ".hex", hex); | |
fs.writeFileSync("./list1.json", json); | |
} | |
else if (buf[2] === 0x79) { // 0x79, 121 / 0x9b, 155 | |
//fs.writeFileSync("./list2-" + now() + ".hex", hex); | |
fs.writeFileSync("./list2.json", json); | |
} | |
else if (buf[2] === 0x9b) { // S0x9b, 155 | |
//fs.writeFileSync("./list3-" + now() + ".hex", hex); | |
fs.writeFileSync("./list3.json", json); | |
} | |
else { | |
// Packet failure | |
//fs.writeFileSync("./list0-" + now() + ".hex", hex); | |
fs.writeFileSync("./list0-" + now() + ".json", json); | |
if (pulse.debug) { | |
let msg = message.toString(); | |
console.log("Event message: ", msg); | |
console.log("Raw data packet exception : ", JSON.stringify(buf)); | |
} | |
} | |
} // 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