Skip to content

Commit

Permalink
Dev (#9)
Browse files Browse the repository at this point in the history
* Fix total bandwidth counters

* bump version

* Merge build changes from main (#8)

* Update build.yaml

* Update build.yaml

* Update build.yaml - Bump release action
  • Loading branch information
stonegray authored Oct 14, 2023
1 parent ef592bf commit 081c950
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"name": "speedtest2ha",
"description": "Speedtest results in Home Assistant via MQTT.",
"version": "1.0.13",
"version": "1.0.14",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
2 changes: 1 addition & 1 deletion src/backendSpeedtest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export async function speedtest(server, exclude, useSingleMode) {
// Temporary hack until I decide where to put this logic:
result.download = ~~(result.download / 1e4) / 1e2;
result.upload = ~~(result.upload / 1e4) / 1e2;
result.bytes_recieved = ~~(result.bytes_recieved / 1e4) / 1e2;
result.bytes_received = ~~(result.bytes_received / 1e4) / 1e2;
result.bytes_sent = ~~(result.bytes_sent / 1e4) / 1e2;

// Else we should be fine:
Expand Down
4 changes: 2 additions & 2 deletions src/fields.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const entities = [
unique_id: uidPrefix + "uploadtotal",
enabled_by_default: true
},
backendKey: "upstream"
backendKey: "uploadtotal"
},
{
component: "sensor",
Expand All @@ -86,7 +86,7 @@ export const entities = [
unique_id: uidPrefix + "downloadtotal",
enabled_by_default: true
},
backendKey: "downstream"
backendKey: "downloadtotal"
},
{
component: "binary_sensor",
Expand Down
7 changes: 4 additions & 3 deletions src/mqtt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ export async function sendFields(data, allowsingle) {

if (data[en.name] == undefined) continue;

if (!allowsingle) console.log("Processing sensor", en.name, data[en.name], mqttPath + "/" + en.name);
if (!allowsingle) console.log("Processing sensor", en.name, data[en.name], mqttPath + "/" + en.name + " - Sending");

if (!!data[en.name]) {
await client.publish(mqttPath + "/" + en.name, "" + data[en.name]);
if (!allowsingle) console.log("Processing sensor", en.name, data[en.name], mqttPath + "/" + en.name + " - OK");
} else {
//console.warn("Backend did not provide the following key, skipping:", en.name);
}
Expand All @@ -69,9 +70,9 @@ export async function sendFields(data, allowsingle) {

export async function connect(){

console.log(process.env.MQTT_HOST ?? 'mqtt://localhost', options);
console.log("Connecting to MQTT server: " + process.env.MQTT_HOST ?? 'mqtt://localhost', options);

client = mqtt.connect(process.env.MQTT_HOST ?? 'mqtt://localhost', options)
console.log(process.env.MQTT_HOST ?? 'mqtt://localhost', options);

return new Promise((resolveFunc) => {
client.on('connect', async function () {
Expand Down
16 changes: 11 additions & 5 deletions src/speedtest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const counters = {

let attempt = 1;
const t = setInterval(()=>{
console.log("Waiting for connection...");
console.log("Connecting taking longer than expected...");

if (attempt++ == 6){
console.error("Failed to connect to MQTT after 60 seconds, exiting");
Expand All @@ -23,7 +23,6 @@ const t = setInterval(()=>{
}
}, 10000);

console.log("Connecting to MQTT... ");
await connect();

console.log("Connect OK...");
Expand Down Expand Up @@ -53,15 +52,22 @@ async function runTest() {

// Calculate totals:
if (results.bytes_sent > 1) counters.uploadtotal += results.bytes_sent;
if (results.bytes_recieved > 1) counters.downnstream += results.bytes_recieved;
if (results.bytes_received > 1) counters.downloadtotal += results.bytes_received;
counters.total = counters.uploadtotal + counters.downloadtotal;

results.downloadtotal = counters.downloadtotal;
results.uploadtotal = counters.uploadtotal;

sendFields(results);

}

if (!process.env.NO_TEST_ON_STARTUP)
await runTest();
if (!process.env.NO_TEST_ON_STARTUP){
await runTest();
console.log( "Initial test finished, waiting for next scheduled test");
} else {
console.log( "Skipping initial test, waiting for next scheduled test");
}

cron.schedule(process.env.CRON ?? '* */1 * * *', async () => {
await runTest();
Expand Down

0 comments on commit 081c950

Please sign in to comment.