forked from Hypfer/Valetudo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValetudo.js
More file actions
138 lines (117 loc) · 4.88 KB
/
Copy pathValetudo.js
File metadata and controls
138 lines (117 loc) · 4.88 KB
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
const v8 = require("v8");
const Webserver = require("./webserver/WebServer");
const MqttClient = require("./MqttClient");
const Configuration = require("./Configuration");
const Events = require("./Events");
const SSHManager = require("./SSHManager");
const Model = require("./miio/Model");
const Logger = require("./Logger");
const Viomi = require("./devices/Viomi");
const RoborockV1 = require("./devices/RoborockV1");
const RoborockS5 = require("./devices/RoborockS5");
const RoborockGen3 = require("./devices/RoborockGen3");
const RoborockS6 = require("./devices/RoborockS6");
const Dreame = require("./devices/Dreame");
class Valetudo {
constructor() {
this.configuration = new Configuration();
this.events = new Events();
try {
Logger.LogLevel = this.configuration.get("logLevel");
} catch (e) {
Logger.error("Initialising Logger: " + e);
}
const modelConf = this.configuration.get("model");
this.model = new Model({
identifier: modelConf.type,
embedded: modelConf.embedded,
config: modelConf.config
});
/** @type {import("./devices/MiioVacuum")} */
this.vacuum;
const robotArgs = {
events: this.events,
configuration: this.configuration,
model: this.model
};
Logger.info("Starting Valetudo for Vacuum Model " + this.model.getModelIdentifier());
Logger.info("DeviceId " + this.model.getDeviceId());
Logger.info("IP " + this.model.getIP());
Logger.info("CloudSecret " + this.model.getCloudSecret());
Logger.info("LocalSecret " + this.model.getLocalSecretProvider()());
Logger.info("JS Runtime Version " + process.version);
Logger.info("Max Heap Size: " + v8.getHeapStatistics().heap_size_limit/1024/1024 + " MiB");
if (MODEL_TO_IMPLEMENTATION[this.model.getModelIdentifier()]) {
this.vacuum = new MODEL_TO_IMPLEMENTATION[this.model.getModelIdentifier()](robotArgs);
} else {
throw new Error("No implementation found for " + this.model.getModelIdentifier());
}
this.sshManager = new SSHManager();
this.webserver = new Webserver({
vacuum: this.vacuum,
configuration: this.configuration,
events: this.events,
model: this.model,
sshManager: this.sshManager
});
this.mqttClient = new MqttClient({
configuration: this.configuration,
vacuum: this.vacuum,
model: this.model,
events: this.events
});
//This might get refactored if there are more of these options
if (this.configuration.get("debug") && typeof this.configuration.get("debug").memoryStatInterval === "number") {
this.memoryStatInterval = setInterval(() => {
const output = {};
const heapStatistics = v8.getHeapStatistics();
try {
output.totalHeapSize = (heapStatistics.total_heap_size/1024/1024).toFixed(3) + " MiB";
output.mallocedMemory = (heapStatistics.malloced_memory/1024/1024).toFixed(3) + " MiB";
output.peakMallocedMemory = (heapStatistics.peak_malloced_memory/1024/1024).toFixed(3) + " MiB";
} catch (e) {
//Intentional if something is 0
}
Logger.info("Memory Stats", output);
}, this.configuration.get("debug").memoryStatInterval);
}
}
async shutdown() {
Logger.info("Valetudo shutdown in progress...");
const forceShutdownTimeout = setTimeout(() => {
Logger.warn("Failed to shutdown valetudo in a timely manner. Using (the) force");
process.exit(1);
}, 5000);
// shuts down valetudo (reverse startup sequence):
if (this.mqttClient) {
await this.mqttClient.shutdown();
}
await this.webserver.shutdown();
await this.vacuum.shutdown();
//Debug foo
if (this.memoryStatInterval) {
clearInterval(this.memoryStatInterval);
}
Logger.info("Valetudo shutdown done");
clearTimeout(forceShutdownTimeout);
}
}
const MODEL_TO_IMPLEMENTATION = {
"rockrobo.vacuum.v1": RoborockV1,
"roborock.vacuum.s5": RoborockS5,
"viomi.vacuum.v6": Viomi,
"viomi.vacuum.v7": Viomi,
"viomi.vacuum.v8": Viomi,
"roborock.vacuum.t6": RoborockS6,
"roborock.vacuum.s6": RoborockS6,
"roborock.vacuum.s5e": RoborockGen3,
"roborock.vacuum.t4": RoborockGen3,
"roborock.vacuum.s4": RoborockGen3,
"roborock.vacuum.m1s": RoborockGen3,
"roborock.vacuum.a10": RoborockGen3,
"roborock.vacuum.a11": RoborockGen3,
"roborock.vacuum.a08": RoborockGen3,
"roborock.vacuum.p5": RoborockGen3,
"dreame.vacuum.mc1808": Dreame
};
module.exports = Valetudo;