github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/plugins/weather_owm/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 weather_owm 20 21 import ( 22 "context" 23 "embed" 24 25 "github.com/e154/smart-home/common/logger" 26 m "github.com/e154/smart-home/models" 27 "github.com/e154/smart-home/plugins/weather" 28 "github.com/e154/smart-home/system/scheduler" 29 "github.com/e154/smart-home/system/supervisor" 30 ) 31 32 const ( 33 // Name ... 34 Name = "weather_owm" 35 ) 36 37 var ( 38 log = logger.MustGetLogger("plugins.owm") 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 weather *WeatherOwm 54 task scheduler.EntryID 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.Service.EventBus().Subscribe("system/entities/+", p.eventHandler) 73 74 p.task, err = p.Service.Scheduler().AddFunc("0 */30 * * * *", func() { 75 p.Actors.Range(func(key, value any) bool { 76 actor := value.(*Actor) 77 actor.update() 78 return true 79 }) 80 }) 81 82 return 83 } 84 85 // Unload ... 86 func (p *plugin) Unload(ctx context.Context) (err error) { 87 if err = p.Plugin.Unload(ctx); err != nil { 88 return 89 } 90 p.Service.Scheduler().Remove(p.task) 91 _ = p.Service.EventBus().Unsubscribe("system/entities/+", p.eventHandler) 92 return nil 93 } 94 95 // ActorConstructor ... 96 func (p *plugin) ActorConstructor(entity *m.Entity) (actor supervisor.PluginActor, err error) { 97 actor = NewActor(entity, p.Service) 98 return 99 } 100 101 // Name ... 102 func (p plugin) Name() string { 103 return Name 104 } 105 106 func (p *plugin) eventHandler(_ string, msg interface{}) { 107 108 } 109 110 // Type ... 111 func (p *plugin) Type() supervisor.PluginType { 112 return supervisor.PluginInstallable 113 } 114 115 // Depends ... 116 func (p *plugin) Depends() []string { 117 return []string{} 118 } 119 120 // Version ... 121 func (p *plugin) Version() string { 122 return Version 123 } 124 125 // Options ... 126 func (p *plugin) Options() m.PluginOptions { 127 return m.PluginOptions{ 128 Actors: true, 129 ActorAttrs: weather.BaseForecast(), 130 ActorStates: supervisor.ToEntityStateShort(weather.NewActorStates(false, false)), 131 ActorSetts: NewSettings(), 132 } 133 }