Update ups-exporter.js

This commit is contained in:
Zakary Timson 2024-07-22 15:29:56 -04:00
parent e916ca7ab7
commit ee8fc6e7ee

View File

@ -11,12 +11,19 @@ async function scrape() {
while(!!(found = r.exec(data))) while(!!(found = r.exec(data)))
map[found[1]] = found[2]; map[found[1]] = found[2];
if(map["Remaining Runtime"]) {
const parsed = /(\d+) (.)/gi.exec(map["Remaining Runtime"]);
const unit = parsed[2].toLowerCase();
if(unit == 'm') map["Remaining Runtime"] = Number(parsed[1]) * 60;
if(unit == 'h') map["Remaining Runtime"] = Number(parsed[1]) * 60 * 60;
}
return { return {
model: map["Model Name"], model: map["Model Name"],
vendor: map["Power Supply by"], vendor: map["Power Supply by"],
state: map["State"], state: map["State"],
charge: Number(/\d+/g.exec(map["Battery Capacity"])[0]) / 100, charge: Number(/\d+/g.exec(map["Battery Capacity"])[0]) / 100,
remaining: map["Remaining Runtime"], battery_time: map["Remaining Runtime"],
rated_voltage: Number(/\d+/g.exec(map["Rating Voltage"])[0]), rated_voltage: Number(/\d+/g.exec(map["Rating Voltage"])[0]),
rated_wattage: Number(/\d+/g.exec(map["Rating Power"])[0]), rated_wattage: Number(/\d+/g.exec(map["Rating Power"])[0]),
input_voltage: Number(/\d+/g.exec(map["Utility Voltage"])[0]), input_voltage: Number(/\d+/g.exec(map["Utility Voltage"])[0]),
@ -34,8 +41,8 @@ function format(a) {
.join(', '); .join(', ');
if(labels) labels = `{${labels}}`; if(labels) labels = `{${labels}}`;
return Object.entries(a).filter(([key, value]) => typeof value != 'string') return Object.entries(a)
.map(([key, value]) => `${key}${labels} ${value}`) .map(([key, value]) => `ups_${key}${labels} ${typeof value == 'string' ? 1 : value}`)
.join('\n'); .join('\n');
} }