Skip to content

Commit

Permalink
implement csv writing. yet to be tested
Browse files Browse the repository at this point in the history
  • Loading branch information
ubergesundheit committed Dec 3, 2017
1 parent b8b0f12 commit 58673fe
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 24 deletions.
10 changes: 9 additions & 1 deletion cmd/senseboxpi/senseboxpi.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func readFlags() (configPath, csvOutputPath string, offline bool) {
}

func main() {
configPath, _, offline := readFlags()
configPath, csvOutputPath, offline := readFlags()

configBytes, err := ioutil.ReadFile(configPath)
if err != nil {
Expand All @@ -54,6 +54,13 @@ func main() {
log.Fatal(err)
}

if csvOutputPath != "" {
err := senseBox.AppendCSV(csvOutputPath)
if err != nil {
log.Fatal(err)
}
}

if offline == false {
errs := senseBox.SubmitMeasurements()
if errs != nil {
Expand All @@ -62,4 +69,5 @@ func main() {
}
}
}
senseBox.ClearMeasurements()
}
60 changes: 50 additions & 10 deletions sensebox/sensebox.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package sensebox

import (
"encoding/csv"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"time"

"github.com/parnurzeal/gorequest"
)

type id string

type senseBox struct {
ID id `json:"_id"`
Sensors []*sensor `json:"sensors"`
PostDomain string `json:"postDomain"`
ID id `json:"_id"`
Sensors []*sensor `json:"sensors"`
PostDomain string `json:"postDomain"`
measurements []measurement
}

func validateID(id string) error {
Expand Down Expand Up @@ -45,18 +50,16 @@ func NewFromJSON(jsonBytes []byte) (senseBox, error) {
// SubmitMeasurements tries to send the measurements of the Sensors of the senseBox
// to the openSenseMap
func (s *senseBox) SubmitMeasurements() []error {
var measurements []measurement
for _, sensor := range s.Sensors {
measurements = append(measurements, sensor.measurements...)
// clear measurements
sensor.measurements = nil
if len(s.measurements) == 0 {
return []error{errors.New("No measurements. Did you forgot to call ReadSensors?")}
}

postURL, err := url.Parse("https://" + s.PostDomain + "/boxes/" + string(s.ID) + "/data")
if err != nil {
return []error{err}
}
resp, body, errs := gorequest.New().Post(postURL.String()).
Send(measurements).
Send(s.measurements).
End()

if errs != nil {
Expand All @@ -68,15 +71,25 @@ func (s *senseBox) SubmitMeasurements() []error {
return nil
}

// ClearMeasurements clears the measurements previously read through ReadSensors
func (s *senseBox) ClearMeasurements() {
s.measurements = make([]measurement, len(s.Sensors))
}

// ReadSensors reads measurements from all sensors
func (s *senseBox) ReadSensors() error {
for _, sensor := range s.Sensors {
if err := sensor.AddMeasurementReading(); err != nil {
m, err := sensor.ReadMeasurement()
if err != nil {
return err
}
s.measurements = append(s.measurements, m)
}
return nil
}

// ReadSensorsAndSubmitMeasurements takes readings from all sensors and submits
// these measurements through calling SubmitMeasurements
func (s *senseBox) ReadSensorsAndSubmitMeasurements() []error {
err := s.ReadSensors()
if err != nil {
Expand All @@ -85,3 +98,30 @@ func (s *senseBox) ReadSensorsAndSubmitMeasurements() []error {

return s.SubmitMeasurements()
}

func (s *senseBox) AppendCSV(path string) error {
if len(s.measurements) == 0 {
return errors.New("No measurements. Did you forgot to call ReadSensors?")
}

// If the file doesn't exist, create it, or append to the file
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()

writer := csv.NewWriter(f)
defer writer.Flush()

// csv-stringify the measurements
for _, measurement := range s.measurements {
m := []string{measurement.Sensor.ID.String(), measurement.Value.String(), measurement.Timestamp.Format(time.RFC3339)}
writer.Write(m)
}

if err := writer.Error(); err != nil {
return err
}
return nil
}
16 changes: 4 additions & 12 deletions sensebox/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ type sensor struct {
ID id `json:"_id"`
Phenomenon string `json:"phenomenon"`
SensorType string `json:"sensorType"`
measurements []measurement
sensorDevice sensors.SensorI
}

Expand Down Expand Up @@ -46,18 +45,11 @@ func (s *sensor) TakeReading() (float64, error) {
return s.sensorDevice.ReadValue(s.Phenomenon)
}

// AddMeasurementReading calls the sensors TakeReading function and adds the
//result to the sensors measurements through AddMeasurement
func (s *sensor) AddMeasurementReading() error {
// ReadMeasurement calls the sensors TakeReading function and returns a measurement
func (s *sensor) ReadMeasurement() (measurement, error) {
reading, err := s.TakeReading()
if err != nil {
return err
return measurement{}, err
}
s.AddMeasurement(reading, time.Now())
return nil
}

// AddMeasurement adds a new measurement to the Sensor
func (s *sensor) AddMeasurement(value float64, timestamp time.Time) {
s.measurements = append(s.measurements, measurement{s, number(value), timestamp.UTC()})
return measurement{s, number(reading), time.Now().UTC()}, nil
}
13 changes: 12 additions & 1 deletion sensebox/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ type number float64
// MarshalJSON of type number formats the float with two decimals and trims
// excess zeroes and dots
func (f number) MarshalJSON() ([]byte, error) {
return []byte(strings.TrimRight(strings.TrimRight(fmt.Sprintf("%.2f", f), "0"), ".")), nil
return []byte(f.String()), nil
}

// String returns the numbers string representation (float with two decimals and
// trimed excess zeroes and dot
func (f number) String() string {
return strings.TrimRight(strings.TrimRight(fmt.Sprintf("%.2f", f), "0"), ".")
}

// UnmarshalJSON of type id checks the id for validity
Expand All @@ -29,3 +35,8 @@ func (i *id) UnmarshalJSON(jsonBytes []byte) error {

return nil
}

// String returns the id as 24 digit hex string
func (i *id) String() string {
return string(*i)
}

0 comments on commit 58673fe

Please sign in to comment.