github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/plugins/triggers/plugin.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  	"context"
    23  	"embed"
    24  	"fmt"
    25  	"sync"
    26  
    27  	"github.com/e154/smart-home/system/supervisor"
    28  
    29  	"github.com/e154/smart-home/common/apperr"
    30  
    31  	"github.com/e154/smart-home/common/logger"
    32  
    33  	"github.com/pkg/errors"
    34  
    35  	m "github.com/e154/smart-home/models"
    36  )
    37  
    38  var (
    39  	log = logger.MustGetLogger("plugins.triggers")
    40  )
    41  
    42  var _ supervisor.Pluggable = (*plugin)(nil)
    43  
    44  //go:embed Readme.md
    45  //go:embed Readme.ru.md
    46  var F embed.FS
    47  
    48  func init() {
    49  	supervisor.RegisterPlugin(Name, New)
    50  }
    51  
    52  type plugin struct {
    53  	*supervisor.Plugin
    54  	mu       *sync.Mutex
    55  	triggers map[string]ITrigger
    56  }
    57  
    58  // New ...
    59  func New() supervisor.Pluggable {
    60  	p := &plugin{
    61  		Plugin:   supervisor.NewPlugin(),
    62  		mu:       &sync.Mutex{},
    63  		triggers: make(map[string]ITrigger),
    64  	}
    65  	p.F = F
    66  	return p
    67  }
    68  
    69  // Load ...
    70  func (p *plugin) Load(ctx context.Context, service supervisor.Service) (err error) {
    71  	if err = p.Plugin.Load(ctx, service, nil); err != nil {
    72  		return
    73  	}
    74  
    75  	p.attachTrigger()
    76  
    77  	return
    78  }
    79  
    80  // Unload ...
    81  func (p *plugin) Unload(ctx context.Context) (err error) {
    82  	if err = p.Plugin.Unload(ctx); err != nil {
    83  		return
    84  	}
    85  	return
    86  }
    87  
    88  // Name ...
    89  func (p plugin) Name() string {
    90  	return Name
    91  }
    92  
    93  func (p *plugin) attachTrigger() {
    94  
    95  	p.mu.Lock()
    96  	defer p.mu.Unlock()
    97  
    98  	// init triggers ...
    99  	p.triggers[StateChangeName] = NewStateChangedTrigger(p.Service.EventBus())
   100  	p.triggers[SystemName] = NewSystemTrigger(p.Service.EventBus())
   101  	p.triggers[TimeName] = NewTimeTrigger(p.Service.EventBus(), p.Service.Scheduler())
   102  
   103  	wg := &sync.WaitGroup{}
   104  
   105  	for _, tr := range p.triggers {
   106  		wg.Add(1)
   107  		go func(tr ITrigger, wg *sync.WaitGroup) {
   108  			log.Infof("register trigger '%s'", tr.Name())
   109  			tr.AsyncAttach(wg)
   110  		}(tr, wg)
   111  	}
   112  
   113  	wg.Wait()
   114  }
   115  
   116  // Type ...
   117  func (p *plugin) Type() supervisor.PluginType {
   118  	return supervisor.PluginBuiltIn
   119  }
   120  
   121  // Depends ...
   122  func (p *plugin) Depends() []string {
   123  	return nil
   124  }
   125  
   126  // Version ...
   127  func (p *plugin) Version() string {
   128  	return Version
   129  }
   130  
   131  // GetTrigger ...
   132  func (p *plugin) GetTrigger(name string) (trigger ITrigger, err error) {
   133  
   134  	p.mu.Lock()
   135  	defer p.mu.Unlock()
   136  
   137  	var ok bool
   138  	if trigger, ok = p.triggers[name]; !ok {
   139  		err = errors.Wrap(apperr.ErrNotFound, fmt.Sprintf("trigger name '%s'", name))
   140  	}
   141  	return
   142  }
   143  
   144  // RegisterTrigger ...
   145  func (p *plugin) RegisterTrigger(tr ITrigger) (err error) {
   146  
   147  	p.mu.Lock()
   148  	defer p.mu.Unlock()
   149  
   150  	if _, ok := p.triggers[tr.Name()]; ok {
   151  		err = errors.Wrap(apperr.ErrInternal, fmt.Sprintf("trigger '%s' is registerred", tr.Name()))
   152  		return
   153  	}
   154  
   155  	p.triggers[tr.Name()] = tr
   156  	wg := &sync.WaitGroup{}
   157  	wg.Add(1)
   158  	log.Infof("register trigger '%s'", tr.Name())
   159  	go tr.AsyncAttach(wg)
   160  	wg.Wait()
   161  
   162  	return
   163  }
   164  
   165  // UnregisterTrigger ...
   166  func (p *plugin) UnregisterTrigger(name string) error {
   167  
   168  	p.mu.Lock()
   169  	defer p.mu.Unlock()
   170  
   171  	if _, ok := p.triggers[name]; ok {
   172  		delete(p.triggers, name)
   173  		return nil
   174  	}
   175  
   176  	return nil
   177  }
   178  
   179  // TriggerList ...
   180  func (p *plugin) TriggerList() (list []string) {
   181  
   182  	p.mu.Lock()
   183  	defer p.mu.Unlock()
   184  
   185  	list = make([]string, 0, len(p.triggers))
   186  	for name := range p.triggers {
   187  		list = append(list, name)
   188  	}
   189  	return
   190  }
   191  
   192  // Options ...
   193  func (p *plugin) Options() m.PluginOptions {
   194  	return m.PluginOptions{
   195  		Triggers: true,
   196  	}
   197  }