-
Notifications
You must be signed in to change notification settings - Fork 2
/
handlers_arduino.go
79 lines (63 loc) · 1.66 KB
/
handlers_arduino.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
package server
import (
"database/sql"
"fmt"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/usb-radiology/light-messenger/src/configuration"
"github.com/usb-radiology/light-messenger/src/lmdatabase"
)
func arduinoStatusHandler(config *configuration.Configuration, db *sql.DB, w http.ResponseWriter, r *http.Request) error {
w.Header().Set(HTMLHeaderContentType, HTMLHeaderContentTypeValueText)
vars := mux.Vars(r)
department := vars["department"]
status := lmdatabase.ArduinoStatus{
DepartmentID: department,
StatusAt: time.Now().Unix(),
}
{
errInsert := lmdatabase.ArduinoStatusInsert(db, status)
if errInsert != nil {
return errInsert
}
}
{
errWrite := writeBytes(w, []byte(fmt.Sprintf("%+v", status)))
if errWrite != nil {
return errors.WithStack(errWrite)
}
}
return nil
}
func openStatusHandler(config *configuration.Configuration, db *sql.DB, w http.ResponseWriter, r *http.Request) error {
w.Header().Set(HTMLHeaderContentType, HTMLHeaderContentTypeValueText)
vars := mux.Vars(r)
department := vars["department"]
notifications, err := lmdatabase.NotificationGetOpenNotificationsByDepartment(db, department)
if err != nil {
return err
}
if len(*notifications) > 0 {
arduinoPrioMap := map[int]string{
1: "HIGH",
2: "MEDIUM",
3: "LOW",
}
{
errWrite := writeBytes(w, []byte(fmt.Sprintf(";1;%v;", arduinoPrioMap[(*notifications)[0].Priority])))
if errWrite != nil {
return errors.WithStack(errWrite)
}
}
} else {
{
errWrite := writeBytes(w, []byte(fmt.Sprintf(";0;")))
if errWrite != nil {
return errors.WithStack(errWrite)
}
}
}
return nil
}