-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapi.go
135 lines (112 loc) · 3.05 KB
/
api.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package openminder
import (
"strconv"
"time"
"github.com/gin-gonic/gin"
)
// AttachAPI will attach an api to the minder so it can setup
// the appropriate endpoints
func (mdr *Minder) AttachAPI(api gin.IRouter) {
api.GET("/errors", mdr.errorsHandler())
api.GET("/calibrations", mdr.calibrationsHandler())
api.PUT("/calibrations/:field/:scale/:offset", mdr.calibrateHandler())
api.GET("/config", mdr.configHandler())
api.GET("/readings", mdr.readingsHandler())
api.PUT("/readings/calibrate/:field/:scale/:offset", mdr.calibrateHandler())
api.GET("/bus", mdr.busHandler())
api.PUT("/bus/scan", mdr.busScanHandler())
api.PUT("/bus/swap", mdr.busSwapHandler())
}
func (mdr *Minder) busScanHandler() func(*gin.Context) {
return func(c *gin.Context) {
go mdr.bus.Rescan()
c.JSON(202, map[string]string{"bus_url": "/v1/bus"})
}
}
func (mdr *Minder) busSwapHandler() func(*gin.Context) {
return func(c *gin.Context) {
mdr.swapECProbes()
c.JSON(200, map[string]string{"bus_url": "/v1/bus"})
}
}
func (mdr *Minder) busHandler() func(*gin.Context) {
return func(c *gin.Context) {
var scanstart interface{}
var scandone interface{}
if !mdr.bus.LastScanStart.IsZero() {
scanstart = time.Since(mdr.bus.LastScanStart).String()
}
if !mdr.bus.LastScanDone.IsZero() {
scanstart = time.Since(mdr.bus.LastScanDone).String()
}
data := map[string]interface{}{
"available": mdr.bus.Serials(),
"configured": map[string]string{
"irrig": mdr.cfg.IrrigECProbe,
"runoff": mdr.cfg.RunoffECProbe,
},
"last_scan_start": scanstart,
"last_scan_done": scandone,
}
c.JSON(200, data)
}
}
func (mdr *Minder) errorsHandler() func(*gin.Context) {
return func(c *gin.Context) {
c.JSON(200, mdr.errors.Map())
}
}
func (mdr *Minder) calibrationsHandler() func(*gin.Context) {
return func(c *gin.Context) {
data := map[string]calibration{}
for _, f := range translatableFields {
c, _ := mdr.tr.getCalibration(f)
data[f] = c
}
c.JSON(200, data)
}
}
func (mdr *Minder) configHandler() func(*gin.Context) {
return func(c *gin.Context) {
c.JSON(200, mdr.cfg)
}
}
func (mdr *Minder) readingsHandler() func(*gin.Context) {
return func(c *gin.Context) {
c.JSON(200, mdr.Readings)
}
}
func (mdr *Minder) calibrateHandler() func(*gin.Context) {
return func(c *gin.Context) {
field := c.Param("field")
if field == "" {
c.AbortWithStatusJSON(400, errmsg("field cannot be empty"))
return
}
scale, err := strconv.ParseFloat(c.Param("scale"), 64)
if err != nil {
c.AbortWithStatusJSON(400, errmsg("scale must be a float"))
return
}
offset, err := strconv.ParseFloat(c.Param("offset"), 64)
if err != nil {
c.AbortWithStatusJSON(400, errmsg("offset must be a float"))
return
}
err = mdr.tr.SetCalibration(field, scale, offset)
if err == ErrNotTranslatable {
c.AbortWithStatusJSON(400, errmsg("that field is not translatable"))
return
}
if err != nil {
c.AbortWithError(500, err)
return
}
c.Status(204)
}
}
func errmsg(msg string) interface{} {
return struct {
Msg string `json:"error"`
}{msg}
}