import { Battery20, Battery30, Battery50, Battery60, Battery80, Battery90, BatteryAlert, BatteryCharging20, BatteryCharging30, BatteryCharging50, BatteryCharging60, BatteryCharging80, BatteryCharging90, BatteryChargingFull, BatteryFull, BatteryUnknown, PowerOff, Power } from "@mui/icons-material"; import { pond } from "protobuf-ts/pond"; import { quack } from "protobuf-ts/quack"; export interface PowerDescriber { description: string; icon: any; } interface batteryThreshold { threshold: number; icon: any; } let chargingThresholds: batteryThreshold[] = [ { threshold: 100, icon: }, { threshold: 90, icon: }, { threshold: 80, icon: }, { threshold: 60, icon: }, { threshold: 40, icon: }, { threshold: 30, icon: }, { threshold: 20, icon: }, { threshold: 1, icon: }, { threshold: 0, icon: } ]; let notChargingThresholds: batteryThreshold[] = [ { threshold: 100, icon: }, { threshold: 90, icon: }, { threshold: 80, icon: }, { threshold: 60, icon: }, { threshold: 40, icon: }, { threshold: 30, icon: }, { threshold: 20, icon: }, { threshold: 1, icon: }, { threshold: 0, icon: } ]; export function describePower(power?: pond.DevicePower | null): PowerDescriber { let result = { description: "", icon: }; if (!power) return result; if (power.type === quack.PowerSubtype.POWER_SUBTYPE_NO_BATTERY) { if (power.inputVoltage > 0) { result.icon = ; result.description = "Plugged in"; } else { result.icon = ; result.description = "Unplugged"; } } else { let thresholds = notChargingThresholds; // we would not actually know if it is charging here since it is just looking at the voltage now and not comparing to previous voltage // let suffix = "not charging"; // if (power.inputVoltage >= 4) { // thresholds = chargingThresholds; // if (power.chargePercent === 100) { // suffix = "charged"; // } else { // suffix = "charging"; // } // } // result.description = power.chargePercent.toString() + "%, " + suffix; // TODO: there is a plan in place to add a boolean value to power in the backend to have it check and set the boolean, we can then use that to know if it is charging or not result.description = power.chargePercent.toString() + "%" for (let i = 0; i < thresholds.length; i++) { if (power.chargePercent >= thresholds[i].threshold) { result.icon = thresholds[i].icon; break; } } } return result; } //returns a boolean indicating whether the search term matches a power query export function filterByPower(searchValue: string, power: pond.DevicePower | undefined): boolean { let matches: string[] = []; if (power) { switch (power.type) { case quack.PowerSubtype.POWER_SUBTYPE_NO_BATTERY: if (power.inputVoltage > 0) { matches.push("on"); } else { matches.push("off"); } break; default: const isCharging = power.inputVoltage >= 4.0; if (isCharging) { matches.push("charging"); } if (power.chargePercent >= 0 && power.chargePercent <= 33) { matches.push("low"); } else if (power.chargePercent <= 66) { matches.push("medium"); } else { matches.push("high"); } break; } } else { matches = ["unknown", "?"]; } return matches.some(match => match.indexOf(searchValue.replace("power:", "").trim()) > -1); }