|
|
@@ -16,18 +16,19 @@ import (
|
|
|
const MODULE_NAME = "NetworkManager"
|
|
|
|
|
|
var (
|
|
|
- isOnline atomic.Bool // 标记是否联网
|
|
|
- offlineStartTs atomic.Int64 // 离线开始时间
|
|
|
- isSyncNTPTimeOK atomic.Bool // 标记本地时间是否已同步成功
|
|
|
- lastSyncNTPTime time.Time // 记录同步网络时间成功的时间
|
|
|
- curNetType NetType // 当前的网络类型: 有线、蜂窝
|
|
|
- mu1 sync.Mutex
|
|
|
- isRunning1 bool
|
|
|
- exitCh1 chan struct{}
|
|
|
- wg1 sync.WaitGroup
|
|
|
+ isOnline atomic.Bool // 标记是否联网
|
|
|
+ offlineStartTime time.Time // 离线开始时间
|
|
|
+ enableTimeSync atomic.Bool /// 是否启用时间同步(默认启用)
|
|
|
+ isSyncNTPTimeOK atomic.Bool /// 标记本地时间是否已同步成功
|
|
|
+ lastSyncNTPTime time.Time /// 记录同步网络时间成功的时间
|
|
|
+ curNetType atomic.Int32 /// 当前的网络类型: 有线、蜂窝
|
|
|
+ mu1 sync.Mutex
|
|
|
+ isRunning1 bool
|
|
|
+ exitCh1 chan struct{}
|
|
|
+ wg1 sync.WaitGroup
|
|
|
)
|
|
|
|
|
|
-type NetType int
|
|
|
+type NetType int32
|
|
|
|
|
|
const (
|
|
|
NetNone NetType = iota
|
|
|
@@ -46,12 +47,29 @@ func (n NetType) String() string {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func setNetType(n NetType) {
|
|
|
+ curNetType.Store(int32(n))
|
|
|
+}
|
|
|
+
|
|
|
+func getNetType() NetType {
|
|
|
+ return NetType(curNetType.Load())
|
|
|
+}
|
|
|
+
|
|
|
+func init() {
|
|
|
+ enableTimeSync.Store(true) // 默认开启时间同步
|
|
|
+}
|
|
|
+
|
|
|
+func SetTimeSyncEnabled(enable bool) {
|
|
|
+ enableTimeSync.Store(enable)
|
|
|
+}
|
|
|
+
|
|
|
const (
|
|
|
interval1 = time.Duration(1) * time.Second
|
|
|
interval2 = time.Duration(5) * time.Second
|
|
|
)
|
|
|
|
|
|
func ModuleInit() {
|
|
|
+ offlineStartTime = time.Now()
|
|
|
go serviceRun()
|
|
|
}
|
|
|
|
|
|
@@ -68,23 +86,28 @@ func serviceRun() {
|
|
|
case <-t.C:
|
|
|
// 3.1 切换网络-看情况
|
|
|
eth0CableOK, _ := isEth0CableConnected()
|
|
|
- if eth0CableOK && curNetType != NetEth { // 有线插入 && 当前不是有线
|
|
|
+ if eth0CableOK && getNetType() != NetEth { // 有线插入 && 当前不是有线
|
|
|
baseapp.Logger.Warnf("[%s] 检测到有线接入,正在尝试切换到有线网络...", MODULE_NAME)
|
|
|
openNetwork()
|
|
|
- } else if !eth0CableOK && curNetType == NetEth { // 有线拔出 && 当前还是有线
|
|
|
+ } else if !eth0CableOK && getNetType() == NetEth { // 有线拔出 && 当前还是有线
|
|
|
baseapp.Logger.Warnf("[%s] 检测到有线断开,正在尝试切换到蜂窝网络...", MODULE_NAME)
|
|
|
openNetwork()
|
|
|
}
|
|
|
|
|
|
// 3.2, 联网检测-看结果
|
|
|
dnsOK, pingOK, tcpOK, httpOK, httpTime := CheckNetwork()
|
|
|
- baseapp.Logger.Infof("[%s] 联网类型: %s; 检测结果: DNS OK=%v, PING OK=%v, TCP OK=%v, HTTP OK=%v", MODULE_NAME, curNetType.String(), dnsOK, pingOK, tcpOK, httpOK)
|
|
|
+ baseapp.Logger.Infof("[%s] 联网类型: %s; 检测结果: DNS OK=%v, PING OK=%v, TCP OK=%v, HTTP OK=%v", MODULE_NAME, getNetType().String(), dnsOK, pingOK, tcpOK, httpOK)
|
|
|
|
|
|
// 3.3, 联网成功-在线时
|
|
|
netOK := tcpOK || pingOK
|
|
|
if netOK {
|
|
|
isOnline.Store(true)
|
|
|
- offlineStartTs.Store(0)
|
|
|
+
|
|
|
+ if !enableTimeSync.Load() { // 时间同步功能被禁用, 下面不再同步
|
|
|
+ isSyncNTPTimeOK.Store(false)
|
|
|
+ t.Reset(interval2)
|
|
|
+ continue
|
|
|
+ }
|
|
|
|
|
|
if isSyncNTPTimeOK.Load() && time.Since(lastSyncNTPTime) < (time.Duration(60)*time.Minute) {
|
|
|
t.Reset(interval2)
|
|
|
@@ -116,11 +139,11 @@ func serviceRun() {
|
|
|
|
|
|
// 3.4, 联网失败-离线时
|
|
|
if isOnline.Load() { // 状态由"1"变为"0"
|
|
|
+ offlineStartTime = time.Now() // 记录离线开始时间
|
|
|
isOnline.Store(false)
|
|
|
- offlineStartTs.Store(time.Now().UnixNano()) // 记录离线开始时间
|
|
|
}
|
|
|
|
|
|
- if offlineDuration() >= (time.Duration(60) * time.Second) {
|
|
|
+ if time.Since(offlineStartTime) >= (time.Duration(60) * time.Second) {
|
|
|
baseapp.Logger.Warnf("[%s] 网络长时间的断开, 尝试重新激活...", MODULE_NAME)
|
|
|
switch curModemType {
|
|
|
case Air720U: //重启合宙4G调制解调器
|
|
|
@@ -131,7 +154,7 @@ func serviceRun() {
|
|
|
modem2.ModuleInit(true)
|
|
|
}
|
|
|
openNetwork()
|
|
|
- offlineStartTs.Store(time.Now().UnixNano()) // 重置离线开始时间
|
|
|
+ offlineStartTime = time.Now() // 重置离线开始时间
|
|
|
}
|
|
|
|
|
|
t.Reset(interval1)
|
|
|
@@ -157,8 +180,10 @@ func WaitAllOK(timeout time.Duration) bool {
|
|
|
tick := 50 * time.Millisecond
|
|
|
|
|
|
for {
|
|
|
- if IsInetAvailable() && IsSyncedNtpTime() {
|
|
|
- return true
|
|
|
+ if IsInetAvailable() {
|
|
|
+ if !enableTimeSync.Load() || IsSyncedNtpTime() {
|
|
|
+ return true
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
remaining := time.Until(deadline)
|
|
|
@@ -171,19 +196,6 @@ func WaitAllOK(timeout time.Duration) bool {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// 返回断网时长
|
|
|
-func offlineDuration() time.Duration {
|
|
|
- if IsInetAvailable() {
|
|
|
- return 0
|
|
|
- }
|
|
|
- ts := offlineStartTs.Load()
|
|
|
- if ts == 0 {
|
|
|
- offlineStartTs.Store(time.Now().UnixNano())
|
|
|
- ts = offlineStartTs.Load()
|
|
|
- }
|
|
|
- return time.Since(time.Unix(0, ts))
|
|
|
-}
|
|
|
-
|
|
|
// 打开有线网络
|
|
|
func openEth0Net() bool {
|
|
|
mu1.Lock()
|
|
|
@@ -260,12 +272,12 @@ func openNetwork() {
|
|
|
|
|
|
eth0CableOK, _ := isEth0CableConnected()
|
|
|
if eth0CableOK && openEth0Net() {
|
|
|
- curNetType = NetEth
|
|
|
+ setNetType(NetEth)
|
|
|
baseapp.Logger.Infof("[%s] ✅ 有线网络已连接", MODULE_NAME)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- if curNetType == NetLTE && IsInetAvailable() { // 当前已是蜂窝网络且联网正常
|
|
|
+ if getNetType() == NetLTE && IsInetAvailable() { // 当前已是蜂窝网络且联网正常
|
|
|
return
|
|
|
}
|
|
|
|
|
|
@@ -289,18 +301,18 @@ func openNetwork() {
|
|
|
}
|
|
|
|
|
|
if start4GNetwork() {
|
|
|
- curNetType = NetLTE
|
|
|
+ setNetType(NetLTE)
|
|
|
baseapp.Logger.Infof("[%s] ✅ 蜂窝网络已连接(%s)", MODULE_NAME, curModemType.String())
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- curNetType = NetNone
|
|
|
+ setNetType(NetNone)
|
|
|
baseapp.Logger.Warnf("[%s] 当前无可用网络接口(有线/蜂窝均不可用)!", MODULE_NAME)
|
|
|
}
|
|
|
|
|
|
// 得到当前联网类型: 有线、蜂窝
|
|
|
func GetCurrentNetType() NetType {
|
|
|
- return curNetType
|
|
|
+ return getNetType()
|
|
|
}
|
|
|
|
|
|
//export RTU_IsInetAvailable
|