WIP: panel functions added to the api.

This commit is contained in:
Jesse Malotaux 2025-04-05 23:19:26 +02:00
parent 2f391bce4e
commit 022b11c81f
4 changed files with 111 additions and 0 deletions

View file

@ -85,6 +85,10 @@ func ApiPost(w http.ResponseWriter, r *http.Request) {
RemoveLink("", w, r)
case "/device/handshake":
Handshake(w, r)
case "/panel/list":
PanelList(w, r)
case "/panel/get":
GetPanel(w, r)
}
}

89
be/app/panel.go Normal file
View file

@ -0,0 +1,89 @@
package app
import (
"be/app/structs"
"encoding/json"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
)
func PanelList(w http.ResponseWriter, r *http.Request) {
panelDirs, err := os.ReadDir("../panels")
if err != nil {
json.NewEncoder(w).Encode(false)
return
}
var panelList []interface{}
for _, panelDir := range panelDirs {
if panelDir.IsDir() {
// log.Println(getPanelInfo(panelDir.Name()))
panelList = append(panelList, getPanelInfo(panelDir.Name()))
}
}
if len(panelList) == 0 {
json.NewEncoder(w).Encode(false)
return
}
json.NewEncoder(w).Encode(panelList)
}
func getPanelInfo(dirname string) structs.PanelInfo {
var panelInfo structs.PanelInfo
jsonFile, err := os.ReadFile("../panels/" + dirname + "/panel.json")
if err != nil {
panelInfo.Name = strings.Replace(dirname, "_", " ", -1)
panelInfo.Description = "null"
panelInfo.AspectRatio = "null"
panelInfo.Macros = make(map[string]string)
} else {
err = json.Unmarshal(jsonFile, &panelInfo)
if err != nil {
log.Println(err)
}
}
thumb := getPanelThumb(dirname)
panelInfo.Thumb = thumb
return panelInfo
}
func getPanelThumb(dirname string) string {
re := regexp.MustCompile(`^thumb\.(jpg|jpeg|png)$`)
files, _ := os.ReadDir("../panels/" + dirname)
for _, file := range files {
if file.IsDir() {
continue
}
if re.MatchString(filepath.Base(file.Name())) {
return filepath.Base(file.Name())
}
}
return ""
}
func GetPanel(w http.ResponseWriter, r *http.Request) {
html, _ := os.ReadFile("../panels/test_panel/index.html")
css, _ := os.ReadFile("../panels/test_panel/output.css")
type Response struct {
Html string `json:"html"`
Css string `json:"css"`
}
resp := Response{Html: string(html), Css: string(css)}
json.NewEncoder(w).Encode(resp)
}

View file

@ -20,6 +20,8 @@ var Endpoints = Allowed{
"/device/link/poll",
"/device/link/remove",
"/device/handshake",
"/panel/get",
"/panel/list",
},
Remote: []string{
"/macro/list",

View file

@ -0,0 +1,16 @@
package structs
type PanelJSON struct {
Name string `json:"name"`
Description string `json:"description"`
AspectRatio string `json:"aspectRatio"`
Macros map[string]string `json:"macros"`
}
type PanelInfo struct {
Name string `json:"name"`
Description string `json:"description"`
AspectRatio string `json:"aspectRatio"`
Macros map[string]string `json:"macros"`
Thumb string `json:"thumb"`
}