diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index e79fe74..5944f7a 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -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 }} diff --git a/README.md b/README.md index 7e6dc2c..4e3fd10 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ Settings you probably don't need to change: - `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` diff --git a/package-lock.json b/package-lock.json index c77fc5f..c41e661 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,16 @@ { "name": "speedtest2ha", + "version": "1.0.12", "lockfileVersion": 2, "requires": true, "packages": { "": { + "name": "speedtest2ha", + "version": "1.0.12", + "license": "GPLv3", "dependencies": { "async-mqtt": "^2.6.3", + "fast-speedtest-api": "^0.3.2", "mqtt": "^4.3.7", "node-cron": "^3.0.2" } @@ -152,6 +157,17 @@ "once": "^1.4.0" } }, + "node_modules/fast-speedtest-api": { + "version": "0.3.2", + "resolved": "https://registry.npmjs.org/fast-speedtest-api/-/fast-speedtest-api-0.3.2.tgz", + "integrity": "sha512-QEfo3wytVo3x/ds4grti1nJUGZhmgdX9OpQbkXc6levJamjUFenb1SdbiVYhrTx27pvOAHSJOAXMu62K0dIEDQ==", + "bin": { + "fast-speedtest": "src/cli.js" + }, + "engines": { + "node": ">=7.10.1" + } + }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -582,6 +598,11 @@ "once": "^1.4.0" } }, + "fast-speedtest-api": { + "version": "0.3.2", + "resolved": "https://registry.npmjs.org/fast-speedtest-api/-/fast-speedtest-api-0.3.2.tgz", + "integrity": "sha512-QEfo3wytVo3x/ds4grti1nJUGZhmgdX9OpQbkXc6levJamjUFenb1SdbiVYhrTx27pvOAHSJOAXMu62K0dIEDQ==" + }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", diff --git a/package.json b/package.json index cb9ec3c..71edec9 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/backendFast.mjs b/src/backendFast.mjs index e69de29..33f93d6 100644 --- a/src/backendFast.mjs +++ b/src/backendFast.mjs @@ -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 + }); + }); + }); +} diff --git a/src/speedtest.mjs b/src/speedtest.mjs index cff15a7..7972ca3 100644 --- a/src/speedtest.mjs +++ b/src/speedtest.mjs @@ -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 = { @@ -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...")