Skip to content

Commit d5a3838

Browse files
Small bugfixes for controlbox and hems example (#155)
Very small bugfix in the LPP Event Handler of the hems example. Bugfix in the LPP Event Handler of controlbox example. Don't use lpc interfaces in lpp scenarios.
2 parents c35ab07 + 07416fa commit d5a3838

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

examples/controlbox/main.go

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,14 @@ func (h *controlbox) AllowWaitingForTrust(ski string) bool {
141141

142142
// LPC Event Handler
143143

144-
func (h *controlbox) sendLimit(entity spineapi.EntityRemoteInterface) {
144+
func (h *controlbox) sendConsumptionLimit(entity spineapi.EntityRemoteInterface) {
145145
scenarios := h.uclpc.AvailableScenariosForEntity(entity)
146146
if len(scenarios) == 0 ||
147147
!slices.Contains(scenarios, 1) {
148148
return
149149
}
150150

151-
fmt.Println("Sending a limit in 5s...")
151+
fmt.Println("Sending a consumption limit in 5s...")
152152
time.AfterFunc(time.Second*5, func() {
153153
limit := ucapi.LoadLimit{
154154
Duration: time.Hour*1 + time.Minute*2 + time.Second*3,
@@ -158,17 +158,17 @@ func (h *controlbox) sendLimit(entity spineapi.EntityRemoteInterface) {
158158

159159
resultCB := func(msg model.ResultDataType) {
160160
if *msg.ErrorNumber == model.ErrorNumberTypeNoError {
161-
fmt.Println("Limit accepted.")
161+
fmt.Println("Consumption limit accepted.")
162162
} else {
163-
fmt.Println("Limit rejected. Code", *msg.ErrorNumber, "Description", *msg.Description)
163+
fmt.Println("Consumption limit rejected. Code", *msg.ErrorNumber, "Description", *msg.Description)
164164
}
165165
}
166166
msgCounter, err := h.uclpc.WriteConsumptionLimit(entity, limit, resultCB)
167167
if err != nil {
168-
fmt.Println("Failed to send limit", err)
168+
fmt.Println("Failed to send consumption limit", err)
169169
return
170170
}
171-
fmt.Println("Sent limit to", entity.Device().Ski(), "with msgCounter", msgCounter)
171+
fmt.Println("Sent consumption limit to", entity.Device().Ski(), "with msgCounter", msgCounter)
172172
})
173173
}
174174
func (h *controlbox) OnLPCEvent(ski string, device spineapi.DeviceRemoteInterface, entity spineapi.EntityRemoteInterface, event api.EventType) {
@@ -178,27 +178,59 @@ func (h *controlbox) OnLPCEvent(ski string, device spineapi.DeviceRemoteInterfac
178178

179179
switch event {
180180
case lpc.UseCaseSupportUpdate:
181-
h.sendLimit(entity)
181+
h.sendConsumptionLimit(entity)
182182
case lpc.DataUpdateLimit:
183183
if currentLimit, err := h.uclpc.ConsumptionLimit(entity); err == nil {
184-
fmt.Println("New Limit received", currentLimit.Value, "W")
184+
fmt.Println("New consumption limit received", currentLimit.Value, "W")
185185
}
186186
default:
187187
return
188188
}
189189
}
190190

191+
// LPP Event Handler
192+
193+
func (h *controlbox) sendProductionLimit(entity spineapi.EntityRemoteInterface) {
194+
scenarios := h.uclpc.AvailableScenariosForEntity(entity)
195+
if len(scenarios) == 0 ||
196+
!slices.Contains(scenarios, 1) {
197+
return
198+
}
199+
200+
fmt.Println("Sending a production limit in 5s...")
201+
time.AfterFunc(time.Second*5, func() {
202+
limit := ucapi.LoadLimit{
203+
Duration: time.Hour*1 + time.Minute*2 + time.Second*3,
204+
IsActive: true,
205+
Value: 100,
206+
}
207+
208+
resultCB := func(msg model.ResultDataType) {
209+
if *msg.ErrorNumber == model.ErrorNumberTypeNoError {
210+
fmt.Println("Production limit accepted.")
211+
} else {
212+
fmt.Println("Production limit rejected. Code", *msg.ErrorNumber, "Description", *msg.Description)
213+
}
214+
}
215+
msgCounter, err := h.uclpp.WriteProductionLimit(entity, limit, resultCB)
216+
if err != nil {
217+
fmt.Println("Failed to send production limit", err)
218+
return
219+
}
220+
fmt.Println("Sent production limit to", entity.Device().Ski(), "with msgCounter", msgCounter)
221+
})
222+
}
191223
func (h *controlbox) OnLPPEvent(ski string, device spineapi.DeviceRemoteInterface, entity spineapi.EntityRemoteInterface, event api.EventType) {
192224
if !h.isConnected {
193225
return
194226
}
195227

196228
switch event {
197-
case lpc.UseCaseSupportUpdate:
198-
h.sendLimit(entity)
199-
case lpc.DataUpdateLimit:
200-
if currentLimit, err := h.uclpc.ConsumptionLimit(entity); err == nil {
201-
fmt.Println("New Limit received", currentLimit.Value, "W")
229+
case lpp.UseCaseSupportUpdate:
230+
h.sendProductionLimit(entity)
231+
case lpp.DataUpdateLimit:
232+
if currentLimit, err := h.uclpp.ProductionLimit(entity); err == nil {
233+
fmt.Println("New production limit received", currentLimit.Value, "W")
202234
}
203235
default:
204236
return

examples/hems/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func (h *hems) OnLPPEvent(ski string, device spineapi.DeviceRemoteInterface, ent
179179
h.uccslpp.ApproveOrDenyProductionLimit(msgCounter, true, "")
180180
}
181181
case cslpp.DataUpdateLimit:
182-
if currentLimit, err := h.uccslpp.ProductionLimit(); err != nil {
182+
if currentLimit, err := h.uccslpp.ProductionLimit(); err == nil {
183183
fmt.Println("New LPP Limit set to", currentLimit.Value, "W")
184184
}
185185
}

0 commit comments

Comments
 (0)