-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
56 lines (43 loc) · 1.22 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"errors"
"strconv"
"time"
"github.com/dancannon/gorethink"
)
// Reading reading used to persist in rethindb
type Reading struct {
ID string
Value float64
SType string
TS time.Time
}
// SensorService small sensor service for recording readings
type SensorService interface {
Record(string, string, string) error
}
type sensorService struct {
session *gorethink.Session
dbName string
}
// NewSensorService new user store
func NewSensorService(session *gorethink.Session) SensorService {
return &sensorService{session, "sensor_data"}
}
func (s *sensorService) Record(id, value, stype string) error {
f, err := strconv.ParseFloat(value, 64)
if err != nil {
return ErrInvalid
}
r := &Reading{id, f, stype, time.Now()}
if _, err = gorethink.Table("sensor_data").Insert(r).RunWrite(s.session); err != nil {
return ErrInternal
}
return nil
}
// ErrInvalid is returned when a reading fails validation
var ErrInvalid = errors.New("invalid reading")
// ErrInternal is returned when an internal error occurs
var ErrInternal = errors.New("internal server error")
// ServiceMiddleware is a chainable behavior modifier for SensorService.
type ServiceMiddleware func(SensorService) SensorService