From df794921a35a231eba9ee61eafb68d5c1cbc7043 Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@smartai.com>
Date: 星期四, 13 六月 2024 18:35:57 +0800
Subject: [PATCH] 完善A2梯控的电梯运行状态显示
---
client/client.go | 67 ++++++++++++++++++++++++++++++++-
1 files changed, 65 insertions(+), 2 deletions(-)
diff --git a/client/client.go b/client/client.go
index d1004d8..80f7280 100644
--- a/client/client.go
+++ b/client/client.go
@@ -1,15 +1,78 @@
package client
import (
+ "context"
+ "fmt"
+ "math/rand"
+ "time"
+
"gat1400Exchange/config"
"gat1400Exchange/pkg/logger"
+ "gat1400Exchange/vo"
)
-func Init1400Client() {
+func Init1400Client(ctx context.Context) {
if !config.ClientConf.Enable {
logger.Debug("GAT/1400 Client disabled")
return
}
- register()
+ go registerLoop(ctx)
+ go keepaliveLoop(ctx)
+ go syncTimeLoop(ctx)
+}
+
+func registerLoop(ctx context.Context) {
+ ticker := time.NewTicker(1 * time.Second)
+ for {
+ select {
+ case <-ctx.Done():
+ unRegister()
+ return
+ case <-ticker.C:
+ if clientStatus != vo.StatusSuccess {
+ fmt.Println("register")
+ if !register() {
+ randNum := rand.Intn(300) + 1
+ // 闅忔満绛夊緟300s, 閲嶆柊娉ㄥ唽
+ ticker.Reset(time.Duration(randNum) * time.Second)
+ } else {
+ ticker.Reset(1 * time.Second)
+ }
+ }
+ }
+ }
+}
+
+func keepaliveLoop(ctx context.Context) {
+ var failCount int
+ ticker := time.NewTicker(time.Duration(config.ClientConf.HeartbeatInterval) * time.Second)
+ for {
+ select {
+ case <-ctx.Done():
+ return
+ case <-ticker.C:
+ if status := keepalive(); status != vo.StatusSuccess {
+ failCount++
+
+ if failCount > config.ClientConf.HeartbeatFailCount {
+ clientStatus = status
+ }
+ } else {
+ failCount = 0
+ }
+ }
+ }
+}
+
+func syncTimeLoop(ctx context.Context) {
+ ticker := time.NewTicker(time.Duration(config.ClientConf.HeartbeatInterval*10) * time.Second)
+ for {
+ select {
+ case <-ctx.Done():
+ return
+ case <-ticker.C:
+ syncTime()
+ }
+ }
}
--
Gitblit v1.8.0