Просмотр исходного кода

1, 新增同步系统时间的小程序;2, 优化之前提交的新增HTTP同步获取时间的netmgrd.go代码;3, 重新编译打包

niujiuru 1 неделя назад
Родитель
Сommit
2ff37580af

+ 7 - 1
Makefile

@@ -29,7 +29,7 @@ SETGO_ENV = \
 DATE := $(shell date +%Y%m%d_%H%M%S)
 
 # 编译的目标
-all : camera_test.out hk_takephoto.out dh_takephoto.out air720u_4g.out rtu_linux_modules.out yfkj_sshd.out yfkj_ssh_client.out yfkj_ssh_client.exe
+all : camera_test.out hk_takephoto.out dh_takephoto.out air720u_4g.out rtu_linux_modules.out yfkj_sshd.out yfkj_ssh_client.out yfkj_ssh_client.exe yfkj_timesync.out
 
 # 通用基础库
 libswapi.a :
@@ -142,6 +142,12 @@ yfkj_ssh_client.exe : ./sshd/client/client.go
 	GOOS=windows GOARCH=amd64 CGO_ENABLED=0 $(GO_BUILD) $(GO_FLAGS) -o $@ ./sshd/client/*.go
 	@cp $@ ./build/$(basename $@)_$(DATE)$(suffix $@)
 
+yfkj_timesync.out   : libswapi.a libair530z.a ./tools/timesync/main.go
+	mkdir -p ./build
+	$(GO) mod tidy
+	$(SETGO_ENV) CGO_LDFLAGS="$(LIB6)" $(GO_BUILD) $(GO_FLAGS) -o $@ ./tools/timesync/main.go
+	@cp $@ ./build/$(basename $@)_$(DATE)$(suffix $@)
+
 # 综合应用测试程序
 LIBS := -Wl,-Bstatic -L./swapi -lswapi -L./hk_takephoto -lhk_takephoto -L./dh_takephoto -ldh_takephoto -L./air720u -lair720u -L./air530z -lair530z -L./ec200u -lec200u
 ifeq ($(target),armv7hf)

+ 1 - 1
netmgrd/netmgrd.go

@@ -96,7 +96,7 @@ func serviceRun() {
 					err = SetSystemTime(httpTime)
 				}
 
-				if err != nil {
+				if httpTime.IsZero() || err != nil {
 					err = SyncNTPTime() //// 同步HTTP时间失败, 则尝试同步标准时间
 				}
 

BIN
package/rtu_linux_modules_1.0.0.1.tar.gz


BIN
package/yfkj_ssh_client_1.0.0.1.tar.gz


+ 42 - 0
tools/timesync/main.go

@@ -0,0 +1,42 @@
+package main
+
+import (
+	"time"
+
+	baseapp "hnyfkj.com.cn/rtu/linux/baseapp"
+	netmgrd "hnyfkj.com.cn/rtu/linux/netmgrd"
+)
+
+const MODULE_NAME = "TimeSync"
+
+func main() {
+	baseapp.InitLogger("")
+
+	for {
+		_, pingOK, tcpOK, _, httpTime := netmgrd.CheckNetwork()
+
+		netOK := tcpOK || pingOK
+		if !netOK {
+			time.Sleep(time.Second)
+			continue
+		}
+
+		var err error
+		if !httpTime.IsZero() { // 优先使用HTTP时间, 实测速度快且精度高
+			err = netmgrd.SetSystemTime(httpTime)
+		}
+
+		if httpTime.IsZero() || err != nil {
+			err = netmgrd.SyncNTPTime() // 同步HTTP时间失败, 尝试NTP同步
+		}
+
+		if err != nil {
+			baseapp.Logger.Errorf("[%s] Error synchronizing system time: %v!!", MODULE_NAME, err)
+			time.Sleep(time.Second)
+			continue
+		}
+
+		baseapp.Logger.Infof("[%s] ✅ System time synchronized successfully: %s", MODULE_NAME, time.Now().Format("2006-01-02 15:04:05"))
+		time.Sleep(time.Hour)
+	}
+}