sungrow/main.go
2023-01-22 14:09:39 +11:00

258 lines
6.6 KiB
Go

/*
Copyright 2023 Josh Deprez
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// The sungrow binary periodically reads inverter data from a sungrow inverter
// and exports the data as prometheus metrics.
package main
import (
"errors"
"flag"
"fmt"
"log"
"net/http"
"strings"
"sync"
"time"
"gitea.drjosh.dev/josh/sungrow/modbus"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const maxScrapeAge = 3 * time.Second
var (
httpAddr = flag.String("http-addr", ":9455", "Address to listen on")
inverterAddrs = flag.String("inverter-addrs", "rakmodule_00DBC1:502,192.168.86.6:502", "Comma-separated list of inverter addresses (modbus-tcp with 'encryption')")
scrapeInterval = flag.Duration("scrape-interval", 15*time.Second, "Period of modbus scraping loop")
promHandler = promhttp.Handler()
scrapeMu sync.RWMutex
lastScrape time.Time
lastValues = make(map[uint16]float64)
scrapeCounter = promauto.NewCounter(prometheus.CounterOpts{
Namespace: "sungrow",
Subsystem: "scraper",
Name: "scrapes_total",
Help: "Number of successful scrapes of the inverter input registers",
})
scrapeStart = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "sungrow",
Subsystem: "scraper",
Name: "scrape_start",
Help: "Start time of the most recent scrape attempt",
})
scrapeEnd = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "sungrow",
Subsystem: "scraper",
Name: "scrape_end",
Help: "End time of the most recent successful scrape",
})
scrapeDuration = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "sungrow",
Subsystem: "scraper",
Name: "scrape_duration",
Help: "units:s",
})
)
func init() {
promauto.NewGaugeFunc(
prometheus.GaugeOpts{
Namespace: "sungrow",
Subsystem: "scraper",
Name: "scrape_interval",
Help: "units:s",
},
func() float64 { return scrapeInterval.Seconds() },
)
promauto.NewGaugeFunc(
prometheus.GaugeOpts{
Namespace: "sungrow",
Subsystem: "tariff",
Name: "daily_charge",
Help: "units:$",
},
func() float64 { return dailySupplyCharge },
)
promauto.NewGaugeFunc(
prometheus.GaugeOpts{
Namespace: "sungrow",
Subsystem: "tariff",
Name: "import_tariff",
Help: "units:$",
},
func() float64 { return tariff93.pricePerKWh(time.Now()) },
)
promauto.NewGaugeFunc(
prometheus.GaugeOpts{
Namespace: "sungrow",
Subsystem: "tariff",
Name: "export_tariff",
Help: "units:$",
},
func() float64 { return solarFeedInTariff.pricePerKWh(time.Now()) },
)
}
func statusHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "current time: %v\n", time.Now())
}
func dialInverter() (*sungrowConn, error) {
for _, addr := range strings.Split(*inverterAddrs, ",") {
conn, err := dialSungrow(addr)
if err != nil {
log.Printf("Couldn't dial inverter: %v", err)
continue
}
return conn, nil
}
return nil, fmt.Errorf("all addresses unreachable")
}
// Called under scrapeMu.
func readRegs(vals map[uint16]float64, client modbus.Client, start, qty uint16) error {
data, err := client.ReadInputRegisters(start, qty)
if err != nil {
return fmt.Errorf("read input registers %d-%d: %v", start+1, start+qty, err)
}
if len(data) != int(2*qty) {
return fmt.Errorf("read input registers %d-%d: len(data) = %d != %d = 2*qty", start+1, start+qty, len(data), 2*qty)
}
for addr, reg := range sungrowInputRegs {
if addr <= start || addr > start+qty {
continue
}
val, err := reg.read(data[(addr-start-1)*2:])
if err != nil {
if !errors.Is(err, errSkippableRead) {
return fmt.Errorf("parse input register data at %d: %v", addr, err)
}
log.Printf("Couldn't parse input register data at %d, skipping: %v", addr, err)
val = lastValues[addr]
}
//fmt.Printf("%s: %v %s\n", reg.name, val, reg.unit)
vals[addr] = val
}
return nil
}
// Called under scrapeMu.
func scrape() error {
sgc, err := dialInverter()
if err != nil {
return err
}
defer sgc.Close()
handler := modbus.TCPHandlerFromConnection(sgc)
handler.SlaveId = 0x01
//handler.Connect()
client := modbus.NewClient(handler)
vals := make(map[uint16]float64)
start := time.Now()
scrapeStart.SetToCurrentTime()
if err := readRegs(vals, client, 5000, 24); err != nil {
return err
}
if err := readRegs(vals, client, 5030, 7); err != nil {
return err
}
if err := readRegs(vals, client, 5048, 1); err != nil {
return err
}
if err := readRegs(vals, client, 5082, 18); err != nil {
return err
}
if err := readRegs(vals, client, 5112, 1); err != nil {
return err
}
if err := readRegs(vals, client, 5143, 6); err != nil {
return err
}
lastValues = vals
scrapeEnd.SetToCurrentTime()
lastScrape = time.Now()
scrapeDuration.Set(time.Since(start).Seconds())
scrapeCounter.Inc()
return nil
}
func metricsHandler(w http.ResponseWriter, r *http.Request) {
// In normal mode, always serve metrics
defer promHandler.ServeHTTP(w, r)
scrapeMu.Lock()
defer scrapeMu.Unlock()
if time.Since(lastScrape) <= maxScrapeAge {
return
}
var lastErr error
for i := 0; i < 3; i++ {
if err := scrape(); err != nil {
log.Printf("Scrape error: %v", err)
lastErr = err
continue
}
return
}
log.Fatalf("Scrape failed, bailing entirely: %v", lastErr)
}
func main() {
flag.Parse()
// These are GaugeFuncs to more closely align Prometheus scrape time with
// the modbus scrape time.
for addr, reg := range sungrowInputRegs {
addr, reg := addr, reg
promauto.NewGaugeFunc(
prometheus.GaugeOpts{
Namespace: "sungrow",
Subsystem: "inverter",
Name: reg.name,
Help: fmt.Sprintf("addr: %d, unit: %s", addr, reg.unit),
},
func() float64 {
scrapeMu.RLock()
defer scrapeMu.RUnlock()
return lastValues[addr]
},
)
}
// Startup paranoia check: Is the inverter reachable?
sgc, err := dialInverter()
if err != nil {
log.Fatal("Couldn't dial any addresses, aborting")
}
sgc.Close()
// HTTP setup
http.HandleFunc("/metrics", metricsHandler)
http.HandleFunc("/", statusHandler)
log.Fatalf("http.ListenAndServe: %v", http.ListenAndServe(*httpAddr, nil))
}