diff --git a/evcc.dist.yaml b/evcc.dist.yaml index fd9a3a209..50a83c629 100644 --- a/evcc.dist.yaml +++ b/evcc.dist.yaml @@ -61,6 +61,12 @@ meters: type: script # use script cmd: /bin/sh -c "echo 0" # actual command timeout: 3s # kill script after 3 seconds +#- name: grid +# type: smameter # SMA Home Manager 2.0 or SMA Energy Meter 30 +# uri: 192.168.1.4 # IP Address of the device +#- name: pv +# type: smameter # SMA Home Manager 2.0 or SMA Energy Meter 30 +# uri: 192.168.1.4 # IP Address of the device chargers: - name: wallbe diff --git a/meter/config.go b/meter/config.go index cb9af6da7..26c4a1d0a 100644 --- a/meter/config.go +++ b/meter/config.go @@ -13,6 +13,8 @@ func NewFromConfig(log *api.Logger, typ string, other map[string]interface{}) ap switch strings.ToLower(typ) { case "default", "configurable": c = NewConfigurableFromConfig(log, other) + case "smameter": + c = NewSMAFromConfig(log, other) default: log.FATAL.Fatalf("invalid meter type '%s'", typ) } diff --git a/meter/sma.go b/meter/sma.go new file mode 100644 index 000000000..1dcbec792 --- /dev/null +++ b/meter/sma.go @@ -0,0 +1,116 @@ +package meter + +import ( + "errors" + "sync" + "time" + + "github.com/andig/evcc/api" + "github.com/andig/evcc/meter/sma" +) + +const ( + udpTimeout = 10 * time.Second + waitTimeout = 50 * time.Millisecond // interval when waiting for initial value +) + +// SMA supporting SMA Home Manager 2.0 and SMA Energy Meter 30 +type SMA struct { + log *api.Logger + uri string + power float64 + lastUpdate time.Time + recv chan sma.TelegramData + mux sync.Mutex + once sync.Once +} + +// NewSMAFromConfig creates a SMA Meter from generic config +func NewSMAFromConfig(log *api.Logger, other map[string]interface{}) api.Meter { + sm := struct { + URI string + }{} + api.DecodeOther(log, other, &sm) + + return NewSMA(sm.URI) +} + +// NewSMA creates a SMA Meter +func NewSMA(uri string) *SMA { + log := api.NewLogger("sma ") + + sm := &SMA{ + log: log, + uri: uri, + recv: make(chan sma.TelegramData), + } + + if sma.Instance == nil { + sma.Instance = sma.New(log, sm.uri) + } + + sma.Instance.Subscribe(uri, sm.recv) + + go sm.receive() + + return sm +} + +// waitForInitialValue makes sure we don't start with an error +func (sm *SMA) waitForInitialValue() { + sm.mux.Lock() + defer sm.mux.Unlock() + + if sm.lastUpdate.IsZero() { + sm.log.TRACE.Print("waiting for initial value") + + // wait for initial update + for sm.lastUpdate.IsZero() { + sm.mux.Unlock() + time.Sleep(waitTimeout) + sm.mux.Lock() + } + } +} + +// receive processes the channel message containing the multicast data +func (sm *SMA) receive() { + for msg := range sm.recv { + if msg.Data == nil { + continue + } + + var powerIn, powerOut float64 + var ok bool + + if powerIn, ok = msg.Data[sma.ObisImportPower]; !ok { + continue + } + + if powerOut, ok = msg.Data[sma.ObisExportPower]; !ok { + continue + } + + sm.mux.Lock() + sm.lastUpdate = time.Now() + if powerOut > 0 { + sm.power = -powerOut + } else { + sm.power = powerIn + } + sm.mux.Unlock() + } +} + +// CurrentPower implements the Meter.CurrentPower interface +func (sm *SMA) CurrentPower() (float64, error) { + sm.once.Do(sm.waitForInitialValue) + sm.mux.Lock() + defer sm.mux.Unlock() + + if time.Since(sm.lastUpdate) > udpTimeout { + return 0, errors.New("recv timeout") + } + + return sm.power, nil +} diff --git a/meter/sma/listener.go b/meter/sma/listener.go new file mode 100644 index 000000000..6c668ce4d --- /dev/null +++ b/meter/sma/listener.go @@ -0,0 +1,218 @@ +package sma + +import ( + "encoding/binary" + "errors" + "fmt" + "net" + "strconv" + "sync" + + "github.com/andig/evcc/api" +) + +const ( + multicastAddr = "239.12.255.254:9522" + udpBufferSize = 8192 + obisCodeLength = 4 + ObisImportPower = "1:1.4.0" // Wirkleistung (W) + ObisImportEnergy = "1:1.8.0" // Wirkarbeit (Ws) + + ObisExportPower = "1:2.4.0" // Wirkleistung (W) + ObisExportEnergy = "1:2.8.0" // Wirkarbeit (Ws) − +) + +// obisCodeProp defines the properties needed to parse the SMA multicast telegram values +type obisCodeProp struct { + length int // data size in bytes of the return value + factor float64 // the factor to multiply the value by to get the proper value in the given unit +} + +// list of Obis codes and their properties as defined in the SMA EMETER-Protokoll-TI-de-10.pdf document +var knownObisCodes = map[string]obisCodeProp{ + // Overal sums + ObisImportPower: {4, 0.1}, ObisImportEnergy: {8, 1}, // Wirkleistung (W)/-arbeit (Ws) + + ObisExportPower: {4, 0.1}, ObisExportEnergy: {8, 1}, // Wirkleistung (W)/-arbeit (Ws) − + "1:3.4.0": {4, 0.1}, "1:3.8.0": {8, 1}, // Blindleistung (W)/-arbeit (Ws) + + "1:4.4.0": {4, 0.1}, "1:4.8.0": {8, 1}, // Blindleistung (W)/-arbeit (Ws) − + "1:9.4.0": {4, 0.1}, "1:9.8.0": {8, 1}, // Scheinleistung (W)/-arbeit (Ws) + + "1:10.4.0": {4, 0.1}, "1:10.8.0": {8, 1}, // Scheinleistung (W)/-arbeit (Ws) − + "1:13.4.0": {4, 0.001}, // Leistungsfaktor (Φ) + // Phase 1: { + "1:21.4.0": {4, 0.1}, "1:21.8.0": {8, 1}, // Wirkleistung (W)/-arbeit (Ws) + + "1:22.4.0": {4, 0.1}, "1:22.8.0": {8, 1}, // Wirkleistung (W)/-arbeit (Ws) − + "1:23.4.0": {4, 0.1}, "1:23.8.0": {8, 1}, // Blindleistung (W)/-arbeit (Ws) + + "1:24.4.0": {4, 0.1}, "1:24.8.0": {8, 1}, // Blindleistung (W)/-arbeit (Ws) − + "1:29.4.0": {4, 0.1}, "1:29.8.0": {8, 1}, // Scheinleistung (W)/-arbeit (Ws) + + "1:30.4.0": {4, 0.1}, "1:30.8.0": {8, 1}, // Scheinleistung (W)/-arbeit (Ws) − + "1:31.4.0": {4, 0.001}, // Strom (A) + "1:32.4.0": {4, 0.001}, // Spannung (V + // Phase 2: { + "1:41.4.0": {4, 0.1}, "1:41.8.0": {8, 1}, // Wirkleistung (W)/-arbeit (Ws) + + "1:42.4.0": {4, 0.1}, "1:42.8.0": {8, 1}, // Wirkleistung (W)/-arbeit (Ws) − + "1:43.4.0": {4, 0.1}, "1:43.8.0": {8, 1}, // Blindleistung (W)/-arbeit (Ws) + + "1:44.4.0": {4, 0.1}, "1:44.8.0": {8, 1}, // Blindleistung (W)/-arbeit (Ws) − + "1:49.4.0": {4, 0.1}, "1:49.8.0": {8, 1}, // Scheinleistung (W)/-arbeit (Ws) + + "1:50.4.0": {4, 0.1}, "1:50.8.0": {8, 1}, // Scheinleistung (W)/-arbeit (Ws) − + "1:51.4.0": {4, 0.001}, // Strom (A) + "1:52.4.0": {4, 0.001}, // Spannung (V) + // Phase 3: { + "1:61.4.0": {4, 0.1}, "1:61.8.0": {8, 1}, // Wirkleistung (W)/-arbeit (Ws) + + "1:62.4.0": {4, 0.1}, "1:62.8.0": {8, 1}, // Wirkleistung (W)/-arbeit (Ws) − + "1:63.4.0": {4, 0.1}, "1:63.8.0": {8, 1}, // Blindleistung (W)/-arbeit (Ws) + + "1:64.4.0": {4, 0.1}, "1:64.8.0": {8, 1}, // Blindleistung (W)/-arbeit (Ws) − + "1:69.4.0": {4, 0.1}, "1:69.8.0": {8, 1}, // Scheinleistung (W)/-arbeit (Ws) + + "1:70.4.0": {4, 0.1}, "1:70.8.0": {8, 1}, // Scheinleistung (W)/-arbeit (Ws) − + "1:71.4.0": {4, 0.001}, // Strom (A) + "1:72.4.0": {4, 0.001}, // Spannung (V) + // Others + "144:0.0.0": {4, 1}, // SW Version +} + +var Instance *Listener + +// TelegramData defines the data structure of a SMA multicast data package +type TelegramData struct { + Addr string + Serial string + Data map[string]float64 +} + +// Listener for receiving SMA multicast data packages +type Listener struct { + mux sync.Mutex + log *api.Logger + conn *net.UDPConn + clients map[string]chan<- TelegramData +} + +// New creates a Listener +func New(log *api.Logger, addr string) *Listener { + // Parse the string address + laddr, err := net.ResolveUDPAddr("udp4", multicastAddr) + if err != nil { + log.FATAL.Fatalf("error resolving udp address: %s", err) + } + + // Open up a connection + conn, err := net.ListenMulticastUDP("udp4", nil, laddr) + if err != nil { + log.FATAL.Fatalf("error opening connecting: %s", err) + } + + if err := conn.SetReadBuffer(udpBufferSize); err != nil { + log.FATAL.Fatalf("error setting read buffer: %s", err) + } + + l := &Listener{ + log: log, + conn: conn, + } + + go l.listen() + + return l +} + +// processUDPData converts a SMA Multicast data package into TelegramData +func (l *Listener) processUDPData(src *net.UDPAddr, buffer []byte) (TelegramData, error) { + numBytes := len(buffer) + + if numBytes < 29 { + return TelegramData{}, errors.New("received data package is too small") + } + + obisCodeValues := make(map[string]float64) + + // yes this doesn't look nice, but keeping it until we found a better way to parse the data + // read obis code values, start at position 28, after initial static stuff + for i := 28; i < numBytes; i++ { + if i+obisCodeLength > numBytes-1 { + break + } + + // create the string notation of the potential obis code + b := buffer[i : i+obisCodeLength] + + // Spec says value should be 1, but reading contains 0 + b3 := b[0] + if b3 == 0 { + b3 = 1 + } + + code := fmt.Sprintf("%d:%d.%d.%d", b3, b[1], b[2], b[3]) + + element, ok := knownObisCodes[code] + if !ok { + continue + } + + dataIndex := i + obisCodeLength + + var value float64 + switch element.length { + case 4: + value = float64(binary.BigEndian.Uint32(buffer[dataIndex : dataIndex+element.length+1])) + case 8: + value = float64(binary.BigEndian.Uint64(buffer[dataIndex : dataIndex+element.length+1])) + } + + obisCodeValues[code] = value * element.factor + + i = dataIndex + element.length - 1 + } + + serial := strconv.FormatUint(uint64(binary.BigEndian.Uint32(buffer[20:24])), 10) + + msg := TelegramData{ + Addr: src.IP.String(), + Serial: serial, + Data: obisCodeValues, + } + + return msg, nil +} + +// listen for Multicast data packages +func (l *Listener) listen() { + buffer := make([]byte, udpBufferSize) + // Loop forever reading + for { + numBytes, src, err := l.conn.ReadFromUDP(buffer) + if err != nil { + l.log.WARN.Printf("readfromudp failed: %s", err) + continue + } + + if msg, err := l.processUDPData(src, buffer[:numBytes-1]); err == nil { + l.send(msg) + } + } +} + +// Subscribe adds a client address and message channel +func (l *Listener) Subscribe(addr string, c chan<- TelegramData) { + l.mux.Lock() + defer l.mux.Unlock() + + if l.clients == nil { + l.clients = make(map[string]chan<- TelegramData) + } + + l.clients[addr] = c +} + +func (l *Listener) send(msg TelegramData) { + l.mux.Lock() + defer l.mux.Unlock() + + for addr, client := range l.clients { + if addr == msg.Addr { + select { + case client <- msg: + default: + l.log.TRACE.Println("listener: recv blocked") + } + break + } + } +} diff --git a/meter/sma/listener_test.go b/meter/sma/listener_test.go new file mode 100644 index 000000000..efc05133e --- /dev/null +++ b/meter/sma/listener_test.go @@ -0,0 +1,206 @@ +package sma + +import ( + "net" + "reflect" + "testing" +) + +func TestListenerProcessUDPData(t *testing.T) { + tests := []struct { + name string + ip net.IP + port int + response []byte + wantErr bool + want TelegramData + }{ + { + "SMA Home Manager - success", + []byte{192, 168, 1, 4}, + 33168, + []byte{ + 0x53, 0x4d, 0x41, 0x00, 0x00, 0x04, 0x02, 0xa0, 0x00, 0x00, 0x00, 0x01, 0x02, 0x4c, 0x00, 0x10, + 0x60, 0x69, 0x01, 0x74, 0x00, 0x00, 0x00, 0x00, 0xb2, 0x79, 0xb9, 0x63, 0x00, 0x01, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x01, 0x9a, 0xc1, 0x2f, 0x28, + 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x01, 0x7b, 0x00, 0x02, 0x08, 0x00, 0x00, 0x00, 0x00, 0x05, + 0x85, 0x61, 0x66, 0x80, 0x00, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x01, 0x77, 0xbb, 0xd1, 0x90, 0x00, 0x04, 0x04, 0x00, 0x00, 0x00, 0x1b, 0x17, + 0x00, 0x04, 0x08, 0x00, 0x00, 0x00, 0x00, 0x05, 0x1c, 0xea, 0x4c, 0x98, 0x00, 0x09, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x08, 0x00, 0x00, 0x00, 0x00, 0x04, 0x35, 0xe1, 0xe4, 0x00, + 0x00, 0x0a, 0x04, 0x00, 0x00, 0x00, 0x1b, 0x22, 0x00, 0x0a, 0x08, 0x00, 0x00, 0x00, 0x00, 0x07, + 0xd1, 0x54, 0xcd, 0xc8, 0x00, 0x0d, 0x04, 0x00, 0x00, 0x00, 0x00, 0x37, 0x00, 0x0e, 0x04, 0x00, + 0x00, 0x00, 0xc3, 0x50, 0x00, 0x15, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x01, 0x70, 0x4b, 0x83, 0xb0, 0x00, 0x16, 0x04, 0x00, 0x00, 0x00, 0x01, 0x1e, + 0x00, 0x16, 0x08, 0x00, 0x00, 0x00, 0x00, 0x05, 0x66, 0x12, 0xc5, 0x08, 0x00, 0x17, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73, 0x37, 0xec, 0x68, + 0x00, 0x18, 0x04, 0x00, 0x00, 0x00, 0x14, 0x64, 0x00, 0x18, 0x08, 0x00, 0x00, 0x00, 0x00, 0x02, + 0x20, 0x3b, 0x02, 0xa0, 0x00, 0x1d, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1d, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x02, 0x20, 0x65, 0xfa, 0x50, 0x00, 0x1e, 0x04, 0x00, 0x00, 0x00, 0x14, 0x6b, + 0x00, 0x1e, 0x08, 0x00, 0x00, 0x00, 0x00, 0x06, 0x11, 0x8d, 0x2a, 0x40, 0x00, 0x1f, 0x04, 0x00, + 0x00, 0x00, 0x09, 0xf5, 0x00, 0x20, 0x04, 0x00, 0x00, 0x03, 0xa8, 0xf3, 0x00, 0x21, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x37, 0x00, 0x29, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x01, 0x5c, 0x87, 0xe4, 0x90, 0x00, 0x2a, 0x04, 0x00, 0x00, 0x00, 0x00, 0x38, + 0x00, 0x2a, 0x08, 0x00, 0x00, 0x00, 0x00, 0x01, 0x4a, 0x41, 0xd0, 0xc0, 0x00, 0x2b, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x2b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x95, 0x17, 0x47, 0x48, + 0x00, 0x2c, 0x04, 0x00, 0x00, 0x00, 0x01, 0xf9, 0x00, 0x2c, 0x08, 0x00, 0x00, 0x00, 0x00, 0x02, + 0x08, 0xf0, 0x1a, 0x90, 0x00, 0x31, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x31, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x01, 0xf6, 0x0b, 0xe2, 0x38, 0x00, 0x32, 0x04, 0x00, 0x00, 0x00, 0x01, 0xfc, + 0x00, 0x32, 0x08, 0x00, 0x00, 0x00, 0x00, 0x02, 0x2d, 0x8f, 0x04, 0x88, 0x00, 0x33, 0x04, 0x00, + 0x00, 0x00, 0x01, 0x8d, 0x00, 0x34, 0x04, 0x00, 0x00, 0x03, 0xa6, 0xb4, 0x00, 0x35, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x6f, 0x00, 0x3d, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3d, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x00, 0xcf, 0x2b, 0x4b, 0xc8, 0x00, 0x3e, 0x04, 0x00, 0x00, 0x00, 0x00, 0x25, + 0x00, 0x3e, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd6, 0x4a, 0x54, 0x30, 0x00, 0x3f, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x84, 0x69, 0xbf, 0xe8, + 0x00, 0x40, 0x04, 0x00, 0x00, 0x00, 0x04, 0xbb, 0x00, 0x40, 0x08, 0x00, 0x00, 0x00, 0x00, 0x01, + 0x08, 0xbc, 0x51, 0x70, 0x00, 0x45, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x01, 0xa2, 0x02, 0x34, 0x08, 0x00, 0x46, 0x04, 0x00, 0x00, 0x00, 0x04, 0xbb, + 0x00, 0x46, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0xee, 0x6d, 0xbe, 0x30, 0x00, 0x47, 0x04, 0x00, + 0x00, 0x00, 0x02, 0x66, 0x00, 0x48, 0x04, 0x00, 0x00, 0x03, 0xa8, 0x0f, 0x00, 0x49, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x1e, 0x90, 0x00, 0x00, 0x00, 0x02, 0x03, 0x05, 0x52, 0x00, 0x00, 0x00, 0x00, + }, + false, + TelegramData{ + Addr: "192.168.1.4", + Serial: "0", + Data: map[string]float64{ + "1:1.4.0": 0, "1:1.8.0": 6.89131908e+09, + "1:2.4.0": 37.9, "1:2.8.0": 2.371258944e+10, + "1:3.4.0": 0, "1:3.8.0": 6.30376488e+09, + "1:4.4.0": 693.5, "1:4.8.0": 2.195995356e+10, + "1:9.4.0": 0, "1:9.8.0": 1.80838656e+10, + "1:10.4.0": 694.6, "1:10.8.0": 3.357676692e+10, + "1:13.4.0": 0.055, + "1:21.4.0": 0, "1:21.8.0": 6.1789644e+09, + "1:22.4.0": 28.6, "1:22.8.0": 2.31873426e+10, + "1:23.4.0": 0, "1:23.8.0": 1.93304484e+09, + "1:24.4.0": 522, "1:24.8.0": 9.1306728e+09, + "1:29.4.0": 0, "1:29.8.0": 9.13348872e+09, + "1:30.4.0": 522.7, "1:30.8.0": 2.606426784e+10, + "1:31.4.0": 2.549, + "1:32.4.0": 239.859, + "1:41.4.0": 0, "1:41.8.0": 5.84737704e+09, + "1:42.4.0": 5.6000000000000005, "1:42.8.0": 5.54079456e+09, + "1:43.4.0": 0, "1:43.8.0": 2.50133076e+09, + "1:44.4.0": 50.5, "1:44.8.0": 8.73988776e+09, + "1:49.4.0": 0, "1:49.8.0": 8.42294124e+09, + "1:50.4.0": 50.800000000000004, "1:50.8.0": 9.35428212e+09, + "1:51.4.0": 0.397, + "1:52.4.0": 239.284, + "1:61.4.0": 0, "1:61.8.0": 3.47572116e+09, + "1:62.4.0": 3.7, "1:62.8.0": 3.59519544e+09, + "1:63.4.0": 0, "1:63.8.0": 2.22152292e+09, + "1:64.4.0": 121.10000000000001, "1:64.8.0": 4.44152664e+09, + "1:69.4.0": 0, "1:69.8.0": 7.01302068e+09, + "1:70.4.0": 121.10000000000001, "1:70.8.0": 4.00016952e+09, + "1:71.4.0": 0.614, + "1:72.4.0": 239.631, + "144:0.0.0": 3.3752402e+07, + }, + }, + }, + { + "SMA Energy Manager - success", + []byte{192, 168, 1, 4}, + 33168, + []byte{ + 0x53, 0x4d, 0x41, 0x00, 0x00, 0x04, 0x02, 0xa0, 0x00, 0x00, 0x00, 0x01, 0x02, 0x44, 0x00, 0x10, + 0x60, 0x69, 0x01, 0x5d, 0x00, 0x00, 0x00, 0x00, 0xb2, 0x79, 0xba, 0x32, 0x00, 0x01, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x61, 0xcd, 0x60, + 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x08, 0xac, 0x00, 0x02, 0x08, 0x00, 0x00, 0x00, 0x00, 0x09, + 0xa2, 0x63, 0x4e, 0xc0, 0x00, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x63, 0x57, 0x7c, 0xc8, 0x00, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x01, + 0x00, 0x04, 0x08, 0x00, 0x00, 0x00, 0x00, 0x01, 0x56, 0x41, 0x9e, 0xc8, 0x00, 0x09, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x08, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x51, 0x3b, 0x20, + 0x00, 0x0a, 0x04, 0x00, 0x00, 0x00, 0x08, 0xac, 0x00, 0x0a, 0x08, 0x00, 0x00, 0x00, 0x00, 0x09, + 0xed, 0x57, 0xc3, 0xc0, 0x00, 0x0d, 0x04, 0x00, 0x00, 0x00, 0x03, 0xe8, 0x00, 0x15, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xa2, 0x73, 0x88, + 0x00, 0x16, 0x04, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00, 0x16, 0x08, 0x00, 0x00, 0x00, 0x00, 0x07, + 0x7d, 0x59, 0x22, 0xf0, 0x00, 0x17, 0x04, 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x17, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x20, 0xd6, 0xb0, 0x58, 0x00, 0x18, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x18, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x16, 0x93, 0x80, 0x00, 0x1d, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x1d, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6f, 0x58, 0x9a, 0xe8, + 0x00, 0x1e, 0x04, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00, 0x1e, 0x08, 0x00, 0x00, 0x00, 0x00, 0x07, + 0x91, 0xf3, 0x65, 0xe0, 0x00, 0x1f, 0x04, 0x00, 0x00, 0x00, 0x02, 0x8e, 0x00, 0x20, 0x04, 0x00, + 0x00, 0x03, 0xa8, 0xdc, 0x00, 0x21, 0x04, 0x00, 0x00, 0x00, 0x03, 0xe8, 0x00, 0x29, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xf1, 0xb0, + 0x00, 0x2a, 0x04, 0x00, 0x00, 0x00, 0x01, 0xc1, 0x00, 0x2a, 0x08, 0x00, 0x00, 0x00, 0x00, 0x01, + 0x12, 0x13, 0x13, 0x30, 0x00, 0x2b, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2b, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x21, 0xf4, 0xf7, 0x10, 0x00, 0x2c, 0x04, 0x00, 0x00, 0x00, 0x00, 0x07, + 0x00, 0x2c, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0xde, 0x75, 0x90, 0x00, 0x31, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x31, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x28, 0xa4, 0x20, + 0x00, 0x32, 0x04, 0x00, 0x00, 0x00, 0x01, 0xc1, 0x00, 0x32, 0x08, 0x00, 0x00, 0x00, 0x00, 0x01, + 0x78, 0x16, 0x49, 0x18, 0x00, 0x33, 0x04, 0x00, 0x00, 0x00, 0x00, 0xf5, 0x00, 0x34, 0x04, 0x00, + 0x00, 0x03, 0xa6, 0xb3, 0x00, 0x35, 0x04, 0x00, 0x00, 0x00, 0x03, 0xe8, 0x00, 0x3d, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x3d, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x71, 0x38, + 0x00, 0x3e, 0x04, 0x00, 0x00, 0x00, 0x01, 0xcb, 0x00, 0x3e, 0x08, 0x00, 0x00, 0x00, 0x00, 0x01, + 0x13, 0x47, 0x1e, 0xe0, 0x00, 0x3f, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x21, 0x45, 0x0d, 0x50, 0x00, 0x40, 0x04, 0x00, 0x00, 0x00, 0x00, 0x0c, + 0x00, 0x40, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x05, 0xcc, 0x40, 0x00, 0x45, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x45, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1d, 0x95, 0xe0, 0x30, + 0x00, 0x46, 0x04, 0x00, 0x00, 0x00, 0x01, 0xcb, 0x00, 0x46, 0x08, 0x00, 0x00, 0x00, 0x00, 0x01, + 0x5e, 0xad, 0xe3, 0xf0, 0x00, 0x47, 0x04, 0x00, 0x00, 0x00, 0x00, 0xe7, 0x00, 0x48, 0x04, 0x00, + 0x00, 0x03, 0xa7, 0xa9, 0x00, 0x49, 0x04, 0x00, 0x00, 0x00, 0x03, 0xe8, 0x90, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x12, 0x52, 0x00, 0x00, 0x00, 0x00, + }, + false, + TelegramData{ + Addr: "192.168.1.4", + Serial: "0", + Data: map[string]float64{ + "1:1.4.0": 0, "1:1.8.0": 1.2385008e+08, + "1:2.4.0": 222, "1:2.8.0": 4.137912288e+10, + "1:3.4.0": 0, "1:3.8.0": 1.66667796e+09, + "1:4.4.0": 0.1, "1:4.8.0": 5.74210836e+09, + "1:9.4.0": 0, "1:9.8.0": 4.41773136e+09, + "1:10.4.0": 222, "1:10.8.0": 4.26366576e+10, + "1:13.4.0": 1, + "1:21.4.0": 0, "1:21.8.0": 1.2808692e+08, + "1:22.4.0": 131.20000000000002, "1:22.8.0": 3.216776472e+10, + "1:23.4.0": 1.8, "1:23.8.0": 5.5094076e+08, + "1:24.4.0": 0, "1:24.8.0": 2.31673536e+09, + "1:29.4.0": 0, "1:29.8.0": 1.8680778e+09, + "1:30.4.0": 131.20000000000002, "1:30.8.0": 3.251341872e+10, + "1:31.4.0": 0.654, + "1:32.4.0": 239.836, + "1:41.4.0": 0, "1:41.8.0": 258480, + "1:42.4.0": 44.900000000000006, "1:42.8.0": 4.59820728e+09, + "1:43.4.0": 0, "1:43.8.0": 5.6970216e+08, + "1:44.4.0": 0.7000000000000001, "1:44.8.0": 1.72585512e+09, + "1:49.4.0": 0, "1:49.8.0": 6.977232e+07, + "1:50.4.0": 44.900000000000006, "1:50.8.0": 6.30969372e+09, + "1:51.4.0": 0.245, + "1:52.4.0": 239.28300000000002, + "1:61.4.0": 0, "1:61.8.0": 749880, + "1:62.4.0": 45.900000000000006, "1:62.8.0": 4.61839536e+09, + "1:63.4.0": 0, "1:63.8.0": 5.5817352e+08, + "1:64.4.0": 1.2000000000000002, "1:64.8.0": 1.711656e+09, + "1:69.4.0": 0, "1:69.8.0": 4.9636152e+08, + "1:70.4.0": 45.900000000000006, "1:70.8.0": 5.88342168e+09, + "1:71.4.0": 0.231, + "1:72.4.0": 239.529, + "144:0.0.0": 3.3559122e+07, + }, + }, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + l := &Listener{} + + buffer := tc.response + numBytes := len(buffer) + src := &net.UDPAddr{IP: tc.ip, Port: tc.port} + + got, err := l.processUDPData(src, buffer[:numBytes-1]) + if (err != nil) != tc.wantErr { + t.Errorf("Listener.processUDPData() error = %v, wantErr %v", err, tc.wantErr) + return + } + if !reflect.DeepEqual(got, tc.want) { + t.Errorf("Listener.processUDPData() = %v, want %v", got, tc.want) + } + + }) + } +}