solar-toolkit/cmd/status/main.go

62 lines
1.2 KiB
Go
Raw Normal View History

2022-07-17 09:05:41 +00:00
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net"
"os"
"git.netflux.io/rob/solar-toolkit/inverter"
)
func main() {
var inverterAddr string
2022-07-18 21:29:14 +00:00
var meterData bool
var err error
2022-07-17 09:05:41 +00:00
flag.StringVar(&inverterAddr, "inverter-addr", "", "IP+port of solar inverter")
2022-07-18 21:29:14 +00:00
flag.BoolVar(&meterData, "meter-data", false, "print meter data, not sensors")
2022-07-17 09:05:41 +00:00
flag.Parse()
if inverterAddr == "" {
flag.Usage()
os.Exit(1)
}
conn, err := net.Dial("udp", inverterAddr)
if err != nil {
log.Fatalf("error dialing: %s", err)
}
defer conn.Close()
var inv inverter.ET
2022-07-18 21:29:14 +00:00
var result []byte
if meterData {
meterData, err := inv.MeterData(context.Background(), conn)
if err != nil {
log.Fatalf("error fetching meter data: %s", err)
}
result, err = json.Marshal(meterData)
if err != nil {
log.Fatalf("error encoding meter data: %s", err)
}
} else {
runtimeData, err := inv.RuntimeData(context.Background(), conn)
if err != nil {
log.Fatalf("error fetching runtime data: %s", err)
}
result, err = json.Marshal(runtimeData)
if err != nil {
log.Fatalf("error encoding runtime data: %s", err)
}
2022-07-17 09:05:41 +00:00
}
2022-07-18 21:29:14 +00:00
fmt.Fprint(os.Stdout, string(result))
2022-07-17 09:05:41 +00:00
}