standardize device operation responses with codes and richer local simulations

This commit is contained in:
DBT
2026-02-24 16:27:37 +00:00
parent 04cbb22c57
commit bbc0690eaa
5 changed files with 32 additions and 10 deletions

View File

@@ -96,18 +96,18 @@ func actionHTTPPath(action string) string {
func (s *HTTPRelayTransport) Send(ctx context.Context, req Request) (Response, error) {
if s.Manager == nil {
return Response{OK: false, Node: req.Node, Action: req.Action, Error: "relay manager not configured"}, nil
return Response{OK: false, Code: "relay_unavailable", Node: req.Node, Action: req.Action, Error: "relay manager not configured"}, nil
}
if resp, ok := s.Manager.Invoke(req); ok {
return resp, nil
}
n, ok := s.Manager.Get(req.Node)
if !ok {
return Response{OK: false, Node: req.Node, Action: req.Action, Error: "node not found"}, nil
return Response{OK: false, Code: "node_not_found", Node: req.Node, Action: req.Action, Error: "node not found"}, nil
}
endpoint := strings.TrimRight(strings.TrimSpace(n.Endpoint), "/")
if endpoint == "" {
return Response{OK: false, Node: req.Node, Action: req.Action, Error: "node endpoint not configured"}, nil
return Response{OK: false, Code: "endpoint_missing", Node: req.Node, Action: req.Action, Error: "node endpoint not configured"}, nil
}
client := s.Client
if client == nil {
@@ -125,13 +125,13 @@ func (s *HTTPRelayTransport) Send(ctx context.Context, req Request) (Response, e
}
hresp, err := client.Do(hreq)
if err != nil {
return Response{OK: false, Node: req.Node, Action: req.Action, Error: err.Error()}, nil
return Response{OK: false, Code: "transport_error", Node: req.Node, Action: req.Action, Error: err.Error()}, nil
}
defer hresp.Body.Close()
payload, _ := io.ReadAll(io.LimitReader(hresp.Body, 1<<20))
var resp Response
if err := json.Unmarshal(payload, &resp); err != nil {
return Response{OK: false, Node: req.Node, Action: req.Action, Error: fmt.Sprintf("invalid node response: %s", strings.TrimSpace(string(payload)))}, nil
return Response{OK: false, Code: "invalid_response", Node: req.Node, Action: req.Action, Error: fmt.Sprintf("invalid node response: %s", strings.TrimSpace(string(payload)))}, nil
}
if strings.TrimSpace(resp.Node) == "" {
resp.Node = req.Node
@@ -139,5 +139,12 @@ func (s *HTTPRelayTransport) Send(ctx context.Context, req Request) (Response, e
if strings.TrimSpace(resp.Action) == "" {
resp.Action = req.Action
}
if strings.TrimSpace(resp.Code) == "" {
if resp.OK {
resp.Code = "ok"
} else {
resp.Code = "remote_error"
}
}
return resp, nil
}

View File

@@ -36,6 +36,7 @@ type Request struct {
// Envelope for node responses.
type Response struct {
OK bool `json:"ok"`
Code string `json:"code,omitempty"`
Error string `json:"error,omitempty"`
Node string `json:"node,omitempty"`
Action string `json:"action,omitempty"`