Skip to content

Commit

Permalink
Dev (#5)
Browse files Browse the repository at this point in the history
* Test improved Github actions config 1

* Test improved Github actions config 2

* Test improved Github actions config 3

* update

* update

* update docs

* Enable dev builds on docker

* Add Fast.com backend and options
  • Loading branch information
stonegray authored Jan 12, 2023
1 parent 047cbc0 commit 2639950
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 9 deletions.
1 change: 0 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ jobs:
with:
images: stonegray/speedtest2ha
- name: Build and push Docker image
if: github.ref == 'refs/heads/main'
uses: docker/build-push-action@v2
with:
builder: ${{ steps.buildx.outputs.name }}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Settings you probably don't need to change:
<!--- `PERIOD`: Ignored if `CRON` is set. Number of seconds between tests. Must be greater than 300.-->
<!--- `MQTT_NO_LAST_WILL`: Disable MQTT last will.-->
- `CRON`: Defaults to "0 0,6,12,18 * * *". This updates four times per day.
- `SPEEDTEST_BACKEND_FAST`: Use the experimental Fast.com API. Currently does not report upload speed.
- `SPEEDTEST_SERVER_ID`: Defaults to automatic. Set to the integer server ID (eg. 12345) to skip server selection and force connecting so a specific ID.
- `SPEEDTEST_SERVER_EXCLUDE`: Defaults to none. Set to an integer server ID to ignore this server during selection.
- `SPEEDTEST_SINGLE_MODE`: Use a single thread; may measure actual file transfer performance from a single host better. Equilvilent to `--single` on `speedtest-cli`
Expand Down
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
{
"dependencies": {
"async-mqtt": "^2.6.3",
"mqtt": "^4.3.7",
"fast-speedtest-api": "^0.3.2",
"node-cron": "^3.0.2"
},
"name": "speedtest2ha",
"description": "Speedtest results in Home Assistant via MQTT.",
"version": "1.0.12",
"version": "1.0.13",
"main": "index.js",
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down
41 changes: 41 additions & 0 deletions src/backendFast.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import FastSpeedtest from 'fast-speedtest-api';

async function getFastToken() {
const response = await fetch('https://fast.com/app-ed402d.js');

const data = await response.text();

let token = data.match(/token:"(.{32})"/)[1];

console.log("Recieved API token: ", token);

return token;
}

export async function speedtestFast() {

const token = await getFastToken();

let speedtest = new FastSpeedtest({
token: token, // required
verbose: false, // default: false
timeout: 10000, // default: 5000
https: true, // default: true
urlCount: 5, // default: 5
bufferSize: 8, // default: 8
});

return new Promise((resolveFunc) => {
speedtest.getSpeed().then(s => {
resolveFunc({
error: false,
download: ~~(s / 1e4) / 1e2
});
}).catch(e => {
resolveFunc({
error: true,
message: e.message
});
});
});
}
17 changes: 12 additions & 5 deletions src/speedtest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as cron from 'node-cron';
import { entities, mqttPath } from "./fields.mjs";
import { sendFields } from "./mqtt.mjs";
import { speedtest } from "./backendSpeedtest.mjs";
import { speedtestFast } from './backendFast.mjs';

// Store total bandwidth:
const counters = {
Expand All @@ -17,11 +18,17 @@ async function runTest() {
// Get all the info from the testing backend:

sendFields({ testinprogress: "true" });
const results = await speedtest(
process.env.SPEEDTEST_SERVER_ID,
process.env.SPEEDTEST_EXCLUDE_ID,
process.env.SPEEDTEST_SINGLE_MODE
);

let results;
if (process.env.SPEEDTEST_BACKEND_FAST) {
results = await speedtestFast();
} else {
results = await speedtest(
process.env.SPEEDTEST_SERVER_ID,
process.env.SPEEDTEST_EXCLUDE_ID,
process.env.SPEEDTEST_SINGLE_MODE
);
}
sendFields({ testinprogress: "false" });

console.log("Sending data...")
Expand Down

0 comments on commit 2639950

Please sign in to comment.