tempestas

A REST API for processing sensor.community data
git clone https://git.bracken.jp/tempestas.git
Log | Files | Refs | README | LICENSE

commit 9b2c1d174c7c7137443de1f202cd4fd92fd9e207
parent 2d9e0f59faf22edaba52811c21e10fc4a2d42704
Author: Chris Bracken <chris@bracken.jp>
Date:   Thu, 11 Nov 2021 15:47:21 -0800

Create storage.Reading type

This decouples the storage layer from the JSON representation of
readings and provides a type we can return when we add the ability to
query the storage layer for records.

Diffstat:
Mhttp/http.go | 11+++++++----
Mstorage/storage.go | 28++++++++++++++++------------
2 files changed, 23 insertions(+), 16 deletions(-)

diff --git a/http/http.go b/http/http.go @@ -34,16 +34,19 @@ func (s HttpServer) PostAirrohr(w http.ResponseWriter, r *http.Request) { errorResponse(w, "Content-Type must be application/json", http.StatusUnsupportedMediaType) return } - var report airrohr.Report - err := airrohr.Parse(r.Body, &report) + var rpt airrohr.Report + err := airrohr.Parse(r.Body, &rpt) if err != nil { fmt.Println("Error: unknown JSON format from sensor: " + err.Error()) errorResponse(w, "Bad Request. "+err.Error(), http.StatusBadRequest) return } t := time.Now().UTC() - fmt.Println(t.String() + " Report received from sensor " + report.SensorId) - s.ds.StoreReport(&report, t) + fmt.Println(t.String() + " Report received from sensor " + rpt.SensorId) + for _, v := range rpt.Values { + rdg := storage.Reading{rpt.SensorId, rpt.SoftwareVersion, t, v.Type, v.Value} + s.ds.StoreReading(&rdg) + } errorResponse(w, "Success", http.StatusOK) } diff --git a/storage/storage.go b/storage/storage.go @@ -5,19 +5,25 @@ import ( "database/sql" "fmt" "time" - - "git.bracken.jp/tempestas/airrohr" ) const ( - INSERT_REPORT = "INSERT INTO " + + INSERT_READING = "INSERT INTO " + "sensor_data(sensor_id, sw_version, reading_time, reading_type, reading_value) " + "VALUES($1, $2, $3, $4, $5)" ) +type Reading struct { + SensorId string + SoftwareVersion string + Time time.Time + Type string + Value float64 +} + type DataStore interface { Close() - StoreReport(report *airrohr.Report, t time.Time) + StoreReading(r *Reading) } type PostgresDataStore struct { @@ -48,16 +54,14 @@ func (s *PostgresDataStore) Close() { s.db.Close() } -func (s *PostgresDataStore) StoreReport(report *airrohr.Report, t time.Time) { +func (s *PostgresDataStore) StoreReading(r *Reading) { if s.db == nil { panic("Not connected to database") } - for _, v := range report.Values { - _, err := s.db.Exec(INSERT_REPORT, report.SensorId, report.SoftwareVersion, t, v.Type, v.Value) - if err != nil { - fmt.Println("Error: failed to write sensor data") - fmt.Println(err) - } + _, err := s.db.Exec(INSERT_READING, r.SensorId, r.SoftwareVersion, r.Time, r.Type, r.Value) + if err != nil { + fmt.Println("Error: failed to write sensor data") + fmt.Println(err) } } @@ -71,5 +75,5 @@ func ConnectNull() *NullDataStore { func (s *NullDataStore) Close() { } -func (s *NullDataStore) StoreReport(report *airrohr.Report, t time.Time) { +func (s *NullDataStore) StoreReading(r *Reading) { }