Files
BLE_emulator/internal/tui/model.go
2026-02-19 11:43:56 +01:00

134 lines
2.7 KiB
Go

package tui
import (
"ble_simulator/internal/device"
)
// ControlType represents the type of control
type ControlType int
const (
ControlButton ControlType = iota
ControlSlider
)
// Control represents an interactive control in the TUI
type Control struct {
Name string
Type ControlType
Action func() // For buttons
GetValue func() int // For sliders
SetValue func(int) // For sliders
Min int // For sliders
Max int // For sliders
Step int // For sliders
}
// FocusPanel represents which panel is focused
type FocusPanel int
const (
PanelControls FocusPanel = iota
PanelLogs
)
// Model is the main Bubble Tea model
type Model struct {
state *device.DeviceState
logBuffer *LogBuffer
notifyCh chan string // Channel to send alarm notifications
disconnectCh chan struct{} // Channel to trigger disconnect all clients
controls []Control
focusedCtrl int
focusedPanel FocusPanel
logOffset int // Scroll offset for log view
width int
height int
quitting bool
}
// NewModel creates a new TUI model
func NewModel(state *device.DeviceState, logBuffer *LogBuffer, notifyCh chan string, disconnectCh chan struct{}) Model {
m := Model{
state: state,
logBuffer: logBuffer,
notifyCh: notifyCh,
disconnectCh: disconnectCh,
focusedCtrl: 0,
focusedPanel: PanelControls,
logOffset: 0,
width: 80,
height: 24,
}
// Setup controls
m.controls = []Control{
{
Name: "Trigger Alarm",
Type: ControlButton,
Action: func() {
state.TriggerAlarm("TRIGGERED")
logBuffer.Info("Alarm triggered manually")
if notifyCh != nil {
select {
case notifyCh <- "ALARM: TRIGGERED":
default:
}
}
},
},
{
Name: "Trigger Tamper",
Type: ControlButton,
Action: func() {
state.TriggerAlarm("TAMPER")
logBuffer.Info("Tamper alarm triggered - disconnecting all clients")
if notifyCh != nil {
select {
case notifyCh <- "ALARM: TAMPER":
default:
}
}
if disconnectCh != nil {
select {
case disconnectCh <- struct{}{}:
default:
}
}
},
},
{
Name: "Reset Alarm",
Type: ControlButton,
Action: func() {
state.ResetAlarm()
logBuffer.Info("Alarm reset")
},
},
{
Name: "Sensor Value",
Type: ControlSlider,
GetValue: state.GetSensorValue,
SetValue: state.SetSensorValue,
Min: 0,
Max: 4095,
Step: 50,
},
{
Name: "Jitter Range",
Type: ControlSlider,
GetValue: state.GetJitterRange,
SetValue: state.SetJitterRange,
Min: 0,
Max: 100,
Step: 5,
},
}
return m
}