mirror of
https://github.com/compute-blade-community/compute-blade-agent.git
synced 2026-04-21 17:45:43 +02:00
This PR introduces a comprehensive set of new subcommands to bladectl, expanding its capabilities for querying and managing compute blade state. It also includes an internal refactor to simplify interface management across the gRPC API.
* `get`
* `fan`: Returns current fan speed.
* `identify`: Indicates whether the identify mode is active.
* `stealth`: Shows if stealth mode is currently enabled.
* `status`: Prints a full blade status report.
* `temperature`: Retrieves current SoC temperature.
* `critical`: Shows whether critical mode is active.
* `power`: Reports the current power source (e.g., PoE+ or USB).
* `set`
* `stealth`: Enables stealth mode.
* `remove`
* `stealth`: Disables stealth mode.
* `describe`
* `fan`: Outputs the current fan curve configuration.
* `monitor`: plot some charts about the state of the compute-blade-agent
* **gRPC API refactor**: The gRPC service definitions previously located in `internal/api` have been folded into `internal/agent`. This eliminates redundant interface declarations and ensures that all ComputeBladeAgent implementations are directly compatible with the gRPC API.
This reduces duplication and improves long-term maintainability and clarity of the interface contract.
```bash
bladectl set fan --percent 90 --blade 1 --blade 2
bladectl unset identify --blade 1 --blade 2 --blade 3 --blade 4
bladectl set stealth --blade 1 --blade 2 --blade 3 --blade 4
bladectl get status --blade 1 --blade 2 --blade 3 --blade 4
┌───────┬─────────────┬────────────────────┬───────────────┬──────────────┬──────────┬───────────────┬──────────────┐
│ BLADE │ TEMPERATURE │ FAN SPEED OVERRIDE │ FAN SPEED │ STEALTH MODE │ IDENTIFY │ CRITICAL MODE │ POWER STATUS │
├───────┼─────────────┼────────────────────┼───────────────┼──────────────┼──────────┼───────────────┼──────────────┤
│ 1 │ 50°C │ 90% │ 5825 RPM(90%) │ Active │ Off │ Off │ poe+ │
│ 2 │ 48°C │ 90% │ 5825 RPM(90%) │ Active │ Off │ Off │ poe+ │
│ 3 │ 49°C │ Not set │ 4643 RPM(56%) │ Active │ Off │ Off │ poe+ │
│ 4 │ 49°C │ Not set │ 4774 RPM(58%) │ Active │ Off │ Off │ poe+ │
└───────┴─────────────┴────────────────────┴───────────────┴──────────────┴──────────┴───────────────┴──────────────┘
bladectl rm stealth --blade 1 --blade 2 --blade 3 --blade 4
bladectl rm fan --blade 1 --blade 2 --blade 3 --blade 4
bladectl get status --blade 1 --blade 2 --blade 3 --blade 4
┌───────┬─────────────┬────────────────────┬───────────────┬──────────────┬──────────┬───────────────┬──────────────┐
│ BLADE │ TEMPERATURE │ FAN SPEED OVERRIDE │ FAN SPEED │ STEALTH MODE │ IDENTIFY │ CRITICAL MODE │ POWER STATUS │
├───────┼─────────────┼────────────────────┼───────────────┼──────────────┼──────────┼───────────────┼──────────────┤
│ 1 │ 51°C │ Not set │ 5177 RPM(66%) │ Off │ Off │ Off │ poe+ │
│ 2 │ 49°C │ Not set │ 5177 RPM(58%) │ Off │ Off │ Off │ poe+ │
│ 3 │ 50°C │ Not set │ 4659 RPM(60%) │ Off │ Off │ Off │ poe+ │
│ 4 │ 48°C │ Not set │ 4659 RPM(54%) │ Off │ Off │ Off │ poe+ │
└───────┴─────────────┴────────────────────┴───────────────┴──────────────┴──────────┴───────────────┴──────────────┘
```
when having multiple compute-blades in your bladeconfig:
```yaml
blades:
- name: 1
blade:
server: blade-pi1:8081
cert:
certificate-authority-data: <redacted>
client-certificate-data: <redacted>
client-key-data: <redacted>
- name: 2
blade:
server: blade-pi2:8081
cert:
certificate-authority-data: <redacted>
client-certificate-data: <redacted>
client-key-data: <redacted>
- name: 3
blade:
server: blade-pi3:8081
cert:
certificate-authority-data: <redacted>
client-certificate-data: <redacted>
client-key-data: <redacted>
- name: 4
blade:
server: blade-pi4:8081
cert:
certificate-authority-data: <redacted>
client-certificate-data: <redacted>
client-key-data: <redacted>
- name: 4
blade:
server: blade-pi4:8081
cert:
certificate-authority-data: <redacted>
client-certificate-data: <redacted>
client-key-data: <redacted>
current-blade: 1
```
Fixes #4, #9 (partially), should help with #5
* test: improve unit-testing
* fix: pin github.com/warthog618/gpiod
---------
Co-authored-by: Cedric Kienzler <cedric@specht-labs.de>
107 lines
2.7 KiB
Go
107 lines
2.7 KiB
Go
//go:build linux && !tinygo
|
|
|
|
package hal
|
|
|
|
import (
|
|
"context"
|
|
"math"
|
|
|
|
"github.com/compute-blade-community/compute-blade-agent/pkg/hal/led"
|
|
"github.com/compute-blade-community/compute-blade-agent/pkg/log"
|
|
"github.com/warthog618/gpiod"
|
|
"github.com/warthog618/gpiod/device/rpi"
|
|
)
|
|
|
|
type standardFanUnitBcm2711 struct {
|
|
GpioChip0 *gpiod.Chip
|
|
SetFanSpeedPwmFunc func(speed uint8) error
|
|
DisableRpmReporting bool
|
|
|
|
// Fan tachometer input
|
|
fanEdgeLine *gpiod.Line
|
|
lastFanEdgeEvent *gpiod.LineEvent
|
|
fanRpm float64
|
|
}
|
|
|
|
func (fu standardFanUnitBcm2711) Kind() FanUnitKind {
|
|
if fu.DisableRpmReporting {
|
|
return FanUnitKindStandardNoRPM
|
|
}
|
|
return FanUnitKindStandard
|
|
}
|
|
|
|
func (fu standardFanUnitBcm2711) Run(ctx context.Context) error {
|
|
var err error
|
|
fanUnit.WithLabelValues("standard").Set(1)
|
|
|
|
// Register edge event handler for fan tachometer input
|
|
if !fu.DisableRpmReporting {
|
|
fu.fanEdgeLine, err = fu.GpioChip0.RequestLine(
|
|
rpi.GPIO13,
|
|
gpiod.WithEventHandler(fu.handleFanEdge),
|
|
gpiod.WithFallingEdge,
|
|
gpiod.WithPullUp,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func(fanEdgeLine *gpiod.Line) {
|
|
err := fanEdgeLine.Close()
|
|
if err != nil {
|
|
log.FromContext(ctx).WithError(err).Error("failed to close fanEdgeLine")
|
|
}
|
|
}(fu.fanEdgeLine)
|
|
}
|
|
|
|
<-ctx.Done()
|
|
return ctx.Err()
|
|
}
|
|
|
|
// handleFanEdge handles an edge event on the fan tachometer input for the standard fan unite.
|
|
// Exponential moving average is used to smooth out the fan speed.
|
|
func (fu *standardFanUnitBcm2711) handleFanEdge(evt gpiod.LineEvent) {
|
|
// Ensure we're always storing the last event
|
|
defer func() {
|
|
fu.lastFanEdgeEvent = &evt
|
|
}()
|
|
|
|
// First event, we cannot extrapolate the fan speed yet
|
|
if fu.lastFanEdgeEvent == nil {
|
|
return
|
|
}
|
|
|
|
// Calculate time delta between events
|
|
delta := evt.Timestamp - fu.lastFanEdgeEvent.Timestamp
|
|
ticksPerSecond := 1000.0 / float64(delta.Milliseconds())
|
|
rpm := (ticksPerSecond * 60.0) / 2.0 // 2 ticks per revolution
|
|
|
|
// Simple moving average to smooth out the fan speed
|
|
fu.fanRpm = (rpm * 0.1) + (fu.fanRpm * 0.9)
|
|
fanSpeed.Set(fu.fanRpm)
|
|
}
|
|
|
|
func (fu *standardFanUnitBcm2711) SetFanSpeedPercent(_ context.Context, percent uint8) error {
|
|
return fu.SetFanSpeedPwmFunc(percent)
|
|
}
|
|
|
|
func (fu *standardFanUnitBcm2711) SetLed(_ context.Context, _ led.Color) error {
|
|
return nil
|
|
}
|
|
|
|
func (fu *standardFanUnitBcm2711) FanSpeedRPM(_ context.Context) (float64, error) {
|
|
return fu.fanRpm, nil
|
|
}
|
|
|
|
func (fu *standardFanUnitBcm2711) WaitForButtonPress(ctx context.Context) error {
|
|
<-ctx.Done()
|
|
return ctx.Err()
|
|
}
|
|
|
|
func (fu *standardFanUnitBcm2711) AirFlowTemperature(_ context.Context) (float32, error) {
|
|
return -1 * math.MaxFloat32, nil
|
|
}
|
|
|
|
func (fu *standardFanUnitBcm2711) Close() error {
|
|
return nil
|
|
}
|