Got it working!
This commit is contained in:
41
Snapshot.go
Normal file
41
Snapshot.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"time"
|
||||
)
|
||||
|
||||
type SnapshotRecord struct {
|
||||
Id int
|
||||
Timestamp time.Time
|
||||
Temp float32
|
||||
Humidity float32
|
||||
Co2 float32
|
||||
}
|
||||
|
||||
type SnapshotSubmission struct {
|
||||
Timestamp float32 `json:"time"`
|
||||
Temp float32 `json:"temp"`
|
||||
Humidity float32 `json:"humidity"`
|
||||
Co2 float32 `json:"co2"`
|
||||
}
|
||||
|
||||
func createSnapshotFromJson(jsonBodyStream io.ReadCloser) (*SnapshotSubmission, error) {
|
||||
var snapshotSub SnapshotSubmission
|
||||
body, err := ioutil.ReadAll(jsonBodyStream)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error reading body stream: %w", err)
|
||||
}
|
||||
err = jsonBodyStream.Close()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("couldn't close body stream: %w", err)
|
||||
}
|
||||
err = json.Unmarshal(body, &snapshotSub)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("couldn't unmarshal json: %w", err)
|
||||
}
|
||||
return &snapshotSub, nil
|
||||
}
|
||||
Reference in New Issue
Block a user