|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "math" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + |
| 10 | + "github.com/charlie0129/batt/pkg/calibration" |
| 11 | + "github.com/charlie0129/batt/pkg/config" |
| 12 | + "github.com/charlie0129/batt/pkg/powerinfo" |
| 13 | +) |
| 14 | + |
| 15 | +type statusJSON struct { |
| 16 | + Charging statusChargingJSON `json:"charging"` |
| 17 | + Battery statusBatteryJSON `json:"battery"` |
| 18 | + Configuration statusConfigJSON `json:"configuration"` |
| 19 | + // Calibration is omitted when telemetry data is unavailable (e.g. API error). |
| 20 | + Calibration *statusCalibrationJSON `json:"calibration,omitempty"` |
| 21 | +} |
| 22 | + |
| 23 | +type statusChargingJSON struct { |
| 24 | + AllowCharging bool `json:"allowCharging"` |
| 25 | + UseAdapter bool `json:"useAdapter"` |
| 26 | + PluggedIn bool `json:"pluggedIn"` |
| 27 | +} |
| 28 | + |
| 29 | +type statusBatteryJSON struct { |
| 30 | + CurrentChargePercent int `json:"currentChargePercent"` |
| 31 | + State string `json:"state"` |
| 32 | + TimeToLimitMinutes *int `json:"timeToLimitMinutes"` |
| 33 | + FullCapacityMah int `json:"fullCapacityMah"` |
| 34 | + ChargeRateWatts float64 `json:"chargeRateWatts"` |
| 35 | + VoltageVolts float64 `json:"voltageVolts"` |
| 36 | +} |
| 37 | + |
| 38 | +type statusConfigJSON struct { |
| 39 | + Enabled bool `json:"enabled"` |
| 40 | + UpperLimitPercent int `json:"upperLimitPercent"` |
| 41 | + LowerLimitPercent int `json:"lowerLimitPercent"` |
| 42 | + PreventIdleSleep bool `json:"preventIdleSleep"` |
| 43 | + DisableChargingPreSleep bool `json:"disableChargingPreSleep"` |
| 44 | + PreventSystemSleep bool `json:"preventSystemSleep"` |
| 45 | + AllowNonRootAccess bool `json:"allowNonRootAccess"` |
| 46 | + ControlMagSafeLed statusMagSafeLedJSON `json:"controlMagSafeLed"` |
| 47 | +} |
| 48 | + |
| 49 | +type statusMagSafeLedJSON struct { |
| 50 | + Enabled bool `json:"enabled"` |
| 51 | + Mode string `json:"mode"` |
| 52 | +} |
| 53 | + |
| 54 | +type statusCalibrationJSON struct { |
| 55 | + Phase string `json:"phase"` |
| 56 | + StartedAt *time.Time `json:"startedAt"` |
| 57 | + Paused bool `json:"paused"` |
| 58 | + CanPause bool `json:"canPause"` |
| 59 | + CanCancel bool `json:"canCancel"` |
| 60 | + Message string `json:"message"` |
| 61 | + Schedule statusCalibrationSchedJSON `json:"schedule"` |
| 62 | +} |
| 63 | + |
| 64 | +type statusCalibrationSchedJSON struct { |
| 65 | + Enabled bool `json:"enabled"` |
| 66 | + Cron string `json:"cron"` |
| 67 | + ScheduledAt *time.Time `json:"scheduledAt"` |
| 68 | +} |
| 69 | + |
| 70 | +// batteryStateString returns a camelCase string for the battery state. |
| 71 | +func batteryStateString(state powerinfo.BatteryState, chargeRate int) string { |
| 72 | + switch state { |
| 73 | + case powerinfo.Charging: |
| 74 | + return "charging" |
| 75 | + case powerinfo.Discharging: |
| 76 | + if chargeRate != 0 { |
| 77 | + return "discharging" |
| 78 | + } |
| 79 | + return "notCharging" |
| 80 | + case powerinfo.Full: |
| 81 | + return "full" |
| 82 | + default: |
| 83 | + return "notCharging" |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func printStatusJSON(cmd *cobra.Command, data *statusData, cfg *config.File) error { |
| 88 | + mode := cfg.ControlMagSafeLED() |
| 89 | + upperLimit := cfg.UpperLimit() |
| 90 | + enabled := upperLimit < 100 |
| 91 | + |
| 92 | + // When batt is disabled (limit=100%), lower limit equals upper limit |
| 93 | + // because the battery is always allowed to charge freely. |
| 94 | + lowerLimit := cfg.LowerLimit() |
| 95 | + if !enabled { |
| 96 | + lowerLimit = upperLimit |
| 97 | + } |
| 98 | + |
| 99 | + out := statusJSON{ |
| 100 | + Charging: statusChargingJSON{ |
| 101 | + AllowCharging: data.charging, |
| 102 | + UseAdapter: data.adapter, |
| 103 | + PluggedIn: data.pluggedIn, |
| 104 | + }, |
| 105 | + Battery: statusBatteryJSON{ |
| 106 | + CurrentChargePercent: data.currentCharge, |
| 107 | + State: batteryStateString(data.batteryInfo.State, data.batteryInfo.ChargeRate), |
| 108 | + TimeToLimitMinutes: computeTimeToLimit(data, cfg), |
| 109 | + FullCapacityMah: data.batteryInfo.Design, |
| 110 | + ChargeRateWatts: math.Round(float64(data.batteryInfo.ChargeRate)/1e3*10) / 10, |
| 111 | + VoltageVolts: math.Round(data.batteryInfo.DesignVoltage*100) / 100, |
| 112 | + }, |
| 113 | + Configuration: statusConfigJSON{ |
| 114 | + Enabled: enabled, |
| 115 | + UpperLimitPercent: upperLimit, |
| 116 | + LowerLimitPercent: lowerLimit, |
| 117 | + PreventIdleSleep: cfg.PreventIdleSleep(), |
| 118 | + DisableChargingPreSleep: cfg.DisableChargingPreSleep(), |
| 119 | + PreventSystemSleep: cfg.PreventSystemSleep(), |
| 120 | + AllowNonRootAccess: cfg.AllowNonRootAccess(), |
| 121 | + ControlMagSafeLed: statusMagSafeLedJSON{ |
| 122 | + Enabled: mode != config.ControlMagSafeModeDisabled, |
| 123 | + Mode: string(mode), |
| 124 | + }, |
| 125 | + }, |
| 126 | + } |
| 127 | + |
| 128 | + tr, err := apiClient.GetTelemetry(false, true) |
| 129 | + if err == nil && tr.Calibration != nil { |
| 130 | + cal := tr.Calibration |
| 131 | + |
| 132 | + var startedAt *time.Time |
| 133 | + if cal.Phase != calibration.PhaseIdle && !cal.StartedAt.IsZero() { |
| 134 | + startedAt = &cal.StartedAt |
| 135 | + } |
| 136 | + |
| 137 | + cron := cfg.Cron() |
| 138 | + sched := statusCalibrationSchedJSON{ |
| 139 | + Enabled: cron != "", |
| 140 | + Cron: cron, |
| 141 | + } |
| 142 | + if cron != "" && !cal.ScheduledAt.IsZero() { |
| 143 | + sched.ScheduledAt = &cal.ScheduledAt |
| 144 | + } |
| 145 | + |
| 146 | + out.Calibration = &statusCalibrationJSON{ |
| 147 | + Phase: string(cal.Phase), |
| 148 | + StartedAt: startedAt, |
| 149 | + Paused: cal.Paused, |
| 150 | + CanPause: cal.CanPause, |
| 151 | + CanCancel: cal.CanCancel, |
| 152 | + Message: cal.Message, |
| 153 | + Schedule: sched, |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + enc := json.NewEncoder(cmd.OutOrStdout()) |
| 158 | + enc.SetIndent("", " ") |
| 159 | + return enc.Encode(out) |
| 160 | +} |
0 commit comments