UART RESTART (client_id 0 = local reboot, >0 = unicast); dashboard and CLI hooks; delayed esp_restart after response. Co-authored-by: Cursor <cursoragent@cursor.com>
62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
"powerpod/gotool/pb"
|
|
)
|
|
|
|
func runRestart(sp *serialPort, args []string) error {
|
|
fs := flag.NewFlagSet("restart", flag.ExitOnError)
|
|
clientID := fs.Uint("client", 0, "0=master, >0=ESP-NOW unicast to slave id")
|
|
if err := fs.Parse(args); err != nil {
|
|
return err
|
|
}
|
|
return runRestartClient(sp, uint32(*clientID))
|
|
}
|
|
|
|
func runRestartClient(sp *serialPort, clientID uint32) error {
|
|
resp, err := sp.restart(clientID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !resp.GetSuccess() {
|
|
return fmt.Errorf("restart rejected (client_id=%d)", resp.GetClientId())
|
|
}
|
|
if clientID == 0 {
|
|
fmt.Println("restart scheduled on master")
|
|
} else {
|
|
fmt.Printf("restart sent to slave %d\n", clientID)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *serialPort) restart(clientID uint32) (*pb.RestartResponse, error) {
|
|
msg := &pb.UartMessage{
|
|
Type: pb.MessageType_RESTART,
|
|
Payload: &pb.UartMessage_RestartRequest{
|
|
RestartRequest: &pb.RestartRequest{ClientId: clientID},
|
|
},
|
|
}
|
|
body, err := proto.Marshal(msg)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("encode: %w", err)
|
|
}
|
|
payload := append([]byte{byte(pb.MessageType_RESTART)}, body...)
|
|
respPayload, err := s.exchangePayload(payload, "RESTART")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var respMsg pb.UartMessage
|
|
if err := proto.Unmarshal(respPayload[1:], &respMsg); err != nil {
|
|
return nil, fmt.Errorf("decode: %w", err)
|
|
}
|
|
r := respMsg.GetRestartResponse()
|
|
if r == nil {
|
|
return nil, fmt.Errorf("missing restart_response")
|
|
}
|
|
return r, nil
|
|
}
|