-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
409 lines (334 loc) · 11.2 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package main
import (
"context"
"crypto/tls"
"encoding/base64"
"encoding/json"
"fmt"
"html/template"
"io"
"log"
"net/http"
"os"
"strings"
"time"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)
type PrinterProgress struct {
Completion float64 `json:"completion"`
}
type PrinterResponse struct {
State string `json:"state"`
Progress PrinterProgress `json:"progress"`
}
func readFileContentsTrimmed(path string) string {
data, err := os.ReadFile(path)
if err != nil {
panic(err)
}
return strings.TrimSpace(string(data))
}
var username = "hakierspejs"
var password = readFileContentsTrimmed("secrets/http-password.txt")
var matrixUserID = readFileContentsTrimmed("secrets/matrix-user-id.txt")
var matrixUsername = readFileContentsTrimmed("secrets/matrix-username.txt")
var matrixPassword = readFileContentsTrimmed("secrets/matrix-password.txt")
var matrixRoomID = readFileContentsTrimmed("secrets/matrix-room-id.txt")
func main() {
go monitorPrinterState("ender-d")
go monitorPrinterState("ender-c")
http.HandleFunc("/", indexHandler)
http.HandleFunc("/ender-d/cam/", webcamHandler("ender-d"))
http.HandleFunc("/ender-d/status/", printersStatusHandler("ender-d"))
http.HandleFunc("/ender-d/cancel/", cancelPrintJob("ender-d"))
http.HandleFunc("/ender-d/", viewHandler("ender-d"))
http.HandleFunc("/ender-c/cam/", webcamHandler("ender-c"))
http.HandleFunc("/ender-c/status/", printersStatusHandler("ender-c"))
http.HandleFunc("/ender-c/cancel/", cancelPrintJob("ender-c"))
http.HandleFunc("/ender-c/", viewHandler("ender-c"))
http.HandleFunc("/lights/off/", lightsOff)
http.HandleFunc("/lights/on/", lightsOn)
fmt.Println("Server is listening on port 5000...")
if err := http.ListenAndServe("0.0.0.0:5000", nil); err != nil {
fmt.Println("Error starting server:", err)
}
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
// Check basic auth
auth := r.Header.Get("Authorization")
expectedAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(username+":"+password))
if auth != expectedAuth {
w.Header().Set("WWW-Authenticate", `Basic realm="Login Required"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
// Print a list with links to supported printers
_, _ = fmt.Fprintf(w, "<h1>Supported printers:</h1>")
_, _ = fmt.Fprintf(w, "<ul>")
_, _ = fmt.Fprintf(w, "<li><a href=\"/ender-d/\">Ender D</a></li>")
_, _ = fmt.Fprintf(w, "<li><a href=\"/ender-c/\">Ender C</a></li>")
_, _ = fmt.Fprintf(w, "</ul>")
}
func getPrinterState(printer string) (PrinterResponse, error) {
// Read API key from file
apiKey := readFileContentsTrimmed("secrets/" + printer + "-api-key.txt")
// Create HTTP request
req, err := http.NewRequest("GET", "https://opti3d.siedziba.hs-ldz.pl/"+printer+"/api/job", nil)
if err != nil {
return PrinterResponse{}, fmt.Errorf("failed to create HTTP request: %w", err)
}
// Add API key header
req.Header.Add("X-Api-Key", apiKey)
// Create HTTP client that skips SSL verification
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
// Make the request
resp, err := client.Do(req)
if err != nil {
return PrinterResponse{}, fmt.Errorf("failed to execute HTTP request: %w", err)
}
defer func() {
_ = resp.Body.Close()
}()
// Read response body
body, err := io.ReadAll(resp.Body)
if err != nil {
return PrinterResponse{}, fmt.Errorf("failed to read response body: %w", err)
}
// Parse JSON
var printerResp PrinterResponse
if err := json.Unmarshal(body, &printerResp); err != nil {
return PrinterResponse{}, fmt.Errorf("failed to parse JSON response: %w", err)
}
return printerResp, nil
}
// curl -k --json '{"command": "cancel"}' 'https://localhost/ender-c/api/job' -H "X-Api-Key: `cat ~/.octo-ender-c-api`"
func cancelPrintJob(printer string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Read API key from file
apiKey := readFileContentsTrimmed("secrets/" + printer + "-api-key.txt")
// Create HTTP request
req, err := http.NewRequest("POST", "https://opti3d.siedziba.hs-ldz.pl/"+printer+"/api/job", strings.NewReader(`{"command": "cancel"}`))
if err != nil {
log.Printf("failed to create HTTP request: %v", err)
}
// Add API key header
req.Header.Add("X-Api-Key", apiKey)
req.Header.Add("Content-Type", "application/json")
// Create HTTP client that skips SSL verification
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
// Make the request
resp, err := client.Do(req)
if err != nil {
log.Printf("failed to execute HTTP request: %v", err)
}
defer func() {
_ = resp.Body.Close()
}()
// Read response body
respBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("failed to read response body: %v", err)
}
msg := fmt.Sprintf("Cancel button clicked. Response: %s", respBytes)
log.Println(msg)
sendMatrixMsg(msg)
}
}
func getClient() (*mautrix.Client, id.RoomID, error) {
// Read password from file
// Create a new Matrix client
client, err := mautrix.NewClient("https://matrix.org", id.UserID(matrixUserID), "")
if err != nil {
return nil, "", fmt.Errorf("failed to create Matrix client: %w", err)
}
// Login with password
ctx := context.Background()
resp, err := client.Login(ctx, &mautrix.ReqLogin{
Type: mautrix.AuthTypePassword,
Identifier: mautrix.UserIdentifier{
Type: mautrix.IdentifierTypeUser,
User: matrixUsername,
},
Password: matrixPassword,
})
if err != nil {
return nil, "", fmt.Errorf("failed to login: %w", err)
}
// Set the access token from the login response
client.AccessToken = resp.AccessToken
return client, id.RoomID(matrixRoomID), nil
}
func sendMatrixMsg(msg string) {
client, roomID, err := getClient()
if err != nil {
log.Printf("failed to get Matrix client: %v", err)
return
}
// Send the message
ctx := context.Background()
sendResp, err := client.SendMessageEvent(ctx, roomID, event.EventMessage, &event.MessageEventContent{
MsgType: event.MsgText,
Body: msg,
})
if err != nil {
log.Printf("failed to send message: %v", err)
return
}
log.Printf("sent text message(msg=%q) => %s", msg, sendResp.EventID)
}
func monitorPrinterState(printer string) {
log.Println("Starting printer state monitoring...")
// Get initial state
resp, err := getPrinterState(printer)
if err != nil {
log.Printf("initial state check failed: %v", err)
}
currentState := resp.State
log.Printf("Initial printer state: %s", currentState)
// Poll for state changes every 30 seconds
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
for range ticker.C {
resp, err := getPrinterState(printer)
if err != nil {
log.Printf("Error checking printer state: %v", err)
continue
}
newState := resp.State
// If state changed, send notification
if newState != currentState {
log.Printf("Printer %s state changed: %s -> %s", printer, currentState, newState)
message := fmt.Sprintf("🖨️ Printer %s state changed: %s -> %s", printer, currentState, newState)
sendMatrixMsg(message)
// Update current state
currentState = newState
}
}
}
func webcamHandler(printer string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Uwierzytelnienie Basic Auth
auth := r.Header.Get("Authorization")
expectedAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(username+":"+password))
if auth != expectedAuth {
w.Header().Set("WWW-Authenticate", `Basic realm="Login Required"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
var streamURL = "https://opti3d.siedziba.hs-ldz.pl/" + printer + "/webcam/?action=stream"
// Żądanie strumienia wideo od zewnętrznego serwera
req, err := http.NewRequest("GET", streamURL, nil)
if err != nil {
http.Error(w, "Error creating request", http.StatusInternalServerError)
return
}
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(username+":"+password)))
// Wyłącz weryfikację SSL (tylko w przypadku problemów z certyfikatami SSL)
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
resp, err := client.Do(req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
defer func() {
_ = resp.Body.Close()
}()
// Przekaż nagłówki odpowiedzi do klienta
for k, v := range resp.Header {
w.Header()[k] = v
}
// Ustaw rodzaj treści na multipart
w.Header().Set("Content-Type", resp.Header.Get("Content-Type"))
// Przewód treści strumienia do klienta
if _, err = io.Copy(w, resp.Body); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
func printersStatusHandler(printer string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
expectedAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(username+":"+password))
if auth != expectedAuth {
w.Header().Set("WWW-Authenticate", `Basic realm="Login Required"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
resp, err := getPrinterState(printer)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Ustaw nagłówki odpowiedzi
w.Header().Set("Content-Type", "application/json")
// Zwróć stan drukarki jako JSON
_ = json.NewEncoder(w).Encode(resp)
}
}
func viewHandler(printer string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
expectedAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(username+":"+password))
if auth != expectedAuth {
w.Header().Set("WWW-Authenticate", `Basic realm="Login Required"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
tmpl, err := template.ParseFiles("view_template.html")
if err != nil {
http.Error(w, "Could not load template", http.StatusInternalServerError)
return
}
data := struct {
Printer string
}{
Printer: printer,
}
w.Header().Set("Content-Type", "text/html")
_ = tmpl.Execute(w, data)
}
}
func lightsSet(w http.ResponseWriter, r *http.Request, power string) {
auth := r.Header.Get("Authorization")
expectedAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(username+":"+password))
if auth != expectedAuth {
w.Header().Set("WWW-Authenticate", `Basic realm="Login Required"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
url := "http://10.14.22.148/cm?cmnd=Power%20" + power
//url := "http://localhost/cm?cmnd=Power%20" + power
req, err := http.NewRequest("GET", url, nil)
if err != nil {
http.Error(w, "Error creating request", http.StatusInternalServerError)
return
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
defer func() {
_ = resp.Body.Close()
}()
w.WriteHeader(http.StatusOK)
}
func lightsOn(w http.ResponseWriter, r *http.Request) {
lightsSet(w, r, "On")
}
func lightsOff(w http.ResponseWriter, r *http.Request) {
lightsSet(w, r, "Off")
}