github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/plugins/triggers/system_trigger.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  	"github.com/e154/smart-home/common/events"
    23  	"go.uber.org/atomic"
    24  	"sync"
    25  
    26  	"github.com/e154/smart-home/system/bus"
    27  )
    28  
    29  const (
    30  	// TopicSystem ...
    31  	TopicSystem = "system/#"
    32  	// SystemName ...
    33  	SystemName = "system"
    34  	// SystemFunctionName ...
    35  	SystemFunctionName = "automationTriggerSystem"
    36  )
    37  
    38  var _ ITrigger = (*SystemTrigger)(nil)
    39  
    40  // SystemTrigger ...
    41  type SystemTrigger struct {
    42  	baseTrigger
    43  	counter *atomic.Int32
    44  }
    45  
    46  // NewSystemTrigger ...
    47  func NewSystemTrigger(eventBus bus.Bus) ITrigger {
    48  	return &SystemTrigger{
    49  		baseTrigger: baseTrigger{
    50  			eventBus:     eventBus,
    51  			msgQueue:     bus.NewBus(),
    52  			functionName: SystemFunctionName,
    53  			name:         SystemName,
    54  		},
    55  		counter: atomic.NewInt32(0),
    56  	}
    57  }
    58  
    59  // AsyncAttach ...
    60  func (t *SystemTrigger) AsyncAttach(wg *sync.WaitGroup) {
    61  
    62  	if err := t.eventBus.Subscribe(TopicSystem, t.eventHandler); err != nil {
    63  		log.Error(err.Error())
    64  	}
    65  
    66  	wg.Done()
    67  }
    68  
    69  func (t *SystemTrigger) eventHandler(topic string, event interface{}) {
    70  	if t.counter.Load() <= 0 {
    71  		return
    72  	}
    73  	switch event.(type) {
    74  	case events.EventStateChanged:
    75  		return
    76  	}
    77  
    78  	t.msgQueue.Publish(topic, &SystemTriggerMessage{
    79  		Topic:     topic,
    80  		EventName: events.EventName(event),
    81  		Event:     event,
    82  	})
    83  }
    84  
    85  // Subscribe ...
    86  func (t *SystemTrigger) Subscribe(options Subscriber) error {
    87  	//log.Infof("subscribe topic %s", TopicSystem)
    88  	t.counter.Inc()
    89  	return t.msgQueue.Subscribe(TopicSystem, options.Handler)
    90  }
    91  
    92  // Unsubscribe ...
    93  func (t *SystemTrigger) Unsubscribe(options Subscriber) error {
    94  	//log.Infof("unsubscribe topic %s", TopicSystem)
    95  	t.counter.Dec()
    96  	return t.msgQueue.Unsubscribe(TopicSystem, options.Handler)
    97  }