Working
This commit is contained in:
27
Database.go
27
Database.go
@@ -24,4 +24,31 @@ func CloseDb() {
|
|||||||
fmt.Println("error closing database: " + err.Error())
|
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
|
||||||
}
|
}
|
||||||
18
Snapshot.go
18
Snapshot.go
@@ -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 {
|
||||||
@@ -38,4 +34,12 @@ func createSnapshotFromJson(jsonBodyStream io.ReadCloser) (*SnapshotSubmission,
|
|||||||
return nil, fmt.Errorf("couldn't unmarshal json: %w", err)
|
return nil, fmt.Errorf("couldn't unmarshal json: %w", err)
|
||||||
}
|
}
|
||||||
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
|
||||||
}
|
}
|
||||||
@@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user