新增处理
This commit is contained in:
@@ -35,28 +35,22 @@ func (h *TCPHandler) HandleClient(conn net.Conn) {
|
||||
}
|
||||
|
||||
for {
|
||||
//message, err := reader.ReadBytes('\n')
|
||||
//if err != nil {
|
||||
// fmt.Println("客户端已断开连接:", err)
|
||||
// break
|
||||
//}
|
||||
message, err := readUntilDelimiter(reader, []byte("\r\n"))
|
||||
if err != nil {
|
||||
fmt.Println("Error reading message:", err)
|
||||
break
|
||||
}
|
||||
// 去除末尾的换行符
|
||||
message = bytes.TrimSpace(message)
|
||||
|
||||
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
|
||||
//}
|
||||
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 {
|
||||
@@ -73,18 +67,15 @@ func (h *TCPHandler) HandleClient(conn net.Conn) {
|
||||
|
||||
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()
|
||||
@@ -94,11 +85,9 @@ func (h *TCPHandler) HandleClient(conn net.Conn) {
|
||||
fmt.Printf("心跳错误: %v\n", err)
|
||||
continue
|
||||
}
|
||||
// 广播心跳消息
|
||||
broadcastMessage(message)
|
||||
|
||||
case "ota":
|
||||
// 处理 OTA
|
||||
if !client.IsAuth {
|
||||
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
||||
conn.Close()
|
||||
@@ -108,11 +97,9 @@ func (h *TCPHandler) HandleClient(conn net.Conn) {
|
||||
fmt.Printf("OTA 错误: %v\n", err)
|
||||
continue
|
||||
}
|
||||
// 广播 OTA 消息
|
||||
broadcastMessage(message)
|
||||
|
||||
case "start":
|
||||
// 处理 客户端实时上报数据
|
||||
if !client.IsAuth {
|
||||
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
||||
conn.Close()
|
||||
@@ -122,11 +109,9 @@ func (h *TCPHandler) HandleClient(conn net.Conn) {
|
||||
fmt.Printf("OTA 错误: %v\n", err)
|
||||
continue
|
||||
}
|
||||
// 广播 OTA 消息
|
||||
broadcastMessage(message)
|
||||
|
||||
case "stop":
|
||||
// 处理 客户端停止实时上报数据
|
||||
if !client.IsAuth {
|
||||
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
||||
conn.Close()
|
||||
@@ -136,11 +121,9 @@ func (h *TCPHandler) HandleClient(conn net.Conn) {
|
||||
fmt.Printf("客户端停止实时上报数据 错误: %v\n", err)
|
||||
continue
|
||||
}
|
||||
// 广播 OTA 消息
|
||||
broadcastMessage(message)
|
||||
|
||||
case "up":
|
||||
// 处理 客户端定时上报数据
|
||||
if !client.IsAuth {
|
||||
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
||||
conn.Close()
|
||||
@@ -150,17 +133,14 @@ func (h *TCPHandler) HandleClient(conn net.Conn) {
|
||||
fmt.Printf("客户端定时上报数据 错误: %v\n", err)
|
||||
continue
|
||||
}
|
||||
// 广播 OTA 消息
|
||||
broadcastMessage(message)
|
||||
|
||||
default:
|
||||
// 处理其他消息类型
|
||||
if !client.IsAuth {
|
||||
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
// 广播其他类型的消息
|
||||
broadcastMessage(message)
|
||||
}
|
||||
}
|
||||
@@ -209,24 +189,20 @@ func (s *Server) HandleAuth(client *Client, message []byte) error {
|
||||
return fmt.Errorf("设备密码不正确")
|
||||
}
|
||||
|
||||
// 更新版本号
|
||||
model.DriverVer = msg.Ver
|
||||
err = repository.GroupRepositorys.Device.UpdateDevice(model)
|
||||
if err != nil {
|
||||
return fmt.Errorf("更新设备版本号失败")
|
||||
}
|
||||
|
||||
// 认证成功,停止登录超时定时器
|
||||
if client.authTimer != nil {
|
||||
client.authTimer.Stop()
|
||||
client.authTimer = nil
|
||||
}
|
||||
|
||||
// 认证成功
|
||||
client.Imei = msg.Imei
|
||||
client.IsAuth = true
|
||||
|
||||
// 发送响应
|
||||
response := Message{
|
||||
MessageType: MessageType{Type: "reg"},
|
||||
MessageTime: MessageTime{Time: time.Now().Unix()},
|
||||
@@ -248,45 +224,7 @@ func (s *Server) HandleOta(client *Client, message []byte) error {
|
||||
if msg.Type != "ota" {
|
||||
return fmt.Errorf("unauthorized")
|
||||
}
|
||||
|
||||
fmt.Printf("设备升级结果:%s\r\n", msg.State)
|
||||
|
||||
//model, err := repository.GroupRepositorys.Device.GetDevice(map[string]interface{}{"imei": msg.Imei})
|
||||
//if err != nil {
|
||||
// return fmt.Errorf("设备不存在")
|
||||
//}
|
||||
//if msg.Pwd != model.DriverPass {
|
||||
// return fmt.Errorf("设备密码不正确")
|
||||
//}
|
||||
|
||||
// 更新版本号
|
||||
//model.DriverVer = msg.Ver
|
||||
//err = repository.GroupRepositorys.Device.UpdateDevice(model)
|
||||
//if err != nil {
|
||||
// return fmt.Errorf("更新设备版本号失败")
|
||||
//}
|
||||
|
||||
// 认证成功,停止登录超时定时器
|
||||
//if client.authTimer != nil {
|
||||
// client.authTimer.Stop()
|
||||
// client.authTimer = nil
|
||||
//}
|
||||
|
||||
// 认证成功
|
||||
//client.Imei = msg.Imei
|
||||
//client.IsAuth = true
|
||||
|
||||
// 发送响应
|
||||
//response := Message{
|
||||
// MessageType: MessageType{Type: "reg"},
|
||||
// MessageTime: MessageTime{Time: time.Now().Unix()},
|
||||
//}
|
||||
//responseData, err := json.Marshal(response)
|
||||
//if err != nil {
|
||||
// return err
|
||||
//}
|
||||
//
|
||||
//_, err = client.Conn.Write(append(responseData, '\r', '\n'))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user