-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeeting_signal.go
69 lines (52 loc) · 1.34 KB
/
meeting_signal.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
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
)
type MeetingSignalData struct {
LightStatus bool
}
func main() {
cwd, _ := os.Getwd()
tmpl := template.Must(template.ParseFiles(filepath.Join(cwd, "meeting_signal.html")))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
formErr := r.ParseForm()
if formErr != nil {
log.Fatal(formErr)
}
switchAction := r.FormValue("switch")
if switchAction == "on" {
// fmt.Println("Switching light to on!")
_, onCmdErr := exec.Command("sh", "-c", "sudo ykushcmd ykushxs -u 0").Output()
if onCmdErr != nil {
log.Fatal(onCmdErr)
}
} else if switchAction == "off" {
// fmt.Println("Switching light to off!")
_, offCmdErr := exec.Command("sh", "-c", "sudo ykushcmd ykushxs -d 0").Output()
if offCmdErr != nil {
log.Fatal(offCmdErr)
}
}
out, cmdErr := exec.Command("sh", "-c", "sudo ykushcmd ykushxs -g").Output()
if cmdErr != nil {
log.Fatal(cmdErr)
}
// Convert bytes array to a string
responseOutput := string(out[:])
response := strings.TrimSpace(responseOutput)
status := response == "Downstream port is ON"
data := MeetingSignalData{
LightStatus: status,
}
tmpl.Execute(w, data)
})
fmt.Println("Listening on port 8080")
http.ListenAndServe(":8080", nil)
}