@@ -69,6 +69,65 @@ func NewRouter(cfg config.Config, frontendFS fs.FS) http.Handler {
|
||||
writeJSON(w, http.StatusOK, map[string]interface{}{"points": pts})
|
||||
})
|
||||
|
||||
var controller *ChargerController
|
||||
if cfg.Charger.Host != "" {
|
||||
controller = NewChargerController(cfg.Charger.Host, pool)
|
||||
}
|
||||
|
||||
protected.HandleFunc("GET /api/charger", func(w http.ResponseWriter, r *http.Request) {
|
||||
if controller == nil {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "charger not configured"})
|
||||
return
|
||||
}
|
||||
status, err := fetchChargerStatus(cfg.Charger.Host)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusBadGateway, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, status)
|
||||
})
|
||||
protected.HandleFunc("GET /api/charger/mode", func(w http.ResponseWriter, r *http.Request) {
|
||||
if controller == nil {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "charger not configured"})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, controller.State())
|
||||
})
|
||||
protected.HandleFunc("POST /api/charger/reset", func(w http.ResponseWriter, r *http.Request) {
|
||||
if controller == nil {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "charger not configured"})
|
||||
return
|
||||
}
|
||||
if err := resetCharger(cfg.Charger.Host); err != nil {
|
||||
writeJSON(w, http.StatusBadGateway, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
// Re-apply current mode since reset clears frc to 0
|
||||
_ = controller.SetParams(controller.State().Params)
|
||||
status, err := fetchChargerStatus(cfg.Charger.Host)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusBadGateway, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, status)
|
||||
})
|
||||
protected.HandleFunc("POST /api/charger/mode", func(w http.ResponseWriter, r *http.Request) {
|
||||
if controller == nil {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "charger not configured"})
|
||||
return
|
||||
}
|
||||
var p ChargingParams
|
||||
if err := json.NewDecoder(r.Body).Decode(&p); err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid body"})
|
||||
return
|
||||
}
|
||||
if err := controller.SetParams(p); err != nil {
|
||||
writeJSON(w, http.StatusBadGateway, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, controller.State())
|
||||
})
|
||||
|
||||
mux.Handle("/api/", auth.middleware(protected))
|
||||
|
||||
if frontendFS != nil {
|
||||
|
||||
Reference in New Issue
Block a user