-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
489 lines (401 loc) · 14.5 KB
/
main.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"github.com/PVKonovalov/localcache"
"grid_losses/configuration"
"grid_losses/llog"
"grid_losses/topogrid"
"grid_losses/types"
"grid_losses/webapi"
"grid_losses/zmq_bus"
"os"
"strings"
"time"
)
const ApiGetTopology = "/api/topology/graph"
const ApiGetEquipment = "/api/equipment"
const ApiTimeoutSec = 60
// Resource Types
const (
ResourceTypeIsNotDefine int = 0
ResourceTypeMeasure int = 1
ResourceTypeState int = 2
ResourceTypeControl int = 3
ResourceTypeProtect int = 4
ResourceTypeLink int = 5
ResourceTypeChangeSetGroup int = 6
ResourceTypeReclosing int = 7
ResourceTypeStateLineSegment int = 8
)
type EdgeStruct struct {
EquipmentType string `json:"equipment_type,omitempty"`
EquipmentName string `json:"equipment_name,omitempty"`
EquipmentId int `json:"equipment_id,omitempty"`
EquipmentTypeId int `json:"equipment_type_id,omitempty"`
EquipmentVoltageClassId int `json:"equipment_voltage_class_id,omitempty"`
Id int `json:"id"`
StateNormal int `json:"state_normal"`
Terminal1 int `json:"terminal1"`
Terminal2 int `json:"terminal2"`
}
type TopologyStruct struct {
Edge []EdgeStruct `json:"edge"`
Node []struct {
EquipmentId int `json:"equipment_id,omitempty"`
EquipmentName string `json:"equipment_name,omitempty"`
EquipmentTypeId int `json:"equipment_type_id,omitempty"`
EquipmentVoltageClassId int `json:"equipment_voltage_class_id,omitempty"`
Id int `json:"id"`
} `json:"node"`
}
type EquipmentStruct struct {
electricalState uint32
groundedFrom map[int]bool
energizedFrom map[int]bool
EquipmentType string `json:"equipment_type,omitempty"`
EquipmentVoltageClass string `json:"equipment_voltage_class"`
Id int `json:"id"`
Name string `json:"name"`
TypeId int `json:"type_id,omitempty"`
VoltageClassId int `json:"voltage_class_id"`
Resource []struct {
Id int `json:"id"`
Point string `json:"point"`
PointId uint64 `json:"point_id"`
PointTypeId int `json:"point_type_id"`
Type string `json:"type"`
TypeId int `json:"type_id"`
} `json:"resource,omitempty"`
}
type ResourceStruct struct {
equipmentId int
resourceTypeId int
}
type ThisService struct {
config configuration.Configuration
topologyProfile *TopologyStruct
equipmentFromEquipmentId map[int]EquipmentStruct
pointNameFromPointId map[uint64]string
resourceStructFromPointId map[uint64]ResourceStruct
pointFromEquipmentIdAndResourceTypeId map[int]map[int]uint64
equipmentIdArrayFromResourceTypeId map[int][]int
numberOfCBCheckingLink int
topologyFlisr *topogrid.TopologyGridStruct
topologyGrid *topogrid.TopologyGridStruct
zmq *zmq_bus.ZmqBus
inputDataQueue chan types.RtdbMessage
outputDataQueue chan types.RtdbMessage
switchDataQueue chan types.RtdbMessage
}
// NewService grid Losses service
func NewService() *ThisService {
return &ThisService{
equipmentFromEquipmentId: make(map[int]EquipmentStruct),
pointNameFromPointId: make(map[uint64]string),
resourceStructFromPointId: make(map[uint64]ResourceStruct),
pointFromEquipmentIdAndResourceTypeId: make(map[int]map[int]uint64),
equipmentIdArrayFromResourceTypeId: make(map[int][]int),
}
}
func ParseTopologyData(data []byte) (*TopologyStruct, error) {
var topologyStruct TopologyStruct
err := json.Unmarshal(data, &topologyStruct)
return &topologyStruct, err
}
func ParseEquipmentData(data []byte) (*[]EquipmentStruct, error) {
var equipmentStructs []EquipmentStruct
err := json.Unmarshal(data, &equipmentStructs)
return &equipmentStructs, err
}
// LoadTopologyProfile Loading topologyProfile from configs.configAPIHostList
func (s *ThisService) LoadTopologyProfile(timeoutSec time.Duration, isLoadFromCache bool, cachePath string) error {
var topologyData []byte
cache := localcache.New(cachePath)
if isLoadFromCache {
llog.Logger.Infof("Loading topologyProfile from local cache (%s)", cachePath)
profileData, err := cache.Load()
if err != nil {
return err
}
s.topologyProfile, err = ParseTopologyData(profileData)
return err
}
resultErr := errors.New("unknown error. Check configuration file")
var err error
for _, urlAPIHost := range s.config.ConfigApi.Url {
urlAPIHost = strings.TrimSpace(urlAPIHost)
api := webapi.Connection{
Timeout: timeoutSec,
BaseUrl: urlAPIHost,
HostVirtualName: s.config.ConfigApi.HostName}
llog.Logger.Debugf("Logon to %s as %s", api.BaseUrl, s.config.ConfigApi.UserName)
_, err, _ = api.Logon(s.config.ConfigApi.UserName, s.config.ConfigApi.Password)
if err != nil {
llog.Logger.Errorf("Failed to logon: %v", err)
resultErr = err
continue
}
llog.Logger.Debugf("Getting topology profile ...")
topologyData, err = api.GetProfile(s.config.GridLosses.ApiPrefix + ApiGetTopology)
if err != nil {
llog.Logger.Errorf("Failed to get topology profile: %v", err)
resultErr = err
continue
}
s.topologyProfile, err = ParseTopologyData(topologyData)
if err != nil {
llog.Logger.Errorf("Failed to unmarshal topology profile: %v", err)
resultErr = err
continue
} else {
resultErr = nil
break
}
}
if resultErr == nil {
err = cache.Save(topologyData)
if err != nil {
llog.Logger.Errorf("Failed to write to local cache (%s)", cachePath)
resultErr = err
}
} else {
llog.Logger.Errorf("Failed to load topology profile from API host: %v", resultErr)
llog.Logger.Infof("Loading from local cache (%s)", cachePath)
profileData, err := cache.Load()
if err != nil {
return err
}
s.topologyProfile, err = ParseTopologyData(profileData)
resultErr = err
}
if cache.IsChanged {
llog.Logger.Infof("Configuration changed from the previous loading")
}
return resultErr
}
// LoadEquipmentProfile Loading equipment from config.ConfigAPIHostList
func (s *ThisService) LoadEquipmentProfile(timeoutSec time.Duration, isLoadFromCache bool, cachePath string) error {
var equipmentData []byte
var equipments *[]EquipmentStruct
cache := localcache.New(cachePath)
if isLoadFromCache {
llog.Logger.Infof("Loading equipment from local cache (%s)", cachePath)
profileData, err := cache.Load()
if err != nil {
return err
}
equipments, err = ParseEquipmentData(profileData)
if err == nil {
for _, _equipment := range *equipments {
s.equipmentFromEquipmentId[_equipment.Id] = _equipment
}
}
return err
}
var err error
resultErr := errors.New("unknown error. Check configuration file")
for _, urlAPIHost := range s.config.ConfigApi.Url {
urlAPIHost = strings.TrimSpace(urlAPIHost)
api := webapi.Connection{
Timeout: timeoutSec,
BaseUrl: urlAPIHost,
HostVirtualName: s.config.ConfigApi.HostName}
llog.Logger.Debugf("Logon to %s as %s", api.BaseUrl, s.config.ConfigApi.UserName)
_, err, _ = api.Logon(s.config.ConfigApi.UserName, s.config.ConfigApi.Password)
if err != nil {
llog.Logger.Errorf("Failed to logon: %v", err)
resultErr = err
continue
}
llog.Logger.Debugf("Getting equipment profile ...")
equipmentData, err = api.GetProfile(s.config.GridLosses.ApiPrefix + ApiGetEquipment)
if err != nil {
llog.Logger.Errorf("Failed to get equipment: %v", err)
resultErr = err
continue
}
equipments, err = ParseEquipmentData(equipmentData)
if err != nil {
llog.Logger.Errorf("Failed to unmarshal equipment: %v", err)
resultErr = err
continue
} else {
for _, _equipment := range *equipments {
s.equipmentFromEquipmentId[_equipment.Id] = _equipment
}
resultErr = nil
break
}
}
if resultErr == nil {
err := cache.Save(equipmentData)
if err != nil {
llog.Logger.Errorf("Failed to write to local cache (%s)", cachePath)
resultErr = err
}
} else {
llog.Logger.Errorf("Failed to load equipment from API host: %v", resultErr)
llog.Logger.Infof("Loading from local cache (%s)", cachePath)
profileData, err := cache.Load()
if err != nil {
return err
}
equipments, err = ParseEquipmentData(profileData)
if err == nil {
for _, _equipment := range *equipments {
s.equipmentFromEquipmentId[_equipment.Id] = _equipment
}
}
resultErr = err
}
if cache.IsChanged {
llog.Logger.Infof("Configuration changed from the previous loading")
}
return resultErr
}
func (s *ThisService) CreateInternalParametersFromProfiles() {
for _, equipment := range s.equipmentFromEquipmentId {
for _, resource := range equipment.Resource {
if resource.TypeId == ResourceTypeMeasure ||
resource.TypeId == ResourceTypeState {
s.pointNameFromPointId[resource.PointId] = resource.Point
s.resourceStructFromPointId[resource.PointId] = ResourceStruct{
equipmentId: equipment.Id,
resourceTypeId: resource.TypeId,
}
if _, exists := s.pointFromEquipmentIdAndResourceTypeId[equipment.Id]; !exists {
s.pointFromEquipmentIdAndResourceTypeId[equipment.Id] = make(map[int]uint64)
}
s.pointFromEquipmentIdAndResourceTypeId[equipment.Id][resource.TypeId] = resource.PointId
if resource.TypeId == ResourceTypeLink {
s.numberOfCBCheckingLink += 1
}
}
if _, exists := s.equipmentIdArrayFromResourceTypeId[resource.TypeId]; !exists {
s.equipmentIdArrayFromResourceTypeId[resource.TypeId] = make([]int, 0)
}
s.equipmentIdArrayFromResourceTypeId[resource.TypeId] = append(s.equipmentIdArrayFromResourceTypeId[resource.TypeId], equipment.Id)
}
}
}
func (s *ThisService) LoadTopologyGrid() error {
s.topologyFlisr = topogrid.New(len(s.topologyProfile.Node))
for _, node := range s.topologyProfile.Node {
s.topologyFlisr.AddNode(node.Id, node.EquipmentId, node.EquipmentTypeId, node.EquipmentName)
}
for _, edge := range s.topologyProfile.Edge {
if err := s.topologyFlisr.AddEdge(edge.Id, edge.Terminal1, edge.Terminal2, edge.StateNormal, edge.EquipmentId, edge.EquipmentTypeId, edge.EquipmentName); err != nil {
return err
}
}
s.topologyGrid = topogrid.New(len(s.topologyProfile.Node))
for _, node := range s.topologyProfile.Node {
s.topologyGrid.AddNode(node.Id, node.EquipmentId, node.EquipmentTypeId, node.EquipmentName)
}
for _, edge := range s.topologyProfile.Edge {
if err := s.topologyGrid.AddEdge(edge.Id, edge.Terminal1, edge.Terminal2, edge.StateNormal, edge.EquipmentId, edge.EquipmentTypeId, edge.EquipmentName); err != nil {
return err
}
}
return nil
}
func (s *ThisService) ZmqReceiveDataHandler(msg []string) {
for _, data := range msg {
_message, err := types.ParseScadaRtdbData([]byte(data))
if err != nil {
llog.Logger.Errorf("Failed to parse incoming data (%s): %v", data, err)
continue
}
for _, point := range _message {
if _, exists := s.resourceStructFromPointId[point.Id]; exists {
s.inputDataQueue <- point
}
}
}
}
func (s *ThisService) ReceiveDataWorker() {
for point := range s.inputDataQueue {
if resource, exists := s.resourceStructFromPointId[point.Id]; exists {
switch resource.resourceTypeId {
case ResourceTypeState:
llog.Logger.Debugf("Toggle: %+v", point)
if err := s.topologyGrid.SetSwitchStateByEquipmentId(resource.equipmentId, int(point.Value)); err != nil {
llog.Logger.Warnf("Failed to change state: %v", err)
continue
}
s.topologyGrid.SetEquipmentElectricalState()
case ResourceTypeMeasure:
llog.Logger.Debugf("Measure: %+v", point)
}
}
}
}
func (s *ThisService) OutputEventWorker() {
for event := range s.outputDataQueue {
data, err := json.Marshal([]types.RtdbMessage{event})
if err != nil {
llog.Logger.Fatalf("Failed to marshal (%+v): %v", event, err)
}
_, err = s.zmq.Send(0, data)
if err != nil {
llog.Logger.Fatalf("Failed to send event (%+v): %v", event, err)
}
}
}
func main() {
s := NewService()
var err error
var pathToConfig string
var isLoadFromCache bool
var showEnvVars bool
flag.StringVar(&pathToConfig, "conf", "grid_losses.yml", "path to yml configuration file")
flag.BoolVar(&isLoadFromCache, "cache", false, "load profile from the local cache")
flag.BoolVar(&showEnvVars, "env", false, "show a list of configuration parameters loaded from the environment")
flag.Parse()
if showEnvVars {
fmt.Printf("%+v\n", s.config.ListEnv())
os.Exit(0)
}
if err = s.config.LoadFromFile(pathToConfig); err != nil {
llog.Logger.Fatalf("Failed to read configuration (%s): %v", pathToConfig, err)
}
var logLevel llog.Level
if logLevel, err = llog.ParseLevel(s.config.GridLosses.LogLevel); err != nil {
llog.Logger.Warnf("Failed to parse log level (%s): %v", s.config.GridLosses.LogLevel, err)
llog.Logger.SetLevel(llog.DebugLevel)
} else {
llog.Logger.SetLevel(logLevel)
}
llog.Logger.Infof("Log level: %s", llog.Logger.GetLevel().UpperString())
if err = s.LoadTopologyProfile(time.Second*ApiTimeoutSec, isLoadFromCache, "cache/flisr-topology.json"); err != nil {
llog.Logger.Fatalf("Failed to load topology profile: %v", err)
}
if err = s.LoadEquipmentProfile(time.Second*ApiTimeoutSec, isLoadFromCache, "cache/flisr-equipment.json"); err != nil {
llog.Logger.Fatalf("Failed to load equipment profile: %v", err)
}
s.CreateInternalParametersFromProfiles()
s.inputDataQueue = make(chan types.RtdbMessage, s.config.GridLosses.QueueLength)
s.outputDataQueue = make(chan types.RtdbMessage, s.config.GridLosses.QueueLength)
s.switchDataQueue = make(chan types.RtdbMessage, s.config.GridLosses.QueueLength)
if err = s.LoadTopologyGrid(); err != nil {
llog.Logger.Fatalf("Failed to load topology: %v", err)
}
if s.zmq, err = zmq_bus.New(1, 1); err != nil {
llog.Logger.Fatalf("Failed to create zmq context: %v", err)
}
var subscriberIdx int
if subscriberIdx, err = s.zmq.AddSubscriber(s.config.Rtdb.Output); err != nil {
llog.Logger.Fatalf("Failed to add zmq subscriber [%s]: %v", s.config.Rtdb.Output, err)
}
s.zmq.SetReceiveHandler(subscriberIdx, s.ZmqReceiveDataHandler)
if _, err = s.zmq.AddPublisher(s.config.Rtdb.Input); err != nil {
llog.Logger.Fatalf("Failed to add zmq event publisher [%s]: %v", s.config.Rtdb.Input, err)
}
go s.ReceiveDataWorker()
llog.Logger.Infof("Started")
err = s.zmq.WaitingLoop()
llog.Logger.Errorf("Stopped: %v", err)
}