2022-07-09 09:08:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-07-11 18:01:28 +00:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2022-07-09 09:08:46 +00:00
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
2022-07-11 18:01:28 +00:00
|
|
|
"time"
|
2022-07-09 09:08:46 +00:00
|
|
|
|
2022-07-13 17:01:08 +00:00
|
|
|
"git.netflux.io/rob/solar-toolkit/inverter"
|
2022-07-09 09:08:46 +00:00
|
|
|
)
|
|
|
|
|
2022-07-11 18:01:28 +00:00
|
|
|
const commandTimeout = time.Second * 5
|
|
|
|
|
2022-07-09 09:08:46 +00:00
|
|
|
func main() {
|
|
|
|
var ipAddr string
|
|
|
|
flag.StringVar(&ipAddr, "ipaddr", "", "IP address/port")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if ipAddr == "" {
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2022-07-11 18:01:28 +00:00
|
|
|
arg := flag.Arg(0)
|
|
|
|
if arg != "discover" && arg != "runtime" && arg != "info" {
|
|
|
|
log.Fatal("missing command: [discover|runtime|info]")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), commandTimeout)
|
|
|
|
defer cancel()
|
2022-07-09 09:08:46 +00:00
|
|
|
|
|
|
|
conn, err := net.Dial("udp", ipAddr)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error dialing: %s", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
2022-07-11 18:01:28 +00:00
|
|
|
var (
|
|
|
|
inverter inverter.ET
|
|
|
|
output any
|
|
|
|
)
|
2022-07-10 08:47:12 +00:00
|
|
|
|
2022-07-11 18:01:28 +00:00
|
|
|
switch arg {
|
|
|
|
case "discover":
|
|
|
|
log.Fatal("not yet implemented")
|
|
|
|
case "info":
|
|
|
|
output, err = inverter.DeviceInfo(ctx, conn)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error getting device info: %s", err)
|
|
|
|
}
|
|
|
|
case "runtime":
|
|
|
|
output, err = inverter.RuntimeData(ctx, conn)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error getting runtime data: %s", err)
|
|
|
|
}
|
2022-07-09 09:08:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 18:01:28 +00:00
|
|
|
json, err := json.Marshal(output)
|
2022-07-09 09:08:46 +00:00
|
|
|
if err != nil {
|
2022-07-11 18:01:28 +00:00
|
|
|
log.Fatalf("error encoding JSON: %s", err)
|
2022-07-09 09:08:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 18:01:28 +00:00
|
|
|
if _, err = os.Stdout.Write(json); err != nil {
|
|
|
|
log.Fatalf("error writing to stdout: %s", err)
|
|
|
|
}
|
2022-07-09 09:08:46 +00:00
|
|
|
}
|