Skip to content

Commit

Permalink
Merge pull request #41 from diwise/feature/water-quality-as-a-feature
Browse files Browse the repository at this point in the history
added a simple water quality feature
  • Loading branch information
nordbergmikael authored Mar 3, 2023
2 parents 23ab6fd + fd0e59d commit 4e4e1d3
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.0 // indirect
github.com/matryer/is v1.4.0
github.com/rabbitmq/amqp091-go v1.5.0 // indirect
github.com/rabbitmq/amqp091-go v1.5.0
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.12.0 // indirect
go.opentelemetry.io/otel/metric v0.35.0 // indirect
go.opentelemetry.io/otel/trace v1.12.0 // indirect
Expand Down
8 changes: 5 additions & 3 deletions internal/pkg/application/features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/diwise/iot-core/internal/pkg/application/features/counters"
"github.com/diwise/iot-core/internal/pkg/application/features/levels"
"github.com/diwise/iot-core/internal/pkg/application/features/presences"
"github.com/diwise/iot-core/internal/pkg/application/features/waterqualities"
"github.com/diwise/iot-core/pkg/messaging/events"
"github.com/diwise/messaging-golang/pkg/messaging"
"github.com/diwise/service-chassis/pkg/infrastructure/o11y/logging"
Expand All @@ -28,9 +29,10 @@ type feat struct {
Location *location `json:"location,omitempty"`
Tenant string `json:"tenant,omitempty"`

Counter counters.Counter `json:"counter,omitempty"`
Level levels.Level `json:"level,omitempty"`
Presence presences.Presence `json:"presence,omitempty"`
Counter counters.Counter `json:"counter,omitempty"`
Level levels.Level `json:"level,omitempty"`
Presence presences.Presence `json:"presence,omitempty"`
WaterQuality waterqualities.WaterQuality `json:"waterquality,omitempty"`

handle func(context.Context, *events.MessageAccepted) (bool, error)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/application/features/levels/levels.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,5 @@ func hasChanged(previousLevel, newLevel float64) bool {
}

func isNotZero(value float64) bool {
return (math.Abs(value) >= 0.01)
return (math.Abs(value) >= 0.001)
}
4 changes: 4 additions & 0 deletions internal/pkg/application/features/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/diwise/iot-core/internal/pkg/application/features/counters"
"github.com/diwise/iot-core/internal/pkg/application/features/levels"
"github.com/diwise/iot-core/internal/pkg/application/features/presences"
"github.com/diwise/iot-core/internal/pkg/application/features/waterqualities"
"github.com/diwise/service-chassis/pkg/infrastructure/o11y/logging"
)

Expand Down Expand Up @@ -60,6 +61,9 @@ func NewRegistry(ctx context.Context, input io.Reader) (Registry, error) {
} else if f.Type == presences.FeatureTypeName {
f.Presence = presences.New()
f.handle = f.Presence.Handle
} else if f.Type == waterqualities.FeatureTypeName {
f.WaterQuality = waterqualities.New()
f.handle = f.WaterQuality.Handle
} else {
numErrors++
if numErrors > 1 {
Expand Down
51 changes: 51 additions & 0 deletions internal/pkg/application/features/waterqualities/waterqualities.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package waterqualities

import (
"context"
"math"

"github.com/diwise/iot-core/pkg/lwm2m"
"github.com/diwise/iot-core/pkg/messaging/events"
)

const (
FeatureTypeName string = "waterquality"
)

type WaterQuality interface {
Handle(ctx context.Context, e *events.MessageAccepted) (bool, error)
}

func New() WaterQuality {
return &waterquality{}
}

type waterquality struct {
Temperature float64 `json:"temperature"`
}

func (wq *waterquality) Handle(ctx context.Context, e *events.MessageAccepted) (bool, error) {

if !e.BaseNameMatches(lwm2m.Temperature) {
return false, nil
}

const SensorValue string = "5700"
temp, tempOk := e.GetFloat64(SensorValue)

if tempOk {
oldTemp := wq.Temperature
wq.Temperature = temp
return hasChanged(oldTemp, temp), nil
}

return false, nil
}

func hasChanged(previousLevel, newLevel float64) bool {
return isNotZero(newLevel - previousLevel)
}

func isNotZero(value float64) bool {
return (math.Abs(value) >= 0.001)
}

0 comments on commit 4e4e1d3

Please sign in to comment.