220 lines
5.6 KiB
Go
220 lines
5.6 KiB
Go
package tcpserver
|
|
|
|
import (
|
|
"DT/ws"
|
|
"bufio"
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type TCPHandler struct {
|
|
Server *Server
|
|
Hub *ws.Hub
|
|
}
|
|
|
|
func (h *TCPHandler) HandleClient(conn net.Conn) {
|
|
reader := bufio.NewReader(conn)
|
|
clientID := conn.RemoteAddr().String()
|
|
|
|
// 创建临时客户端对象
|
|
tempClient := &Client{
|
|
ID: clientID,
|
|
Conn: conn,
|
|
ConnectedAt: time.Now(),
|
|
LastPing: time.Now(),
|
|
Done: make(chan struct{}),
|
|
}
|
|
|
|
broadcastMessage := func(data []byte) {
|
|
if h.Hub != nil && tempClient.IsAuth {
|
|
h.Hub.Broadcast <- &ws.WsMessage{IMEI: tempClient.Imei, Data: string(data)}
|
|
}
|
|
}
|
|
|
|
for {
|
|
message, err := readUntilDelimiter(reader, []byte("\r\n"))
|
|
if err != nil {
|
|
fmt.Println("Error reading message:", err)
|
|
break
|
|
}
|
|
|
|
message = bytes.TrimSpace(message)
|
|
output := strings.ReplaceAll(string(message), "\t", "")
|
|
output = strings.ReplaceAll(output, "\n", "")
|
|
message = []byte(output)
|
|
fmt.Printf("收到消息来自 %s: %s\n", conn.RemoteAddr(), message)
|
|
|
|
if !json.Valid(message) {
|
|
fmt.Printf("来自客户端的数据非法 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
|
|
var fullMsg map[string]interface{}
|
|
if err := json.Unmarshal(message, &fullMsg); err != nil {
|
|
fmt.Printf("Error parsing message: %v\n", err)
|
|
continue
|
|
}
|
|
|
|
msgType, _ := fullMsg["Type"].(string)
|
|
if msgType == "" {
|
|
fmt.Printf("接收到的消息类型为空 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
|
|
switch msgType {
|
|
case "reg":
|
|
// 处理注册
|
|
if err := h.Server.HandleAuth(tempClient, message); err != nil {
|
|
fmt.Printf("客户端授权失败: %v\n", err)
|
|
conn.Close()
|
|
return
|
|
}
|
|
// 注册成功后,将客户端添加到 clients map
|
|
h.Server.addClient(tempClient)
|
|
fmt.Printf("客户端已授权并注册: %s\n", tempClient.Imei)
|
|
broadcastMessage(message)
|
|
|
|
case "ping":
|
|
if !tempClient.IsAuth {
|
|
fmt.Printf("来自未授权客户端的心跳 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
if err := h.Server.HandleHeartbeat(tempClient, message); err != nil {
|
|
fmt.Printf("心跳错误: %v\n", err)
|
|
continue
|
|
}
|
|
broadcastMessage(message)
|
|
|
|
case "ota":
|
|
if !tempClient.IsAuth {
|
|
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
if err := h.Server.HandleOta(tempClient, message); err != nil {
|
|
fmt.Printf("OTA 错误: %v\n", err)
|
|
continue
|
|
}
|
|
broadcastMessage(message)
|
|
|
|
case "start":
|
|
if !tempClient.IsAuth {
|
|
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
if err := h.Server.RealTimeReporting(tempClient, message); err != nil {
|
|
fmt.Printf("实时上报错误: %v\n", err)
|
|
continue
|
|
}
|
|
broadcastMessage(message)
|
|
|
|
case "stop":
|
|
if !tempClient.IsAuth {
|
|
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
if err := h.Server.StopRealTimeReporting(tempClient, message); err != nil {
|
|
fmt.Printf("客户端停止实时上报数据 错误: %v\n", err)
|
|
continue
|
|
}
|
|
broadcastMessage(message)
|
|
|
|
case "up":
|
|
if !tempClient.IsAuth {
|
|
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
if err := h.Server.TimingReporting(tempClient, message); err != nil {
|
|
fmt.Printf("客户端定时上报数据 错误: %v\n", err)
|
|
continue
|
|
}
|
|
broadcastMessage(message)
|
|
case "SetConfig":
|
|
if !tempClient.IsAuth {
|
|
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
if err := h.Server.SetConfig(tempClient, message); err != nil {
|
|
fmt.Printf("客户端楼层设置 错误: %v\n", err)
|
|
continue
|
|
}
|
|
broadcastMessage(message)
|
|
|
|
case "GetConfig":
|
|
if !tempClient.IsAuth {
|
|
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
if err := h.Server.GetConfig(tempClient, message); err != nil {
|
|
fmt.Printf("获取客户端楼层设置错误: %v\n", err)
|
|
continue
|
|
}
|
|
broadcastMessage(message)
|
|
|
|
case "mp3a", "mp3b", "mp3c":
|
|
if !tempClient.IsAuth {
|
|
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
if err := h.Server.Mp3(tempClient, message); err != nil {
|
|
fmt.Printf("客户端设置语音内容错误: %v\n", err)
|
|
continue
|
|
}
|
|
broadcastMessage(message)
|
|
case "SetVoiceConf":
|
|
if !tempClient.IsAuth {
|
|
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
if err := h.Server.SetVoiceConf(tempClient, message); err != nil {
|
|
fmt.Printf("客户端语音配置错误: %v\n", err)
|
|
continue
|
|
}
|
|
broadcastMessage(message)
|
|
case "GetVoiceConf":
|
|
if !tempClient.IsAuth {
|
|
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
if err := h.Server.GetVoiceConf(tempClient, message); err != nil {
|
|
fmt.Printf("获取客户端语音配置错误: %v\n", err)
|
|
continue
|
|
}
|
|
broadcastMessage(message)
|
|
case "db":
|
|
if !tempClient.IsAuth {
|
|
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
if err := h.Server.Db(tempClient, message); err != nil {
|
|
fmt.Printf("获取客户端音量分贝错误: %v\n", err)
|
|
continue
|
|
}
|
|
broadcastMessage(message)
|
|
default:
|
|
if !tempClient.IsAuth {
|
|
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
broadcastMessage(message)
|
|
}
|
|
}
|
|
}
|