github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/plugins/sun/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 sun
    20  
    21  import (
    22  	"context"
    23  	"embed"
    24  	"time"
    25  
    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.sun")
    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  	ticker *time.Ticker
    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  	go func() {
    66  		const pause = 240
    67  		p.ticker = time.NewTicker(time.Second * pause)
    68  
    69  		for range p.ticker.C {
    70  			p.updatePositionForAll()
    71  		}
    72  	}()
    73  
    74  	return nil
    75  }
    76  
    77  // ActorConstructor ...
    78  func (p *plugin) ActorConstructor(entity *m.Entity) (actor supervisor.PluginActor, err error) {
    79  	actor = NewActor(entity, p.Service)
    80  	return
    81  }
    82  
    83  // Name ...
    84  func (p *plugin) Name() string {
    85  	return Name
    86  }
    87  
    88  func (p *plugin) updatePositionForAll() {
    89  	now := time.Now()
    90  	p.Actors.Range(func(key, value any) bool {
    91  		actor := value.(*Actor)
    92  		actor.UpdateSunPosition(now)
    93  		return true
    94  	})
    95  }
    96  
    97  // Type ...
    98  func (p *plugin) Type() supervisor.PluginType {
    99  	return supervisor.PluginBuiltIn
   100  }
   101  
   102  // Depends ...
   103  func (p *plugin) Depends() []string {
   104  	return nil
   105  }
   106  
   107  // Version ...
   108  func (p *plugin) Version() string {
   109  	return Version
   110  }
   111  
   112  // Options ...
   113  func (p *plugin) Options() m.PluginOptions {
   114  	return m.PluginOptions{
   115  		Actors:      true,
   116  		ActorAttrs:  NewAttr(),
   117  		ActorSetts:  NewSettings(),
   118  		ActorStates: supervisor.ToEntityStateShort(NewStates()),
   119  	}
   120  }