Skip to content

Commit 4ba82d6

Browse files
committed
fix(plc4go/bacnetip): task time calculation
1 parent 3e7bb14 commit 4ba82d6

File tree

7 files changed

+60
-48
lines changed

7 files changed

+60
-48
lines changed

plc4go/internal/bacnetip/BACnetVirtualLinkLayerService.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,15 @@ func (b *BIPForeign) registrationExpired(_ Args, _ KWArgs) error {
991991
}
992992

993993
func (b *BIPForeign) String() string {
994-
return fmt.Sprintf("BIPForeign(taskTime: %v, isScheduled: %t, registrationStatus: %d, bbmdAddress: %s, bbmdTimeToLive: %d)", b.taskTime, b.isScheduled, b.registrationStatus, b.bbmdAddress, b.bbmdTimeToLive)
994+
taskTime := "unscheduled"
995+
if b.taskTime != nil {
996+
taskTime = b.taskTime.String()
997+
}
998+
bbmdTimeToLive := "unknown"
999+
if b.bbmdTimeToLive != nil {
1000+
bbmdTimeToLive = fmt.Sprintf("%ds", *b.bbmdTimeToLive)
1001+
}
1002+
return fmt.Sprintf("BIPForeign(taskTime: %s, isScheduled: %t, registrationStatus: %d, bbmdAddress: %s, bbmdTimeToLive: %s)", taskTime, b.isScheduled, b.registrationStatus, b.bbmdAddress, bbmdTimeToLive)
9951003
}
9961004

