This commit is contained in:
Daniel Ledda
2020-11-04 18:08:42 +01:00
parent fccba1e4d7
commit cc8cd4bbc4
3 changed files with 54 additions and 14 deletions

View File

@@ -25,3 +25,30 @@ func CloseDb() {
} }
} }
} }
func writeSnapshotToDb(snapshotSub *SnapshotSubmission) error {
query := fmt.Sprintf("INSERT INTO `snapshots` (`temp`, `humidity`, `co2`, `time`, `id`) VALUES (%v, %v, %v, '%v', NULL);",
snapshotSub.Temp,
snapshotSub.Humidity,
snapshotSub.Co2,
snapshotSub.Timestamp)
_, err := ClimateDb.Query(query)
if err != nil {
return err
}
return nil
}
func getSnapshotRecordsFromDb(rowCount int) ([]*SnapshotRecord, error) {
records := make([]*SnapshotRecord, rowCount)
query := fmt.Sprintf("SELECT * FROM `snapshots` ORDER BY `id` DESC LIMIT %v;", rowCount)
rows, err := ClimateDb.Query(query)
if err != nil {
return nil, err
}
err = rows.Scan(records)
if err != nil {
return nil, err
}
return records, nil
}

View File

@@ -5,15 +5,11 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"time"
) )
type SnapshotRecord struct { type SnapshotRecord struct {
Id int Id int `json:"id"`
Timestamp time.Time SnapshotSubmission
Temp float32
Humidity float32
Co2 float32
} }
type SnapshotSubmission struct { type SnapshotSubmission struct {
@@ -23,7 +19,7 @@ type SnapshotSubmission struct {
Co2 float32 `json:"co2"` Co2 float32 `json:"co2"`
} }
func createSnapshotFromJson(jsonBodyStream io.ReadCloser) (*SnapshotSubmission, error) { func createSnapshotSubFromJsonBodyStream(jsonBodyStream io.ReadCloser) (*SnapshotSubmission, error) {
var snapshotSub SnapshotSubmission var snapshotSub SnapshotSubmission
body, err := ioutil.ReadAll(jsonBodyStream) body, err := ioutil.ReadAll(jsonBodyStream)
if err != nil { if err != nil {
@@ -39,3 +35,11 @@ func createSnapshotFromJson(jsonBodyStream io.ReadCloser) (*SnapshotSubmission,
} }
return &snapshotSub, nil return &snapshotSub, nil
} }
func createJsonFromSnapshotRecords(records []*SnapshotRecord) ([]byte, error) {
jsonResult, err := json.Marshal(records)
if err != nil {
return nil, err
}
return jsonResult, nil
}

View File

@@ -32,6 +32,7 @@ func teardown() {
func startServer() { func startServer() {
port := "8001" port := "8001"
MainRouter.setGet("/", showCharts) MainRouter.setGet("/", showCharts)
MainRouter.setGet("/data", sendData)
MainRouter.setPost("/", saveSnapshot) MainRouter.setPost("/", saveSnapshot)
fmt.Printf("Listening on port %s...\n", port) fmt.Printf("Listening on port %s...\n", port)
log.Fatal(http.ListenAndServe(":" + port, nil)) log.Fatal(http.ListenAndServe(":" + port, nil))
@@ -42,17 +43,25 @@ func showCharts(w http.ResponseWriter, r *http.Request) error {
return err return err
} }
func sendData(w http.ResponseWriter, r *http.Request) error {
records, err := getSnapshotRecordsFromDb(50)
if err != nil {
return fmt.Errorf("couldn't read rows from the database: %w", err)
}
json, err := createJsonFromSnapshotRecords(records)
if err != nil {
return fmt.Errorf("couldn't create a json from the records: %w", err)
}
_, err = fmt.Fprintf(w, string(json))
return err
}
func saveSnapshot(w http.ResponseWriter, r *http.Request) error { func saveSnapshot(w http.ResponseWriter, r *http.Request) error {
snapshotSub, err := createSnapshotFromJson(r.Body) snapshotSub, err := createSnapshotSubFromJsonBodyStream(r.Body)
if err != nil { if err != nil {
return fmt.Errorf("couldn't create snapshot from JSON: %w", err) return fmt.Errorf("couldn't create snapshot from JSON: %w", err)
} }
query := fmt.Sprintf("INSERT INTO `snapshots` (`temp`, `humidity`, `co2`, `time`, `id`) VALUES (%v, %v, %v, '%v', NULL);", err = writeSnapshotToDb(snapshotSub)
snapshotSub.Temp,
snapshotSub.Humidity,
snapshotSub.Co2,
snapshotSub.Timestamp)
_, err = ClimateDb.Query(query)
if err != nil { if err != nil {
return fmt.Errorf("couldn't submit snapshot into the database: %w", err) return fmt.Errorf("couldn't submit snapshot into the database: %w", err)
} }