-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (64 loc) · 1.2 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
81
82
83
84
85
86
87
88
package main
import (
"fmt"
"net"
"os"
"os/signal"
"syscall"
"time"
"github.com/hink/go-blink1"
)
const (
pingTimeout = time.Duration(10 * time.Second)
pingEvery = time.Duration(20 * time.Second)
)
var (
blue = blink1.State{Blue: 20}
red = blink1.State{Red: 20}
green = blink1.State{Green: 20}
amber = blink1.State{Red: 20, Green: 15}
off = blink1.State{}
)
func main() {
device, err := blink1.OpenNextDevice()
defer device.Close()
if err != nil {
panic(err)
}
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
stopBlink(device)
os.Exit(1)
}()
fmt.Println("# monitoring internet connectivity #")
device.SetState(blue)
for {
err := connectedToInternet()
switch err := err.(type) {
case net.Error:
if err.Timeout() {
device.SetState(amber)
} else {
device.SetState(red)
}
case nil:
device.SetState(green)
}
time.Sleep(pingEvery)
}
}
func connectedToInternet() error {
conn, err := net.DialTimeout("tcp", "www.google.com:80", pingTimeout)
if err != nil {
return err
}
conn.Close()
return err
}
func stopBlink(d *blink1.Device) {
fmt.Println("stopping blink1")
d.SetState(off)
d.Close()
}