9971005
type BIPBBMD struct {

plc4go/internal/bacnetip/Task.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,12 @@ type Task struct {
9898
isScheduled bool
9999
}
100100

101-
func NewTask(taskRequirements TaskRequirements) *Task {
102-
return &Task{TaskRequirements: taskRequirements}
101+
func NewTask(taskRequirements TaskRequirements, opts ...func(*Task)) *Task {
102+
t := &Task{TaskRequirements: taskRequirements}
103+
for _, opt := range opts {
104+
opt(t)
105+
}
106+
return t
103107
}
104108

105109
func (t *Task) InstallTask(options InstallTaskOptions) {
@@ -155,10 +159,9 @@ type OneShotTask struct {
155159

156160
func NewOneShotTask(taskRequirements TaskRequirements, when *time.Time) *OneShotTask {
157161
o := &OneShotTask{}
158-
o.Task = NewTask(taskRequirements)
159-
if when != nil {
160-
o.taskTime = when
161-
}
162+
o.Task = NewTask(taskRequirements, func(task *Task) {
163+
task.taskTime = when
164+
})
162165
return o
163166
}
164167

@@ -172,10 +175,9 @@ type OneShotDeleteTask struct {
172175

173176
func NewOneShotDeleteTask(taskRequirements TaskRequirements, when *time.Time) *OneShotDeleteTask {
174177
o := &OneShotDeleteTask{}
175-
o.Task = NewTask(taskRequirements)
176-
if when != nil {
177-
o.taskTime = when
178-
}
178+
o.Task = NewTask(taskRequirements, func(task *Task) {
179+
task.taskTime = when
180+
})
179181
return o
180182
}
181183

@@ -379,11 +381,15 @@ func ClearTaskManager(localLog zerolog.Logger) {
379381
}
380382

381383
type taskItem struct {
382-
taskTime time.Time
384+
taskTime *time.Time
383385
id int
384386
task TaskRequirements
385387
}
386388

389+
func (t taskItem) String() string {
390+
return fmt.Sprintf("taskItem(taskTime:%v, id:%d, %v)", t.taskTime, t.id, t.task)
391+
}
392+
387393
type taskManager struct {
388394
sync.Mutex
389395

@@ -455,7 +461,7 @@ func (m *taskManager) InstallTask(task TaskRequirements) {
455461
// save this in the task list
456462
m.count++
457463
heap.Push(&m.tasks, &PriorityItem[int64, taskItem]{
458-
value: taskItem{taskTime: GetTaskManagerTime(), id: m.count, task: task},
464+
value: taskItem{taskTime: task.GetTaskTime(), id: m.count, task: task},
459465
priority: task.GetTaskTime().UnixNano() - time.Time{}.UnixNano(),
460466
})
461467
m.log.Debug().Stringer("tasks", m.tasks).Msg("tasks")

plc4go/internal/bacnetip/comp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ type PriorityItem[P cmp.Ordered, V any] struct {
205205
index int // The index of the item in the heap.
206206
}
207207

208-
func (p PriorityItem[P, V]) String() string {
208+
func (p *PriorityItem[P, V]) String() string {
209209
return fmt.Sprintf("[%v: %v-%v], ", p.index, p.priority, p.value)
210210
}
211211

plc4go/internal/bacnetip/tests/test_apdu/test_max_apdu_length_accepted_test.go

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,4 @@
1919

2020
package test_apdu
2121

22-
import (
23-
"context"
24-
"testing"
25-
26-
"github.com/stretchr/testify/assert"
27-
"github.com/stretchr/testify/require"
28-
29-
"github.com/apache/plc4x/plc4go/protocols/bacnetip/readwrite/model"
30-
)
31-
32-
func TestMaxApduLengthAcceptedEncode(t *testing.T) {
33-
t.Skip("Plc4x doesn't normalise at model level")
34-
apdu := model.NewAPDU(50)
35-
assert.Equal(t, 0, 50, apdu.ApduLength)
36-
}
37-
38-
func TestMaxApduLengthAcceptedDecode(t *testing.T) {
39-
t.Skip("Plc4x doesn't normalise at model level")
40-
apdu := model.NewAPDU(0)
41-
serialize, err := apdu.Serialize()
42-
require.NoError(t, err)
43-
apduParse, err := model.APDUParse(context.Background(), serialize, 0)
44-
require.NoError(t, err)
45-
// TODO: no way to access the length
46-
_ = apduParse
47-
}
22+
// TODO: implement
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package test_apdu
21+
22+
// TODO implement

plc4go/internal/bacnetip/tests/test_utilities/test_time_machine_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,17 +251,18 @@ func (suite *TimeMachineSuite) TestRecurringTask3() {
251251
ft := NewSampleRecurringTask(suite.log)
252252

253253
// reset the time machine to midnight, install the task, let it run
254-
tests.ResetTimeMachine(tests.StartTime)
254+
startTime := time.Time{}.Add(1 * time.Hour) // We add an hour to avoid underflow
255+
tests.ResetTimeMachine(startTime)
255256
ft.InstallTask(bacnetip.WithInstallTaskOptionsInterval(1000 * time.Millisecond).WithOffset(100 * time.Millisecond))
256257
tests.RunTimeMachine(suite.log, 5*time.Second, time.Time{})
257258

258259
// function called, 60 seconds passed
259-
suite.Equal(tests.StartTime.Add(100*time.Millisecond), ft.processTaskCalled[0])
260-
suite.Equal(tests.StartTime.Add(1100*time.Millisecond), ft.processTaskCalled[1])
261-
suite.Equal(tests.StartTime.Add(2100*time.Millisecond), ft.processTaskCalled[2])
262-
suite.Equal(tests.StartTime.Add(3100*time.Millisecond), ft.processTaskCalled[3])
263-
suite.Equal(tests.StartTime.Add(4100*time.Millisecond), ft.processTaskCalled[4])
264-
suite.InDelta(5*time.Second, tests.GlobalTimeMachineCurrentTime().Sub(tests.StartTime), float64(100*time.Millisecond))
260+
suite.Equal(startTime.Add(100*time.Millisecond), ft.processTaskCalled[0])
261+
suite.Equal(startTime.Add(1100*time.Millisecond), ft.processTaskCalled[1])
262+
suite.Equal(startTime.Add(2100*time.Millisecond), ft.processTaskCalled[2])
263+
suite.Equal(startTime.Add(3100*time.Millisecond), ft.processTaskCalled[3])
264+
suite.Equal(startTime.Add(4100*time.Millisecond), ft.processTaskCalled[4])
265+
suite.InDelta(5*time.Second, tests.GlobalTimeMachineCurrentTime().Sub(startTime), float64(100*time.Millisecond))
265266
}
266267

267268
func (suite *TimeMachineSuite) TestRecurringTask4() {

plc4go/internal/bacnetip/tests/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"github.com/apache/plc4x/plc4go/internal/bacnetip"
2727
)
2828

29-
var StartTime = time.Time{}.Add(1 * time.Hour)
29+
var StartTime = time.Time{}
3030

3131
type DummyMessage struct {
3232
bacnetip.MessageBridge

0 commit comments

Comments
 (0)