From cc8cd4bbc49f94535647d9b17085ff0f26f3676e Mon Sep 17 00:00:00 2001 From: Daniel Ledda Date: Wed, 4 Nov 2020 18:08:42 +0100 Subject: [PATCH] Working --- Database.go | 27 +++++++++++++++++++++++++++ Snapshot.go | 18 +++++++++++------- climate-server.go | 23 ++++++++++++++++------- 3 files changed, 54 insertions(+), 14 deletions(-) diff --git a/Database.go b/Database.go index 4e1316e..15c8693 100644 --- a/Database.go +++ b/Database.go @@ -24,4 +24,31 @@ func CloseDb() { fmt.Println("error closing database: " + err.Error()) } } +} + +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 } \ No newline at end of file diff --git a/Snapshot.go b/Snapshot.go index 7911028..c57cb16 100644 --- a/Snapshot.go +++ b/Snapshot.go @@ -5,15 +5,11 @@ import ( "fmt" "io" "io/ioutil" - "time" ) type SnapshotRecord struct { - Id int - Timestamp time.Time - Temp float32 - Humidity float32 - Co2 float32 + Id int `json:"id"` + SnapshotSubmission } type SnapshotSubmission struct { @@ -23,7 +19,7 @@ type SnapshotSubmission struct { Co2 float32 `json:"co2"` } -func createSnapshotFromJson(jsonBodyStream io.ReadCloser) (*SnapshotSubmission, error) { +func createSnapshotSubFromJsonBodyStream(jsonBodyStream io.ReadCloser) (*SnapshotSubmission, error) { var snapshotSub SnapshotSubmission body, err := ioutil.ReadAll(jsonBodyStream) if err != nil { @@ -38,4 +34,12 @@ func createSnapshotFromJson(jsonBodyStream io.ReadCloser) (*SnapshotSubmission, return nil, fmt.Errorf("couldn't unmarshal json: %w", err) } return &snapshotSub, nil +} + +func createJsonFromSnapshotRecords(records []*SnapshotRecord) ([]byte, error) { + jsonResult, err := json.Marshal(records) + if err != nil { + return nil, err + } + return jsonResult, nil } \ No newline at end of file diff --git a/climate-server.go b/climate-server.go index 4c2a3fa..791e9bc 100644 --- a/climate-server.go +++ b/climate-server.go @@ -32,6 +32,7 @@ func teardown() { func startServer() { port := "8001" MainRouter.setGet("/", showCharts) + MainRouter.setGet("/data", sendData) MainRouter.setPost("/", saveSnapshot) fmt.Printf("Listening on port %s...\n", port) log.Fatal(http.ListenAndServe(":" + port, nil)) @@ -42,17 +43,25 @@ func showCharts(w http.ResponseWriter, r *http.Request) error { 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 { - snapshotSub, err := createSnapshotFromJson(r.Body) + snapshotSub, err := createSnapshotSubFromJsonBodyStream(r.Body) if err != nil { 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);", - snapshotSub.Temp, - snapshotSub.Humidity, - snapshotSub.Co2, - snapshotSub.Timestamp) - _, err = ClimateDb.Query(query) + err = writeSnapshotToDb(snapshotSub) if err != nil { return fmt.Errorf("couldn't submit snapshot into the database: %w", err) }