Skip to content

Commit

Permalink
improve api "ping" (#139)
Browse files Browse the repository at this point in the history
local DNS could resolve even when API is not reachable
  • Loading branch information
philipparndt authored Jun 11, 2023
1 parent 935d0d9 commit c1295d7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
33 changes: 16 additions & 17 deletions app/lib/connection.ts
Original file line number Diff line number Diff line change
@@ -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<typeof setTimeout>
let connectionLost = false
Expand All @@ -17,23 +17,22 @@ export const registerConnectionCheck = (restartHook: () => Promise<void>, 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)
}
6 changes: 5 additions & 1 deletion app/lib/miele/miele.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -38,4 +38,8 @@ describe("miele", () => {
state: "UNKNOWN"
})
})

test("ping", async () => {
expect(await ping()).toBeTruthy()
})
})
15 changes: 15 additions & 0 deletions app/lib/miele/miele.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit c1295d7

Please sign in to comment.