Skip to content

Commit

Permalink
feat: NTPClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Hypfer committed Jan 31, 2021
1 parent ef949be commit 8b54c7d
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 4 deletions.
108 changes: 108 additions & 0 deletions lib/NTPClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
const ntp = require("@destinationstransfers/ntp");

const execSync = require("child_process").execSync;

const Logger = require("./Logger");


class NTPClient {
/**
* @param {object} options
* @param {import("./Configuration")} options.config
*/

constructor(options) {
this.config = options.config;

this.nextPollTimeout = undefined;

if (this.config.get("embedded") === true) {
this.pollTime().then(() => {});
}
}

/**
* @private
*/
async pollTime() {
clearTimeout(this.nextPollTimeout);
const ntpConfig = this.config.get("ntpClient");

try {
Logger.debug("Starting NTP Query for", {
server: ntpConfig.server,
port: ntpConfig.port,
timeout: ntpConfig.timeout
});
const currentNTPTime = await ntp.getNetworkTime({
server: ntpConfig.server,
port: ntpConfig.port,
timeout: ntpConfig.timeout
});

Logger.debug("Got Time from NTP Server:", currentNTPTime);

this.setTime(currentNTPTime);

Logger.debug("Next NTP sync in " + ntpConfig.interval + " ms");

this.nextPollTimeout = setTimeout(() => {
this.pollTime();
}, ntpConfig.interval);
} catch (e) {
Logger.warn("Error during time sync:", e);

Logger.debug("Next NTP sync in " + FAILURE_RETRY_INTERVAL + " ms");

this.nextPollTimeout = setTimeout(() => {
this.pollTime();
}, FAILURE_RETRY_INTERVAL);
}
}


setTime(date) {
if (this.config.get("embedded") === true) {
let dateString = "";

dateString += date.getFullYear().toString();
dateString += "-";
dateString += (date.getMonth() + 1).toString().padStart(2, 0);
dateString += "-";
dateString += date.getDate().toString().padStart(2, 0);
dateString += " ";
dateString += date.getHours().toString().padStart(2,0);
dateString += ":";
dateString += date.getMinutes().toString().padStart(2,0);
dateString += ":";
dateString += date.getSeconds().toString().padStart(2,0);


execSync("date -s \""+dateString+"\"");

Logger.info("Successfully set the robot time via NTP to", date);
} else {
Logger.warn("Cannot set the time. We are not embedded.");
}
}

/**
* Shutdown NTPClient
*
* @public
* @returns {Promise<void>}
*/
shutdown() {
return new Promise((resolve, reject) => {
Logger.debug("NTPClient shutdown in progress...");

clearTimeout(this.nextPollTimeout);
Logger.debug("NTPClient shutdown done");
resolve();
});
}
}

const FAILURE_RETRY_INTERVAL = 60*1000; //1 Minute

module.exports = NTPClient;
7 changes: 7 additions & 0 deletions lib/Valetudo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const v8 = require("v8");
const NTPClient = require("./NTPClient");
const Webserver = require("./webserver/WebServer");
const MqttClient = require("./mqtt/MqttClient");
const Configuration = require("./Configuration");
Expand Down Expand Up @@ -36,6 +37,11 @@ class Valetudo {
Logger.info("Max Heap Size: " + v8.getHeapStatistics().heap_size_limit/1024/1024 + " MiB");
Logger.info("Node Flags: " + process.execArgv.join(" "));


this.ntpClient = new NTPClient({
config: this.config
});

this.robot.startup();

this.webserver = new Webserver({
Expand Down Expand Up @@ -84,6 +90,7 @@ class Valetudo {

await this.webserver.shutdown();
await this.robot.shutdown();
await this.ntpClient.shutdown();

Logger.info("Valetudo shutdown done");
clearTimeout(forceShutdownTimeout);
Expand Down
8 changes: 7 additions & 1 deletion lib/res/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@
"provideMapData": true,
"base64EncodeMapData": true
},

"ntpClient": {
"enabled": true,
"server": "pool.ntp.org",
"port": 123,
"interval": 28800000,
"timeout": 10000
},
"allowSSHKeyUpload": true,
"logLevel": "info",
"debug": {
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
},
"author": "",
"dependencies": {
"expresse": "git+https://github.com/Hypfer/expresse",
"@destinationstransfers/ntp": "^2.0.0",
"body-parser": "^1.18.3",
"compression": "^1.7.2",
"express": "^4.17.1",
"express-basic-auth": "^1.2.0",
"express-dynamic-middleware": "^1.0.0",
"express-list-endpoints": "^5.0.0",
"expresse": "git+https://github.com/Hypfer/expresse",
"mqtt": "^4.0.0",
"multer": "^1.4.1",
"uuid": "^8.3.0",
Expand All @@ -38,13 +39,13 @@
"@types/bytes": "^3.1.0",
"@types/compression": "^1.7.0",
"@types/express": "^4.17.3",
"@types/express-list-endpoints": "^4.0.1",
"@types/mocha": "^7.0.2",
"@types/multer": "^1.4.2",
"@types/node": "^13.9.1",
"@types/on-headers": "^1.0.0",
"@types/ws": "^7.2.2",
"@types/uuid": "^8.3.0",
"@types/express-list-endpoints": "^4.0.1",
"@types/ws": "^7.2.2",
"cross-env": "7.0.2",
"eslint": "^7.5.0",
"eslint-plugin-jsdoc": "^30.2.1",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
dependencies:
regenerator-runtime "^0.13.4"

"@destinationstransfers/ntp@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@destinationstransfers/ntp/-/ntp-2.0.0.tgz#b3efa47f99287309754356aefd28029325c859fa"
integrity sha512-0gYbtXpyNlk0p+jWy+lftUkB+uJIBjNUWTB2tv7/fxl8zO3pQsaVFDLFDozbQ1wnYw/PrHDeRdYwAg2frY48Tg==

"@nodelib/fs.scandir@2.1.3":
version "2.1.3"
resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.3.tgz#3a582bdb53804c6ba6d146579c46e52130cf4a3b"
Expand Down

0 comments on commit 8b54c7d

Please sign in to comment.