mirror of
https://github.com/compute-blade-community/compute-blade-agent.git
synced 2026-04-21 17:45:43 +02:00
chore!: more refactoring
Signed-off-by: Matthias Riegler <matthias.riegler@ankorstore.com>
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
package computeblade
|
||||
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
mode: set
|
||||
github.com/xvzf/computeblade-agent/pkg/computeblade/ledengine.go:40.56,46.2 1 1
|
||||
github.com/xvzf/computeblade-agent/pkg/computeblade/ledengine.go:49.84,62.2 1 1
|
||||
github.com/xvzf/computeblade-agent/pkg/computeblade/ledengine.go:65.89,74.2 1 0
|
||||
github.com/xvzf/computeblade-agent/pkg/computeblade/ledengine.go:86.54,94.2 1 1
|
||||
github.com/xvzf/computeblade-agent/pkg/computeblade/ledengine.go:96.64,97.30 1 1
|
||||
github.com/xvzf/computeblade-agent/pkg/computeblade/ledengine.go:97.30,99.3 1 0
|
||||
github.com/xvzf/computeblade-agent/pkg/computeblade/ledengine.go:101.2,105.12 4 1
|
||||
github.com/xvzf/computeblade-agent/pkg/computeblade/ledengine.go:109.56,111.6 1 1
|
||||
github.com/xvzf/computeblade-agent/pkg/computeblade/ledengine.go:111.6,113.69 1 1
|
||||
github.com/xvzf/computeblade-agent/pkg/computeblade/ledengine.go:113.69,115.4 1 0
|
||||
github.com/xvzf/computeblade-agent/pkg/computeblade/ledengine.go:117.2,118.44 1 1
|
||||
github.com/xvzf/computeblade-agent/pkg/computeblade/ledengine.go:118.44,119.11 1 1
|
||||
github.com/xvzf/computeblade-agent/pkg/computeblade/ledengine.go:121.21,122.22 1 1
|
||||
github.com/xvzf/computeblade-agent/pkg/computeblade/ledengine.go:124.22,126.21 2 1
|
||||
github.com/xvzf/computeblade-agent/pkg/computeblade/ledengine.go:128.32,129.43 1 0
|
||||
github.com/xvzf/computeblade-agent/pkg/computeblade/ledengine.go:130.5,131.19 2 0
|
||||
github.com/xvzf/computeblade-agent/pkg/computeblade/ledengine.go:131.19,133.6 1 0
|
||||
github.com/xvzf/computeblade-agent/pkg/computeblade/ledengine.go:134.5,134.57 1 0
|
||||
github.com/xvzf/computeblade-agent/pkg/computeblade/ledengine.go:134.57,136.6 1 0
|
||||
@@ -1,4 +1,5 @@
|
||||
//go:build linux
|
||||
|
||||
package hal
|
||||
|
||||
import (
|
||||
@@ -176,7 +177,7 @@ func (bcm *bcm283x) handleEdgeButtonEdge(evt gpiod.LineEvent) {
|
||||
}
|
||||
|
||||
// GetEdgeButtonPressChan returns a channel that can be used to watch for edge button presses
|
||||
func (bcm *bcm283x) GetEdgeButtonPressChan(ctx context.Context) error {
|
||||
func (bcm *bcm283x) WaitForEdgeButtonPress(ctx context.Context) error {
|
||||
// Either wait for the context to be cancelled or the edge button to be pressed
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
65
pkg/hal/hal_bcm283x_simulated.go
Normal file
65
pkg/hal/hal_bcm283x_simulated.go
Normal file
@@ -0,0 +1,65 @@
|
||||
//go:build darwin
|
||||
|
||||
package hal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// fails if SimulatedHal does not implement ComputeBladeHal
|
||||
var _ ComputeBladeHal = &SimulatedHal{}
|
||||
|
||||
// ComputeBladeMock implements a mock for the ComputeBladeHal interface
|
||||
type SimulatedHal struct {
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
func NewCm4Hal(opts ComputeBladeHalOpts) (ComputeBladeHal, error) {
|
||||
logger := zap.L().Named("hal").Named("simulated-cm4")
|
||||
logger.Warn("Using simulated hal")
|
||||
|
||||
return &SimulatedHal{
|
||||
logger: logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m *SimulatedHal) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *SimulatedHal) SetFanSpeed(percent uint8) error {
|
||||
m.logger.Info("SetFanSpeed", zap.Uint8("percent", percent))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *SimulatedHal) GetFanRPM() (float64, error) {
|
||||
return 1337, nil
|
||||
}
|
||||
|
||||
func (m *SimulatedHal) SetStealthMode(enabled bool) error {
|
||||
m.logger.Info("SetStealthMode", zap.Bool("enabled", enabled))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *SimulatedHal) GetPowerStatus() (PowerStatus, error) {
|
||||
m.logger.Info("GetPowerStatus")
|
||||
return PowerPoe802at, nil
|
||||
}
|
||||
|
||||
func (m *SimulatedHal) WaitForEdgeButtonPress(ctx context.Context) error {
|
||||
m.logger.Info("WaitForEdgeButtonPress")
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-time.After(5 * time.Second):
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (m *SimulatedHal) SetLed(idx uint, color LedColor) error {
|
||||
m.logger.Info("SetLed", zap.Uint("idx", idx), zap.Any("color", color))
|
||||
return nil
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package computeblade
|
||||
package ledengine
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/xvzf/computeblade-agent/pkg/computeblade/hal"
|
||||
"github.com/xvzf/computeblade-agent/pkg/hal"
|
||||
"github.com/xvzf/computeblade-agent/pkg/util"
|
||||
)
|
||||
|
||||
@@ -36,6 +36,34 @@ type BlinkPattern struct {
|
||||
Delays []time.Duration
|
||||
}
|
||||
|
||||
func mapBrighnessUint8(brightness float64) uint8 {
|
||||
return uint8(255.0 * brightness)
|
||||
}
|
||||
|
||||
func LedColorPurple(brightness float64) hal.LedColor {
|
||||
return hal.LedColor{
|
||||
Red: mapBrighnessUint8(brightness),
|
||||
Green: 0,
|
||||
Blue: mapBrighnessUint8(brightness),
|
||||
}
|
||||
}
|
||||
|
||||
func LedColorRed(brightness float64) hal.LedColor {
|
||||
return hal.LedColor{
|
||||
Red: mapBrighnessUint8(brightness),
|
||||
Green: 0,
|
||||
Blue: 0,
|
||||
}
|
||||
}
|
||||
|
||||
func LedColorGreen(brightness float64) hal.LedColor {
|
||||
return hal.LedColor{
|
||||
Red: 0,
|
||||
Green: mapBrighnessUint8(brightness),
|
||||
Blue: 0,
|
||||
}
|
||||
}
|
||||
|
||||
// NewStaticPattern creates a new static pattern (no color changes)
|
||||
func NewStaticPattern(color hal.LedColor) BlinkPattern {
|
||||
return BlinkPattern{
|
||||
@@ -84,12 +112,16 @@ type LedEngineOpts struct {
|
||||
}
|
||||
|
||||
func NewLedEngine(opts LedEngineOpts) *ledEngineImpl {
|
||||
clock := opts.Clock
|
||||
if clock == nil {
|
||||
clock = util.RealClock{}
|
||||
}
|
||||
return &ledEngineImpl{
|
||||
ledIdx: opts.LedIdx,
|
||||
hal: opts.Hal,
|
||||
restart: make(chan struct{}), // restart channel controls cancelation of any pattern
|
||||
pattern: NewStaticPattern(hal.LedColor{}), // Turn off LEDs by default
|
||||
clock: opts.Clock,
|
||||
clock: clock,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package computeblade_test
|
||||
package ledengine_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -9,8 +9,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/xvzf/computeblade-agent/pkg/computeblade"
|
||||
"github.com/xvzf/computeblade-agent/pkg/computeblade/hal"
|
||||
"github.com/xvzf/computeblade-agent/pkg/hal"
|
||||
"github.com/xvzf/computeblade-agent/pkg/ledengine"
|
||||
"github.com/xvzf/computeblade-agent/pkg/util"
|
||||
)
|
||||
|
||||
@@ -23,12 +23,12 @@ func TestNewStaticPattern(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want computeblade.BlinkPattern
|
||||
want ledengine.BlinkPattern
|
||||
}{
|
||||
{
|
||||
"Green",
|
||||
args{hal.LedColor{Green: 255}},
|
||||
computeblade.BlinkPattern{
|
||||
ledengine.BlinkPattern{
|
||||
BaseColor: hal.LedColor{Green: 255},
|
||||
ActiveColor: hal.LedColor{Green: 255},
|
||||
Delays: []time.Duration{time.Hour},
|
||||
@@ -37,7 +37,7 @@ func TestNewStaticPattern(t *testing.T) {
|
||||
{
|
||||
"Red",
|
||||
args{hal.LedColor{Red: 255}},
|
||||
computeblade.BlinkPattern{
|
||||
ledengine.BlinkPattern{
|
||||
BaseColor: hal.LedColor{Red: 255},
|
||||
ActiveColor: hal.LedColor{Red: 255},
|
||||
Delays: []time.Duration{time.Hour},
|
||||
@@ -46,7 +46,7 @@ func TestNewStaticPattern(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := computeblade.NewStaticPattern(tt.args.color); !reflect.DeepEqual(got, tt.want) {
|
||||
if got := ledengine.NewStaticPattern(tt.args.color); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("NewStaticPattern() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
@@ -62,7 +62,7 @@ func TestNewBurstPattern(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want computeblade.BlinkPattern
|
||||
want ledengine.BlinkPattern
|
||||
}{
|
||||
{
|
||||
"Green <-> Red",
|
||||
@@ -70,7 +70,7 @@ func TestNewBurstPattern(t *testing.T) {
|
||||
baseColor: hal.LedColor{Green: 255},
|
||||
burstColor: hal.LedColor{Red: 255},
|
||||
},
|
||||
computeblade.BlinkPattern{
|
||||
ledengine.BlinkPattern{
|
||||
BaseColor: hal.LedColor{Green: 255},
|
||||
ActiveColor: hal.LedColor{Red: 255},
|
||||
Delays: []time.Duration{
|
||||
@@ -89,7 +89,7 @@ func TestNewBurstPattern(t *testing.T) {
|
||||
baseColor: hal.LedColor{Green: 255},
|
||||
burstColor: hal.LedColor{Green: 255},
|
||||
},
|
||||
computeblade.BlinkPattern{
|
||||
ledengine.BlinkPattern{
|
||||
BaseColor: hal.LedColor{Green: 255},
|
||||
ActiveColor: hal.LedColor{Green: 255},
|
||||
Delays: []time.Duration{
|
||||
@@ -105,7 +105,7 @@ func TestNewBurstPattern(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := computeblade.NewBurstPattern(tt.args.baseColor, tt.args.burstColor); !reflect.DeepEqual(got, tt.want) {
|
||||
if got := ledengine.NewBurstPattern(tt.args.baseColor, tt.args.burstColor); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("NewBurstPattern() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
@@ -120,17 +120,17 @@ func TestNewSlowBlinkPattern(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want computeblade.BlinkPattern
|
||||
want ledengine.BlinkPattern
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := computeblade.NewSlowBlinkPattern(tt.args.baseColor, tt.args.activeColor); !reflect.DeepEqual(
|
||||
if got := ledengine.NewSlowBlinkPattern(tt.args.baseColor, tt.args.activeColor); !reflect.DeepEqual(
|
||||
got,
|
||||
tt.want,
|
||||
) {
|
||||
t.Errorf("NewSlowcomputeblade.BlinkPattern() = %v, want %v", got, tt.want)
|
||||
t.Errorf("NewSlowledengine.BlinkPattern() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -138,7 +138,7 @@ func TestNewSlowBlinkPattern(t *testing.T) {
|
||||
|
||||
func TestNewLedEngine(t *testing.T) {
|
||||
t.Parallel()
|
||||
engine := computeblade.LedEngineOpts{
|
||||
engine := ledengine.LedEngineOpts{
|
||||
Clock: util.RealClock{},
|
||||
LedIdx: 0,
|
||||
Hal: &hal.ComputeBladeHalMock{},
|
||||
@@ -157,13 +157,13 @@ func Test_LedEngine_SetPattern_WhileRunning(t *testing.T) {
|
||||
cbMock.On("SetLed", uint(0), hal.LedColor{Green: 0, Blue: 0, Red: 0}).Once().Return(nil)
|
||||
cbMock.On("SetLed", uint(0), hal.LedColor{Green: 0, Blue: 0, Red: 255}).Once().Return(nil)
|
||||
|
||||
opts := computeblade.LedEngineOpts{
|
||||
opts := ledengine.LedEngineOpts{
|
||||
Hal: &cbMock,
|
||||
Clock: &clk,
|
||||
LedIdx: 0,
|
||||
}
|
||||
|
||||
engine := computeblade.NewLedEngine(opts)
|
||||
engine := ledengine.NewLedEngine(opts)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
@@ -177,11 +177,11 @@ func Test_LedEngine_SetPattern_WhileRunning(t *testing.T) {
|
||||
}()
|
||||
|
||||
// We want to change the pattern while the engine is running
|
||||
time.Sleep(5*time.Millisecond)
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
|
||||
// Set pattern
|
||||
t.Log("Setting pattern")
|
||||
engine.SetPattern(computeblade.NewStaticPattern(hal.LedColor{Red: 255}))
|
||||
engine.SetPattern(ledengine.NewStaticPattern(hal.LedColor{Red: 255}))
|
||||
|
||||
t.Log("Canceling context")
|
||||
cancel()
|
||||
@@ -201,16 +201,16 @@ func Test_LedEngine_SetPattern_BeforeRun(t *testing.T) {
|
||||
cbMock := hal.ComputeBladeHalMock{}
|
||||
cbMock.On("SetLed", uint(0), hal.LedColor{Green: 0, Blue: 0, Red: 255}).Once().Return(nil)
|
||||
|
||||
opts := computeblade.LedEngineOpts{
|
||||
opts := ledengine.LedEngineOpts{
|
||||
Hal: &cbMock,
|
||||
Clock: &clk,
|
||||
LedIdx: 0,
|
||||
}
|
||||
|
||||
engine := computeblade.NewLedEngine(opts)
|
||||
engine := ledengine.NewLedEngine(opts)
|
||||
// We want to change the pattern BEFORE the engine is started
|
||||
t.Log("Setting pattern")
|
||||
engine.SetPattern(computeblade.NewStaticPattern(hal.LedColor{Red: 255}))
|
||||
engine.SetPattern(ledengine.NewStaticPattern(hal.LedColor{Red: 255}))
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
|
||||
defer cancel()
|
||||
@@ -242,13 +242,13 @@ func Test_LedEngine_SetPattern_SetLedFailureInPattern(t *testing.T) {
|
||||
call0 := cbMock.On("SetLed", uint(0), hal.LedColor{Green: 0, Blue: 0, Red: 0}).Once().Return(nil)
|
||||
cbMock.On("SetLed", uint(0), hal.LedColor{Green: 0, Blue: 0, Red: 0}).Once().Return(errors.New("failure")).NotBefore(call0)
|
||||
|
||||
opts := computeblade.LedEngineOpts{
|
||||
opts := ledengine.LedEngineOpts{
|
||||
Hal: &cbMock,
|
||||
Clock: &clk,
|
||||
LedIdx: 0,
|
||||
}
|
||||
|
||||
engine := computeblade.NewLedEngine(opts)
|
||||
engine := ledengine.NewLedEngine(opts)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
|
||||
defer cancel()
|
||||
@@ -262,7 +262,7 @@ func Test_LedEngine_SetPattern_SetLedFailureInPattern(t *testing.T) {
|
||||
assert.Error(t, err)
|
||||
t.Log("LedEngine.Run() exited")
|
||||
}()
|
||||
time.Sleep(5*time.Millisecond)
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
|
||||
// Time tick -> SetLed() fails
|
||||
clkAfterChan <- time.Now()
|
||||
22
pkg/log/logger.go
Normal file
22
pkg/log/logger.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type logCtxKey int
|
||||
|
||||
func IntoContext(ctx context.Context, logger *zap.Logger) context.Context {
|
||||
return context.WithValue(ctx, logCtxKey(0), logger)
|
||||
}
|
||||
|
||||
func FromContext(ctx context.Context) *zap.Logger {
|
||||
val := ctx.Value(logCtxKey(0))
|
||||
if val != nil {
|
||||
return val.(*zap.Logger)
|
||||
}
|
||||
zap.L().Warn("No logger in context, passing default")
|
||||
return zap.L()
|
||||
}
|
||||
Reference in New Issue
Block a user