/
initial.go
189 lines (161 loc) · 4.76 KB
/
initial.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// This file is part of the Smart Home
// Program complex distribution https://github.com/e154/smart-home
// Copyright (C) 2016-2021, Filippov Alex
//
// This library is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library. If not, see
// <https://www.gnu.org/licenses/>.
package initial
import (
"context"
"errors"
"fmt"
"github.com/e154/smart-home/system/scheduler"
"go.uber.org/fx"
. "github.com/e154/smart-home/adaptors"
"github.com/e154/smart-home/api"
"github.com/e154/smart-home/common"
"github.com/e154/smart-home/common/apperr"
"github.com/e154/smart-home/common/logger"
m "github.com/e154/smart-home/models"
_ "github.com/e154/smart-home/plugins"
"github.com/e154/smart-home/system/access_list"
"github.com/e154/smart-home/system/automation"
"github.com/e154/smart-home/system/entity_manager"
"github.com/e154/smart-home/system/gate_client"
. "github.com/e154/smart-home/system/initial/assertions"
"github.com/e154/smart-home/system/initial/demo"
localMigrations "github.com/e154/smart-home/system/initial/local_migrations"
"github.com/e154/smart-home/system/logging_ws"
"github.com/e154/smart-home/system/migrations"
"github.com/e154/smart-home/system/scripts"
"github.com/e154/smart-home/system/validation"
)
var (
log = logger.MustGetLogger("initial")
)
// Initial ...
type Initial struct {
migrations *migrations.Migrations
adaptors *Adaptors
scriptService scripts.ScriptService
accessList access_list.AccessListService
entityManager entity_manager.EntityManager
pluginManager common.PluginManager
automation automation.Automation
api *api.Api
gateClient *gate_client.GateClient
validation *validation.Validate
localMigrations *localMigrations.Migrations
demo *demo.Demos
}
// NewInitial ...
func NewInitial(lc fx.Lifecycle,
migrations *migrations.Migrations,
adaptors *Adaptors,
scriptService scripts.ScriptService,
accessList access_list.AccessListService,
entityManager entity_manager.EntityManager,
pluginManager common.PluginManager,
automation automation.Automation,
api *api.Api,
gateClient *gate_client.GateClient,
validation *validation.Validate,
_ *logging_ws.LoggingWs,
localMigrations *localMigrations.Migrations,
demo *demo.Demos,
_ *scheduler.Scheduler) *Initial {
initial := &Initial{
migrations: migrations,
adaptors: adaptors,
scriptService: scriptService,
accessList: accessList,
entityManager: entityManager,
pluginManager: pluginManager,
automation: automation,
api: api,
gateClient: gateClient,
validation: validation,
localMigrations: localMigrations,
demo: demo,
}
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) (err error) {
return initial.Start(ctx)
},
OnStop: func(ctx context.Context) (err error) {
return initial.Shutdown(ctx)
},
})
return initial
}
// Reset ...
func (n *Initial) Reset() {
log.Info("full reset")
_ = n.migrations.Purge()
log.Info("complete")
}
// InstallDemoData ...
func (n *Initial) InstallDemoData() {
log.Info("install demo data")
tx := n.adaptors.Begin()
// install demo
_ = n.demo.InstallByName(context.TODO(), tx, "example1")
_ = tx.Commit()
log.Info("complete")
}
// checkForUpgrade ...
func (n *Initial) checkForUpgrade() {
defer func() {
fmt.Println("")
}()
tx := n.adaptors.Begin()
v, err := tx.Variable.GetByName("initial_version")
if err != nil {
if errors.Is(err, apperr.ErrNotFound) {
v = m.Variable{
Name: "initial_version",
Value: fmt.Sprintf("%d", 1),
}
err = tx.Variable.Add(v)
So(err, ShouldBeNil)
}
}
oldVersion := v.Value
So(err, ShouldBeNil)
var currentVersion string
if currentVersion, err = n.localMigrations.Up(context.TODO(), tx, oldVersion); err != nil {
return
}
v.Value = currentVersion
err = tx.Variable.Update(v)
So(err, ShouldBeNil)
_ = tx.Commit()
}
// Start ...
func (n *Initial) Start(ctx context.Context) (err error) {
n.checkForUpgrade()
n.entityManager.SetPluginManager(n.pluginManager)
n.pluginManager.Start()
_ = n.automation.Start()
go func() {
_ = n.api.Start()
}()
n.gateClient.Start()
return
}
// Shutdown ...
func (n *Initial) Shutdown(ctx context.Context) (err error) {
_ = n.api.Shutdown(ctx)
return
}