github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/plugins/triggers/types.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2016-2023, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package triggers
    20  
    21  import (
    22  	"sync"
    23  	"time"
    24  
    25  	"github.com/e154/smart-home/common"
    26  	"github.com/e154/smart-home/common/events"
    27  	m "github.com/e154/smart-home/models"
    28  )
    29  
    30  const (
    31  	// Name ...
    32  	Name = "triggers"
    33  	// TopicSystemStart ...
    34  	TopicSystemStart = "system/event/start" //todo move
    35  	// TopicSystemStop ...
    36  	TopicSystemStop = "system/event/stop" //todo move
    37  
    38  	Version = "0.0.1"
    39  )
    40  
    41  // IGetTrigger ...
    42  type IGetTrigger interface {
    43  	GetTrigger(string) (ITrigger, error)
    44  }
    45  
    46  // IRegistrar ...
    47  type IRegistrar interface {
    48  	RegisterTrigger(ITrigger) error
    49  	UnregisterTrigger(string) error
    50  	TriggerList() []string
    51  }
    52  
    53  // todo deAttach
    54  type ITrigger interface {
    55  	Name() string
    56  	AsyncAttach(wg *sync.WaitGroup)
    57  	Subscribe(Subscriber) error
    58  	Unsubscribe(Subscriber) error
    59  	FunctionName() string
    60  }
    61  
    62  // Subscriber ...
    63  type Subscriber struct {
    64  	EntityId *common.EntityId
    65  	Handler  interface{}
    66  	Payload  m.Attributes
    67  }
    68  
    69  const (
    70  	// CronOptionTrigger ...
    71  	CronOptionTrigger = "cron"
    72  )
    73  
    74  type SystemTriggerMessage struct {
    75  	Topic     string      `json:"topic"`
    76  	EventName string      `json:"event_name"`
    77  	Event     interface{} `json:"event"`
    78  }
    79  
    80  type EventEntityState struct {
    81  	EntityId    common.EntityId     `json:"entity_id"`
    82  	Value       interface{}         `json:"value"`
    83  	State       *events.EntityState `json:"state"`
    84  	Attributes  m.AttributeValue    `json:"attributes"`
    85  	Settings    m.AttributeValue    `json:"settings"`
    86  	LastChanged *time.Time          `json:"last_changed"`
    87  	LastUpdated *time.Time          `json:"last_updated"`
    88  }
    89  
    90  type TriggerStateChangedMessage struct {
    91  	StorageSave     bool             `json:"storage_save"`
    92  	DoNotSaveMetric bool             `json:"do_not_save_metric"`
    93  	PluginName      string           `json:"plugin_name"`
    94  	EntityId        common.EntityId  `json:"entity_id"`
    95  	OldState        EventEntityState `json:"old_state"`
    96  	NewState        EventEntityState `json:"new_state"`
    97  }