add configuration editor
This commit is contained in:
135
main.go
135
main.go
@@ -8,6 +8,7 @@ import (
|
||||
"os/signal"
|
||||
"strconv"
|
||||
|
||||
"git.k7n.net/mats/go-telldus/pkg/config"
|
||||
"git.k7n.net/mats/go-telldus/pkg/datastore"
|
||||
"git.k7n.net/mats/go-telldus/pkg/devices"
|
||||
"git.k7n.net/mats/go-telldus/pkg/mqtt"
|
||||
@@ -23,6 +24,8 @@ var mqttClient *mqtt.Client
|
||||
var store *datastore.DataStore
|
||||
var daemonMgr *daemon.Manager
|
||||
var eventMgr *devices.EventManager
|
||||
var configParser *config.Parser
|
||||
var configPath string
|
||||
|
||||
const maxEvents = 1000
|
||||
|
||||
@@ -83,8 +86,11 @@ func main() {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Initialize config parser
|
||||
configPath = "/etc/tellstick.conf"
|
||||
configParser = config.NewParser(configPath)
|
||||
|
||||
// Start watching config file
|
||||
configPath := "/etc/tellstick.conf"
|
||||
watcher := daemon.NewWatcher(configPath, reloadDevices)
|
||||
go func() {
|
||||
if err := watcher.Watch(); err != nil {
|
||||
@@ -257,6 +263,13 @@ func setupRoutes() *http.ServeMux {
|
||||
mux.HandleFunc("/api/events/raw", getRawEvents)
|
||||
mux.HandleFunc("/api/events/sensor", getSensorEvents)
|
||||
mux.HandleFunc("/api/potential_devices", getPotentialDevices)
|
||||
// Config endpoints
|
||||
mux.HandleFunc("GET /api/config", getConfig)
|
||||
mux.HandleFunc("GET /api/config/devices", getConfigDevices)
|
||||
mux.HandleFunc("GET /api/config/devices/{id}", getConfigDevice)
|
||||
mux.HandleFunc("POST /api/config/devices", createConfigDevice)
|
||||
mux.HandleFunc("PUT /api/config/devices/{id}", updateConfigDevice)
|
||||
mux.HandleFunc("DELETE /api/config/devices/{id}", deleteConfigDevice)
|
||||
// Serve static files for the frontend
|
||||
mux.Handle("/", http.FileServer(http.Dir("./dist")))
|
||||
return mux
|
||||
@@ -352,3 +365,123 @@ func getSensor(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(sensor)
|
||||
}
|
||||
|
||||
// Config CRUD handlers
|
||||
func getConfig(w http.ResponseWriter, r *http.Request) {
|
||||
cfg, err := configParser.Parse()
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to parse config", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(cfg)
|
||||
}
|
||||
|
||||
func getConfigDevices(w http.ResponseWriter, r *http.Request) {
|
||||
cfg, err := configParser.Parse()
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to parse config", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(cfg.Devices)
|
||||
}
|
||||
|
||||
func getConfigDevice(w http.ResponseWriter, r *http.Request) {
|
||||
idStr := r.PathValue("id")
|
||||
id, err := strconv.Atoi(idStr)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid device ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
cfg, err := configParser.Parse()
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to parse config", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
device := cfg.GetDevice(id)
|
||||
if device == nil {
|
||||
http.Error(w, "Device not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(device)
|
||||
}
|
||||
|
||||
func createConfigDevice(w http.ResponseWriter, r *http.Request) {
|
||||
var device config.Device
|
||||
if err := json.NewDecoder(r.Body).Decode(&device); err != nil {
|
||||
http.Error(w, "Bad request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
cfg, err := configParser.Parse()
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to parse config", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
// Auto-assign ID if not provided or if ID is 0
|
||||
if device.ID == 0 {
|
||||
device.ID = cfg.GetNextDeviceID()
|
||||
}
|
||||
cfg.AddDevice(device)
|
||||
if err := configParser.Write(cfg); err != nil {
|
||||
http.Error(w, "Failed to write config", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode(device)
|
||||
}
|
||||
|
||||
func updateConfigDevice(w http.ResponseWriter, r *http.Request) {
|
||||
idStr := r.PathValue("id")
|
||||
id, err := strconv.Atoi(idStr)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid device ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
var device config.Device
|
||||
if err := json.NewDecoder(r.Body).Decode(&device); err != nil {
|
||||
http.Error(w, "Bad request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
device.ID = id // Ensure ID matches path parameter
|
||||
cfg, err := configParser.Parse()
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to parse config", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if !cfg.UpdateDevice(device) {
|
||||
http.Error(w, "Device not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if err := configParser.Write(cfg); err != nil {
|
||||
http.Error(w, "Failed to write config", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(device)
|
||||
}
|
||||
|
||||
func deleteConfigDevice(w http.ResponseWriter, r *http.Request) {
|
||||
idStr := r.PathValue("id")
|
||||
id, err := strconv.Atoi(idStr)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid device ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
cfg, err := configParser.Parse()
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to parse config", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if !cfg.DeleteDevice(id) {
|
||||
http.Error(w, "Device not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if err := configParser.Write(cfg); err != nil {
|
||||
http.Error(w, "Failed to write config", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user