Files
invertergui/plugins/webui/webgui.go
Nathan Coad e8153e2953
All checks were successful
continuous-integration/drone/push Build is passing
implement some features of Venus OS
2026-02-19 15:37:41 +11:00

480 lines
14 KiB
Go

/*
Copyright (c) 2015, 2017 Hendrik van Wyk
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of invertergui nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package webui
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"sync"
"time"
"git.coadcorp.com/nathan/invertergui/mk2driver"
"git.coadcorp.com/nathan/invertergui/websocket"
"github.com/sirupsen/logrus"
)
var log = logrus.WithField("ctx", "inverter-gui-webgui")
const (
LedOff = "dot-off"
LedRed = "dot-red"
BlinkRed = "blink-red"
LedGreen = "dot-green"
BlinkGreen = "blink-green"
)
const (
modeChargerOnly = "charger_only"
modeInverterOnly = "inverter_only"
modeOn = "on"
modeOff = "off"
modeUnknown = "unknown"
)
type WebGui struct {
mk2driver.Mk2
writer mk2driver.SettingsWriter
stopChan chan struct{}
wg sync.WaitGroup
hub *websocket.Hub
stateMu sync.RWMutex
latest *templateInput
remote remotePanelState
}
func NewWebGui(source mk2driver.Mk2, writer mk2driver.SettingsWriter) *WebGui {
w := &WebGui{
stopChan: make(chan struct{}),
Mk2: source,
writer: writer,
hub: websocket.NewHub(),
remote: remotePanelState{
Writable: writer != nil,
Mode: modeUnknown,
},
}
log.WithField("remote_writable", writer != nil).Info("Web UI initialized")
w.wg.Add(1)
go w.dataPoll()
return w
}
type remotePanelState struct {
Writable bool `json:"writable"`
Mode string `json:"mode"`
CurrentLimit *float64 `json:"current_limit,omitempty"`
Standby *bool `json:"standby,omitempty"`
LastCommand string `json:"last_command,omitempty"`
LastError string `json:"last_error,omitempty"`
LastUpdated string `json:"last_updated,omitempty"`
}
type templateInput struct {
Error []error `json:"errors"`
Date string `json:"date"`
OutCurrent string `json:"output_current"`
OutVoltage string `json:"output_voltage"`
OutPower string `json:"output_power"`
InCurrent string `json:"input_current"`
InVoltage string `json:"input_voltage"`
InPower string `json:"input_power"`
InMinOut string
BatVoltage string `json:"battery_voltage"`
BatCurrent string `json:"battery_current"`
BatPower string `json:"battery_power"`
BatCharge string `json:"battery_charge"`
InFreq string `json:"input_frequency"`
OutFreq string `json:"output_frequency"`
LedMap map[string]string `json:"led_map"`
RemotePanel remotePanelState `json:"remote_panel"`
}
type setRemotePanelStateRequest struct {
Mode string `json:"mode"`
CurrentLimit *float64 `json:"current_limit"`
}
type setRemotePanelStandbyRequest struct {
Standby bool `json:"standby"`
}
func (w *WebGui) ServeHub(rw http.ResponseWriter, r *http.Request) {
log.WithFields(logrus.Fields{
"remote": r.RemoteAddr,
"path": r.URL.Path,
}).Debug("WebSocket hub request")
w.hub.ServeHTTP(rw, r)
}
func (w *WebGui) ServeRemotePanelState(rw http.ResponseWriter, r *http.Request) {
log.WithFields(logrus.Fields{
"method": r.Method,
"path": r.URL.Path,
"remote": r.RemoteAddr,
}).Debug("Remote panel state API request")
switch r.Method {
case http.MethodGet:
writeJSON(rw, http.StatusOK, w.getRemotePanelState())
case http.MethodPost:
w.handleSetRemotePanelState(rw, r)
default:
log.WithField("method", r.Method).Warn("Remote panel state API received unsupported method")
http.Error(rw, "method not allowed", http.StatusMethodNotAllowed)
}
}
func (w *WebGui) ServeRemotePanelStandby(rw http.ResponseWriter, r *http.Request) {
log.WithFields(logrus.Fields{
"method": r.Method,
"path": r.URL.Path,
"remote": r.RemoteAddr,
}).Debug("Remote panel standby API request")
switch r.Method {
case http.MethodGet:
writeJSON(rw, http.StatusOK, w.getRemotePanelState())
case http.MethodPost:
w.handleSetRemotePanelStandby(rw, r)
default:
log.WithField("method", r.Method).Warn("Remote panel standby API received unsupported method")
http.Error(rw, "method not allowed", http.StatusMethodNotAllowed)
}
}
func (w *WebGui) handleSetRemotePanelState(rw http.ResponseWriter, r *http.Request) {
if w.writer == nil {
log.Warn("Remote panel state write requested, but writer is unavailable")
http.Error(rw, "remote control is not supported by this data source", http.StatusNotImplemented)
return
}
req := setRemotePanelStateRequest{}
if err := decodeJSONBody(r, &req); err != nil {
log.WithError(err).Warn("Invalid remote panel state request body")
http.Error(rw, err.Error(), http.StatusBadRequest)
return
}
logEntry := log.WithField("mode", req.Mode)
if req.CurrentLimit != nil {
logEntry = logEntry.WithField("current_limit_a", *req.CurrentLimit)
}
logEntry.Info("Applying remote panel state from API")
switchState, normalizedMode, err := parsePanelMode(req.Mode)
if err != nil {
logEntry.WithError(err).Warn("Unsupported remote panel mode")
http.Error(rw, err.Error(), http.StatusBadRequest)
return
}
var setErr error
if sourceAware, ok := w.writer.(mk2driver.SourceAwareSettingsWriter); ok {
setErr = sourceAware.SetPanelStateWithSource(mk2driver.CommandSourceUI, switchState, req.CurrentLimit)
} else {
setErr = w.writer.SetPanelState(switchState, req.CurrentLimit)
}
if setErr != nil {
logEntry.WithError(setErr).Error("Failed to apply remote panel state")
w.updateRemotePanelState(func(state *remotePanelState) {
state.LastCommand = "set_remote_panel_state"
state.LastError = setErr.Error()
})
http.Error(rw, setErr.Error(), http.StatusBadGateway)
return
}
w.updateRemotePanelState(func(state *remotePanelState) {
state.Mode = normalizedMode
state.CurrentLimit = copyFloat64Ptr(req.CurrentLimit)
state.LastCommand = "set_remote_panel_state"
state.LastError = ""
})
logEntry.WithField("normalized_mode", normalizedMode).Info("Remote panel state applied")
writeJSON(rw, http.StatusOK, w.getRemotePanelState())
}
func (w *WebGui) handleSetRemotePanelStandby(rw http.ResponseWriter, r *http.Request) {
if w.writer == nil {
log.Warn("Remote panel standby write requested, but writer is unavailable")
http.Error(rw, "remote control is not supported by this data source", http.StatusNotImplemented)
return
}
req := setRemotePanelStandbyRequest{}
if err := decodeJSONBody(r, &req); err != nil {
log.WithError(err).Warn("Invalid remote panel standby request body")
http.Error(rw, err.Error(), http.StatusBadRequest)
return
}
log.WithField("standby", req.Standby).Info("Applying standby state from API")
var err error
if sourceAware, ok := w.writer.(mk2driver.SourceAwareSettingsWriter); ok {
err = sourceAware.SetStandbyWithSource(mk2driver.CommandSourceUI, req.Standby)
} else {
err = w.writer.SetStandby(req.Standby)
}
if err != nil {
log.WithError(err).WithField("standby", req.Standby).Error("Failed to apply standby state")
w.updateRemotePanelState(func(state *remotePanelState) {
state.LastCommand = "set_remote_panel_standby"
state.LastError = err.Error()
})
http.Error(rw, err.Error(), http.StatusBadGateway)
return
}
w.updateRemotePanelState(func(state *remotePanelState) {
state.Standby = copyBoolPtr(&req.Standby)
state.LastCommand = "set_remote_panel_standby"
state.LastError = ""
})
log.WithField("standby", req.Standby).Info("Standby state applied")
writeJSON(rw, http.StatusOK, w.getRemotePanelState())
}
func parsePanelMode(raw string) (mk2driver.PanelSwitchState, string, error) {
switch strings.TrimSpace(strings.ToLower(raw)) {
case modeChargerOnly:
return mk2driver.PanelSwitchChargerOnly, modeChargerOnly, nil
case modeInverterOnly:
return mk2driver.PanelSwitchInverterOnly, modeInverterOnly, nil
case modeOn:
return mk2driver.PanelSwitchOn, modeOn, nil
case modeOff:
return mk2driver.PanelSwitchOff, modeOff, nil
default:
return 0, "", fmt.Errorf("unsupported panel mode %q", raw)
}
}
func ledName(led mk2driver.Led) string {
name, ok := mk2driver.LedNames[led]
if !ok {
return "Unknown led"
}
return name
}
func buildTemplateInput(status *mk2driver.Mk2Info) *templateInput {
outPower := status.OutVoltage * status.OutCurrent
inPower := status.InCurrent * status.InVoltage
tmpInput := &templateInput{
Error: status.Errors,
Date: status.Timestamp.Format(time.RFC1123Z),
OutCurrent: fmt.Sprintf("%.2f", status.OutCurrent),
OutVoltage: fmt.Sprintf("%.2f", status.OutVoltage),
OutPower: fmt.Sprintf("%.2f", outPower),
InCurrent: fmt.Sprintf("%.2f", status.InCurrent),
InVoltage: fmt.Sprintf("%.2f", status.InVoltage),
InFreq: fmt.Sprintf("%.2f", status.InFrequency),
OutFreq: fmt.Sprintf("%.2f", status.OutFrequency),
InPower: fmt.Sprintf("%.2f", inPower),
InMinOut: fmt.Sprintf("%.2f", inPower-outPower),
BatCurrent: fmt.Sprintf("%.2f", status.BatCurrent),
BatVoltage: fmt.Sprintf("%.2f", status.BatVoltage),
BatPower: fmt.Sprintf("%.2f", status.BatVoltage*status.BatCurrent),
BatCharge: fmt.Sprintf("%.2f", status.ChargeState*100),
LedMap: map[string]string{},
}
for k, v := range status.LEDs {
if k == mk2driver.LedOverload || k == mk2driver.LedTemperature || k == mk2driver.LedLowBattery {
switch v {
case mk2driver.LedOn:
tmpInput.LedMap[ledName(k)] = LedRed
case mk2driver.LedBlink:
tmpInput.LedMap[ledName(k)] = BlinkRed
default:
tmpInput.LedMap[ledName(k)] = LedOff
}
} else {
switch v {
case mk2driver.LedOn:
tmpInput.LedMap[ledName(k)] = LedGreen
case mk2driver.LedBlink:
tmpInput.LedMap[ledName(k)] = BlinkGreen
default:
tmpInput.LedMap[ledName(k)] = LedOff
}
}
}
return tmpInput
}
func (w *WebGui) Stop() {
log.Info("Stopping Web UI polling")
close(w.stopChan)
w.wg.Wait()
log.Info("Web UI polling stopped")
}
func (w *WebGui) dataPoll() {
for {
select {
case s := <-w.C():
if s == nil {
log.Debug("Skipping nil MK2 update in Web UI poller")
continue
}
if !s.Valid {
log.WithField("errors", len(s.Errors)).Debug("Skipping invalid MK2 update in Web UI poller")
continue
}
payload := buildTemplateInput(s)
w.stateMu.Lock()
payload.RemotePanel = w.remote
w.latest = payload
w.stateMu.Unlock()
if err := w.hub.Broadcast(payload); err != nil {
log.Errorf("Could not send update to clients: %v", err)
}
case <-w.stopChan:
w.wg.Done()
return
}
}
}
func (w *WebGui) getRemotePanelState() remotePanelState {
w.stateMu.RLock()
defer w.stateMu.RUnlock()
return copyRemotePanelState(w.remote)
}
func (w *WebGui) updateRemotePanelState(update func(state *remotePanelState)) {
w.stateMu.Lock()
update(&w.remote)
w.remote.LastUpdated = time.Now().UTC().Format(time.RFC3339)
log.WithFields(logrus.Fields{
"mode": w.remote.Mode,
"has_limit": w.remote.CurrentLimit != nil,
"has_standby": w.remote.Standby != nil,
"last_command": w.remote.LastCommand,
"last_error": w.remote.LastError,
}).Debug("Updated remote panel state cache")
snapshot := w.snapshotLocked()
w.stateMu.Unlock()
if snapshot != nil {
if err := w.hub.Broadcast(snapshot); err != nil {
log.Errorf("Could not send control update to clients: %v", err)
}
}
}
func (w *WebGui) snapshotLocked() *templateInput {
if w.latest == nil {
return nil
}
snapshot := cloneTemplateInput(w.latest)
snapshot.RemotePanel = copyRemotePanelState(w.remote)
return snapshot
}
func cloneTemplateInput(in *templateInput) *templateInput {
if in == nil {
return nil
}
out := *in
if in.Error != nil {
out.Error = append([]error(nil), in.Error...)
}
if in.LedMap != nil {
out.LedMap = make(map[string]string, len(in.LedMap))
for k, v := range in.LedMap {
out.LedMap[k] = v
}
}
out.RemotePanel = copyRemotePanelState(in.RemotePanel)
return &out
}
func copyRemotePanelState(in remotePanelState) remotePanelState {
in.CurrentLimit = copyFloat64Ptr(in.CurrentLimit)
in.Standby = copyBoolPtr(in.Standby)
return in
}
func copyFloat64Ptr(in *float64) *float64 {
if in == nil {
return nil
}
value := *in
return &value
}
func copyBoolPtr(in *bool) *bool {
if in == nil {
return nil
}
value := *in
return &value
}
func decodeJSONBody(r *http.Request, destination any) error {
defer r.Body.Close()
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
if err := decoder.Decode(destination); err != nil {
return fmt.Errorf("invalid request body: %w", err)
}
return nil
}
func writeJSON(rw http.ResponseWriter, statusCode int, payload any) {
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(statusCode)
if err := json.NewEncoder(rw).Encode(payload); err != nil {
log.Errorf("Could not encode webui API response: %v", err)
}
}