chore: use generic queue

This commit is contained in:
andig 2022-12-12 14:09:00 +01:00
parent cf94adf537
commit 3d39375b99
4 changed files with 59 additions and 9 deletions

View file

@ -24,8 +24,6 @@ import (
"github.com/avast/retry-go/v3"
"github.com/benbjohnson/clock"
"github.com/cjrd/allocate"
"github.com/emirpasic/gods/queues"
aq "github.com/emirpasic/gods/queues/arrayqueue"
)
const (
@ -89,6 +87,9 @@ type ThresholdConfig struct {
Threshold float64
}
// Task is the task type
type Task = func()
// LoadPoint is responsible for controlling charge depending on
// SoC needs and power availability.
type LoadPoint struct {
@ -162,7 +163,7 @@ type LoadPoint struct {
db db.Database
session *db.Session
tasks queues.Queue // tasks to be executed
tasks *util.Queue[Task] // tasks to be executed
}
// NewLoadPointFromConfig creates a new loadpoint
@ -283,7 +284,7 @@ func NewLoadPoint(log *util.Logger) *LoadPoint {
GuardDuration: 5 * time.Minute,
progress: NewProgress(0, 10), // soc progress indicator
coordinator: coordinator.NewDummy(), // dummy vehicle coordinator
tasks: aq.New(), // task queue
tasks: util.NewQueue[Task](), // task queue
}
// allow target charge handler to access loadpoint
@ -1656,7 +1657,7 @@ func (lp *LoadPoint) addTask(task func()) {
// test guard
if lp.tasks != nil {
// don't add twice
if t, ok := lp.tasks.Peek(); ok &&
if t, ok := lp.tasks.First(); ok &&
reflect.ValueOf(t).Pointer() == reflect.ValueOf(task).Pointer() {
return
}
@ -1669,7 +1670,7 @@ func (lp *LoadPoint) processTasks() {
// test guard
if lp.tasks != nil {
if task, ok := lp.tasks.Dequeue(); ok {
task.(func())()
task()
}
}
}

1
go.mod
View file

@ -26,7 +26,6 @@ require (
github.com/dustin/go-humanize v1.0.0
github.com/dylanmei/iso8601 v0.1.0
github.com/eclipse/paho.mqtt.golang v1.4.2
github.com/emirpasic/gods v1.18.1
github.com/enbility/cemd v0.1.5
github.com/enbility/eebus-go v0.1.5
github.com/fatih/structs v1.1.0

2
go.sum
View file

@ -222,8 +222,6 @@ github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFP
github.com/eclipse/paho.mqtt.golang v1.4.2 h1:66wOzfUHSSI1zamx7jR6yMEI5EuHnT1G6rNA5PM12m4=
github.com/eclipse/paho.mqtt.golang v1.4.2/go.mod h1:JGt0RsEwEX+Xa/agj90YJ9d9DH2b7upDZMK9HRbFvCA=
github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
github.com/enbility/cemd v0.1.5 h1:9DHDH90YoldCpbg2gt06y0ZgOOk6pJB0BspBMrwnUq0=
github.com/enbility/cemd v0.1.5/go.mod h1:PZfCRYAe9PDI8fKLADtvotiVTym9ZJ2LZaLfi4EeIaE=
github.com/enbility/eebus-go v0.1.5 h1:IkRxwN+jCqQ4TCbbtjJf70iaeSUUicZZSUsMTLFtVpg=

52
util/queue.go Normal file
View file

@ -0,0 +1,52 @@
package util
// Queue is based on https://github.com/golang-ds/queue
type Queue[T any] struct {
data []T
}
// NewQueue constructs and returns an empty slice-queue.
func NewQueue[T any]() *Queue[T] {
return new(Queue[T])
}
// Enqueue adds an element to the end of the queue.
func (q *Queue[T]) Enqueue(data T) {
q.data = append(q.data, data)
}
// Dequeue removes and returns the front element of the queue. It returns false if the queue was empty.
func (q *Queue[T]) Dequeue() (val T, ok bool) {
if q.IsEmpty() {
return
}
val = q.data[0]
q.data = q.data[1:]
return val, true
}
// First returns the front element of the queue. It returns false if the queue was empty.
func (q *Queue[T]) First() (val T, ok bool) {
if q.IsEmpty() {
return
}
return q.data[0], true
}
// Size returns the number of the elements in the queue.
func (q *Queue[T]) Size() int {
return len(q.data)
}
// Clear empties the queue.
func (q *Queue[T]) Clear() {
q.data = nil
}
// IsEmpty returns true if the queue is empty.
func (q *Queue[T]) IsEmpty() bool {
return q.Size() == 0
}