-
Notifications
You must be signed in to change notification settings - Fork 5
/
management-interface.go
642 lines (560 loc) · 17.3 KB
/
management-interface.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/*
management-interface - Web based management of Raspberry Pis over WiFi
Copyright (C) 2019, The Cacophony Project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package managementinterface
import (
"encoding/json"
"html/template"
"io"
"net"
"net/http"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"time"
"github.com/TheCacophonyProject/audiobait/v3/audiofilelibrary"
"github.com/TheCacophonyProject/audiobait/v3/playlist"
goconfig "github.com/TheCacophonyProject/go-config"
"github.com/TheCacophonyProject/rpi-net-manager/netmanagerclient"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"github.com/TheCacophonyProject/go-utils/logging"
"github.com/gobuffalo/packr"
"github.com/gorilla/mux"
)
var log = logging.NewLogger("info")
// Using a packr box means the html files are bundled up in the binary application.
var templateBox = packr.NewBox("./html")
// tmpl is our pointer to our parsed templates.
var tmpl *template.Template
// This does some initialisation. It parses our html templates up front and
// finds the location where this executable was started.
func init() {
// The name of the device we are running this executable on.
deviceName := getDeviceName()
tmpl = template.New("")
tmpl.Funcs(template.FuncMap{"DeviceName": func() string { return deviceName }})
for _, name := range templateBox.List() {
t := tmpl.New(name)
s, err := templateBox.FindString(name)
if err != nil {
log.Fatal(err)
}
template.Must(t.Parse(s))
}
}
// NetworkConfig is a struct to store our network configuration values in.
type NetworkConfig struct {
Online bool `yaml:"online"`
}
// Get the host name (device name) this executable was started on.
// Store it in a module level variable. It is inserted into the html templates at run time.
func getDeviceName() string {
name, err := os.Hostname()
if err != nil {
log.Println(err.Error())
return "Unknown"
}
// Make sure we handle the case when name could be something like: 'host.corp.com'
// If it is, just use the part before the first dot.
return strings.SplitN(name, ".", 2)[0]
}
// Return the serial number for the Raspberr Pi in the device.
func getRaspberryPiSerialNumber() string {
if runtime.GOOS == "windows" {
return ""
}
// The /proc/cpuinfo file normally contains a serial number.
file, err := os.Open("/proc/cpuinfo")
if err != nil {
return ""
}
defer file.Close()
out, err := io.ReadAll(file)
if err != nil {
return ""
}
// Extract the serial number.
serialNumber := ""
rows := strings.Split(string(out), "\n")
for _, row := range rows {
parts := strings.Split(row, ":")
if len(parts) == 2 {
field := strings.ToUpper(strings.TrimSpace(parts[0]))
if field == "SERIAL" {
return strings.TrimSpace(parts[1])
}
}
}
return serialNumber
}
// Return the salt minion ID for the device.
func getSaltMinionID() string {
return strings.TrimSpace(readFile("/etc/salt/minion_id"))
}
// Return the time of the last salt update.
func getLastSaltUpdate() string {
timeStr := strings.TrimSpace(readFile("/etc/cacophony/last-salt-update"))
if timeStr == "" {
return ""
}
t, err := time.Parse(time.RFC3339, timeStr)
if err != nil {
return ""
}
return t.Format("2006-01-02 15:04:05")
}
// Return context from file returning an empty string if on windows or if read fails
func readFile(file string) string {
if runtime.GOOS == "windows" {
return ""
}
// The /etc/salt/minion_id file contains the ID.
out, err := os.ReadFile(file)
if err != nil {
return ""
}
return string(out)
}
// Return info on the disk space available, disk space used etc.
func getDiskSpace() (string, error) {
var out []byte
err := error(nil)
if runtime.GOOS == "windows" {
// On Windows, commands need to be handled like this:
out, err = exec.Command("cmd", "/C", "dir").Output()
} else {
// 'Nix. Run df command to show disk space available on SD card.
out, err = exec.Command("sh", "-c", "df -h").Output()
}
if err != nil {
log.Println(err.Error())
return err.Error(), err
}
return string(out), nil
}
// Return info on memory e.g. memory used, memory available etc.
func getMemoryStats() (string, error) {
var out []byte
err := error(nil)
if runtime.GOOS == "windows" {
// Will show more than just memory stuff.
out, err = exec.Command("cmd", "/C", "systeminfo").Output()
} else {
// 'Nix. Run vmstat command to show memory stats.
out, err = exec.Command("sh", "-c", "vmstat -s").Output()
}
if err != nil {
log.Println(err.Error())
return err.Error(), err
}
return string(out), nil
}
// DiskMemoryHandler shows disk space usage and memory usage
func DiskMemoryHandler(w http.ResponseWriter, r *http.Request) {
diskData, err := getDiskSpace()
if err != nil {
log.Fatal(err)
}
// Want to separate this into separate fields so that can display in a table in HTML
outputStrings := [][]string{}
rows := strings.Split(diskData, "\n")
for _, row := range rows[1:] {
words := strings.Fields(row)
if len(words) >= 6 {
words[0], words[5] = words[5], words[0] // This swaps these 2 columns
outputStrings = append(outputStrings, words)
}
}
memoryData, err := getMemoryStats()
if err != nil {
log.Fatal(err)
}
// Want to separate this into separate fields so that can display in a table in HTML
outputStrings2 := [][]string{}
rows = strings.Split(memoryData, "\n")
for _, row := range rows[1:] {
cleanRow := strings.Trim(row, " \t")
words := strings.SplitN(cleanRow, " ", 2)
if len(words) > 1 && strings.HasPrefix(words[1], "K ") {
words[0] = words[0] + " K"
words[1] = words[1][2:]
words[0], words[1] = words[1], words[0] // This reverses the 2 columns
titleCaser := cases.Title(language.Und)
words[0] = titleCaser.String(words[0])
}
outputStrings2 = append(outputStrings2, words)
if words[0] == "Free Swap" {
// Don't want any of the output after this line.
break
}
}
// Put it all in a struct so we can access it from HTML
type table struct {
NumDiskRows int
DiskDataRows [][]string
NumMemoryRows int
MemoryDataRows [][]string
}
outputStruct := table{
NumDiskRows: len(outputStrings), DiskDataRows: outputStrings,
NumMemoryRows: len(outputStrings2), MemoryDataRows: outputStrings2,
}
// Execute the actual template.
tmpl.ExecuteTemplate(w, "disk-memory.html", outputStruct)
}
// IndexHandler is the root handler.
func IndexHandler(w http.ResponseWriter, r *http.Request) {
tmpl.ExecuteTemplate(w, "index.html", nil)
}
// AdvancedMenuHandler is a screen to more advanced settings.
func AdvancedMenuHandler(w http.ResponseWriter, r *http.Request) {
tmpl.ExecuteTemplate(w, "advanced.html", nil)
}
// Get the IP address for a given interface. There can be 0, 1 or 2 (e.g. IPv4 and IPv6)
func getIPAddresses(iface net.Interface) []string {
var IPAddresses []string
addrs, err := iface.Addrs()
if err != nil {
return IPAddresses // Blank entry.
}
for _, addr := range addrs {
IPAddresses = append(IPAddresses, " "+addr.String())
}
return IPAddresses
}
// NetworkHandler - Show the status of each network interface
func NetworkHandler(w http.ResponseWriter, r *http.Request) {
// Type used in serving interface information.
type interfaceProperties struct {
Name string
IPAddresses []string
}
type networkState struct {
Interfaces []interfaceProperties
Config NetworkConfig
ErrorEncountered bool
ErrorMessage string
}
errorMessage := ""
ifaces, err := net.Interfaces()
interfaces := []interfaceProperties{}
if err != nil {
errorMessage = err.Error()
} else {
// Filter out loopback interfaces
for _, iface := range ifaces {
if iface.Flags&net.FlagLoopback == 0 {
// Not a loopback interface
addresses := getIPAddresses(iface)
ifaceProperties := interfaceProperties{Name: iface.Name, IPAddresses: addresses}
interfaces = append(interfaces, ifaceProperties)
}
}
}
config := &NetworkConfig{
Online: true,
}
state := networkState{
Interfaces: interfaces,
Config: *config,
ErrorEncountered: err != nil,
ErrorMessage: errorMessage,
}
// Need to respond to individual requests to test if a network status is up or down.
tmpl.ExecuteTemplate(w, "network.html", state)
}
type wifiNetwork struct {
ID string
NetworkID int
}
// WifiNetworkHandler show the wireless networks listed in the wpa_supplicant configuration
func WifiNetworkHandler(w http.ResponseWriter, r *http.Request) {
type wifiProperties struct {
AvailableNetworks []string
Networks []wifiNetwork
Error string
}
wifiProps := wifiProperties{}
wifiNetworks, err := netmanagerclient.ListUserSavedWifiNetworks()
if err != nil {
log.Println(err)
wifiProps.Error = "Error while getting saved networks: " + err.Error()
}
wifiProps.Networks = []wifiNetwork{}
for _, network := range wifiNetworks {
if network.ID == "" || network.ID == "bushnet" || network.ID == "Bushnet" || network.ID == "BushnetHotspot" {
continue
}
wifiProps.Networks = append(wifiProps.Networks, wifiNetwork{ID: network.ID})
}
availableWifiNetworks, err := netmanagerclient.ScanWiFiNetworks()
wifiProps.AvailableNetworks = []string{}
if err != nil {
log.Println(err)
wifiProps.Error = "Error while getting available networks: " + err.Error()
}
for _, network := range availableWifiNetworks {
wifiProps.AvailableNetworks = append(wifiProps.AvailableNetworks, network.SSID)
}
if wifiProps.Error == "" && err != nil {
wifiProps.Error = "Wifi Error: " + err.Error()
}
tmpl.ExecuteTemplate(w, "wifi-networks.html", wifiProps)
}
// Return info on the packages that are currently installed on the device.
func getInstalledPackages() (string, error) {
if runtime.GOOS == "windows" {
return "", nil
}
out, err := exec.Command("/usr/bin/dpkg-query", "--show", "--showformat", "${Package}|${Version}|${Maintainer}\n").Output()
if err != nil {
return "", err
}
return string(out), nil
}
// AboutHandlerGen is a wrapper for the AboutHandler function.
func AboutHandlerGen(conf *goconfig.Config) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
AboutHandler(w, r, conf)
}
}
// AboutHandler shows the currently installed packages on the device.
func AboutHandler(w http.ResponseWriter, r *http.Request, conf *goconfig.Config) {
type aboutResponse struct {
RaspberryPiSerialNumber string
SaltMinionID string
Group string
DeviceID int
LastSaltUpdate string
SaltNodegroup string
PackageDataRows [][]string
ErrorMessage string
}
// Get the device group from the API
var device goconfig.Device
if err := conf.Unmarshal(goconfig.DeviceKey, &device); err != nil {
log.Printf("/device-info failed: %v", err)
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, "failed to read device config\n")
return
}
// Create response
resp := aboutResponse{
RaspberryPiSerialNumber: getRaspberryPiSerialNumber(),
SaltMinionID: getSaltMinionID(),
Group: device.Group,
DeviceID: device.ID,
LastSaltUpdate: getLastSaltUpdate(),
SaltNodegroup: readFile("/etc/cacophony/salt-nodegroup"),
}
// Get installed packages.
packagesData, err := getInstalledPackages()
if err != nil {
resp.ErrorMessage = errorMessage(err)
tmpl.ExecuteTemplate(w, "about.html", resp)
}
// Want to separate this into separate fields so that can display in a table in HTML
dataRows := [][]string{}
rows := strings.Split(packagesData, "\n")
for _, row := range rows {
// We only want packages related to cacophony.
if !strings.Contains(strings.ToUpper(row), "CACOPHONY") {
continue
}
words := strings.Split(strings.TrimSpace(row), "|")
dataRows = append(dataRows, words[:2])
}
resp.PackageDataRows = dataRows
tmpl.ExecuteTemplate(w, "about.html", resp)
}
// CheckInterfaceHandler checks an interface to see if it is up or down.
// To do this the ping command is used to send data to Cloudfare at 1.1.1.1
func CheckInterfaceHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
response := make(map[string]string)
// Extract interface name
interfaceName := mux.Vars(r)["name"]
// Lookup interface by name
iface, err := net.InterfaceByName(interfaceName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
response["status"] = "unknown"
response["result"] = "Unable to find interface with name " + interfaceName
json.NewEncoder(w).Encode(response)
return
}
args := []string{"-I", iface.Name, "-c", "3", "-n", "-W", "15", "1.1.1.1"}
output, err := exec.Command("ping", args...).Output()
w.WriteHeader(http.StatusOK)
response["result"] = string(output)
if err != nil {
// Ping was not successful
response["status"] = "down"
} else {
response["status"] = "up"
}
json.NewEncoder(w).Encode(response)
}
// CameraHandler will show a frame from the camera to help with positioning
func CameraHandler(w http.ResponseWriter, r *http.Request) {
tmpl.ExecuteTemplate(w, "camera.html", nil)
}
// CameraSnapshot - Still image from Lepton camera
func CameraSnapshot(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "/var/spool/cptv/still.png")
}
func TimeHandler(w http.ResponseWriter, r *http.Request) {
tmpl.ExecuteTemplate(w, "clock.html", nil)
}
// Rename page to change device name and group
func Rename(w http.ResponseWriter, r *http.Request) {
tmpl.ExecuteTemplate(w, "rename.html", nil)
}
// Config page to change devices config
func Config(w http.ResponseWriter, r *http.Request) {
tmpl.ExecuteTemplate(w, "config.html", nil)
}
func Modem(w http.ResponseWriter, r *http.Request) {
tmpl.ExecuteTemplate(w, "modem.html", nil)
}
func Battery(w http.ResponseWriter, r *http.Request) {
tmpl.ExecuteTemplate(w, "battery.html", nil)
}
func DownloadTemperatureCSV(w http.ResponseWriter, r *http.Request) {
filePath := "/var/log/temperature.csv"
file, err := os.Open(filePath)
if err != nil {
log.Println(err)
return
}
defer file.Close()
w.Header().Set("Content-Type", "text/csv")
w.Header().Set("Content-Disposition", "attachment; filename=temperature.csv")
_, err = io.Copy(w, file)
if err != nil {
log.Println(err)
return
}
}
func DownloadBatteryCSV(w http.ResponseWriter, r *http.Request) {
filePath := "/var/log/battery-readings.csv"
file, err := os.Open(filePath)
if err != nil {
log.Println(err)
return
}
defer file.Close()
w.Header().Set("Content-Type", "text/csv")
w.Header().Set("Content-Disposition", "attachment; filename=battery-readings.csv")
_, err = io.Copy(w, file)
if err != nil {
log.Println(err)
return
}
}
type audiobaitResponse struct {
Running bool
Schedule schedule
Message string
ErrorMessage string
}
type schedule struct {
Combos []combo
PlayNights int
ControlNights int
StartDay int
Description string
}
type combo struct {
Every int // Minutes
From string
Until string
SoundInfo []soundInfo
}
type soundInfo struct {
SoundFileName string
Volume int
SoundFileDisplayText string
Waits int
ID int
}
func Audiobait(w http.ResponseWriter, r *http.Request) {
// TODO Rather than generating the HTML like this it should probably use the
// audiobait APIs that are now available...
playSchedule, err := playlist.LoadScheduleFromDisk(goconfig.DefaultAudioBait().Dir)
if err != nil {
log.Println(err)
tmpl.ExecuteTemplate(w, "audiobait.html", audiobaitResponse{
ErrorMessage: err.Error(),
})
return
}
library, err := audiofilelibrary.OpenLibrary(goconfig.DefaultAudioBait().Dir)
if err != nil {
log.Println(err)
tmpl.ExecuteTemplate(w, "audiobait.html", audiobaitResponse{
ErrorMessage: err.Error(),
})
return
}
combos := make([]combo, len(playSchedule.Combos))
for i, c := range playSchedule.Combos {
soundInfos := make([]soundInfo, len(c.Sounds))
for i, sound := range c.Sounds {
si := soundInfo{
Volume: c.Volumes[i],
Waits: c.Waits[i] / 60,
}
if sound == "random" || sound == "same" {
si.SoundFileDisplayText = sound
} else {
soundId, err := strconv.Atoi(sound)
if err != nil {
log.Println(err)
return
}
si.ID = soundId
si.SoundFileDisplayText = library.FilesByID[soundId]
si.SoundFileName = library.FilesByID[soundId]
}
soundInfos[i] = si
}
combos[i] = combo{
Every: c.Every / 60,
From: c.From.Format("15:04"),
Until: c.Until.Format("15:04"),
SoundInfo: soundInfos,
}
}
ar := &audiobaitResponse{
Schedule: schedule{
ControlNights: playSchedule.ControlNights,
Combos: combos,
PlayNights: playSchedule.PlayNights,
StartDay: playSchedule.StartDay,
Description: playSchedule.Description,
},
Running: true,
}
tmpl.ExecuteTemplate(w, "audiobait.html", ar)
}
func errorMessage(err error) string {
if err == nil {
return ""
}
return err.Error()
}