214 lines
5.3 KiB
Go
214 lines
5.3 KiB
Go
package tcpserver
|
|
|
|
import (
|
|
"DT/ws"
|
|
"bufio"
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"strings"
|
|
)
|
|
|
|
type TCPHandler struct {
|
|
Server *Server
|
|
Hub *ws.Hub
|
|
}
|
|
|
|
func (h *TCPHandler) HandleClient(conn net.Conn) {
|
|
reader := bufio.NewReader(conn)
|
|
clientID := conn.RemoteAddr().String()
|
|
var client *Client
|
|
if value, ok := h.Server.GetClient(clientID); ok {
|
|
client = value
|
|
} else {
|
|
fmt.Println("找不到客户端:", clientID)
|
|
return
|
|
}
|
|
|
|
broadcastMessage := func(data []byte) {
|
|
if h.Hub != nil {
|
|
h.Hub.Broadcast <- &ws.WsMessage{IMEI: client.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(client, message); err != nil {
|
|
fmt.Printf("客户端授权失败: %v\n", err)
|
|
conn.Close()
|
|
return
|
|
}
|
|
fmt.Printf("客户端已授权: %s\n", client.Imei)
|
|
broadcastMessage(message)
|
|
|
|
case "ping":
|
|
if !client.IsAuth {
|
|
fmt.Printf("来自未授权客户端的心跳 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
if err := h.Server.HandleHeartbeat(client, message); err != nil {
|
|
fmt.Printf("心跳错误: %v\n", err)
|
|
continue
|
|
}
|
|
broadcastMessage(message)
|
|
|
|
case "ota":
|
|
if !client.IsAuth {
|
|
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
if err := h.Server.HandleOta(client, message); err != nil {
|
|
fmt.Printf("OTA 错误: %v\n", err)
|
|
continue
|
|
}
|
|
broadcastMessage(message)
|
|
|
|
case "start":
|
|
if !client.IsAuth {
|
|
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
if err := h.Server.RealTimeReporting(client, message); err != nil {
|
|
fmt.Printf("实时上报错误: %v\n", err)
|
|
continue
|
|
}
|
|
broadcastMessage(message)
|
|
|
|
case "stop":
|
|
if !client.IsAuth {
|
|
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
if err := h.Server.StopRealTimeReporting(client, message); err != nil {
|
|
fmt.Printf("客户端停止实时上报数据 错误: %v\n", err)
|
|
continue
|
|
}
|
|
broadcastMessage(message)
|
|
|
|
case "up":
|
|
if !client.IsAuth {
|
|
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
if err := h.Server.TimingReporting(client, message); err != nil {
|
|
fmt.Printf("客户端定时上报数据 错误: %v\n", err)
|
|
continue
|
|
}
|
|
broadcastMessage(message)
|
|
case "SetConfig":
|
|
if !client.IsAuth {
|
|
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
if err := h.Server.SetConfig(client, message); err != nil {
|
|
fmt.Printf("客户端楼层设置 错误: %v\n", err)
|
|
continue
|
|
}
|
|
broadcastMessage(message)
|
|
|
|
case "GetConfig":
|
|
if !client.IsAuth {
|
|
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
if err := h.Server.GetConfig(client, message); err != nil {
|
|
fmt.Printf("获取客户端楼层设置错误: %v\n", err)
|
|
continue
|
|
}
|
|
broadcastMessage(message)
|
|
|
|
case "mp3a", "mp3b", "mp3c":
|
|
if !client.IsAuth {
|
|
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
if err := h.Server.Mp3(client, message); err != nil {
|
|
fmt.Printf("客户端设置语音内容错误: %v\n", err)
|
|
continue
|
|
}
|
|
broadcastMessage(message)
|
|
case "SetVoiceConf":
|
|
if !client.IsAuth {
|
|
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
if err := h.Server.SetVoiceConf(client, message); err != nil {
|
|
fmt.Printf("客户端语音配置错误: %v\n", err)
|
|
continue
|
|
}
|
|
broadcastMessage(message)
|
|
case "GetVoiceConf":
|
|
if !client.IsAuth {
|
|
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
if err := h.Server.GetVoiceConf(client, message); err != nil {
|
|
fmt.Printf("获取客户端语音配置错误: %v\n", err)
|
|
continue
|
|
}
|
|
broadcastMessage(message)
|
|
case "db":
|
|
if !client.IsAuth {
|
|
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
if err := h.Server.Db(client, message); err != nil {
|
|
fmt.Printf("获取客户端音量分贝错误: %v\n", err)
|
|
continue
|
|
}
|
|
broadcastMessage(message)
|
|
default:
|
|
if !client.IsAuth {
|
|
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
|
conn.Close()
|
|
return
|
|
}
|
|
broadcastMessage(message)
|
|
}
|
|
}
|
|
}
|