package state import ( "sync" "sync/atomic" "time" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/known/timestamppb" "hth/internal/clock" hthv1 "hth/internal/gen/hth/api/v1" "hth/internal/ids" ) type Store struct { mu sync.RWMutex deviceID string daemonInstanceID string epoch uint64 seq uint64 controlRev uint64 eventID atomic.Uint64 lifecycle hthv1.DeviceLifecycleState snap *hthv1.Snapshot clk clock.Clock } func NewStore(deviceID, daemonID string, clk clock.Clock) *Store { if clk == nil { clk = clock.Real{} } s := &Store{ deviceID: deviceID, daemonInstanceID: daemonID, epoch: 1, controlRev: 1, lifecycle: hthv1.DeviceLifecycleState_DEVICE_LIFECYCLE_STATE_STARTING, clk: clk, } s.snap = s.base(nil) return s } func (s *Store) DeviceID() string { return s.deviceID } func (s *Store) Epoch() uint64 { s.mu.RLock() defer s.mu.RUnlock() return s.epoch } func (s *Store) Seq() uint64 { s.mu.RLock() defer s.mu.RUnlock() return s.seq } func (s *Store) ControlRevision() uint64 { s.mu.RLock() defer s.mu.RUnlock() return s.controlRev } func (s *Store) Lifecycle() hthv1.DeviceLifecycleState { s.mu.RLock() defer s.mu.RUnlock() return s.lifecycle } func (s *Store) SetLifecycle(st hthv1.DeviceLifecycleState) { s.mu.Lock() s.lifecycle = st s.mu.Unlock() } func (s *Store) BumpEpoch() uint64 { s.mu.Lock() defer s.mu.Unlock() s.epoch++ s.lifecycle = hthv1.DeviceLifecycleState_DEVICE_LIFECYCLE_STATE_UNAVAILABLE return s.epoch } func (s *Store) BumpControlRevision() uint64 { s.mu.Lock() defer s.mu.Unlock() s.controlRev++ return s.controlRev } func (s *Store) NextEventID() uint64 { id := s.eventID.Add(1) if id == 0 { id = s.eventID.Add(1) } return id } func (s *Store) Get() *hthv1.Snapshot { s.mu.RLock() defer s.mu.RUnlock() if s.snap == nil { return nil } return proto.Clone(s.snap).(*hthv1.Snapshot) } func (s *Store) Commit(mutator func(*hthv1.Snapshot)) *hthv1.Snapshot { s.mu.Lock() defer s.mu.Unlock() s.seq++ eid := s.eventID.Add(1) snap := s.base(s.snap) snap.SnapshotSeq = s.seq snap.EventId = eid snap.DeviceEpoch = s.epoch snap.ControlRevision = s.controlRev snap.Lifecycle = s.lifecycle if mutator != nil { mutator(snap) } s.snap = snap return proto.Clone(snap).(*hthv1.Snapshot) } func (s *Store) ReplaceFromReplay(snap *hthv1.Snapshot) *hthv1.Snapshot { s.mu.Lock() defer s.mu.Unlock() cp := proto.Clone(snap).(*hthv1.Snapshot) cp.EventId = s.eventID.Add(1) s.seq++ cp.SnapshotSeq = s.seq s.snap = cp s.epoch = cp.DeviceEpoch s.controlRev = cp.ControlRevision s.lifecycle = cp.Lifecycle return proto.Clone(cp).(*hthv1.Snapshot) } func (s *Store) base(prev *hthv1.Snapshot) *hthv1.Snapshot { now := s.clk.Now() out := &hthv1.Snapshot{ DeviceId: s.deviceID, DaemonInstanceId: s.daemonInstanceID, DeviceEpoch: s.epoch, ControlRevision: s.controlRev, WallTime: timestamppb.New(now), MonotonicNs: s.clk.MonotonicNS(), Lifecycle: s.lifecycle, } if prev != nil { out.Measurements = cloneMeas(prev.Measurements) out.ContinuousTargets = cloneCommanded(prev.ContinuousTargets) out.LastRequested = cloneRecord(prev.LastRequested) out.LastAccepted = cloneRecord(prev.LastAccepted) out.LastDispatched = cloneRecord(prev.LastDispatched) out.DeviceConfirmation = prev.DeviceConfirmation out.Lease = cloneLease(prev.Lease) out.DroppedTelemetryCount = prev.DroppedTelemetryCount out.ActiveFaults = cloneFaults(prev.ActiveFaults) out.CaptureWindow = cloneWindow(prev.CaptureWindow) out.TransportState = prev.TransportState out.DeviceReadiness = prev.DeviceReadiness out.SafetyControlMode = prev.SafetyControlMode } return out } func cloneMeas(in []*hthv1.Measurement) []*hthv1.Measurement { out := make([]*hthv1.Measurement, 0, len(in)) for _, m := range in { out = append(out, proto.Clone(m).(*hthv1.Measurement)) } return out } func cloneCommanded(in []*hthv1.CommandedActuator) []*hthv1.CommandedActuator { if in == nil { return nil } out := make([]*hthv1.CommandedActuator, 0, len(in)) for _, a := range in { if a == nil { out = append(out, nil) continue } out = append(out, proto.Clone(a).(*hthv1.CommandedActuator)) } return out } func cloneRecord(in *hthv1.ActionRecord) *hthv1.ActionRecord { if in == nil { return nil } return proto.Clone(in).(*hthv1.ActionRecord) } func cloneLease(in *hthv1.LeaseSummary) *hthv1.LeaseSummary { if in == nil { return nil } return proto.Clone(in).(*hthv1.LeaseSummary) } func cloneFaults(in []*hthv1.Fault) []*hthv1.Fault { if in == nil { return nil } out := make([]*hthv1.Fault, 0, len(in)) for _, f := range in { if f == nil { out = append(out, nil) continue } out = append(out, proto.Clone(f).(*hthv1.Fault)) } return out } func cloneWindow(in *hthv1.TimeWindow) *hthv1.TimeWindow { if in == nil { return nil } return proto.Clone(in).(*hthv1.TimeWindow) } func MissingTemps(n int, now time.Time, reason string) []*hthv1.Measurement { out := make([]*hthv1.Measurement, 0, n) for i := 0; i < n; i++ { out = append(out, &hthv1.Measurement{ Id: ids.TempChannel(i), Unit: "degC", Quality: hthv1.DataQuality_DATA_QUALITY_MISSING, QualityReason: reason, HostTime: timestamppb.New(now), SourceTimestampAvailable: false, }) } return out }