-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathevents.go
More file actions
203 lines (177 loc) · 7.4 KB
/
Copy pathevents.go
File metadata and controls
203 lines (177 loc) · 7.4 KB
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
package lpp
import (
"github.com/enbility/eebus-go/features/client"
internal "github.com/enbility/eebus-go/usecases/internal"
"github.com/enbility/ship-go/logging"
spineapi "github.com/enbility/spine-go/api"
"github.com/enbility/spine-go/model"
"github.com/enbility/spine-go/util"
)
// handle SPINE events
func (e *LPP) HandleEvent(payload spineapi.EventPayload) {
if !e.IsCompatibleEntityType(payload.Entity) {
return
}
if internal.IsEntityAdded(payload) {
e.connected(payload.Entity)
return
}
if internal.IsHeartbeat(payload) && e.EventCB != nil {
e.EventCB(payload.Ski, payload.Device, payload.Entity, DataUpdateHeartbeat)
return
}
if payload.EventType != spineapi.EventTypeDataChange ||
payload.ChangeType != spineapi.ElementChangeUpdate {
return
}
switch payload.Data.(type) {
case *model.LoadControlLimitDescriptionListDataType:
e.loadControlLimitDescriptionDataUpdate(payload.Entity)
case *model.LoadControlLimitListDataType:
e.loadControlLimitDataUpdate(payload)
case *model.DeviceConfigurationKeyValueDescriptionListDataType:
e.configurationDescriptionDataUpdate(payload.Entity)
case *model.DeviceConfigurationKeyValueListDataType:
e.configurationDataUpdate(payload)
case *model.ElectricalConnectionCharacteristicListDataType:
e.electricalConnectionCharacteristicDataUpdate(payload)
}
}
// the remote entity was connected
func (e *LPP) connected(entity spineapi.EntityRemoteInterface) {
// initialise features, e.g. subscriptions, descriptions
if loadControl, err := client.NewLoadControl(e.LocalEntity, entity); err == nil {
if !loadControl.HasSubscription() {
if _, err := loadControl.Subscribe(); err != nil {
logging.Log().Debug(err)
}
}
if !loadControl.HasBinding() {
if _, err := loadControl.Bind(); err != nil {
logging.Log().Debug(err)
}
}
// get descriptions
selector := &model.LoadControlLimitDescriptionListDataSelectorsType{
LimitType: util.Ptr(model.LoadControlLimitTypeTypeSignDependentAbsValueLimit),
LimitDirection: util.Ptr(model.EnergyDirectionTypeProduce),
ScopeType: util.Ptr(model.ScopeTypeTypeActivePowerLimit),
}
if _, err := loadControl.RequestLimitDescriptions(selector, nil); err != nil {
logging.Log().Debug(err)
}
}
if deviceConfiguration, err := client.NewDeviceConfiguration(e.LocalEntity, entity); err == nil {
if !deviceConfiguration.HasSubscription() {
if _, err := deviceConfiguration.Subscribe(); err != nil {
logging.Log().Debug(err)
}
}
if !deviceConfiguration.HasBinding() {
if _, err := deviceConfiguration.Bind(); err != nil {
logging.Log().Debug(err)
}
}
// get descriptions
// don't use selectors yet, as we would have to query 2 which could result in 2 full reads
if _, err := deviceConfiguration.RequestKeyValueDescriptions(nil, nil); err != nil {
logging.Log().Debug(err)
}
}
if deviceDiagnosis, err := client.NewDeviceDiagnosis(e.LocalEntity, entity); err == nil {
if !deviceDiagnosis.HasSubscription() {
if _, err := deviceDiagnosis.Subscribe(); err != nil {
logging.Log().Debug(err)
}
}
if _, err := deviceDiagnosis.RequestHeartbeat(); err != nil {
logging.Log().Debug(err)
}
}
if electricalConnection, err := client.NewElectricalConnection(e.LocalEntity, entity); err == nil {
if !electricalConnection.HasSubscription() {
if _, err := electricalConnection.Subscribe(); err != nil {
logging.Log().Debug(err)
}
}
// get characteristics
selector := &model.ElectricalConnectionCharacteristicListDataSelectorsType{
CharacteristicContext: util.Ptr(model.ElectricalConnectionCharacteristicContextTypeEntity),
CharacteristicType: util.Ptr(e.characteristicType(entity)),
}
if _, err := electricalConnection.RequestCharacteristics(selector, nil); err != nil {
logging.Log().Debug(err)
}
}
}
// the electrical connection power production nominal max was updated
func (e *LPP) electricalConnectionCharacteristicDataUpdate(payload spineapi.EventPayload) {
if electricalConnection, err := client.NewElectricalConnection(e.LocalEntity, payload.Entity); err == nil {
filter := model.ElectricalConnectionCharacteristicDataType{
ElectricalConnectionId: util.Ptr(model.ElectricalConnectionIdType(0)),
ParameterId: util.Ptr(model.ElectricalConnectionParameterIdType(0)),
CharacteristicType: util.Ptr(e.characteristicType(payload.Entity)),
CharacteristicContext: util.Ptr(model.ElectricalConnectionCharacteristicContextTypeEntity),
}
if electricalConnection.CheckEventPayloadDataForFilterDescriptionList(payload.Data, filter) && e.EventCB != nil {
e.EventCB(payload.Ski, payload.Device, payload.Entity, DataUpdatePowerProductionNominalMax)
}
}
}
// the load control limit description data was updated
func (e *LPP) loadControlLimitDescriptionDataUpdate(entity spineapi.EntityRemoteInterface) {
if loadControl, err := client.NewLoadControl(e.LocalEntity, entity); err == nil {
// get values
var selector *model.LoadControlLimitListDataSelectorsType
filter := model.LoadControlLimitDescriptionDataType{
LimitType: util.Ptr(model.LoadControlLimitTypeTypeSignDependentAbsValueLimit),
LimitDirection: util.Ptr(model.EnergyDirectionTypeProduce),
ScopeType: util.Ptr(model.ScopeTypeTypeActivePowerLimit),
}
if descs, err := loadControl.GetLimitDescriptionsForFilter(filter); err == nil && len(descs) > 0 {
selector = &model.LoadControlLimitListDataSelectorsType{
LimitId: descs[0].LimitId,
}
}
if _, err := loadControl.RequestLimitData(selector, nil); err != nil {
logging.Log().Debug(err)
}
}
}
// the load control limit data was updated
func (e *LPP) loadControlLimitDataUpdate(payload spineapi.EventPayload) {
if lc, err := client.NewLoadControl(e.LocalEntity, payload.Entity); err == nil {
filter := model.LoadControlLimitDescriptionDataType{
LimitType: util.Ptr(model.LoadControlLimitTypeTypeSignDependentAbsValueLimit),
LimitDirection: util.Ptr(model.EnergyDirectionTypeProduce),
ScopeType: util.Ptr(model.ScopeTypeTypeActivePowerLimit),
}
if lc.CheckEventPayloadDataForFilter(payload.Data, filter) && e.EventCB != nil {
e.EventCB(payload.Ski, payload.Device, payload.Entity, DataUpdateLimit)
}
}
}
// the configuration key description data was updated
func (e *LPP) configurationDescriptionDataUpdate(entity spineapi.EntityRemoteInterface) {
if deviceConfiguration, err := client.NewDeviceConfiguration(e.LocalEntity, entity); err == nil {
// key value descriptions received, now get the data
if _, err := deviceConfiguration.RequestKeyValues(nil, nil); err != nil {
logging.Log().Debug("Error getting configuration key values:", err)
}
}
}
// the configuration key data was updated
func (e *LPP) configurationDataUpdate(payload spineapi.EventPayload) {
if dc, err := client.NewDeviceConfiguration(e.LocalEntity, payload.Entity); err == nil {
filter := model.DeviceConfigurationKeyValueDescriptionDataType{
KeyName: util.Ptr(model.DeviceConfigurationKeyNameTypeFailsafeProductionActivePowerLimit),
}
if dc.CheckEventPayloadDataForFilter(payload.Data, filter) && e.EventCB != nil {
e.EventCB(payload.Ski, payload.Device, payload.Entity, DataUpdateFailsafeProductionActivePowerLimit)
}
filter.KeyName = util.Ptr(model.DeviceConfigurationKeyNameTypeFailsafeDurationMinimum)
if dc.CheckEventPayloadDataForFilter(payload.Data, filter) && e.EventCB != nil {
e.EventCB(payload.Ski, payload.Device, payload.Entity, DataUpdateFailsafeDurationMinimum)
}
}
}