-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MQTT gateway: add support for CO2 measurements
- Loading branch information
Showing
3 changed files
with
72 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/bash | ||
|
||
set -euo pipefail | ||
|
||
cd "$(dirname "$0")" | ||
|
||
rm --force airmon | ||
|
||
podman run --rm -it -v .:/app golang:buster bash -c 'cd /app && go build .' | ||
|
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,37 @@ | ||
package mon | ||
|
||
import ( | ||
"encoding/json" | ||
influxdb2 "github.com/influxdata/influxdb-client-go/v2" | ||
influxdb2Write "github.com/influxdata/influxdb-client-go/v2/api/write" | ||
"log" | ||
"time" | ||
) | ||
|
||
type co2 struct { | ||
Device string `json:"dev"` | ||
Time int64 `json:"time"` | ||
Sensor string `json:"sens"` | ||
Co2 uint16 `json:"co2"` | ||
} | ||
|
||
func (t *co2) toPoint() *influxdb2Write.Point { | ||
return influxdb2.NewPoint("co2", | ||
map[string]string{ | ||
"device": t.Device, | ||
"sensor": t.Sensor, | ||
}, | ||
map[string]interface{}{ | ||
"co2": t.Co2, | ||
}, | ||
time.Unix(t.Time, 0)) | ||
} | ||
|
||
func ParseCarbonDioxide(data []byte) (*influxdb2Write.Point, error) { | ||
var co2 co2 | ||
if err := json.Unmarshal(data, &co2); err != nil { | ||
log.Print("could not parse co2 json: ", err) | ||
return nil, err | ||
} | ||
return co2.toPoint(), nil | ||
} |
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