41 lines
859 B
Go
41 lines
859 B
Go
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
|
|
} |