Переглянути джерело

1, 优化网络模块代码, 当eth0有线插入但不能上网时, 对eth0网卡降级, 设置静态ip(192.168.80.1/24); 2, 优化killEth0Udhcpc()函数, 不再依赖系统的ps指令; 3, 重新编译打包标准模块

niujiuru 1 тиждень тому
батько
коміт
312b252a40

+ 15 - 9
air720u/eth2net.go

@@ -106,22 +106,28 @@ func killEth2Udhcpc() error {
 
 // 强制杀死所有运行中的"udhcpc"进程,包括驻留在后台运行的(SIGKILL)
 func killAllUdhcpc() {
-	cmd := exec.Command("sh", "-c", `ps | grep '[u]dhcpc' | awk '{print $1}'`)
-	output, err := cmd.Output()
+	entries, err := os.ReadDir("/proc")
 	if err != nil {
-		return // 没找到也不是错误
+		return
 	}
 
-	pids := strings.FieldsSeq(string(output))
-	for pidStr := range pids {
-		pid, err := strconv.Atoi(pidStr)
+	for _, entry := range entries {
+		name := entry.Name()
+
+		pid, err := strconv.Atoi(name)
 		if err != nil {
 			continue
 		}
-		if err := syscall.Kill(pid, syscall.SIGTERM); err != nil && err != syscall.ESRCH {
-			syscall.Kill(pid, syscall.SIGKILL)
+
+		comm, err := os.ReadFile("/proc/" + name + "/comm")
+		if err != nil {
+			continue
 		}
-	}
+
+		if strings.TrimSpace(string(comm)) == "udhcpc" {
+			_ = syscall.Kill(pid, syscall.SIGKILL)
+		} // if end
+	} /// for end
 }
 
 // 运行"udhcpc"后, 获取"eth2"网口分配到的IPv4地址, 并校检其合法性

+ 15 - 9
ec200u/usb0net.go

@@ -106,22 +106,28 @@ func killUSB0Udhcpc() error {
 
 // 强制杀死所有运行中的"udhcpc"进程,包括驻留在后台运行的(SIGKILL)
 func killAllUdhcpc() {
-	cmd := exec.Command("sh", "-c", `ps | grep '[u]dhcpc' | awk '{print $1}'`)
-	output, err := cmd.Output()
+	entries, err := os.ReadDir("/proc")
 	if err != nil {
-		return // 没找到也不是错误
+		return
 	}
 
-	pids := strings.FieldsSeq(string(output))
-	for pidStr := range pids {
-		pid, err := strconv.Atoi(pidStr)
+	for _, entry := range entries {
+		name := entry.Name()
+
+		pid, err := strconv.Atoi(name)
 		if err != nil {
 			continue
 		}
-		if err := syscall.Kill(pid, syscall.SIGTERM); err != nil && err != syscall.ESRCH {
-			syscall.Kill(pid, syscall.SIGKILL)
+
+		comm, err := os.ReadFile("/proc/" + name + "/comm")
+		if err != nil {
+			continue
 		}
-	}
+
+		if strings.TrimSpace(string(comm)) == "udhcpc" {
+			_ = syscall.Kill(pid, syscall.SIGKILL)
+		} // if end
+	} /// for end
 }
 
 // 运行"udhcpc"后, 获取"usb0"网口分配到的IPv4地址, 并校检其合法性

+ 1 - 1
main.go

@@ -112,7 +112,7 @@ func main() {
 	netmgrd.WaitAllOK(time.Duration(30) * time.Second) // 等待联网成功和NTP时间同步成功
 
 	// 04, 初始化相机拍照模块
-	if !camera1.ModuleInit(false, "192.168.100.123/24", "192.168.100.100/24") {
+	if !camera1.ModuleInit(false, "192.168.100.1/24", "192.168.100.100/24") {
 		goto end_p
 	}
 

+ 37 - 9
netmgrd/eth0net.go

@@ -107,22 +107,28 @@ func killEth0Udhcpc() error {
 
 // 强制杀死所有运行中的"udhcpc"进程,包括驻留在后台运行的(SIGKILL)
 func killAllUdhcpc() {
-	cmd := exec.Command("sh", "-c", `ps | grep '[u]dhcpc' | awk '{print $1}'`)
-	output, err := cmd.Output()
+	entries, err := os.ReadDir("/proc")
 	if err != nil {
-		return // 没找到也不是错误
+		return
 	}
 
-	pids := strings.FieldsSeq(string(output))
-	for pidStr := range pids {
-		pid, err := strconv.Atoi(pidStr)
+	for _, entry := range entries {
+		name := entry.Name()
+
+		pid, err := strconv.Atoi(name)
 		if err != nil {
 			continue
 		}
-		if err := syscall.Kill(pid, syscall.SIGTERM); err != nil && err != syscall.ESRCH {
-			syscall.Kill(pid, syscall.SIGKILL)
+
+		comm, err := os.ReadFile("/proc/" + name + "/comm")
+		if err != nil {
+			continue
 		}
-	}
+
+		if strings.TrimSpace(string(comm)) == "udhcpc" {
+			_ = syscall.Kill(pid, syscall.SIGKILL)
+		} // if end
+	} /// for end
 }
 
 // 运行"udhcpc"后, 获取"eth0"网口分配到的IPv4地址, 并校检其合法性
@@ -210,3 +216,25 @@ func isEth0CableConnected() (bool, error) {
 
 	return waitEth0Carrier(2000)
 }
+
+func SetupEth0ForManagement(cidr string) error {
+	link, err := netlink.LinkByName("eth0")
+	if err != nil {
+		return err
+	}
+
+	if err = netlink.LinkSetUp(link); err != nil {
+		return err
+	}
+
+	addr, err := netlink.ParseAddr(cidr)
+	if err != nil {
+		return err
+	}
+
+	if err = netlink.AddrAdd(link, addr); err != nil && err != syscall.EEXIST {
+		return err
+	}
+
+	return nil
+}

+ 27 - 18
netmgrd/netmgrd.go

@@ -16,12 +16,13 @@ import (
 const MODULE_NAME = "NetworkManager"
 
 var (
-	isOnline         atomic.Bool  // 标记是否联网
-	offlineStartTime time.Time    // 离线开始时间
-	enableTimeSync   atomic.Bool  /// 是否启用时间同步(默认启用)
-	isSyncNTPTimeOK  atomic.Bool  /// 标记本地时间是否已同步成功
-	lastSyncNTPTime  time.Time    /// 记录同步网络时间成功的时间
-	curNetType       atomic.Int32 /// 当前的网络类型: 有线、蜂窝
+	eth0LinkDegraded atomic.Bool  // 当eth0网卡无法上网时标记降级
+	isOnline         atomic.Bool  // 标记是否联网: true 或 false
+	offlineStartTime time.Time    // 离线开始时间, 不依赖系统时钟
+	enableTimeSync   atomic.Bool  // 是否启用时间同步(默认: 启用)
+	isSyncNTPTimeOK  atomic.Bool  // 标记系统的时间是否已同步成功
+	lastSyncNTPTime  time.Time    // 记录同步时间, 为下次同步准备
+	curNetType       atomic.Int32 // 当前联网类型: "有线"、"蜂窝"
 	mu1              sync.Mutex
 	isRunning1       bool
 	exitCh1          chan struct{}
@@ -65,7 +66,7 @@ func SetTimeSyncEnabled(enable bool) {
 
 const (
 	interval1 = time.Duration(1) * time.Second
-	interval2 = time.Duration(5) * time.Second
+	interval2 = time.Duration(3) * time.Second
 )
 
 func ModuleInit() {
@@ -86,11 +87,15 @@ func serviceRun() {
 		case <-t.C:
 			// 3.1 切换网络-看情况
 			eth0CableOK, _ := isEth0CableConnected()
-			if eth0CableOK && getNetType() != NetEth { // 有线插入 && 当前不是有线
-				baseapp.Logger.Warnf("[%s] 检测到有线接入,正在尝试切换到有线网络...", MODULE_NAME)
+			if !eth0CableOK { // 检测到eth0的网线拔出后, 恢复到降级前状态, 进入下一轮
+				eth0LinkDegraded.CompareAndSwap(true, false)
+			}
+
+			if eth0CableOK && getNetType() != NetEth && !eth0LinkDegraded.Load() { /////////// 有线插入 && 当前不是有线 && 有线还未降级
+				baseapp.Logger.Warnf("[%s] 检测到有线插入,正在尝试切换到有线网络...", MODULE_NAME)
 				openNetwork()
-			} else if !eth0CableOK && getNetType() == NetEth { // 有线拔出 && 当前还是有线
-				baseapp.Logger.Warnf("[%s] 检测到有线断开,正在尝试切换到蜂窝网络...", MODULE_NAME)
+			} else if (!eth0CableOK || eth0LinkDegraded.Load()) && getNetType() == NetEth { // (有线拔出 || 有线已降级) && 当前还是有线
+				baseapp.Logger.Warnf("[%s] 检测到有线断,正在尝试切换到蜂窝网络...", MODULE_NAME)
 				openNetwork()
 			}
 
@@ -143,6 +148,13 @@ func serviceRun() {
 				isOnline.Store(false)
 			}
 
+			if time.Since(offlineStartTime) >= (time.Duration(20)*time.Second) &&
+				getNetType() == NetEth && !eth0LinkDegraded.Load() { // eth0 无法联网时
+				baseapp.Logger.Warnf("[%s] 通过eth0无法联网, 将网卡等级降级!", MODULE_NAME)
+				eth0LinkDegraded.Store(true) // 标记eth0网卡降级, 并设置静态IP地址,运维用
+				SetupEth0ForManagement("192.168.80.1/24")
+			}
+
 			if time.Since(offlineStartTime) >= (time.Duration(60) * time.Second) {
 				baseapp.Logger.Warnf("[%s] 网络长时间的断开, 尝试重新激活...", MODULE_NAME)
 				switch curModemType {
@@ -271,13 +283,10 @@ func openNetwork() {
 	}
 
 	eth0CableOK, _ := isEth0CableConnected()
-	if eth0CableOK && openEth0Net() {
+	if eth0CableOK && !eth0LinkDegraded.Load() { // 有线插入 && 有线未降级
+		_ = openEth0Net()
 		setNetType(NetEth)
-		baseapp.Logger.Infof("[%s] ✅ 有线网络已连接", MODULE_NAME)
-		return
-	}
-
-	if getNetType() == NetLTE && IsInetAvailable() { // 当前已是蜂窝网络且联网正常
+		baseapp.Logger.Infof("[%s] ✅ 有线网络已激活", MODULE_NAME)
 		return
 	}
 
@@ -302,7 +311,7 @@ func openNetwork() {
 
 	if start4GNetwork() {
 		setNetType(NetLTE)
-		baseapp.Logger.Infof("[%s] ✅ 蜂窝网络已连接(%s)", MODULE_NAME, curModemType.String())
+		baseapp.Logger.Infof("[%s] ✅ 蜂窝网络已激活(%s)", MODULE_NAME, curModemType.String())
 		return
 	}
 

BIN
package/rtu_linux_modules_1.0.0.1.tar.gz


BIN
package/yfkj_ssh_client_1.0.0.1.tar.gz