Skip to content

Commit

Permalink
SMA: make network interface for multicast configurable (evcc-io#1075)
Browse files Browse the repository at this point in the history
Co-authored-by: andig <[email protected]>
  • Loading branch information
DesertFox8 and andig authored May 25, 2021
1 parent 3c895ef commit addef62
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion detect/tasks/sma.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (h *SMAHandler) Test(log *util.Logger, in ResultDetails) (res []ResultDetai
}

var err error
if h.listener, err = sma.New(log); err != nil {
if h.listener, err = sma.New(log, ""); err != nil {
log.ERROR.Println("shm:", err)
return nil
}
Expand Down
10 changes: 6 additions & 4 deletions meter/sma.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type SMA struct {
mux *util.Waiter
uri string
serial string
iface string
values values
powerO sma.Obis
energyO sma.Obis
Expand All @@ -42,32 +43,33 @@ func init() {
// NewSMAFromConfig creates a SMA Meter from generic config
func NewSMAFromConfig(other map[string]interface{}) (api.Meter, error) {
cc := struct {
URI, Serial, Power, Energy string
URI, Serial, Interface, Power, Energy string
}{}

if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}

return NewSMA(cc.URI, cc.Serial, cc.Power, cc.Energy)
return NewSMA(cc.URI, cc.Serial, cc.Interface, cc.Power, cc.Energy)
}

// NewSMA creates a SMA Meter
func NewSMA(uri, serial, power, energy string) (api.Meter, error) {
func NewSMA(uri, serial, iface, power, energy string) (api.Meter, error) {
log := util.NewLogger("sma")

sm := &SMA{
mux: util.NewWaiter(udpTimeout, func() { log.TRACE.Println("wait for initial value") }),
log: log,
uri: uri,
serial: serial,
iface: iface,
powerO: sma.Obis(power),
energyO: sma.Obis(energy),
recv: make(chan sma.Telegram),
}

if sma.Instance == nil {
instance, err := sma.New(log)
instance, err := sma.New(log, iface)
if err != nil {
return nil, err
}
Expand Down
18 changes: 14 additions & 4 deletions meter/sma/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,25 @@ type Listener struct {
}

// New creates a Listener
func New(log *util.Logger) (*Listener, error) {
// Parse the string address
func New(log *util.Logger, ifaceName string) (*Listener, error) {
var iface *net.Interface
if ifaceName != "" {
var err error
if iface, err = net.InterfaceByName(ifaceName); err != nil {
return nil, fmt.Errorf("error resolving network interface '%s': %w", ifaceName, err)
}

log.DEBUG.Printf("listening on network interface %s for multicasts", iface.Name)
}

// parse address
gaddr, err := net.ResolveUDPAddr("udp4", multicastAddr)
if err != nil {
return nil, fmt.Errorf("error resolving udp address: %w", err)
}

// Open up a connection
conn, err := net.ListenMulticastUDP("udp4", nil, gaddr)
// open connection
conn, err := net.ListenMulticastUDP("udp4", iface, gaddr)
if err != nil {
return nil, fmt.Errorf("error opening connecting: %w", err)
}
Expand Down

0 comments on commit addef62

Please sign in to comment.