feat:连接后自动发送指令

This commit is contained in:
fengyarnom 2025-05-28 17:00:03 +08:00
parent 8ff575c9b5
commit 095e5e87f9

View File

@ -61,6 +61,17 @@ func handleConnection(conn net.Conn) {
clientConns[remoteAddr] = conn clientConns[remoteAddr] = conn
clientsMutex.Unlock() clientsMutex.Unlock()
// 设备刚连接时立即发送一次查询指令
go func() {
time.Sleep(2 * time.Second) // 等待2秒让连接稳定
command := "@1602301014A*0!\n"
if _, err := conn.Write([]byte(command)); err != nil {
Logger.Printf("发送连接后查询指令到客户端 %s 失败: %v", remoteAddr, err)
} else {
TCPDataLogger.Printf("发送连接后查询指令到客户端 %s: %s", remoteAddr, strings.TrimSpace(command))
}
}()
// 启动定时发送指令的goroutine // 启动定时发送指令的goroutine
go sendPeriodicCommand(conn, remoteAddr) go sendPeriodicCommand(conn, remoteAddr)
@ -113,9 +124,9 @@ func handleConnection(conn net.Conn) {
} }
} }
// sendPeriodicCommand 每30分钟发送一次查询指令 // sendPeriodicCommand 每5分钟发送一次查询指令
func sendPeriodicCommand(conn net.Conn, remoteAddr string) { func sendPeriodicCommand(conn net.Conn, remoteAddr string) {
ticker := time.NewTicker(30 * time.Minute) ticker := time.NewTicker(5 * time.Minute)
defer ticker.Stop() defer ticker.Stop()
for { for {