-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from diwise/develop
Develop
- Loading branch information
Showing
7 changed files
with
372 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package decorators | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math" | ||
|
||
"github.com/diwise/iot-core/internal/pkg/application/measurements" | ||
"github.com/diwise/iot-core/pkg/messaging/events" | ||
"github.com/diwise/senml" | ||
"github.com/diwise/service-chassis/pkg/infrastructure/o11y/logging" | ||
) | ||
|
||
type ValueFinder func() float64 | ||
|
||
const ( | ||
BatteryLevel string = "9" | ||
PowerSourceVoltage string = "7" | ||
DeviceObjectID string = "3" | ||
) | ||
|
||
func GetMaxPowerSourceVoltage(ctx context.Context, maxValueFinder measurements.MaxValueFinder, deviceID string) ValueFinder { | ||
powerSourceVoltageMeasurementID := fmt.Sprintf("%s/%s/%s", deviceID, DeviceObjectID, PowerSourceVoltage) | ||
|
||
m, err := maxValueFinder.GetMaxValue(ctx, powerSourceVoltageMeasurementID) | ||
if err != nil { | ||
return func() float64 { | ||
return 0.0 | ||
} | ||
} | ||
|
||
return func() float64 { | ||
return m | ||
} | ||
} | ||
|
||
func Device(ctx context.Context, max ValueFinder) events.EventDecoratorFunc { | ||
log := logging.GetFromContext(ctx) | ||
|
||
return func(m *events.MessageAccepted) { | ||
objID := events.GetObjectID(m.Pack) | ||
if objID != DeviceObjectID { | ||
return | ||
} | ||
|
||
_, ok := m.Pack.GetValue(senml.FindByName(BatteryLevel)) | ||
if ok { | ||
log.Debug("battery level already set") | ||
return | ||
} | ||
|
||
vvd, ok := m.Pack.GetValue(senml.FindByName(PowerSourceVoltage)) | ||
if !ok { | ||
log.Warn("no power source voltage found") | ||
return | ||
} | ||
|
||
percentage := math.RoundToEven((vvd / max()) * 100) | ||
|
||
if percentage < 0 { | ||
percentage = 0 | ||
} | ||
|
||
if percentage > 100 { | ||
percentage = 100 | ||
} | ||
|
||
m.Pack = append(m.Pack, senml.Record{ | ||
Name: BatteryLevel, | ||
Value: &percentage, | ||
Unit: "%", | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package measurements | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
type CacheItem struct { | ||
Value any | ||
ExpiryTime time.Time | ||
} | ||
|
||
type Cache struct { | ||
items map[string]CacheItem | ||
mutex sync.RWMutex | ||
} | ||
|
||
func NewCache() *Cache { | ||
return &Cache{ | ||
items: make(map[string]CacheItem), | ||
} | ||
} | ||
|
||
func (c *Cache) Set(key string, value any, duration time.Duration) { | ||
c.mutex.Lock() | ||
defer c.mutex.Unlock() | ||
|
||
c.items[key] = CacheItem{ | ||
Value: value, | ||
ExpiryTime: time.Now().Add(duration), | ||
} | ||
} | ||
|
||
func (c *Cache) Get(key string) (any, bool) { | ||
c.mutex.RLock() | ||
defer c.mutex.RUnlock() | ||
|
||
item, exists := c.items[key] | ||
if !exists || item.ExpiryTime.Before(time.Now()) { | ||
return nil, false | ||
} | ||
|
||
return item.Value, true | ||
} | ||
|
||
func (c *Cache) Cleanup(interval time.Duration) { | ||
ticker := time.NewTicker(interval) | ||
go func() { | ||
for { | ||
<-ticker.C | ||
now := time.Now() | ||
c.mutex.Lock() | ||
for key, item := range c.items { | ||
if item.ExpiryTime.Before(now) { | ||
delete(c.items, key) | ||
} | ||
} | ||
c.mutex.Unlock() | ||
} | ||
}() | ||
} |
Oops, something went wrong.