add uptime module

This commit is contained in:
Jeeves 2024-03-15 19:15:59 -06:00
parent 4778b5b77a
commit df38a714e1
2 changed files with 16 additions and 34 deletions

View file

@ -20,13 +20,11 @@ pub fn main() !void {
try stdout.print("{{\"version\":1}}\n[\n", .{});
try bw.flush();
var _display = display.init(arena.allocator());
var _battery = battery.init(arena.allocator());
var _calendar = calendar.init(arena.allocator());
var modules = [_]Module{
_display.module,
_battery.module,
_calendar.module,
uptime.init(arena.allocator()).module,
display.init(arena.allocator()).module,
battery.init(arena.allocator()).module,
calendar.init(arena.allocator()).module,
};
while (true) {
@ -35,12 +33,6 @@ pub fn main() !void {
try json.stringify(try module.getJson(), .{ .emit_null_optional_fields = false }, stdout);
try stdout.print(",", .{});
}
// try stdout.print("[", .{});
// try json.stringify(battery_json, .{ .emit_null_optional_fields = false }, stdout);
// try stdout.print(",", .{});
// try json.stringify(calendar_json, .{ .emit_null_optional_fields = false }, stdout);
// try stdout.print(",", .{});
// try stdout.print("{{\"full_text\":\"test\"}}", .{});
try stdout.print("],\n", .{});
try bw.flush();

View file

@ -4,7 +4,7 @@ const Self = @This();
module: Module,
pub fn init(allocator: std.mem.Allocator) Module {
pub fn init(allocator: std.mem.Allocator) Self {
return .{
.module = .{
.allocator = allocator,
@ -16,30 +16,20 @@ pub fn init(allocator: std.mem.Allocator) Module {
pub fn getJson(module: *const Module) !Module.JSON {
const self = @fieldParentPtr(Self, "module", module);
var energy_full_file = try std.fs.openFileAbsolute("/sys/class/power_supply/BAT0/energy_full", .{});
defer energy_full_file.close();
var energy_now_file = try std.fs.openFileAbsolute("/sys/class/power_supply/BAT0/energy_now", .{});
defer energy_now_file.close();
var status_file = try std.fs.openFileAbsolute("/sys/class/power_supply/BAT0/status", .{});
defer status_file.close();
var uptime_file = try std.fs.openFileAbsolute("/proc/uptime", .{});
defer uptime_file.close();
const energy_full_string = try energy_full_file.reader().readAllAlloc(self.module.allocator, 32);
const energy_now_string = try energy_now_file.reader().readAllAlloc(self.module.allocator, 32);
const status_string = try status_file.reader().readAllAlloc(self.module.allocator, 32);
const energy_full = try std.fmt.parseInt(u32, energy_full_string[0 .. energy_full_string.len - 1], 10);
const energy_now = try std.fmt.parseInt(u32, energy_now_string[0 .. energy_now_string.len - 1], 10);
const uptime_string = try uptime_file.reader().readAllAlloc(self.module.allocator, 256);
var uptime_split = std.mem.splitScalar(u8, uptime_string[0 .. uptime_string.len - 1], ' ');
var uptime = try std.fmt.parseFloat(f32, uptime_split.first());
const status = if (std.mem.eql(u8, status_string[0 .. status_string.len - 1], "Full"))
"="
else if (std.mem.eql(u8, status_string[0 .. status_string.len - 1], "Discharging"))
"v"
else if (std.mem.eql(u8, status_string[0 .. status_string.len - 1], "Charging"))
"^"
else
"?";
const percent_left = @as(f32, @floatFromInt(energy_now)) / @as(f32, @floatFromInt(energy_full)) * 100;
const days = uptime / std.time.s_per_day;
uptime -= std.time.s_per_day * @floor(days);
const hours = uptime / std.time.s_per_hour;
uptime -= std.time.s_per_hour * @floor(hours);
const mins = uptime / std.time.s_per_min;
return .{
.full_text = try std.fmt.allocPrint(self.module.allocator, "{s} {d:.2}%", .{ status, percent_left }),
.full_text = try std.fmt.allocPrint(self.module.allocator, "{d:0>1.0}d {d:0>1.0}h {d:.0}m", .{ days, hours, mins }),
};
}