123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- package MqttServer
- import (
- "Yunlot/Handle"
- "Yunlot/logs"
- "Yunlot/models/Device"
- "encoding/json"
- "time"
- )
- // / - 连接 实体
- type Ms_Project_Connected struct {
- Clientid string `json:"clientid"`
- Username string `json:"username"`
- Ipaddress string `json:"ipaddress"`
- Reason string `json:"reason"` // sha ccv
- }
- // / - 离线断开 实体
- type Ms_Project_Disconnected struct {
- Clientid string `json:"clientid"`
- Username string `json:"username"`
- Ipaddress string `json:"ipaddress"`
- Reason string `json:"reason"`
- //终端连接断开原因:
- //normal:客户端主动断开
- //kicked:服务端踢出,通过 REST API
- //keepalive_timeout: keepalive 超时
- //not_authorized: 认证失败,或者 acl_nomatch = disconnect 时没有权限的 Pub/Sub 会主动断开客户端
- //tcp_closed: 对端关闭了网络连接
- //internal_error: 畸形报文或其他未知错误
- }
- // 设备连接
- func MessageConnected(topicName string, message []byte) {
- logs.Println("============= MessageConnected Mqtt JSON =============")
- logs.Println("topic:", topicName, " message:", string(message))
- //logs.Println("=>", a)
- var Ms_project Ms_Project_Connected
- err := json.Unmarshal(message, &Ms_project)
- if err != nil {
- logs.PrintlnError("MqttServer", "JSON反序列化失败[Ms_Project]", string(message))
- return
- }
- var r_Device Device.Device
- r_Device.T_sn = Ms_project.Clientid
- r_Device.Read()
- if err != nil {
- logs.Println("MessageDisconnected 没有该设备:", Ms_project.Clientid)
- return
- }
- logs.Println("Clientid:", Ms_project.Clientid)
- logs.Println("Username:", Ms_project.Username)
- r_Device.T_online = 1
- // 同步参数
- r_Device.Update("T_online")
- var Device_online_r map[string]interface{}
- Device_online_r = make(map[string]interface{})
- Device_online_r["online"] = r_Device.T_online
- Device_online_r["msg"] = "设备主动上线[" + Ms_project.Ipaddress + "]"
- Device_online_r["time"] = time.Now().Format("2006-01-02 15:04:05")
- Handle.AnalysisMap(&r_Device, map[string]interface{}{"online": Device_online_r}, "")
- logs.Println("============= MessageConnected Mqtt JSON AND =============", topicName)
- }
- func ToReason(str string) (Reason string) {
- switch str {
- case "normal":
- Reason = "客户端主动断开"
- break
- case "kicked":
- Reason = "设备异常掉线,服务端踢出,通过 REST API"
- break
- case "keepalive_timeout":
- Reason = "设备异常掉线,keepalive 超时"
- break
- case "not_authorized":
- Reason = "设备异常掉线,认证失败"
- break
- case "tcp_closed":
- Reason = "设备异常掉线,对端关闭了网络连接"
- break
- case "internal_error":
- Reason = "设备异常掉线,畸形报文或其他未知错误"
- break
- }
- return Reason
- }
- // 设备断开
- func MessageDisconnected(topicName string, message []byte) {
- logs.Println("============= MessageDisconnected Mqtt JSON =============")
- logs.Println("topic:", topicName, " message:", string(message))
- //logs.Println("=>", a)
- var Ms_project Ms_Project_Disconnected
- err := json.Unmarshal(message, &Ms_project)
- if err != nil {
- logs.PrintlnError("MqttServer", "JSON反序列化失败[Ms_Project]", string(message))
- return
- }
- logs.Println("Clientid:", Ms_project.Clientid)
- logs.Println("Username:", Ms_project.Username)
- var r_Device Device.Device
- r_Device.T_sn = Ms_project.Clientid
- r_Device.Read()
- if err != nil {
- logs.Println("MessageDisconnected 没有该设备:", Ms_project.Clientid)
- return
- }
- r_Device.T_online = 2
- // 同步参数
- r_Device.Update("T_online")
- var Device_online_r map[string]interface{}
- Device_online_r = make(map[string]interface{})
- Device_online_r["online"] = r_Device.T_online
- Device_online_r["msg"] = "设备离线[" + ToReason(Ms_project.Reason) + "]"
- Device_online_r["time"] = time.Now().Format("2006-01-02 15:04:05")
- Handle.AnalysisMap(&r_Device, map[string]interface{}{"online": Device_online_r}, "")
- logs.Println("============= MessageDisconnected Mqtt JSON AND =============", topicName)
- }
|