github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/system/automation/automation.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 // 20 // AUTOMATION 21 // ┌──────┐ ┌──────┐ ┌──────┐ 22 // │ TASK │ │ TASK │ │ TASK │ 23 // └──────┘ └──────┘ └──────┘ ... 24 // 25 26 package automation 27 28 import ( 29 "context" 30 31 "go.uber.org/fx" 32 33 "github.com/e154/smart-home/adaptors" 34 "github.com/e154/smart-home/common/events" 35 "github.com/e154/smart-home/common/logger" 36 "github.com/e154/smart-home/common/telemetry" 37 "github.com/e154/smart-home/system/bus" 38 "github.com/e154/smart-home/system/scripts" 39 "github.com/e154/smart-home/system/supervisor" 40 ) 41 42 var ( 43 log = logger.MustGetLogger("automation") 44 ) 45 46 // Automation ... 47 type Automation interface { 48 Start() (err error) 49 Shutdown() (err error) 50 Restart() 51 TaskIsLoaded(id int64) bool 52 TriggerIsLoaded(id int64) bool 53 TaskTelemetry(id int64) telemetry.Telemetry 54 } 55 56 type automation struct { 57 eventBus bus.Bus 58 triggerManager *triggerManager 59 taskManager *taskManager 60 } 61 62 // NewAutomation ... 63 func NewAutomation(lc fx.Lifecycle, 64 eventBus bus.Bus, 65 scriptService scripts.ScriptService, 66 sup supervisor.Supervisor, 67 adaptors *adaptors.Adaptors) (auto Automation) { 68 69 auto = &automation{ 70 eventBus: eventBus, 71 taskManager: NewTaskManager(eventBus, scriptService, sup, adaptors), 72 triggerManager: NewTriggerManager(eventBus, scriptService, sup, adaptors), 73 } 74 75 lc.Append(fx.Hook{ 76 OnStop: func(ctx context.Context) error { 77 return auto.Shutdown() 78 }, 79 }) 80 return 81 } 82 83 // Start ... 84 func (a *automation) Start() (err error) { 85 a.taskManager.Start() 86 a.triggerManager.Start() 87 a.eventBus.Publish("system/services/automation", events.EventServiceStarted{Service: "Automation"}) 88 return 89 } 90 91 // Shutdown ... 92 func (a *automation) Shutdown() (err error) { 93 a.taskManager.Shutdown() 94 a.triggerManager.Shutdown() 95 a.eventBus.Publish("system/services/automation", events.EventServiceStopped{Service: "Automation"}) 96 return 97 } 98 99 // Restart ... 100 func (a *automation) Restart() { 101 _ = a.Shutdown() 102 _ = a.Start() 103 } 104 105 func (a *automation) TaskIsLoaded(id int64) bool { 106 return a.taskManager.IsLoaded(id) 107 } 108 109 func (a *automation) TriggerIsLoaded(id int64) bool { 110 return a.triggerManager.IsLoaded(id) 111 } 112 113 func (a *automation) TaskTelemetry(id int64) telemetry.Telemetry { 114 return a.taskManager.TaskTelemetry(id) 115 }