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