-
Notifications
You must be signed in to change notification settings - Fork 0
/
httphandler.go
99 lines (83 loc) · 2.62 KB
/
httphandler.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
package collector
import (
"net/http"
"strconv"
"time"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
)
// Store a reading from a ShellyHT
// note that this does *not* follow gin's regular parameter syntax
func HandleStoreReading(c *gin.Context, db *DB) {
tempStr := c.Query("temp")
humStr := c.Query("hum")
idStr := c.Query("id")
if tempStr == "" || humStr == "" || idStr == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Missing query parameter"})
return
}
temp, err := strconv.ParseFloat(tempStr, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid temp query parameter"})
return
}
hum, err := strconv.ParseFloat(humStr, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid hum query parameter"})
return
}
// TODO add check on length of stationid
db.SaveReading(temp, hum, idStr)
c.JSON(http.StatusOK, Reading{StationId: idStr, Temperature: temp, Humidity: hum})
// prometheus gauges
Temperature.With(prometheus.Labels{"stationid": idStr}).Set(temp)
Humidity.With(prometheus.Labels{"stationid": idStr}).Set(hum)
}
func HandleReadings(c *gin.Context, db *DB) {
// Calculate the timestamp 24 hours ago
since := time.Now().Add(-24 * time.Hour)
idStr := c.Param("id")
if idStr == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Missing stationid"})
return
}
// Retrieve readings from the database since the specified time
readings, err := db.GetReadingsSince(since, idStr)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Cannot retrieve readings"})
return
}
c.JSON(http.StatusOK, readings)
}
func HandleAllReadings(c *gin.Context, db *DB) {
// Calculate the timestamp 24 hours ago
since := time.Now().Add(-24 * time.Hour)
// Retrieve readings from the database since the specified time
readings, err := db.GetAllReadingsSince(since)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Cannot retrieve readings"})
return
}
c.JSON(http.StatusOK, readings)
}
func HandleAllNames(c *gin.Context, db *DB) {
stationnames, err := db.GetStationNames()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Cannot retrieve stationnames"})
return
}
c.JSON(http.StatusOK, stationnames)
}
func HandleName(c *gin.Context, db *DB) {
var station Station
if err := c.ShouldBind(&station); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := db.UpdateName(&station); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Cannot update stationname"})
return
} else {
c.JSON(http.StatusCreated, station)
}
}