import: 关闭自动发送指令的功能

This commit is contained in:
fengyarnom 2025-05-28 19:21:03 +08:00
parent 095e5e87f9
commit 6ede040d94

View File

@ -61,19 +61,19 @@ func handleConnection(conn net.Conn) {
clientConns[remoteAddr] = conn clientConns[remoteAddr] = conn
clientsMutex.Unlock() clientsMutex.Unlock()
// 设备刚连接时立即发送一次查询指令 // 注释掉自动发送指令 - 设备刚连接时立即发送一次查询指令
go func() { // go func() {
time.Sleep(2 * time.Second) // 等待2秒让连接稳定 // time.Sleep(2 * time.Second) // 等待2秒让连接稳定
command := "@1602301014A*0!\n" // command := "@1602301014A*0!\n"
if _, err := conn.Write([]byte(command)); err != nil { // if _, err := conn.Write([]byte(command)); err != nil {
Logger.Printf("发送连接后查询指令到客户端 %s 失败: %v", remoteAddr, err) // Logger.Printf("发送连接后查询指令到客户端 %s 失败: %v", remoteAddr, err)
} else { // } else {
TCPDataLogger.Printf("发送连接后查询指令到客户端 %s: %s", remoteAddr, strings.TrimSpace(command)) // TCPDataLogger.Printf("发送连接后查询指令到客户端 %s: %s", remoteAddr, strings.TrimSpace(command))
} // }
}() // }()
// 启动定时发送指令的goroutine // 注释掉自动发送指令 - 启动定时发送指令的goroutine
go sendPeriodicCommand(conn, remoteAddr) // go sendPeriodicCommand(conn, remoteAddr)
buffer := make([]byte, 1024) buffer := make([]byte, 1024)
@ -96,6 +96,41 @@ func handleConnection(conn net.Conn) {
rawData := string(buffer[:n]) rawData := string(buffer[:n])
TCPDataLogger.Printf("从客户端 %s 接收到原始数据: %s", remoteAddr, rawData) TCPDataLogger.Printf("从客户端 %s 接收到原始数据: %s", remoteAddr, rawData)
// 注释掉心跳包响应 - 检查是否为心跳包 JML610
// if strings.Contains(rawData, "JML610") {
// TCPDataLogger.Printf("收到心跳包从客户端 %s: %s", remoteAddr, rawData)
//
// // 立即发送查询指令
// 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))
// }
//
// updateClientLastSeen(remoteAddr)
// continue // 跳过数据解析,继续监听
// }
// 检查是否为心跳包 JML610仅记录不发送查询指令
if strings.Contains(rawData, "JML610") {
TCPDataLogger.Printf("收到心跳包从客户端 %s: %s", remoteAddr, rawData)
updateClientLastSeen(remoteAddr)
// 发送OK响应
resp := "OK\n"
if _, err := conn.Write([]byte(resp)); err != nil {
Logger.Printf("发送响应到客户端 %s 失败: %v", remoteAddr, err)
removeClient(remoteAddr)
// 清理连接映射
clientsMutex.Lock()
delete(clientConns, remoteAddr)
clientsMutex.Unlock()
break
}
continue // 跳过数据解析,继续监听
}
sensorID, x, y, z, temperature, err := parseData(rawData) sensorID, x, y, z, temperature, err := parseData(rawData)
if err == nil { if err == nil {
@ -124,23 +159,23 @@ func handleConnection(conn net.Conn) {
} }
} }
// sendPeriodicCommand 每5分钟发送一次查询指令 // 注释掉自动发送指令 - sendPeriodicCommand 每5分钟发送一次查询指令
func sendPeriodicCommand(conn net.Conn, remoteAddr string) { // func sendPeriodicCommand(conn net.Conn, remoteAddr string) {
ticker := time.NewTicker(5 * time.Minute) // ticker := time.NewTicker(5 * time.Minute)
defer ticker.Stop() // defer ticker.Stop()
//
for { // for {
select { // select {
case <-ticker.C: // case <-ticker.C:
command := "@1602301014A*0!\n" // command := "@1602301014A*0!\n"
if _, err := conn.Write([]byte(command)); err != nil { // if _, err := conn.Write([]byte(command)); err != nil {
Logger.Printf("发送定时指令到客户端 %s 失败: %v", remoteAddr, err) // Logger.Printf("发送定时指令到客户端 %s 失败: %v", remoteAddr, err)
return // 连接断开退出goroutine // return // 连接断开退出goroutine
} // }
TCPDataLogger.Printf("发送定时指令到客户端 %s: %s", remoteAddr, strings.TrimSpace(command)) // TCPDataLogger.Printf("发送定时指令到客户端 %s: %s", remoteAddr, strings.TrimSpace(command))
} // }
} // }
} // }
// parseData 使用正则表达式解析传感器数据,支持新格式 #{1602301014-01,1,1,28.4,-6.884,1.540}! // parseData 使用正则表达式解析传感器数据,支持新格式 #{1602301014-01,1,1,28.4,-6.884,1.540}!
func parseData(data string) (int, float64, float64, float64, float64, error) { func parseData(data string) (int, float64, float64, float64, float64, error) {