update emulator
This commit is contained in:
@@ -27,7 +27,7 @@ func SetupService(adapter *bluetooth.Adapter, state *device.DeviceState, logger
|
||||
|
||||
// Non-blocking write in goroutine to avoid blocking the BLE stack
|
||||
go func() {
|
||||
_, err := notifyChar.Write([]byte(response + "\n"))
|
||||
_, err := notifyChar.Write([]byte(response + "\r\n"))
|
||||
if err != nil {
|
||||
logger.Err("Failed to send response: %v", err)
|
||||
}
|
||||
|
||||
@@ -56,6 +56,17 @@ func (s *DeviceState) handleWriteCommand(cmd string) string {
|
||||
return "ERRORE: I comandi 'W' sono accettati solo in modalità programmazione."
|
||||
}
|
||||
|
||||
// Handle special WNC command
|
||||
if strings.HasPrefix(cmd, "WNC=") {
|
||||
valueStr := strings.TrimPrefix(cmd, "WNC=")
|
||||
value, err := strconv.Atoi(valueStr)
|
||||
if err != nil || (value != 0 && value != 1) {
|
||||
return "ERRORE: Valore fuori range per WNC"
|
||||
}
|
||||
s.SetNCParam(value == 1)
|
||||
return fmt.Sprintf("OK: Parametro WNC impostato e salvato: %d", value)
|
||||
}
|
||||
|
||||
// Parse command format: "W<id>=<value>"
|
||||
parts := strings.SplitN(cmd, "=", 2)
|
||||
if len(parts) != 2 {
|
||||
@@ -97,6 +108,15 @@ func (s *DeviceState) handleReadCommand(cmd string) string {
|
||||
return "ERRORE: I comandi 'W' sono accettati solo in modalità programmazione."
|
||||
}
|
||||
|
||||
// Handle special RNC command
|
||||
if cmd == "RNC" {
|
||||
value := 0
|
||||
if s.GetNCParam() {
|
||||
value = 1
|
||||
}
|
||||
return fmt.Sprintf("PARAM: WNC=%d", value)
|
||||
}
|
||||
|
||||
// Extract parameter ID
|
||||
idStr := strings.TrimPrefix(cmd, "R")
|
||||
id, err := strconv.Atoi(idStr)
|
||||
|
||||
@@ -19,6 +19,8 @@ type DeviceState struct {
|
||||
// W4=40 (Time Window), W20=128 (Gain)
|
||||
params map[int]int
|
||||
|
||||
ncParam bool // NC parameter: true = NO (1), false = NC (0), default is NO
|
||||
|
||||
sensorValue int // Default 2067 (ideal rest value)
|
||||
jitterRange int // Default 10 (means ±10)
|
||||
|
||||
@@ -41,6 +43,7 @@ func NewDeviceState() *DeviceState {
|
||||
12: 1000, // W12 - Max Pulse Duration (max)
|
||||
20: 127, // W20 - Gain (middle of 0-255)
|
||||
},
|
||||
ncParam: true, // Default NO mode (1)
|
||||
sensorValue: 2067,
|
||||
jitterRange: 10,
|
||||
connectedClients: 0,
|
||||
@@ -129,6 +132,7 @@ func (s *DeviceState) ResetToDefaults() {
|
||||
12: 1000,
|
||||
20: 127,
|
||||
}
|
||||
s.ncParam = true // Default NO mode (1)
|
||||
s.sensorValue = 2067
|
||||
s.jitterRange = 10
|
||||
}
|
||||
@@ -176,6 +180,20 @@ func (s *DeviceState) GetAlarmState() (bool, string) {
|
||||
return s.alarmActive, s.alarmType
|
||||
}
|
||||
|
||||
// GetNCParam returns the NC parameter value (true = NO, false = NC)
|
||||
func (s *DeviceState) GetNCParam() bool {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.ncParam
|
||||
}
|
||||
|
||||
// SetNCParam sets the NC parameter value (true = NO, false = NC)
|
||||
func (s *DeviceState) SetNCParam(value bool) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.ncParam = value
|
||||
}
|
||||
|
||||
// GetAllParams returns a copy of all parameters
|
||||
func (s *DeviceState) GetAllParams() map[int]int {
|
||||
s.mu.RLock()
|
||||
|
||||
@@ -34,9 +34,10 @@ const (
|
||||
|
||||
// Model is the main Bubble Tea model
|
||||
type Model struct {
|
||||
state *device.DeviceState
|
||||
logBuffer *LogBuffer
|
||||
notifyCh chan string // Channel to send alarm notifications
|
||||
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
|
||||
@@ -51,11 +52,12 @@ type Model struct {
|
||||
}
|
||||
|
||||
// NewModel creates a new TUI model
|
||||
func NewModel(state *device.DeviceState, logBuffer *LogBuffer, notifyCh chan string) 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,
|
||||
@@ -84,13 +86,19 @@ func NewModel(state *device.DeviceState, logBuffer *LogBuffer, notifyCh chan str
|
||||
Type: ControlButton,
|
||||
Action: func() {
|
||||
state.TriggerAlarm("TAMPER")
|
||||
logBuffer.Info("Tamper alarm triggered manually")
|
||||
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:
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
@@ -172,6 +172,13 @@ func (m Model) renderControlsPanel(width int) string {
|
||||
b.WriteString(fmt.Sprintf("%s %s %d\n", sliderLabelStyle.Render(name), bar, val))
|
||||
}
|
||||
|
||||
// NC parameter display
|
||||
ncValue := "True"
|
||||
if !m.state.GetNCParam() {
|
||||
ncValue = "False"
|
||||
}
|
||||
b.WriteString(fmt.Sprintf("%s %s\n", sliderLabelStyle.Render("WNC (Tru/Fal)"), ncValue))
|
||||
|
||||
// Status section
|
||||
b.WriteString("\n")
|
||||
b.WriteString(titleStyle.Render("Status"))
|
||||
|
||||
Reference in New Issue
Block a user