github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/plugins/modbus_tcp/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 modbus_tcp
    20  
    21  import (
    22  	"context"
    23  	"embed"
    24  
    25  	"github.com/e154/smart-home/common/events"
    26  	"github.com/e154/smart-home/common/logger"
    27  	m "github.com/e154/smart-home/models"
    28  	"github.com/e154/smart-home/system/supervisor"
    29  )
    30  
    31  var (
    32  	log = logger.MustGetLogger("plugins.modbus_tcp")
    33  )
    34  
    35  var _ supervisor.Pluggable = (*plugin)(nil)
    36  
    37  //go:embed Readme.md
    38  //go:embed Readme.ru.md
    39  var F embed.FS
    40  
    41  func init() {
    42  	supervisor.RegisterPlugin(Name, New)
    43  }
    44  
    45  type plugin struct {
    46  	*supervisor.Plugin
    47  }
    48  
    49  // New ...
    50  func New() supervisor.Pluggable {
    51  	p := &plugin{
    52  		Plugin: supervisor.NewPlugin(),
    53  	}
    54  	p.F = F
    55  	return p
    56  }
    57  
    58  // Load ...
    59  func (p *plugin) Load(ctx context.Context, service supervisor.Service) (err error) {
    60  	if err = p.Plugin.Load(ctx, service, p.ActorConstructor); err != nil {
    61  		return
    62  	}
    63  
    64  	_ = p.Service.EventBus().Subscribe("system/entities/+", p.eventHandler)
    65  
    66  	return nil
    67  }
    68  
    69  // Unload ...
    70  func (p *plugin) Unload(ctx context.Context) (err error) {
    71  	if err = p.Plugin.Unload(ctx); err != nil {
    72  		return
    73  	}
    74  
    75  	_ = p.Service.EventBus().Unsubscribe("system/entities/+", p.eventHandler)
    76  	return nil
    77  }
    78  
    79  // ActorConstructor ...
    80  func (p *plugin) ActorConstructor(entity *m.Entity) (actor supervisor.PluginActor, err error) {
    81  	actor = NewActor(entity, p.Service)
    82  	return
    83  }
    84  
    85  // Name ...
    86  func (p *plugin) Name() string {
    87  	return Name
    88  }
    89  
    90  func (p *plugin) eventHandler(topic string, msg interface{}) {
    91  
    92  	switch v := msg.(type) {
    93  	case events.EventStateChanged:
    94  	case events.EventCallEntityAction:
    95  		values, ok := p.Check(v)
    96  		if !ok {
    97  			return
    98  		}
    99  		for _, value := range values {
   100  			actor := value.(*Actor)
   101  			actor.addAction(v)
   102  		}
   103  	}
   104  }
   105  
   106  // Type ...
   107  func (p *plugin) Type() supervisor.PluginType {
   108  	return supervisor.PluginInstallable
   109  }
   110  
   111  // Depends ...
   112  func (p *plugin) Depends() []string {
   113  	return []string{"node"}
   114  }
   115  
   116  // Version ...
   117  func (p *plugin) Version() string {
   118  	return Version
   119  }
   120  
   121  // Options ...
   122  func (p *plugin) Options() m.PluginOptions {
   123  	return m.PluginOptions{
   124  		Actors:             true,
   125  		ActorCustomAttrs:   true,
   126  		ActorCustomActions: true,
   127  		ActorCustomStates:  true,
   128  		ActorAttrs:         NewAttr(),
   129  		ActorSetts:         NewSettings(),
   130  	}
   131  }