-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1eade52
commit 935d0d9
Showing
8 changed files
with
187 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import dns from "dns" | ||
import { getAppConfig } from "./config/config" | ||
import { log } from "./logger" | ||
|
||
let checkConnection: ReturnType<typeof setTimeout> | ||
let connectionLost = false | ||
|
||
export const unregisterConnectionCheck = () => { | ||
checkConnection?.unref() | ||
} | ||
|
||
export const registerConnectionCheck = (restartHook: () => Promise<void>, config = getAppConfig().miele) => { | ||
const interval = config["connection-check-interval"] | ||
if (interval === 0) { | ||
log.debug("Internet connection check disabled") | ||
return | ||
} | ||
log.info("Internet connection will be checked every", { ms: interval }) | ||
connectionLost = false | ||
checkConnection = setInterval(() => { | ||
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") | ||
} | ||
}) | ||
}, interval) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters