github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/plugins/uptime/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 uptime 20 21 import ( 22 "context" 23 "embed" 24 "fmt" 25 "time" 26 27 "github.com/e154/smart-home/common" 28 "github.com/e154/smart-home/common/logger" 29 m "github.com/e154/smart-home/models" 30 "github.com/e154/smart-home/system/supervisor" 31 ) 32 33 const ( 34 name = "uptime" 35 ) 36 37 var ( 38 log = logger.MustGetLogger("plugins.uptime") 39 ) 40 41 var _ supervisor.Pluggable = (*plugin)(nil) 42 43 //go:embed Readme.md 44 //go:embed Readme.ru.md 45 var F embed.FS 46 47 func init() { 48 supervisor.RegisterPlugin(Name, New) 49 } 50 51 type plugin struct { 52 *supervisor.Plugin 53 ticker *time.Ticker 54 storyModel *m.RunStory 55 } 56 57 // New ... 58 func New() supervisor.Pluggable { 59 p := &plugin{ 60 Plugin: supervisor.NewPlugin(), 61 } 62 p.F = F 63 return p 64 } 65 66 // Load ... 67 func (p *plugin) Load(ctx context.Context, service supervisor.Service) (err error) { 68 if err = p.Plugin.Load(ctx, service, p.ActorConstructor); err != nil { 69 return 70 } 71 72 p.storyModel = &m.RunStory{ 73 Start: time.Now(), 74 } 75 76 p.storyModel.Id, err = p.Service.Adaptors().RunHistory.Add(context.Background(), p.storyModel) 77 if err != nil { 78 log.Error(err.Error()) 79 return 80 } 81 82 if _, err = p.Service.Adaptors().Entity.GetById(context.Background(), common.EntityId(fmt.Sprintf("%s.%s", EntitySensor, Name))); err != nil { 83 entity := &m.Entity{ 84 Id: common.EntityId(fmt.Sprintf("%s.%s", EntitySensor, Name)), 85 PluginName: Name, 86 Attributes: NewAttr(), 87 } 88 err = p.Service.Adaptors().Entity.Add(context.Background(), entity) 89 } 90 91 go func() { 92 const pause = 60 93 p.ticker = time.NewTicker(time.Second * pause) 94 95 for range p.ticker.C { 96 97 if p.storyModel != nil { 98 p.storyModel.End = common.Time(time.Now()) 99 if err = p.Service.Adaptors().RunHistory.Update(context.Background(), p.storyModel); err != nil { 100 log.Error(err.Error()) 101 } 102 } 103 104 p.Actors.Range(func(key, value any) bool { 105 actor, _ := value.(*Actor) 106 actor.update() 107 return true 108 }) 109 } 110 }() 111 return nil 112 } 113 114 // Unload ... 115 func (p *plugin) Unload(ctx context.Context) (err error) { 116 if p.ticker != nil { 117 p.ticker.Stop() 118 p.ticker = nil 119 } 120 if err = p.Plugin.Unload(ctx); err != nil { 121 return 122 } 123 124 if p.storyModel == nil { 125 return 126 } 127 p.storyModel.End = common.Time(time.Now()) 128 if err = p.Service.Adaptors().RunHistory.Update(context.Background(), p.storyModel); err != nil { 129 log.Error(err.Error()) 130 } 131 return 132 } 133 134 // ActorConstructor ... 135 func (p *plugin) ActorConstructor(entity *m.Entity) (actor supervisor.PluginActor, err error) { 136 actor = NewActor(entity, p.Service) 137 return 138 } 139 140 // Name ... 141 func (p plugin) Name() string { 142 return name 143 } 144 145 // Type ... 146 func (p *plugin) Type() supervisor.PluginType { 147 return supervisor.PluginBuiltIn 148 } 149 150 // Depends ... 151 func (p *plugin) Depends() []string { 152 return nil 153 } 154 155 // Version ... 156 func (p *plugin) Version() string { 157 return Version 158 } 159 160 // Options ... 161 func (p *plugin) Options() m.PluginOptions { 162 return m.PluginOptions{ 163 Actors: false, 164 ActorCustomAttrs: false, 165 ActorAttrs: NewAttr(), 166 ActorCustomActions: false, 167 } 168 }