-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
adaptors.go
162 lines (149 loc) · 4.7 KB
/
adaptors.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
// This file is part of the Smart Home
// Program complex distribution https://github.com/e154/smart-home
// Copyright (C) 2016-2023, 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 adaptors
import (
"context"
"go.uber.org/fx"
"gorm.io/gorm"
"github.com/e154/smart-home/common/logger"
"github.com/e154/smart-home/models"
"github.com/e154/smart-home/system/migrations"
"github.com/e154/smart-home/system/orm"
)
var (
log = logger.MustGetLogger("adaptors")
)
// Adaptors ...
type Adaptors struct {
db *gorm.DB
isTx bool
Script IScript
Tag ITag
Role IRole
Permission IPermission
User IUser
UserMeta IUserMeta
UserDevice IUserDevice
Image IImage
Variable IVariable
Entity IEntity
EntityState IEntityState
EntityAction IEntityAction
EntityStorage IEntityStorage
Log ILog
Template ITemplate
Message IMessage
MessageDelivery IMessageDelivery
Zigbee2mqtt IZigbee2mqtt
Zigbee2mqttDevice IZigbee2mqttDevice
AlexaSkill IAlexaSkill
AlexaIntent IAlexaIntent
Metric IMetric
MetricBucket IMetricBucket
Area IArea
Action IAction
Condition ICondition
Trigger ITrigger
Task ITask
RunHistory IRunHistory
Plugin IPlugin
TelegramChat ITelegramChat
Dashboard IDashboard
DashboardTab IDashboardTab
DashboardCard IDashboardCard
DashboardCardItem IDashboardCardItem
ScriptVersion IScriptVersion
}
// NewAdaptors ...
func NewAdaptors(lc fx.Lifecycle,
db *gorm.DB,
cfg *models.AppConfig,
migrations *migrations.Migrations,
orm *orm.Orm) (adaptors *Adaptors) {
adaptors = &Adaptors{
db: db,
Script: GetScriptAdaptor(db),
Tag: GetTagAdaptor(db),
Role: GetRoleAdaptor(db),
Permission: GetPermissionAdaptor(db),
User: GetUserAdaptor(db),
UserMeta: GetUserMetaAdaptor(db),
UserDevice: GetUserDeviceAdaptor(db),
Image: GetImageAdaptor(db),
Variable: GetVariableAdaptor(db),
Entity: GetEntityAdaptor(db, orm),
EntityState: GetEntityStateAdaptor(db),
EntityAction: GetEntityActionAdaptor(db),
EntityStorage: GetEntityStorageAdaptor(db),
Log: GetLogAdaptor(db),
Template: GetTemplateAdaptor(db),
Message: GetMessageAdaptor(db),
MessageDelivery: GetMessageDeliveryAdaptor(db),
Zigbee2mqtt: GetZigbee2mqttAdaptor(db),
Zigbee2mqttDevice: GetZigbee2mqttDeviceAdaptor(db),
AlexaSkill: GetAlexaSkillAdaptor(db),
AlexaIntent: GetAlexaIntentAdaptor(db),
Metric: GetMetricAdaptor(db, orm),
MetricBucket: GetMetricBucketAdaptor(db, orm),
Area: GetAreaAdaptor(db),
Action: GetActionAdaptor(db, orm),
Condition: GetConditionAdaptor(db),
Trigger: GetTriggerAdaptor(db, orm),
Task: GetTaskAdaptor(db, orm),
RunHistory: GetRunHistoryAdaptor(db),
Plugin: GetPluginAdaptor(db),
TelegramChat: GetTelegramChannelAdaptor(db),
Dashboard: GetDashboardAdaptor(db),
DashboardTab: GetDashboardTabAdaptor(db),
DashboardCard: GetDashboardCardAdaptor(db),
DashboardCardItem: GetDashboardCardItemAdaptor(db),
ScriptVersion: GetScriptVersionAdaptor(db),
}
if lc != nil {
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) (err error) {
if cfg != nil && migrations != nil && cfg.AutoMigrate {
err = migrations.Up()
return
}
return
},
})
}
return
}
// Begin ...
func (a Adaptors) Begin() (adaptors *Adaptors) {
adaptors = NewAdaptors(nil, a.db.Begin(), nil, nil, nil)
adaptors.isTx = true
return
}
// Commit ...
func (a *Adaptors) Commit() error {
if !a.isTx {
return nil
}
return a.db.Commit().Error
}
// Rollback ...
func (a *Adaptors) Rollback() error {
if !a.isTx {
return nil
}
return a.db.Rollback().Error
}