github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/plugins/memory_app/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 memory_app 20 21 import ( 22 "context" 23 "embed" 24 "fmt" 25 "time" 26 27 "github.com/e154/smart-home/common" 28 29 "github.com/e154/smart-home/system/supervisor" 30 31 m "github.com/e154/smart-home/models" 32 ) 33 34 var _ supervisor.Pluggable = (*plugin)(nil) 35 36 //go:embed Readme.md 37 //go:embed Readme.ru.md 38 var F embed.FS 39 40 func init() { 41 supervisor.RegisterPlugin(Name, New) 42 } 43 44 type plugin struct { 45 *supervisor.Plugin 46 ticker *time.Ticker 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 if _, err = p.Service.Adaptors().Entity.GetById(context.Background(), common.EntityId(fmt.Sprintf("%s.%s", EntityMemoryApp, Name))); err != nil { 65 entity := &m.Entity{ 66 Id: common.EntityId(fmt.Sprintf("%s.%s", EntityMemoryApp, Name)), 67 Description: "App metric", 68 PluginName: Name, 69 Metrics: NewMetrics(), 70 Attributes: NewAttr(), 71 } 72 err = p.Service.Adaptors().Entity.Add(context.Background(), entity) 73 } 74 75 go func() { 76 77 const pause = 10 78 p.ticker = time.NewTicker(time.Second * time.Duration(pause)) 79 80 for range p.ticker.C { 81 p.Actors.Range(func(key, value any) bool { 82 actor, _ := value.(*Actor) 83 actor.selfUpdate() 84 return true 85 }) 86 } 87 }() 88 return 89 } 90 91 // Unload ... 92 func (p *plugin) Unload(ctx context.Context) (err error) { 93 if p.ticker != nil { 94 p.ticker.Stop() 95 p.ticker = nil 96 } 97 err = p.Plugin.Unload(ctx) 98 return 99 } 100 101 // ActorConstructor ... 102 func (p *plugin) ActorConstructor(entity *m.Entity) (actor supervisor.PluginActor, err error) { 103 actor = NewActor(entity, p.Service) 104 if entity.Metrics == nil { 105 entity.Metrics = NewMetrics() 106 } 107 return 108 } 109 110 // Name ... 111 func (p plugin) Name() string { 112 return Name 113 } 114 115 // Type ... 116 func (p *plugin) Type() supervisor.PluginType { 117 return supervisor.PluginInstallable 118 } 119 120 // Depends ... 121 func (p *plugin) Depends() []string { 122 return nil 123 } 124 125 // Version ... 126 func (p *plugin) Version() string { 127 return Version 128 } 129 130 // Options ... 131 func (p *plugin) Options() m.PluginOptions { 132 return m.PluginOptions{} 133 }