package main import ( "flag" "fmt" "time" "github.com/abates/insteon" "github.com/abates/insteon/plm" "github.com/golang/glog" "github.com/tarm/serial" ) var ( serialPort = flag.String("port", "/dev/ttyUSB0", "serial port") timeout = flag.Duration("timeout", 3*time.Second, "read/write timeout duration") writeDelay = flag.Duration("writeDelay", 0, "writeDelay duration (default of 0 indicates to compute wait time based on message length and ttl)") ttl = flag.Uint("ttl", 2, "default ttl for sending Insteon messages") ) func openModem() (*plm.PLM, error) { c := &serial.Config{ Name: *serialPort, Baud: 19200, } s, err := serial.OpenPort(c) if err != nil { return nil, fmt.Errorf("error opening serial port: %w", err) } modem, err := plm.New(plm.NewPort(s, *timeout), *timeout, plm.WriteDelay(*writeDelay)) if err != nil { return nil, fmt.Errorf("error opening plm: %w", err) } fmt.Printf("modem opened as %v\n", modem) return modem, nil } func fetch(modem *plm.PLM, addr insteon.Address) error { glog.Infof("fetch(%v)", addr) dev, err := modem.Open(addr, insteon.ConnectionTimeout(*timeout), insteon.ConnectionTTL(uint8(*ttl))) if err != nil { return fmt.Errorf("error connecting to %v: %w", addr, err) } if linkable, ok := dev.(insteon.LinkableDevice); ok { links, err := linkable.Links() if err != nil { return fmt.Errorf("error fetching links from %v: %w", addr, err) } fmt.Printf("found %d links on %v\n", len(links), addr) } return nil } func main() { flag.Parse() insteon.Log.Level(insteon.LevelDebug) m, err := openModem() if err != nil { glog.Fatal(err) } addrs := []insteon.Address{ insteon.Address{0x33, 0xAD, 0x00}, insteon.Address{0x33, 0x14, 0x1D}, insteon.Address{0x2C, 0x7D, 0x24}, } for _, a := range addrs { err := fetch(m, a) if err != nil { glog.Error(err) } } }