tui simulation
This commit is contained in:
125
internal/tui/model.go
Normal file
125
internal/tui/model.go
Normal file
@@ -0,0 +1,125 @@
|
||||
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
|
||||
|
||||
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) Model {
|
||||
m := Model{
|
||||
state: state,
|
||||
logBuffer: logBuffer,
|
||||
notifyCh: notifyCh,
|
||||
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 manually")
|
||||
if notifyCh != nil {
|
||||
select {
|
||||
case notifyCh <- "ALARM: TAMPER":
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user