/
NTPClient.js
187 lines (154 loc) · 6.24 KB
/
NTPClient.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const ntp = require("@destinationstransfers/ntp");
const execSync = require("child_process").execSync;
const Logger = require("./Logger");
const States = require("./entities/core/ntpClient");
const Tools = require("./utils/Tools");
class NTPClient {
/**
* @param {object} options
* @param {import("./Configuration")} options.config
*/
constructor(options) {
this.config = options.config;
this.nextPollTimeout = undefined;
this.retryWaitTime = BASE_RETRY_WAIT_TIME;
this.config.onUpdate((key) => {
if (key === "ntpClient") {
this.reconfigure();
}
});
if (this.config.get("ntpClient").enabled) {
this.state = new States.ValetudoNTPClientEnabledState({});
} else {
this.state = new States.ValetudoNTPClientDisabledState({});
}
// On startup, we need to wait for a while for Valetudo to fully start up (at least when using pkg) or else
// we will get ntp sync timeouts in the log due to something blocking the process for a while
setTimeout(() => {
this.reconfigure();
}, 10000);
}
reconfigure() {
clearTimeout(this.nextPollTimeout);
this.retryWaitTime = BASE_RETRY_WAIT_TIME;
const ntpConfig = this.config.get("ntpClient");
if (ntpConfig.enabled === true) {
this.state = new States.ValetudoNTPClientEnabledState({});
if (this.config.get("embedded") === true) {
this.pollTime().catch(() => {
/* intentional */
});
}
} else {
this.state = new States.ValetudoNTPClientDisabledState({});
}
}
/**
* @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
});
const preSyncTime = new Date();
Logger.debug("Got Time from NTP Server:", currentNTPTime);
this.setTime(currentNTPTime);
this.state = new States.ValetudoNTPClientSyncedState({
offset: currentNTPTime.getTime() - preSyncTime.getTime()
});
this.retryWaitTime = BASE_RETRY_WAIT_TIME;
Logger.debug(`Next NTP sync in ${ntpConfig.interval}ms`);
this.nextPollTimeout = setTimeout(() => {
this.pollTime().catch(() => {
/* intentional */
});
}, ntpConfig.interval);
} catch (e) {
let error = {
type: States.ValetudoNTPClientErrorState.ERROR_TYPE.UNKNOWN,
message: e.message
};
if (typeof e.code === "string") {
switch (e.code) {
case "EAI_AGAIN":
case "ENETUNREACH":
case "ENETDOWN":
case "EAGAIN":
case "ECONNABORTED":
case "ECONNRESET":
case "EPIPE":
error.type = States.ValetudoNTPClientErrorState.ERROR_TYPE.TRANSIENT;
break;
case "ENOTFOUND":
error.type = States.ValetudoNTPClientErrorState.ERROR_TYPE.NAME_RESOLUTION;
break;
}
} else if (typeof e.message === "string" && e.message.indexOf("Timeout waiting") === 0) {
error.type = States.ValetudoNTPClientErrorState.ERROR_TYPE.CONNECTION;
error.message = e.message;
} else if (e.stdout !== undefined) {
error.type = States.ValetudoNTPClientErrorState.ERROR_TYPE.PERSISTING;
error.message = e.stdout.toString() + " " + e.stderr.toString();
}
if (error.type !== States.ValetudoNTPClientErrorState.ERROR_TYPE.TRANSIENT) {
Logger.warn(`${Tools.CAPITALIZE(error.type)} error during time sync: ${error.message}`);
} else {
Logger.debug(`${Tools.CAPITALIZE(error.type)} error during time sync: ${error.message}`);
}
this.state = new States.ValetudoNTPClientErrorState(error);
Logger.debug(`Next NTP sync attempt in ${this.retryWaitTime}ms`);
this.nextPollTimeout = setTimeout(() => {
this.pollTime().catch(() => {
/* intentional */
});
}, this.retryWaitTime);
this.retryWaitTime = Math.min(this.retryWaitTime * 2, 8 * 60 * 60 * 1000);
}
}
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 BASE_RETRY_WAIT_TIME = 60*1000; //1 Minute
module.exports = NTPClient;