13 type thermostatState struct {
15 queue <-chan []driver.Command
17 publisher *driver.Publisher
21 func (s *thermostatState) descriptor(ctx context.Context) error {
22 err := s.publisher.Descriptor(ctx, s.settings.DeviceID, protocol.Metadata{}, protocol.DescriptorPayload{
23 Manufacturer: "alamo",
26 Capabilities: []protocol.Capability{{
28 Type: "floor-heating",
30 AcceptsCommands: true,
34 return fmt.Errorf("publish descriptor: %w", err)
40 func (s *thermostatState) record(ctx context.Context, state State, observedAt time.Time, causationID string) error {
42 metadata := protocol.Metadata{
44 CausationID: causationID,
47 err := s.publisher.Availability(ctx, s.settings.DeviceID, protocol.Metadata{SentAt: &observedAt}, protocol.AvailabilityPayload{Status: "online"})
49 return fmt.Errorf("publish thermostat contact: %w", err)
52 value, err := json.Marshal(newReportedControl(state))
54 return fmt.Errorf("encode thermostat feedback: %w", err)
57 err = s.publisher.Reported(ctx, s.settings.DeviceID, "control", metadata, protocol.ValuePayload{Value: value})
59 return fmt.Errorf("publish thermostat feedback: %w", err)
65 func (s *thermostatState) miss(ctx context.Context) error {
66 s.missed = min(s.missed+1, 3)
71 err := s.publisher.Availability(ctx, s.settings.DeviceID, protocol.Metadata{}, protocol.AvailabilityPayload{Status: "offline"})
73 return fmt.Errorf("publish offline: %w", err)
79 func (s *thermostatState) result(ctx context.Context, command driver.Command, payload protocol.ResultPayload) error {
80 record, err := s.ledger.Remember(command.Metadata.MessageID, payload)
82 return fmt.Errorf("remember thermostat result: %w", err)
85 err = s.publisher.Result(ctx, s.settings.DeviceID, command.Metadata.MessageID, record.Metadata, record.Payload)
87 return fmt.Errorf("publish thermostat result: %w", err)