This commit is contained in:
2026-01-11 17:41:40 +01:00
parent cd93f0ea67
commit 414d802d37
2 changed files with 66 additions and 11 deletions

View File

@@ -15,9 +15,10 @@ type Client struct {
host string
port int
conn net.Conn
reader *bufio.Reader
connMu sync.Mutex
conn net.Conn
reader *bufio.Reader
connMu sync.Mutex // For connection management
writeMu sync.Mutex // For writing to connection (separate from reads)
interlockID string
interlockName string
@@ -144,10 +145,15 @@ func (c *Client) getNextSeq() int {
}
func (c *Client) sendCommand(cmd string) (string, error) {
c.connMu.Lock()
defer c.connMu.Unlock()
// Use writeMu instead of connMu to avoid blocking on messageLoop reads
c.writeMu.Lock()
defer c.writeMu.Unlock()
if c.conn == nil {
c.connMu.Lock()
conn := c.conn
c.connMu.Unlock()
if conn == nil {
return "", fmt.Errorf("not connected")
}
@@ -156,10 +162,12 @@ func (c *Client) sendCommand(cmd string) (string, error) {
log.Printf("FlexRadio TX: %s", strings.TrimSpace(fullCmd))
_, err := c.conn.Write([]byte(fullCmd))
_, err := conn.Write([]byte(fullCmd))
if err != nil {
c.connMu.Lock()
c.conn = nil
c.reader = nil
c.connMu.Unlock()
return "", fmt.Errorf("failed to send command: %w", err)
}
@@ -215,6 +223,7 @@ func (c *Client) messageLoop() {
continue
}
log.Printf("FlexRadio RX: %s", line)
c.handleMessage(line)
}
@@ -325,7 +334,7 @@ func (c *Client) handleStatus(msg string) {
}
}
func (c *Client) handleInterlockState(state string, _ map[string]string) {
func (c *Client) handleInterlockState(state string, statusMap map[string]string) {
log.Printf("FlexRadio: Interlock state changed to: %s", state)
c.statusMu.Lock()
@@ -410,3 +419,32 @@ func (c *Client) GetStatus() (*Status, error) {
return &status, nil
}
// ForceInterlockState proactively sends ready/not_ready to the radio
// This is used when external conditions change (e.g., antenna motors start/stop)
func (c *Client) ForceInterlockState(allowed bool) {
c.interlockMu.RLock()
interlockID := c.interlockID
c.interlockMu.RUnlock()
if interlockID == "" {
log.Println("FlexRadio: No interlock ID, cannot force state")
return
}
if allowed {
log.Println("FlexRadio: PROACTIVE - Sending ready (motors stopped)")
c.sendCommand(fmt.Sprintf("interlock ready %s", interlockID))
// Update state immediately for UI
c.statusMu.Lock()
c.lastStatus.InterlockState = InterlockStateReady
c.statusMu.Unlock()
} else {
log.Println("FlexRadio: PROACTIVE - Sending not_ready (motors moving)")
c.sendCommand(fmt.Sprintf("interlock not_ready %s", interlockID))
// Update state immediately for UI
c.statusMu.Lock()
c.lastStatus.InterlockState = InterlockStateNotReady
c.statusMu.Unlock()
}
}