Persist rover observation configuration and calibrate Insta360 panorama
This commit is contained in:
@@ -41,26 +41,29 @@ type PairState struct {
|
||||
Revocations []CoreBinding `json:"revocations,omitempty"`
|
||||
}
|
||||
type Pairing struct {
|
||||
Monitor *Monitor
|
||||
Sensors *Sensors
|
||||
DeviceEnrollment *DeviceEnrollment
|
||||
mu sync.Mutex
|
||||
path string
|
||||
store *Store
|
||||
state PairState
|
||||
now func() time.Time
|
||||
inventory func() Inventory
|
||||
version string
|
||||
lastSeen int64
|
||||
connection string
|
||||
listenError string
|
||||
failureWindow int64
|
||||
failures int
|
||||
clients map[string]*http.Client
|
||||
RoverConfiguration *RoverConfigurationStore
|
||||
Monitor *Monitor
|
||||
Sensors *Sensors
|
||||
DeviceEnrollment *DeviceEnrollment
|
||||
mu sync.Mutex
|
||||
path string
|
||||
store *Store
|
||||
state PairState
|
||||
now func() time.Time
|
||||
inventory func() Inventory
|
||||
version string
|
||||
lastSeen int64
|
||||
connection string
|
||||
listenError string
|
||||
failureWindow int64
|
||||
failures int
|
||||
clients map[string]*http.Client
|
||||
}
|
||||
|
||||
func OpenPairing(store *Store, dir, version string, inventory func() Inventory) (*Pairing, error) {
|
||||
p := &Pairing{store: store, path: filepath.Join(dir, "core-binding.json"), now: time.Now, inventory: inventory, version: version, connection: "offline", clients: make(map[string]*http.Client), state: PairState{Schema: PairSchema, Phase: "unpaired"}}
|
||||
id, name := store.Public()
|
||||
p.RoverConfiguration = OpenRoverConfiguration(dir, id, name)
|
||||
data, e := os.ReadFile(p.path)
|
||||
if os.IsNotExist(e) {
|
||||
return p, nil
|
||||
|
||||
@@ -301,6 +301,9 @@ func (p *Pairing) channel(ctx context.Context) {
|
||||
if binding != nil {
|
||||
id, name := p.store.Public()
|
||||
payload := map[string]any{"schema": PairSchema, "binding_id": binding.BindingID, "node_id": id, "name": name, "version": p.version, "execution_binding": map[string]string{"node_id": id, "agent_instance_id": instance, "platform": "linux"}, "host": p.inventory(), "devices": []any{}}
|
||||
if p.RoverConfiguration != nil {
|
||||
payload["rover_configuration"] = p.RoverConfiguration.Snapshot()
|
||||
}
|
||||
payload["core_endpoint"] = binding.Endpoint
|
||||
payload["endpoint_revision"] = binding.EndpointRevision
|
||||
if p.Sensors != nil {
|
||||
@@ -320,9 +323,13 @@ func (p *Pairing) channel(ctx context.Context) {
|
||||
payload["monitor"] = p.Monitor.Snapshot()
|
||||
}
|
||||
result, status, e := p.send(ctx, *binding, "/v1/node/heartbeat", payload)
|
||||
var configurationChange json.RawMessage
|
||||
p.mu.Lock()
|
||||
if p.state.Phase == "paired" && p.state.Binding != nil && p.state.Binding.BindingID == binding.BindingID && p.state.Binding.Endpoint == binding.Endpoint && p.state.Binding.EndpointRevision == binding.EndpointRevision {
|
||||
if e == nil && status == 200 {
|
||||
if p.RoverConfiguration != nil {
|
||||
configurationChange = result["rover_configuration_change"]
|
||||
}
|
||||
if p.Monitor != nil {
|
||||
p.Monitor.Acknowledge(result["monitor_ack"])
|
||||
}
|
||||
@@ -373,6 +380,10 @@ func (p *Pairing) channel(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
p.mu.Unlock()
|
||||
// Durable preference IO must not hold the pairing/control mutex.
|
||||
if p.RoverConfiguration != nil {
|
||||
p.RoverConfiguration.Apply(configurationChange)
|
||||
}
|
||||
}
|
||||
for _, b := range revocations {
|
||||
id, _ := p.store.Public()
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
package node
|
||||
|
||||
// RoverConfiguration is the onboard authority for portable rover preferences.
|
||||
// Trust keys and live control state deliberately remain outside this document.
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const roverConfigurationSchema = "missioncore.rover-configuration/v1"
|
||||
const roverConfigurationLimit = 512 * 1024
|
||||
|
||||
type RoverConfiguration struct {
|
||||
Schema string `json:"schema"`
|
||||
NodeID string `json:"node_id"`
|
||||
Name string `json:"name"`
|
||||
Revision int64 `json:"revision"`
|
||||
Observation json.RawMessage `json:"observation"`
|
||||
LastChange string `json:"last_change,omitempty"`
|
||||
}
|
||||
type RoverConfigurationChange struct {
|
||||
ID string `json:"id"`
|
||||
ExpectedRevision int64 `json:"expected_revision"`
|
||||
ExpiresAt int64 `json:"expires_at"`
|
||||
Name string `json:"name"`
|
||||
Observation json.RawMessage `json:"observation"`
|
||||
}
|
||||
type RoverConfigurationResult struct {
|
||||
ID string `json:"id"`
|
||||
State string `json:"state"`
|
||||
}
|
||||
type RoverConfigurationStore struct {
|
||||
mu sync.Mutex
|
||||
path string
|
||||
nodeID string
|
||||
name string
|
||||
result *RoverConfigurationResult
|
||||
}
|
||||
|
||||
func OpenRoverConfiguration(dir, nodeID, name string) *RoverConfigurationStore {
|
||||
return &RoverConfigurationStore{path: filepath.Join(dir, "Rover Config.json"), nodeID: nodeID, name: name}
|
||||
}
|
||||
func decodeRoverJSON(raw []byte, value any) error {
|
||||
if len(raw) > roverConfigurationLimit {
|
||||
return errors.New("configuration too large")
|
||||
}
|
||||
d := json.NewDecoder(bytes.NewReader(raw))
|
||||
d.DisallowUnknownFields()
|
||||
if err := d.Decode(value); err != nil {
|
||||
return err
|
||||
}
|
||||
if d.Decode(new(any)) != io.EOF {
|
||||
return errors.New("trailing configuration data")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func validRoverObservation(raw json.RawMessage) error {
|
||||
var document struct {
|
||||
Layouts map[string]json.RawMessage `json:"layouts"`
|
||||
Cameras map[string]struct {
|
||||
Rotations map[string]int `json:"rotations"`
|
||||
PanoramaYaw float64 `json:"panoramaYaw"`
|
||||
} `json:"cameras"`
|
||||
}
|
||||
if err := decodeRoverJSON(raw, &document); err != nil {
|
||||
return err
|
||||
}
|
||||
if document.Layouts == nil || document.Cameras == nil || len(document.Cameras) > 128 {
|
||||
return errors.New("invalid observation document")
|
||||
}
|
||||
var version int
|
||||
if json.Unmarshal(document.Layouts["version"], &version) != nil || version != 1 {
|
||||
return errors.New("invalid layout version")
|
||||
}
|
||||
for id, camera := range document.Cameras {
|
||||
if len(id) == 0 || len(id) > 512 || camera.Rotations == nil || camera.PanoramaYaw < 0 || camera.PanoramaYaw >= 360 {
|
||||
return errors.New("invalid camera")
|
||||
}
|
||||
for lens, rotation := range camera.Rotations {
|
||||
if (lens != "left" && lens != "right") || (rotation != 0 && rotation != 90 && rotation != 180 && rotation != 270) {
|
||||
return errors.New("invalid lens rotation")
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (s *RoverConfigurationStore) read() (RoverConfiguration, error) {
|
||||
var value RoverConfiguration
|
||||
info, err := os.Lstat(s.path)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
value = RoverConfiguration{Schema: roverConfigurationSchema, NodeID: s.nodeID, Name: s.name, Observation: json.RawMessage("null")}
|
||||
// Even the initial configuration exists on the board before Core sees it.
|
||||
err = savePrivateJSON(s.path, value)
|
||||
return value, err
|
||||
}
|
||||
if err != nil {
|
||||
return value, err
|
||||
}
|
||||
if !info.Mode().IsRegular() || info.Mode().Perm()&0077 != 0 || info.Size() > roverConfigurationLimit {
|
||||
return value, errors.New("invalid rover configuration file")
|
||||
}
|
||||
raw, err := os.ReadFile(s.path)
|
||||
if err != nil {
|
||||
return value, err
|
||||
}
|
||||
if err = decodeRoverJSON(raw, &value); err != nil {
|
||||
return value, err
|
||||
}
|
||||
if value.Schema != roverConfigurationSchema || value.NodeID != s.nodeID || value.Revision < 0 || value.Revision >= 1<<53-1 || len(value.Name) > 256 {
|
||||
return value, errors.New("invalid rover configuration identity")
|
||||
}
|
||||
if string(value.Observation) != "null" {
|
||||
if err = validRoverObservation(value.Observation); err != nil {
|
||||
return value, err
|
||||
}
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
func (s *RoverConfigurationStore) Snapshot() map[string]any {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
value, err := s.read()
|
||||
if err != nil {
|
||||
return map[string]any{"state": "unavailable"}
|
||||
}
|
||||
return map[string]any{"state": "ready", "configuration": value, "result": s.result}
|
||||
}
|
||||
func (s *RoverConfigurationStore) Apply(raw json.RawMessage) {
|
||||
if len(raw) == 0 || string(raw) == "null" {
|
||||
return
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
var change RoverConfigurationChange
|
||||
if decodeRoverJSON(raw, &change) != nil || len(change.ID) != 32 {
|
||||
return
|
||||
}
|
||||
result := &RoverConfigurationResult{ID: change.ID, State: "invalid"}
|
||||
s.result = result
|
||||
if change.ExpectedRevision < 0 || change.ExpectedRevision >= 1<<53-2 || len(change.Name) > 256 || validRoverObservation(change.Observation) != nil {
|
||||
return
|
||||
}
|
||||
before, err := s.read()
|
||||
if err != nil {
|
||||
result.State = "unavailable"
|
||||
return
|
||||
}
|
||||
if before.LastChange == change.ID {
|
||||
var a, b any
|
||||
_ = json.Unmarshal(before.Observation, &a)
|
||||
_ = json.Unmarshal(change.Observation, &b)
|
||||
x, _ := json.Marshal(a)
|
||||
y, _ := json.Marshal(b)
|
||||
if before.Revision == change.ExpectedRevision+1 && bytes.Equal(x, y) && before.Name == change.Name {
|
||||
result.State = "complete"
|
||||
}
|
||||
return
|
||||
}
|
||||
now := time.Now().Unix()
|
||||
if change.ExpiresAt < now || change.ExpiresAt > now+60 {
|
||||
result.State = "expired"
|
||||
return
|
||||
}
|
||||
if before.Revision != change.ExpectedRevision {
|
||||
result.State = "conflict"
|
||||
return
|
||||
}
|
||||
next := before
|
||||
next.Revision++
|
||||
next.Name = change.Name
|
||||
next.Observation = change.Observation
|
||||
next.LastChange = change.ID
|
||||
if savePrivateJSON(s.path, next) != nil {
|
||||
result.State = "unavailable"
|
||||
return
|
||||
}
|
||||
result.State = "complete"
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package node
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const testObservation = `{"layouts":{"version":1,"presets":[]},"cameras":{"camera-a":{"rotations":{"left":90,"right":270},"panoramaYaw":45}}}`
|
||||
|
||||
func configChange(id string, revision int64) json.RawMessage {
|
||||
raw, _ := json.Marshal(RoverConfigurationChange{ID: id, ExpectedRevision: revision, ExpiresAt: time.Now().Unix() + 30, Name: "Rover 006", Observation: json.RawMessage(testObservation)})
|
||||
return raw
|
||||
}
|
||||
func TestRoverConfigurationDurableAckAndRestart(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
s := OpenRoverConfiguration(dir, "node-a", "Rover")
|
||||
initial := s.Snapshot()
|
||||
if initial["state"] != "ready" {
|
||||
t.Fatal(initial)
|
||||
}
|
||||
info, err := os.Stat(filepath.Join(dir, "Rover Config.json"))
|
||||
if err != nil || info.Mode().Perm() != 0600 {
|
||||
t.Fatal(info, err)
|
||||
}
|
||||
id := "11111111111111111111111111111111"
|
||||
s.Apply(configChange(id, 0))
|
||||
if s.result.State != "complete" {
|
||||
t.Fatal(s.result)
|
||||
}
|
||||
restarted := OpenRoverConfiguration(dir, "node-a", "Rover")
|
||||
value, err := restarted.read()
|
||||
if err != nil || value.Revision != 1 || value.LastChange != id {
|
||||
t.Fatal(value, err)
|
||||
}
|
||||
restarted.Apply(configChange(id, 0))
|
||||
if restarted.result.State != "complete" {
|
||||
t.Fatal(restarted.result)
|
||||
}
|
||||
restarted.Apply(configChange("22222222222222222222222222222222", 0))
|
||||
if restarted.result.State != "conflict" {
|
||||
t.Fatal(restarted.result)
|
||||
}
|
||||
value, _ = restarted.read()
|
||||
if value.Revision != 1 {
|
||||
t.Fatal(value)
|
||||
}
|
||||
}
|
||||
func TestRoverConfigurationRejectsInvalidExpiredAndForeign(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
s := OpenRoverConfiguration(dir, "node-a", "Rover")
|
||||
s.Snapshot()
|
||||
var change RoverConfigurationChange
|
||||
json.Unmarshal(configChange("11111111111111111111111111111111", 0), &change)
|
||||
change.ExpiresAt = time.Now().Unix() - 1
|
||||
raw, _ := json.Marshal(change)
|
||||
s.Apply(raw)
|
||||
if s.result.State != "expired" {
|
||||
t.Fatal(s.result)
|
||||
}
|
||||
foreign := OpenRoverConfiguration(dir, "node-b", "Rover")
|
||||
if foreign.Snapshot()["state"] != "unavailable" {
|
||||
t.Fatal("foreign node accepted")
|
||||
}
|
||||
for _, raw := range []string{`null`, `{}`, `{"layouts":{"version":1},"cameras":{"a":{"rotations":{"left":45},"panoramaYaw":0}}}`, `{"layouts":{"version":1},"cameras":{"a":{"rotations":{"left":true},"panoramaYaw":0}}}`} {
|
||||
if validRoverObservation(json.RawMessage(raw)) == nil {
|
||||
t.Fatal("invalid accepted", raw)
|
||||
}
|
||||
}
|
||||
if err := os.WriteFile(s.path, []byte("{broken"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.Apply(configChange("33333333333333333333333333333333", 0))
|
||||
if s.result.State != "unavailable" {
|
||||
t.Fatal(s.result)
|
||||
}
|
||||
content, _ := os.ReadFile(s.path)
|
||||
if string(content) != "{broken" {
|
||||
t.Fatal("corrupt file replaced")
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import sys
|
||||
from build_deb import build, VERSION, BRAND_SHA256
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
DG_COMMIT = "8dd9190573d6616024ef01b9b34bf90b72960f44"
|
||||
DG_COMMIT = "a6e6c8f2d9c3de8d98b60c30da6a70d9538c1ef2"
|
||||
|
||||
|
||||
def guideline_sources():
|
||||
|
||||
@@ -11,8 +11,8 @@ import sys
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
BINARY_VERSION = "0.8.45"
|
||||
VERSION = "0.8.45-1"
|
||||
BINARY_VERSION = "0.8.46"
|
||||
VERSION = "0.8.46-1"
|
||||
sys.path.insert(0, str(ROOT.parents[1] / "scripts/packaging"))
|
||||
from debian import package
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ from pathlib import Path
|
||||
NODE = Path(__file__).resolve().parents[1]
|
||||
REPO = NODE.parents[1]
|
||||
DG = REPO.parent / "NODEDC_DESIGN_GUIDELINE"
|
||||
DG_COMMIT = "8dd9190573d6616024ef01b9b34bf90b72960f44"
|
||||
DG_COMMIT = "a6e6c8f2d9c3de8d98b60c30da6a70d9538c1ef2"
|
||||
|
||||
|
||||
def files(root):
|
||||
|
||||
Reference in New Issue
Block a user