Skip to content

Commit

Permalink
Initial battery support
Browse files Browse the repository at this point in the history
  • Loading branch information
Levminer committed Jul 8, 2024
1 parent 2fd2785 commit bbdfab8
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 14 deletions.
7 changes: 7 additions & 0 deletions packages/ui/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ declare global {
name: string
}

battery?: {
capacity: Sensor[]
level: Sensor[]
remainingTime?: Sensor
cycleCount: string
}

monitor: {
monitors: Monitor[]
}
Expand Down
62 changes: 49 additions & 13 deletions packages/ui/pages/home.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -453,21 +453,57 @@
</div>
</div>

<div class="transparent-800 rounded-xl p-8 sm:p-4">
<div class="mb-5 flex items-center gap-3">
<div class="transparent-900 flex aspect-square items-center justify-center rounded-lg p-3 sm:p-2">
<Monitor />
{#if $hardwareInfo.system.battery?.capacity.length ?? 0 > 0}
<div class="transparent-800 rounded-xl p-8 sm:p-4">
<div class="mb-5 flex items-center gap-3">
<div class="transparent-900 flex aspect-square items-center justify-center rounded-lg p-3 sm:p-2">
<Battery />
</div>
<h2>Battery</h2>
</div>
<h2>Monitors</h2>
</div>
{#each $hardwareInfo.system.monitor.monitors as { name, refreshRate, resolution }}

<div class="mt-5 select-text">
<h3>Name: {name}</h3>
<h3>Resolution: {resolution}</h3>
<h3>Refresh rate: {refreshRate} Hz</h3>
<h3>Charge level: {Math.round($hardwareInfo.system.battery.level[1].value)}%</h3>
<h3>Health: {Math.round(100 - $hardwareInfo.system.battery.level[0].value)}%</h3>
<h3>Cycle count: {$hardwareInfo.system.battery.cycleCount}</h3>
<h3>
Capacity: {Math.round($hardwareInfo.system.battery.capacity[2].value / 1000)}/{Math.round(
$hardwareInfo.system.battery.capacity[1].value / 1000,
)} Wh
</h3>
<h3>
Remaining time: {#if $hardwareInfo.system.battery.remainingTime.value !== null}
{`${Math.floor($hardwareInfo.system.battery.remainingTime.value / 60)}:${(
$hardwareInfo.system.battery.remainingTime.value % 60
)
.toString()
.padStart(2, "0")}`}
{:else}
N/A
{/if}
</h3>
</div>
{/each}
</div>
</div>
{/if}

{#if $hardwareInfo.system.monitor.monitors.length > 0}
<div class="transparent-800 rounded-xl p-8 sm:p-4">
<div class="mb-5 flex items-center gap-3">
<div class="transparent-900 flex aspect-square items-center justify-center rounded-lg p-3 sm:p-2">
<Monitor />
</div>
<h2>Monitors</h2>
</div>

{#each $hardwareInfo.system.monitor.monitors as { name, refreshRate, resolution }}
<div class="mt-5 select-text">
<h3>Name: {name}</h3>
<h3>Resolution: {resolution}</h3>
<h3>Refresh rate: {refreshRate} Hz</h3>
</div>
{/each}
</div>
{/if}

<div class="transparent-800 rounded-xl p-8 sm:p-4">
<div class="mb-5 flex items-center gap-3">
Expand Down Expand Up @@ -512,7 +548,7 @@
import { hardwareInfo } from "ui/stores/hardwareInfo.ts"
import GaugeChart from "ui/charts/gaugeChart.svelte"
import MeterChart from "ui/charts/meterChart.svelte"
import { Gauge, CircuitBoard, Clock, Fan, HardDrive, Monitor, Network, Plug, Thermometer, Zap, Cpu } from "lucide-svelte"
import { Gauge, CircuitBoard, Clock, Fan, HardDrive, Monitor, Network, Plug, Thermometer, Zap, Cpu, Battery } from "lucide-svelte"
import { GpuCard, Memory, PcDisplay } from "svelte-bootstrap-icons"
import Progress from "ui/components/progress.svelte"
</script>
32 changes: 31 additions & 1 deletion platforms/windows/lib/Commands.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using Serilog;
using System.Diagnostics;
using System.Text.Json;

namespace lib;
Expand Down Expand Up @@ -56,4 +57,33 @@ public static string GetGPUInfo() {

return foramttedDate;
}

public class CycleCountInfo {
public int? CycleCount { get; set; }
}

public static string GetCycleCount() {
try {
var command = ExecuteCommand("Get-WmiObject -Class MSBatteryClass -Namespace ROOT/WMI | Select-Object CycleCount | ConvertTo-Json");

var cycles = JsonSerializer.Deserialize<List<CycleCountInfo>>(command);
var c = 0;

if (cycles?.Count > 0) {
foreach (var cycle in cycles) {
c += cycle.CycleCount ?? 0;
}
}

if (c == 0) {
return "N/A";
} else {
return c.ToString();
}
}
catch (Exception) {
Log.Error("Failed to get cycle count");
return "N/A";
}
}
}
59 changes: 59 additions & 0 deletions platforms/windows/lib/HardwareInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class HardwareInfo {
IsMotherboardEnabled = true,
IsStorageEnabled = true,
IsNetworkEnabled = true,
IsBatteryEnabled = true,
};

public API API {
Expand Down Expand Up @@ -594,6 +595,64 @@ public void GetInfo() {
}
}
}

// Battery
if (hardware.HardwareType == HardwareType.Battery) {
var energySensors = hardware.Sensors.Where(x => x.SensorType == SensorType.Energy).ToArray();
var levelSensors = hardware.Sensors.Where(x => x.SensorType == SensorType.Level).ToArray();
var timeSpanSensors = hardware.Sensors.Where(x => x.SensorType == SensorType.TimeSpan).ToArray();

// Energy capacity
for (int j = 0; j < energySensors.Length; j++) {
var data = new Sensor {
Name = energySensors[j].Name,
Value = energySensors[j].Value ?? 0,
Min = energySensors[j].Min ?? 0,
Max = energySensors[j].Max ?? 0,
};

if (firstRun) {
API.System.Battery.Capacity.Add(data);
} else {
API.System.Battery.Capacity[j] = data;
}
}

// Charge level
for (int j = 0; j < levelSensors.Length; j++) {
var data = new Sensor {
Name = levelSensors[j].Name,
Value = levelSensors[j].Value ?? 0,
Min = levelSensors[j].Min ?? 0,
Max = levelSensors[j].Max ?? 0,
};

if (firstRun) {
API.System.Battery.Level.Add(data);
} else {
API.System.Battery.Level[j] = data;
}
}

// Remaining time
for (int j = 0; j < timeSpanSensors.Length; j++) {
var data = new Sensor {
Name = timeSpanSensors[j].Name,
Value = timeSpanSensors[j].Value ?? 0,
Min = timeSpanSensors[j].Min ?? 0,
Max = timeSpanSensors[j].Max ?? 0,
};

API.System.Battery.RemainingTime = data;
}

// Cycle count
if (firstRun) {
var cycles = Commands.GetCycleCount();

API.System.Battery.CycleCount = cycles;
}
}
}

// HWInfo, monitors, network interfaces
Expand Down
15 changes: 15 additions & 0 deletions platforms/windows/lib/Interfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,17 @@ public List<NetInterface> Interfaces {
} = new();
}

public class BatteryInfo {
public List<Sensor> Capacity {
get; set;
} = new();
public List<Sensor> Level {
get; set;
} = new();
public Sensor RemainingTime { get; set; }
public string CycleCount { get; set; }
}

public class SystemAPI {
public OSInfo OS {
get; set;
Expand All @@ -306,6 +317,10 @@ public MotherboardInfo Motherboard {
get; set;
} = new();

public BatteryInfo Battery {
get; set;
} = new();

public MonitorInfo Monitor {
get; set;
} = new();
Expand Down

0 comments on commit bbdfab8

Please sign in to comment.