refactor utils

This commit is contained in:
2025-12-28 19:55:38 +01:00
parent 2d3a481638
commit c7e2447932
6 changed files with 71 additions and 59 deletions

View File

@@ -29,7 +29,7 @@ void sys::getTimeDate(TimeDate* td) {
void sys::getRam(Memory* mem) {
static char buf[128]; // file is longer but we only need first 128 chars
readFile("/proc/meminfo", buf, sizeof(buf));
utils::readFile("/proc/meminfo", buf, sizeof(buf));
mem->free = getVal("MemAvailable:", std::string_view(buf));
mem->total = getVal("MemTotal:", std::string_view(buf));
mem->used = mem->total - mem->free;
@@ -55,8 +55,8 @@ void sys::getNetwork(Network* net, const std::string& device) {
_net current;
current.ts = std::chrono::steady_clock::now();
current.rx = fastReadNum("/sys/class/net/" + device + "/statistics/rx_bytes", buf);
current.tx = fastReadNum("/sys/class/net/" + device + "/statistics/tx_bytes", buf);
current.rx = utils::fastReadNum("/sys/class/net/" + device + "/statistics/rx_bytes", buf);
current.tx = utils::fastReadNum("/sys/class/net/" + device + "/statistics/tx_bytes", buf);
float ts_diff = std::chrono::duration_cast<std::chrono::microseconds>(current.ts - prev.ts).count() / 1000000.0f;
net->download = (current.rx - prev.rx) / ts_diff;
net->upload = (current.tx - prev.tx) / ts_diff;
@@ -66,7 +66,7 @@ void sys::getNetwork(Network* net, const std::string& device) {
void sys::detectHwmon(sysSensors* s, const std::string& platform) {
// we dont really need getFirstDir since its a static folder, but its useful for try/catch
try {
s->hwmon = getFirstDir("/sys/devices/platform/" + platform + "/hwmon");
s->hwmon = utils::getFirstDir("/sys/devices/platform/" + platform + "/hwmon");
} catch (...) {
applog(LOG::ERR, "Can't open sys hwmon. Module loaded? Kernel params added?");
return;
@@ -77,7 +77,7 @@ void sys::updateSensors(sysSensors* s) {
if (s->hwmon.length() < 1)
return;
static char buf[8];
s->fan_speed[0] = fastReadNum(s->hwmon + "/fan2_input", buf);
s->fan_speed[1] = fastReadNum(s->hwmon + "/fan7_input", buf);
s->t_sensor = fastReadNum(s->hwmon + "/temp5_input", buf) / 1000;
s->fan_speed[0] = utils::fastReadNum(s->hwmon + "/fan2_input", buf);
s->fan_speed[1] = utils::fastReadNum(s->hwmon + "/fan7_input", buf);
s->t_sensor = utils::fastReadNum(s->hwmon + "/temp5_input", buf) / 1000;
}