-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
173 lines (151 loc) · 4.19 KB
/
main.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"context"
"fmt"
"html/template"
"net/http"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"time"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
)
func main() {
// Load configuration
viper.SetConfigName("config")
viper.AddConfigPath(".")
viper.SetConfigType("yaml")
if err := viper.ReadInConfig(); err != nil {
panic(fmt.Errorf("Fatal error config file: %s", err))
}
remoteName := viper.GetString("remote_name")
localPort := viper.GetString("port")
// get hostname from OS
hostname, _ := os.Hostname()
// load LIRC remote commands
commands := getIrCommands(remoteName)
fmt.Println("[Web-server] http://" + hostname + "/")
router := gin.Default()
gin.SetMode(gin.ReleaseMode)
for _, command := range commands {
router.GET("/"+strings.ToLower(command), irsendHandler(remoteName, command))
}
router.GET("/", func(c *gin.Context) {
htmlContent, err := generateHTML(commands)
if err != nil {
c.String(http.StatusInternalServerError, "Error generating HTML: %s", err)
return
}
c.Data(http.StatusOK, "text/html; charset=utf-8", []byte(htmlContent))
})
server := &http.Server{
// Addr: localIP + ":" + localPort,
Addr: ":" + localPort,
Handler: router,
}
go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
panic(fmt.Errorf("Fatal error starting server: %s", err))
}
}()
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
<-sig
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
fmt.Println("Error during server shutdown:", err)
}
}
func getIrCommands(remoteName string) []string {
fmt.Println("[EXEC] irsend LIST " + remoteName + " \"\"")
cmd := exec.Command("irsend", "LIST", remoteName, "")
output, err := cmd.Output()
if err != nil {
panic(fmt.Errorf("Error listing IR commands: %s %s", strings.Replace(string(output), "\n\n", "\n", -1), strings.Replace(err.Error(), "\n\n", "", -1)))
}
lines := strings.Split(string(output), "\n")
var commands []string
for _, line := range lines {
parts := strings.Fields(line)
if len(parts) == 2 {
commands = append(commands, parts[1])
}
}
return commands
}
func irsendHandler(remoteName, command string) gin.HandlerFunc {
return func(c *gin.Context) {
fmt.Println("[EXEC] irsend SEND_ONCE " + remoteName + " " + command)
cmd := exec.Command("irsend", "SEND_ONCE", remoteName, command)
if err := cmd.Run(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to send IR command"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "IR command sent successfully"})
}
}
func generateHTML(commands []string) (string, error) {
const tpl = `
<!DOCTYPE html>
<html>
<head>
<title>IR Commands</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f4f4f4;
}
h1 {
text-align: center;
}
.container {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.button {
display: block;
width: 150px;
margin: 10px;
padding: 15px;
text-align: center;
background-color: #007BFF;
color: white;
text-decoration: none;
border-radius: 5px;
transition: background-color 0.3s;
}
.button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<h1>IR Commands</h1>
<div class="container">
{{range $index, $element := .}}
{{if eq (mod $index 3) 0}}
</div><div class="container">
{{end}}
<a href="/{{$element | ToLower}}" class="button">{{$element}}</a>
{{end}}
</div>
</body>
</html>
`
t, err := template.New("index").Funcs(template.FuncMap{"ToLower": strings.ToLower, "mod": func(i, j int) int { return i % j }}).Parse(tpl)
if err != nil {
return "", err
}
var htmlContent strings.Builder
if err := t.Execute(&htmlContent, commands); err != nil {
return "", err
}
return htmlContent.String(), nil
}