58 lines
1.2 KiB
TypeScript
58 lines
1.2 KiB
TypeScript
import * as http from 'http';
|
|
|
|
const port = 4202;
|
|
const youlessUri = 'http://192.168.2.68/a'
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
http.request(youlessUri, response => {
|
|
let str = ''
|
|
response.on('data', function (chunk) {
|
|
str += chunk;
|
|
});
|
|
|
|
response.on('end', function () {
|
|
const parser = new Parser(str);
|
|
|
|
res.end(parser.toPrometheusFormat())
|
|
});
|
|
}).end();
|
|
|
|
})
|
|
|
|
server.listen(port)
|
|
|
|
|
|
class Parser {
|
|
public kWhTotal: number = null;
|
|
public currentPowerWatt: number = null;
|
|
|
|
constructor(
|
|
private input: string
|
|
) {
|
|
this.parse();
|
|
}
|
|
|
|
public toPrometheusFormat(): string {
|
|
return [
|
|
`kwh_total ${this.kWhTotal}`,
|
|
`current_power_watt ${this.currentPowerWatt}`
|
|
].join('\n')
|
|
}
|
|
|
|
private parse() {
|
|
const lines = this.input.split('\n')
|
|
.map(line => line.trim())
|
|
.map(line => line.split(' '))
|
|
|
|
if (lines.length === 0) {
|
|
return;
|
|
}
|
|
|
|
this.kWhTotal = parseFloat(lines[0][0]?.replace(',', '.'));
|
|
this.currentPowerWatt = parseInt(lines[1][0])
|
|
}
|
|
|
|
|
|
}
|