github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/aagent/watchers/timerwatcher/state_notification.go (about)

     1  // Copyright (c) 2020-2021, R.I. Pienaar and the Choria Project contributors
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package timerwatcher
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  	"time"
    11  
    12  	cloudevents "github.com/cloudevents/sdk-go/v2"
    13  
    14  	"github.com/choria-io/go-choria/aagent/watchers/event"
    15  )
    16  
    17  // StateNotification describes the current state of the watcher
    18  // described by io.choria.machine.timer.exec.v1.state
    19  type StateNotification struct {
    20  	event.Event
    21  
    22  	State string        `json:"state"`
    23  	Timer time.Duration `json:"timer"`
    24  }
    25  
    26  // JSON creates a JSON representation of the notification
    27  func (s *StateNotification) JSON() ([]byte, error) {
    28  	return json.Marshal(s.CloudEvent())
    29  }
    30  
    31  // CloudEvent creates a CloudEvent from the state notification
    32  func (s *StateNotification) CloudEvent() cloudevents.Event {
    33  	return s.Event.CloudEvent(s)
    34  }
    35  
    36  // String is a string representation of the notification suitable for printing
    37  func (s *StateNotification) String() string {
    38  	return fmt.Sprintf("%s timer %s#%s state: %s with %v timer", s.Identity, s.Machine, s.Name, s.State, s.Timer)
    39  }