/
alarmguard.go
163 lines (147 loc) · 3.61 KB
/
alarmguard.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"fmt"
"net"
"net/http"
"strings"
"time"
)
var (
info []net.Addr
ip string
geturl string
request *http.Request
resp *http.Response
client *http.Client
ipx string
)
func main() {
info, _ = net.InterfaceAddrs()
ip = strings.Split(info[0].String(), "/")[0]
fmt.Println(ip)
fmt.Println("alarm will begin in 30 second..")
fmt.Println("please DO NOT close..")
i := 30
for i > 0 {
time.Sleep(time.Second * 1)
fmt.Println(i)
i--
}
geturl = "http://" + ip + "/alarmbegin"
request, _ = http.NewRequest("GET", geturl, nil)
///not must
client = &http.Client{}
resp, _ = client.Do(request)
fmt.Println(resp)
fmt.Println("done")
go Iva()
go Ivs()
Ref()
}
func IvsOffLine() {
info, _ = net.InterfaceAddrs()
ipx = strings.Split(info[0].String(), "/")[0]
//fmt.Println(ipx)
if ipx == "0.0.0.0" {
fmt.Println("network problem....")
fmt.Println(strings.Split(info[0].String(), "/")[0])
for strings.Split(info[0].String(), "/")[0] == "0.0.0.0" {
time.Sleep(time.Second * 5)
info, _ = net.InterfaceAddrs()
}
fmt.Println("network problem fixed.. alarm will restart in 1 min")
time.Sleep(time.Second * 30)
geturl = "http://" + strings.Split(info[0].String(), "/")[0] + "/refresh"
request, _ = http.NewRequest("GET", geturl, nil)
resp, _ = client.Do(request)
fmt.Println(resp)
time.Sleep(time.Second * 2)
fmt.Println("alarm begin..")
geturl = "http://" + strings.Split(info[0].String(), "/")[0] + "/alarmbegin"
request, _ = http.NewRequest("GET", geturl, nil)
resp, _ = client.Do(request)
fmt.Println(resp)
fmt.Println("done..")
}
}
func IvaOffLine() {
defer func() {
if err := recover(); err != nil {
fmt.Println(err) //panic
}
}()
geturl = "http://192.168.10.57/milsng/SVSProxy/"
request, _ = http.NewRequest("GET", geturl, nil)
resp, _ = client.Do(request)
if resp == nil {
fmt.Println("iva connect fail...try to reconnect")
for resp == nil {
time.Sleep(time.Second * 5)
geturl = "http://192.168.10.57/milsng/SVSProxy/"
request, _ = http.NewRequest("GET", geturl, nil)
resp, _ = client.Do(request)
fmt.Println("iva connect fail...try to reconnect")
}
fmt.Println("iva connect refreshing... pls wait")
info, _ = net.InterfaceAddrs()
if strings.Split(info[0].String(), "/")[0] != "0.0.0.0" {
time.Sleep(time.Minute * 10)
geturl = "http://" + strings.Split(info[0].String(), "/")[0] + "/refresh"
request, _ = http.NewRequest("GET", geturl, nil)
resp, _ = client.Do(request)
fmt.Println(resp)
time.Sleep(time.Second * 2)
fmt.Println("alarm begin..")
geturl = "http://" + strings.Split(info[0].String(), "/")[0] + "/alarmbegin"
request, _ := http.NewRequest("GET", geturl, nil)
resp, _ = client.Do(request)
fmt.Println(resp)
}
}
if resp != nil {
resp.Body.Close()
}
//fmt.Println("iva connect success")
}
func Refresh() {
geturl = "http://" + strings.Split(info[0].String(), "/")[0] + "/refresh"
request, _ = http.NewRequest("GET", geturl, nil)
resp, _ = client.Do(request)
fmt.Println(resp)
time.Sleep(time.Second * 2)
fmt.Println("alarm begin..")
geturl = "http://" + strings.Split(info[0].String(), "/")[0] + "/alarmbegin"
request, _ := http.NewRequest("GET", geturl, nil)
resp, _ = client.Do(request)
fmt.Println(resp)
if resp != nil {
resp.Body.Close()
}
}
func Ivs() {
ivs := time.NewTicker(10 * time.Second)
for {
select {
case <-ivs.C:
IvsOffLine()
}
}
}
func Iva() {
iva := time.NewTicker(10 * time.Second)
for {
select {
case <-iva.C:
IvaOffLine()
}
}
}
func Ref() {
ref := time.NewTicker(1 * time.Hour)
for {
select {
case <-ref.C:
Refresh()
}
}
}