-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.go
213 lines (177 loc) · 6.75 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
// 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"
"github.com/eliona-smart-building-assistant/go-eliona/app"
"github.com/eliona-smart-building-assistant/go-eliona/asset"
"github.com/eliona-smart-building-assistant/go-eliona/dashboard"
"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"
"hailo/apiserver"
"hailo/apiservices"
"hailo/conf"
"hailo/eliona"
"hailo/hailo"
"net/http"
"time"
)
func initialization() {
ctx := context.Background()
// Necessary to close used init resources
conn := db.NewInitConnectionWithContextAndApplicationName(ctx, app.AppName())
defer conn.Close(ctx)
// Init the app before the first run.
app.Init(conn, app.AppName(),
app.ExecSqlFile("conf/init.sql"),
asset.InitAssetTypeFile("eliona/asset-type-bin.json"),
asset.InitAssetTypeFile("eliona/asset-type-digital-hub.json"),
asset.InitAssetTypeFile("eliona/asset-type-recycling-station.json"),
conf.InitConfiguration,
)
// Patch the app to v2.0.0
app.Patch(conn, app.AppName(), "020000",
app.ExecSqlFile("conf/v2.0.0.sql"),
)
// Patch the app to v2.0.1
app.Patch(conn, app.AppName(), "020001",
dashboard.InitWidgetTypeFile("eliona/widget-type-hailo.json"),
dashboard.InitWidgetTypeFile("eliona/widget-type-hailo-station.json"),
)
}
// collectData collects data based on the configured FDS endpoints in table hailo.config. For each FDS endpoint the
// data is collected in a separate thread. These threads wait until the configured interval time is over. Afterwards
// a new thread is started for this connection.
func collectData() {
// Check if import or update of assets is requested
// Load all configured configs from table hailo.config.
configs, err := conf.GetConfigs(context.Background())
if len(configs) <= 0 || err != nil {
log.Fatal("Hailo", "Couldn't read config from configured database: %v", err)
}
// Start collection data for each config
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("Hailo", "Collecting %d initialized with config:\n"+
"FDS Fds Endpoint: %v\n"+
"FDS Fds Auth Server: %v\n"+
"Auth Timeout: %d\n"+
"Request Timeout: %d",
config.Id,
*config.FdsServer,
config.AuthServer,
config.AuthTimeout,
config.RequestTimeout)
}
// 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 (appId) is started to read the data.
common.RunOnceWithParam(func(c apiserver.Configuration) {
log.Info("Hailo", "Collecting %d started", c.Id)
// Collect data for the config
collectDataForConfig(c)
log.Info("Hailo", "Collecting %d finished", c.Id)
// Waits until the time is excited
time.Sleep(time.Second * time.Duration(c.IntervalSec))
}, config, config.Id)
}
}
// 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.NewAssetMappingApiController(apiservices.NewAssetMappingApiService()),
apiserver.NewConfigurationApiController(apiservices.NewConfigurationApiService()),
apiserver.NewCustomizationApiController(apiservices.NewCustomizationApiService()),
apiserver.NewVersionApiController(apiservices.NewVersionApiService()),
)))
log.Fatal("Hailo", "Error in API Server: %v", err)
}
// collectDataForConfig reads specification of all devices in the given connection. For all devices found asset
// data is written. In case of stations (group multiple component devices) data for each component is read and
// written.
func collectDataForConfig(config apiserver.Configuration) {
// Read specs from Hailo FDS
specs, err := hailo.GetSpecs(config)
if err != nil {
log.Error("Hailo", "Could not read specs for config %d: %v", config.Id, err)
return
}
// For each spec write asset data
for _, spec := range specs.Data {
// If necessary create assets in eliona
err = eliona.CreateAssetsIfNecessary(config, spec)
if err != nil {
return
}
// Writing asset data for specification
err = eliona.UpsertDataForDevices(config, spec)
if err != nil {
return
}
// Get Status
status, err := hailo.GetStatus(config, spec.DeviceId)
if err != nil {
log.Error("Hailo", "Could not read status for config %d and device '%s': %v", config.Id, spec.DeviceId, err)
return
}
// Decide if device is station or single container
if status.IsStation() {
// Upsert status for station
err = eliona.UpsertDataForStation(config, status)
if err != nil {
return
}
// Process station components
for _, compStatus := range status.DeviceTypeSpecific.CompStatuses {
// Get diag for component
diag, err := hailo.GetDiag(config, compStatus.DeviceId)
if err != nil {
log.Error("Hailo", "Could not read diag for config %d and component '%s': %v", config.Id, compStatus.DeviceId, err)
return
}
// Upsert status and diag for station components
err = eliona.UpsertDataForBin(config, compStatus, diag)
if err != nil {
return
}
}
} else {
// Get diag for single container
diag, err := hailo.GetDiag(config, status.DeviceId)
if err != nil {
log.Error("Hailo", "Could not read diag for config %d and station '%s': %v", config.Id, status.DeviceId, err)
return
}
// Upsert status and diag for station single container
err = eliona.UpsertDataForBin(config, status, diag)
if err != nil {
return
}
}
}
}