-
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 #41 from diwise/feature/water-quality-as-a-feature
added a simple water quality feature
- Loading branch information
Showing
5 changed files
with
62 additions
and
5 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
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
51 changes: 51 additions & 0 deletions
51
internal/pkg/application/features/waterqualities/waterqualities.go
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,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) | ||
} |