tempestas

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

commit fb529558596f8b4ac1bd76cc2c36473324ddd2a7
parent 40fda0087b62c8a91923fa5615b51ebe691e0e67
Author: Chris Bracken <chris@bracken.jp>
Date:   Fri, 12 Nov 2021 10:25:30 -0800

Add JSON API to query list of sensors

Adds a JSON API with endpoint /sensor/sensors/ to query the list of
avaialble sensors.

Diffstat:
Mhttp/http.go | 12++++++++++++
Mstorage/storage.go | 32++++++++++++++++++++++++++++++++
2 files changed, 44 insertions(+), 0 deletions(-)

diff --git a/http/http.go b/http/http.go @@ -26,6 +26,7 @@ func (s HttpServer) ListenAndServe(addr string) error { mux := goji.NewMux() mux.HandleFunc(pat.Post("/sensor/airrohr/"), s.PostAirrohr) mux.HandleFunc(pat.Get("/sensor/reading/:sensorId/:readingType"), s.QueryReadings) + mux.HandleFunc(pat.Get("/sensor/sensors/"), s.QuerySensors) return http.ListenAndServe(addr, mux) } @@ -72,6 +73,17 @@ func (s HttpServer) QueryReadings(w http.ResponseWriter, r *http.Request) { writeResponse(w, resp, http.StatusOK) } +func (s HttpServer) QuerySensors(w http.ResponseWriter, r *http.Request) { + sensors, err := s.ds.QuerySensors() + if err != nil { + fmt.Println("Error: failed to query sensors: " + err.Error()) + errorResponse(w, "Bad Request. "+err.Error(), http.StatusBadRequest) + return + } + resp, _ := json.Marshal(sensors) + writeResponse(w, resp, http.StatusOK) +} + func errorResponse(w http.ResponseWriter, message string, httpStatusCode int) { resp := make(map[string]string) resp["message"] = message diff --git a/storage/storage.go b/storage/storage.go @@ -11,6 +11,7 @@ const ( INSERT_READING = "INSERT INTO " + "sensor_data(sensor_id, sw_version, reading_time, reading_type, reading_value) " + "VALUES($1, $2, $3, $4, $5)" + QUERY_SENSORS = "SELECT DISTINCT sensor_id FROM sensor_data ORDER BY sensor_id" QUERY_READINGS = "SELECT " + " sensor_id, sw_version, reading_time, reading_type, reading_value " + "FROM " + @@ -29,10 +30,15 @@ type Reading struct { Value float64 `json:"value"` } +type Sensor struct { + SensorId string `json:"sensor_id"` +} + type DataStore interface { Close() StoreReading(r *Reading) error QueryReadings(sensorId string, readingType string, start time.Time, end time.Time) ([]Reading, error) + QuerySensors() ([]Sensor, error) } type PostgresDataStore struct { @@ -93,6 +99,27 @@ func (s *PostgresDataStore) QueryReadings(sensorId string, return readings, nil } +func (s *PostgresDataStore) QuerySensors() ([]Sensor, error) { + rows, err := s.db.Query(QUERY_SENSORS) + if err != nil { + return nil, err + } + defer rows.Close() + + var sensors []Sensor + for rows.Next() { + var s Sensor + if err := rows.Scan(&s.SensorId); err != nil { + return sensors, err + } + sensors = append(sensors, s) + } + if err = rows.Err(); err != nil { + return sensors, err + } + return sensors, nil +} + type NullDataStore struct { } @@ -116,3 +143,8 @@ func (s *NullDataStore) QueryReadings( var readings []Reading return readings, nil } + +func (s *NullDataStore) QuerySensors() ([]Sensor, error) { + var sensors []Sensor + return sensors, nil +}