64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
)
|
|
|
|
type UnixSnapshotPayload struct {
|
|
Snapshots []*UnixSnapshotRecord `json:"snapshots"`
|
|
}
|
|
|
|
type DatetimeSnapshotPayload struct {
|
|
Snapshots []*DatetimeSnapshotRecord `json:"snapshots"`
|
|
}
|
|
|
|
type UnixSnapshotRecord struct {
|
|
|
|
}
|
|
|
|
type DatetimeSnapshotRecord struct {
|
|
Id int `json:"id"`
|
|
JsonSnapshotSubmission
|
|
}
|
|
|
|
type JsonSnapshotSubmission struct {
|
|
Timestamp string `json:"time"`
|
|
Temp float32 `json:"temp"`
|
|
Humidity float32 `json:"humidity"`
|
|
Co2 float32 `json:"co2"`
|
|
}
|
|
|
|
type SnapshotSubmission struct {
|
|
Timestamp interface{}
|
|
Temp interface{}
|
|
Humidity interface{}
|
|
Co2 interface{}
|
|
}
|
|
|
|
func createSnapshotSubFromJsonBodyStream(jsonBodyStream io.ReadCloser) (*JsonSnapshotSubmission, error) {
|
|
var snapshotSub JsonSnapshotSubmission
|
|
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
|
|
}
|
|
|
|
func createJsonFromSnapshotRecords(records []*DatetimeSnapshotRecord) ([]byte, error) {
|
|
jsonResult, err := json.Marshal(DatetimeSnapshotPayload{records})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("couldn't marshal json: %w", err)
|
|
}
|
|
return jsonResult, nil
|
|
} |