-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
224 lines (202 loc) · 6.37 KB
/
app.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// This file is part of the eliona project.
// Copyright © 2022 LEICOM iTEC AG. All Rights Reserved.
// ______ _ _
// | ____| (_)
// | |__ | |_ ___ _ __ __ _
// | __| | | |/ _ \| '_ \ / _` |
// | |____| | | (_) | | | | (_| |
// |______|_|_|\___/|_| |_|\__,_|
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package main
import (
"context"
"kentix-one/apiserver"
"kentix-one/apiservices"
"kentix-one/conf"
"kentix-one/eliona"
"kentix-one/kentix"
"net/http"
"time"
"github.com/eliona-smart-building-assistant/go-eliona/app"
"github.com/eliona-smart-building-assistant/go-utils/common"
"github.com/eliona-smart-building-assistant/go-utils/db"
utilshttp "github.com/eliona-smart-building-assistant/go-utils/http"
"github.com/eliona-smart-building-assistant/go-utils/log"
)
func initaialize() {
ctx := context.Background()
conn := db.NewInitConnectionWithContextAndApplicationName(ctx, app.AppName())
defer conn.Close(ctx)
app.Init(conn, app.AppName(),
app.ExecSqlFile("conf/init.sql"),
eliona.InitEliona,
)
}
func collectData() {
configs, err := conf.GetConfigs(context.Background())
if err != nil {
log.Fatal("conf", "Couldn't read configs from DB: %v", err)
return
}
if len(configs) == 0 {
log.Info("conf", "No configs in DB")
return
}
for _, config := range configs {
// Skip config if disabled and set inactive
if !conf.IsConfigEnabled(config) {
if conf.IsConfigActive(config) {
conf.SetConfigActiveState(context.Background(), config, false)
}
continue
}
// Signals that this config is active
if !conf.IsConfigActive(config) {
conf.SetConfigActiveState(context.Background(), config, true)
log.Info("conf", "Collecting initialized with Configuration %d:\n"+
"Address: %s\n"+
"API Key: %s\n"+
"Enable: %t\n"+
"Refresh Interval: %d\n"+
"Request Timeout: %d\n"+
"Active: %t\n"+
"Project IDs: %v\n",
*config.Id,
config.Address,
"**************",
*config.Enable,
config.RefreshInterval,
*config.RequestTimeout,
*config.Active,
*config.ProjectIDs)
}
// Runs the ReadNode. If the current node is currently running, skip the execution
// After the execution sleeps the configured timeout. During this timeout no further
// process for this config is started to read the data.
common.RunOnceWithParam(func(config apiserver.Configuration) {
log.Info("main", "Collecting %d started", *config.Id)
collectDataForConfig(config)
log.Info("main", "Collecting %d finished", *config.Id)
time.Sleep(time.Second * time.Duration(config.RefreshInterval))
}, config, *config.Id)
}
}
func collectDataForConfig(config apiserver.Configuration) {
devices, err := kentix.GetDevices(config)
if err != nil {
log.Error("kentix", "getting devices info: %v", err)
return
}
for _, device := range devices {
if config.AssetFilter != nil {
shouldCreate, err := device.AdheresToFilter(config)
if err != nil {
log.Error("config", "determining whether device adheres to filter: %v", err)
return
}
if !shouldCreate {
continue
}
}
if err := eliona.CreateAssetsIfNecessary(config, device); err != nil {
log.Error("eliona", "creating assets: %v", err)
return
}
if err := eliona.UpsertDeviceInfo(config, device); err != nil {
log.Error("eliona", "inserting device info: %v", err)
return
}
if device.Measurements != nil {
if err := eliona.UpsertMultiSensorData(config, device.UUID, *device.Measurements); err != nil {
log.Error("eliona", "upserting MultiSensor data: %v", err)
return
}
}
}
doorlocks, err := kentix.GetDoorlocks(config)
if err != nil {
log.Error("kentix", "getting doorlocks info: %v", err)
return
}
for _, doorlock := range doorlocks {
if config.AssetFilter != nil {
shouldCreate, err := doorlock.AdheresToFilter(config)
if err != nil {
log.Error("config", "determining whether doorlock adheres to filter: %v", err)
return
}
if !shouldCreate {
continue
}
}
if err := eliona.CreateDoorlockAssetsIfNecessary(config, doorlock); err != nil {
log.Error("eliona", "creating assets: %v", err)
return
}
if err := eliona.UpsertDoorlockData(config, doorlock); err != nil {
log.Error("eliona", "inserting doorlock data: %v", err)
return
}
}
}
func listenForOutputChanges() {
outputs, err := eliona.ListenForOutputChanges()
if err != nil {
log.Error("eliona", "listening for output changes: %v", err)
return
}
for output := range outputs {
open, ok := output.Data["open"]
if !ok {
log.Error("eliona", "no 'open' attribute in data")
return
}
switch open.(float64) {
case 0:
continue
case 1:
openDoorlock(output.AssetId)
default:
log.Error("eliona", "invalid value '%v' in 'open' attribute", open)
}
}
}
func openDoorlock(assetID int32) {
configs, err := conf.GetConfigs(context.Background())
if err != nil {
log.Fatal("conf", "Couldn't read configs from DB: %v", err)
return
}
if len(configs) == 0 {
log.Info("conf", "No configs in DB")
return
}
for _, config := range configs {
doorlockID, err := conf.GetDeviceId(context.Background(), assetID)
if err != nil {
log.Error("conf", "getting device ID from asset (%v) id: %v", assetID, err)
return
}
log.Debug("kentix", "opening doorlock %v", doorlockID)
if err := kentix.OpenDoorlock(doorlockID, config); err != nil {
log.Error("kentix", "opening doorlock %v: %v", doorlockID, err)
return
}
}
}
// listenApiRequests starts an API server and listen for API requests
// The API endpoints are defined in the openapi.yaml file
func listenApiRequests() {
err := http.ListenAndServe(":"+common.Getenv("API_SERVER_PORT", "3000"), utilshttp.NewCORSEnabledHandler(
apiserver.NewRouter(
apiserver.NewConfigurationApiController(apiservices.NewConfigurationApiService()),
apiserver.NewVersionApiController(apiservices.NewVersionApiService()),
apiserver.NewCustomizationApiController(apiservices.NewCustomizationApiService()),
)))
log.Fatal("main", "API server: %v", err)
}