yuge overhaul, threads, hass, rewrote display, hwmons, bunc of shit
This commit is contained in:
@@ -1,27 +1,58 @@
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <print>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <utils.hpp>
|
||||
|
||||
/* SENSOR GETTERS + UTILS*/
|
||||
std::string readFile(std::string path, uint8_t trim) {
|
||||
std::string ret;
|
||||
std::stringstream ss;
|
||||
std::ifstream fs(path);
|
||||
if (!fs.good()) {
|
||||
std::println("[readFile] Error reading {}", path);
|
||||
return "";
|
||||
}
|
||||
ss << fs.rdbuf();
|
||||
fs.close();
|
||||
ret = ss.str();
|
||||
if (ret.size() < trim) return ret;
|
||||
return ret.substr(0, ret.size() - trim - 1);
|
||||
// converts number to string with given decimal places
|
||||
std::string pNum(double num, uint8_t decs) {
|
||||
std::stringstream ret;
|
||||
ret << std::fixed << std::setprecision(decs) << num;
|
||||
return ret.str();
|
||||
}
|
||||
|
||||
int get_val(const std::string &target, const std::string &content) {
|
||||
std::string pNum(float num, uint8_t decs) {
|
||||
return pNum((double)num, decs);
|
||||
}
|
||||
|
||||
|
||||
// converts large number in (k)bytes to human readable numer with suffix and sane decimal
|
||||
std::string pBytes(double bytes, bool isKbytes) {
|
||||
std::array suffix_list = {'K', 'M', 'G', 'T', 'P', 'E', 'Z'};
|
||||
for (auto& suffix : suffix_list) {
|
||||
if (suffix == 'K' && isKbytes) continue; // skip first loop if already in Kbytes
|
||||
bytes /= 1024.0;
|
||||
if (bytes < 1024) {
|
||||
uint8_t decs = 2;
|
||||
if (bytes > 10) decs = 1;
|
||||
if (bytes > 100) decs = 0;
|
||||
return pNum(bytes, decs) + suffix;
|
||||
}
|
||||
}
|
||||
return "++++";
|
||||
}
|
||||
|
||||
/* SENSOR GETTERS + UTILS*/
|
||||
ssize_t readFile2(const char* path, char* buffer, size_t maxlen) {
|
||||
int fd = open(path, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
std::println("[readFile] Error reading {}", path);
|
||||
return 0;
|
||||
}
|
||||
ssize_t len = read(fd, buffer, maxlen - 1);
|
||||
if (len >= 0) buffer[len] = '\0';
|
||||
close(fd);
|
||||
return len;
|
||||
}
|
||||
|
||||
ssize_t readFile2(std::string path, char* buffer, size_t maxlen) {
|
||||
return readFile2(path.c_str(), buffer, maxlen);
|
||||
}
|
||||
|
||||
std::string readFile(std::string path, size_t trim) {
|
||||
char buf[trim + 2];
|
||||
ssize_t len = readFile2(path, buf, trim + 2);
|
||||
std::string out(buf);
|
||||
return out.substr(0, len - 1);
|
||||
}
|
||||
|
||||
int get_val(const std::string& target, const std::string& content) {
|
||||
int result = -1;
|
||||
std::size_t start = content.find(target);
|
||||
if (start != std::string::npos) {
|
||||
@@ -33,14 +64,9 @@ int get_val(const std::string &target, const std::string &content) {
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string findHwmonPath(std::string module) {
|
||||
for (uint8_t id = 0; id < 10; id++) {
|
||||
auto f = readFile("/sys/class/hwmon/hwmon" + std::to_string(id) + "/name");
|
||||
if (f.size() == 0) break;
|
||||
if (f == module) {
|
||||
std::println("[Hwmon] Detected {} path is /sys/class/hwmon/hwmon{}", module, id);
|
||||
return "/sys/class/hwmon/hwmon" + std::to_string(id);
|
||||
}
|
||||
}
|
||||
std::string getFirstDir(std::string path) {
|
||||
for (const auto& entry : std::filesystem::directory_iterator(path))
|
||||
return entry.path();
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user