From 6dea379dc74a941a83c2e5582963c0e58e6ca5bf Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@smartai.com>
Date: 星期五, 23 八月 2024 18:32:10 +0800
Subject: [PATCH] 修改楼层解析, 添加老版本兼容

---
 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