From c1295d7c644887f8dbec02400b7427c4a5972c2f Mon Sep 17 00:00:00 2001 From: Philipp Arndt <2f.mail@gmx.de> Date: Sun, 11 Jun 2023 13:37:31 +0200 Subject: [PATCH] improve api "ping" (#139) local DNS could resolve even when API is not reachable --- app/lib/connection.ts | 33 ++++++++++++++++----------------- app/lib/miele/miele.test.ts | 6 +++++- app/lib/miele/miele.ts | 15 +++++++++++++++ 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/app/lib/connection.ts b/app/lib/connection.ts index 2f6e6cb..dfccb34 100644 --- a/app/lib/connection.ts +++ b/app/lib/connection.ts @@ -1,6 +1,6 @@ -import dns from "dns" import { getAppConfig } from "./config/config" import { log } from "./logger" +import { ping } from "./miele/miele" let checkConnection: ReturnType let connectionLost = false @@ -17,23 +17,22 @@ export const registerConnectionCheck = (restartHook: () => Promise, config } log.info("Internet connection will be checked every", { ms: interval }) connectionLost = false - checkConnection = setInterval(() => { + checkConnection = setInterval(async () => { log.debug("Checking connection") - dns.resolve("api.mcs3.miele.com", (err) => { - if (err) { - log.debug("Connection check failed", err) - if (!connectionLost) { - connectionLost = true - log.error("Connection lost. Waiting for connection to come back.", err) - } - } - else if (connectionLost) { - log.debug("Connection check success after connection was lost") - restartHook().then() - } - else { - log.debug("Connection check success") + + if (!await ping()) { + log.debug("Connection check failed") + if (!connectionLost) { + connectionLost = true + log.error("Connection lost. Waiting for connection to come back.") } - }) + } + else if (connectionLost) { + log.debug("Connection check success after connection was lost") + restartHook().then() + } + else { + log.debug("Connection check success") + } }, interval) } diff --git a/app/lib/miele/miele.test.ts b/app/lib/miele/miele.test.ts index 3363441..b8ade41 100644 --- a/app/lib/miele/miele.test.ts +++ b/app/lib/miele/miele.test.ts @@ -2,7 +2,7 @@ import { JEST_INTEGRATION_TIMEOUT } from "../../test/test-utils" import { applyConfig } from "../config/config" import { log } from "../logger" import { getToken } from "./login/login" -import { fetchDevices, smallMessage } from "./miele" +import { fetchDevices, ping, smallMessage } from "./miele" import { testConfig } from "./miele-testutils" jest.setTimeout(JEST_INTEGRATION_TIMEOUT) @@ -38,4 +38,8 @@ describe("miele", () => { state: "UNKNOWN" }) }) + + test("ping", async () => { + expect(await ping()).toBeTruthy() + }) }) diff --git a/app/lib/miele/miele.ts b/app/lib/miele/miele.ts index 1131048..2058e9c 100644 --- a/app/lib/miele/miele.ts +++ b/app/lib/miele/miele.ts @@ -50,3 +50,18 @@ export const smallMessage = (device: MieleDevice) => { timeCompleted: formatTime(add(new Date(), remainingDuration)) } } + +export const ping = async () => { + log.debug("Trying to access Miele endpoint") + try { + await axios.get( + "https://api.mcs3.miele.com/thirdparty/login/" + ) + log.debug("Accessing Miele endpoint success") + return true + } + catch (e) { + log.debug("Accessing Miele endpoint failed") + return false + } +}