-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (69 loc) · 1.61 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"path/filepath"
"runtime"
"syscall"
"github.com/jbielick/zwgo/controller"
log "github.com/sirupsen/logrus"
)
var portFlag = flag.String("port", "", "Name of the zwave serial port (/dev/ttyACM0 for example). Use of this flag disables autodiscovery.")
var verbosityFlag = flag.String("log-level", "INFO", "Logging verbosity")
func init() {
flag.Parse()
level, err := log.ParseLevel(*verbosityFlag)
if err != nil {
log.Fatal(fmt.Errorf("'%s' is not a valid log level, please use one of %v", *verbosityFlag, log.AllLevels))
}
log.SetLevel(level)
}
func main() {
var port string
if len(*portFlag) > 0 {
port = *portFlag
} else {
log.Info("Discovering port...")
ports, err := discoverPorts()
if err != nil {
log.Fatal(err)
}
if len(ports) < 1 {
log.Fatal("Failed to find port, please provide -port flag to specify the port explicitly.")
}
port = ports[0]
}
log.Infof("Using port %s", port)
c := controller.New(controller.NewConfig(port))
err := c.Open()
if err != nil {
log.Fatal(err)
}
defer c.Close()
log.Printf("DEBUG: %+v\n", c)
s := make(chan os.Signal)
signal.Notify(s, os.Interrupt)
signal.Notify(s, syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-s
signal.Stop(s)
cancel()
}()
<-ctx.Done()
}
func discoverPorts() ([]string, error) {
var glob string
switch runtime.GOOS {
case "darwin":
glob = "/dev/tty.usbmodem*"
case "linux":
glob = "/dev/ttyACM*"
default:
log.Fatalf("must provide port argument for platform %s", runtime.GOOS)
}
return filepath.Glob(glob)
}