|
@@ -1,20 +1,30 @@
|
|
|
package TcpServer
|
|
|
|
|
|
import (
|
|
|
+ "Yunlot/Handle"
|
|
|
+ "Yunlot/conf"
|
|
|
+ "Yunlot/lib"
|
|
|
+ "Yunlot/lib/TcpServerLib"
|
|
|
+ "Yunlot/logs"
|
|
|
+ "Yunlot/models/Device"
|
|
|
"container/list"
|
|
|
+ "encoding/json"
|
|
|
"fmt"
|
|
|
+ "net"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
-
|
|
|
var SessionS list.List
|
|
|
+
|
|
|
func TcpServer() {
|
|
|
//SessionS = list.New()
|
|
|
|
|
|
- fmt.Println("TcpServer")
|
|
|
-
|
|
|
+ logs.Println("============TcpServer==============")
|
|
|
+ logs.Println("TCPServer_Port:" + fmt.Sprintf(":%d", conf.TCPServer_Port))
|
|
|
|
|
|
- server := NewServer(":1452")
|
|
|
+ server := TcpServerLib.NewServer(fmt.Sprintf(":%d", conf.TCPServer_Port))
|
|
|
|
|
|
server.OnNewClient(OnNewClient)
|
|
|
server.OnClientConnectionClosed(OnClientConnectionClosed)
|
|
@@ -24,39 +34,133 @@ func TcpServer() {
|
|
|
go Polling()
|
|
|
}
|
|
|
|
|
|
-func Polling() {
|
|
|
+func Polling() {
|
|
|
|
|
|
- for true{
|
|
|
+ for true {
|
|
|
for i := SessionS.Front(); i != nil; i = i.Next() {
|
|
|
- t := i.Value.(*Session)
|
|
|
+ t := i.Value.(*TcpServerLib.Session)
|
|
|
|
|
|
- if t.Device.T_sn == "" {
|
|
|
+ if len(t.Device_Sn) == 0 {
|
|
|
t.Verify_num += 1
|
|
|
if t.Verify_num > 10 {
|
|
|
t.Close()
|
|
|
SessionS.Remove(i)
|
|
|
- fmt.Println("Polling 删除")
|
|
|
+ logs.Println("Polling 删除", t.Conn().LocalAddr().String())
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
time.Sleep(time.Second * 1)
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
-func OnNewClient(c *Session) {
|
|
|
- fmt.Println("OnNewClient")
|
|
|
+func OnNewClient(c *TcpServerLib.Session) {
|
|
|
+ logs.Println("OnNewClient")
|
|
|
SessionS.PushBack(c)
|
|
|
- c.Send("ok!")
|
|
|
+ //c.Send(string([]byte{0xff,0x00,0xff}))
|
|
|
+ //c.Send("ok")
|
|
|
+}
|
|
|
+
|
|
|
+func OnClientConnectionClosed(c *TcpServerLib.Session, closeCase int) {
|
|
|
+ logs.Println("OnClientConnectionClosed:", closeCase)
|
|
|
+ // 离线
|
|
|
+ if len(c.Device_Sn) != 0 {
|
|
|
+ if _, is := lib.TcpMap[c.Device_Sn]; !is {
|
|
|
+ delete(lib.TcpMap, c.Device_Sn)
|
|
|
+ }
|
|
|
+
|
|
|
+ r_Device := Device.Device{T_sn: c.Device_Sn}
|
|
|
+ if !r_Device.Read_Tidy() {
|
|
|
+ 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(closeCase) + "]"
|
|
|
+ Device_online_r["time"] = time.Now().Format("2006-01-02 15:04:05")
|
|
|
+
|
|
|
+ Handle.AnalysisMap(&r_Device, map[string]interface{}{"online": Device_online_r}, "")
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
+func ToReason(str int) (Reason string) {
|
|
|
+ switch str {
|
|
|
+ case 1:
|
|
|
+ Reason = "客户端主动断开"
|
|
|
+ break
|
|
|
+ default:
|
|
|
+ Reason = "客户端异常断开[" + strconv.Itoa(str) + "]"
|
|
|
+ break
|
|
|
+ }
|
|
|
|
|
|
-func OnClientConnectionClosed(c *Session, closeCase int) {
|
|
|
- fmt.Println("OnClientConnectionClosed:",closeCase)
|
|
|
+ return Reason
|
|
|
}
|
|
|
|
|
|
-func OnNewMessage(c *Session, packetData []byte) {
|
|
|
- fmt.Println("OnNewMessage:",packetData)
|
|
|
+func OnNewMessage(c *TcpServerLib.Session, packetData []byte) {
|
|
|
+ if len(c.Device_Sn) == 0 {
|
|
|
+
|
|
|
+ packetData_l := strings.Split(string(packetData), "&")
|
|
|
+ if len(packetData_l) != 2 {
|
|
|
+ logs.Println("登录验证 长度失败:", packetData_l, len(packetData_l))
|
|
|
+ c.Send("e")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ Devicer := Device.Device{T_sn: packetData_l[0]}
|
|
|
+ if !Devicer.Read_Tidy() {
|
|
|
+ logs.Println("登录验证 SN失败:", packetData_l, len(packetData_l))
|
|
|
+ c.Send("e")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if Devicer.T_password != packetData_l[1] {
|
|
|
+ logs.Println("登录验证 秘钥失败:", packetData_l, len(packetData_l))
|
|
|
+ c.Send("e")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ c.Send("ok")
|
|
|
+ c.Device_Sn = Devicer.T_sn
|
|
|
+
|
|
|
+ lib.TcpMap[Devicer.T_sn] = c // 更新 MAP
|
|
|
+
|
|
|
+ // 更新状态
|
|
|
+
|
|
|
+ Devicer.T_online = 1
|
|
|
+ // 同步参数
|
|
|
+ Devicer.Update("T_online")
|
|
|
+
|
|
|
+ // 获取客户端IP地址
|
|
|
+ clientIP, _, _ := net.SplitHostPort(c.Conn().RemoteAddr().String())
|
|
|
+ fmt.Println("Client IP:", clientIP)
|
|
|
+
|
|
|
+ var Device_online_r map[string]interface{}
|
|
|
+ Device_online_r = make(map[string]interface{})
|
|
|
+ Device_online_r["online"] = Devicer.T_online
|
|
|
+ Device_online_r["msg"] = "设备主动上线[" + clientIP + "]"
|
|
|
+ Device_online_r["time"] = time.Now().Format("2006-01-02 15:04:05")
|
|
|
+
|
|
|
+ Handle.AnalysisMap(&Devicer, map[string]interface{}{"online": Device_online_r}, "")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ Device_r := Device.Device{T_sn: c.Device_Sn}
|
|
|
+ if !Device_r.Read_Tidy() {
|
|
|
+ c.Send("e")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ logs.Println("OnNewMessage:", string(packetData))
|
|
|
+
|
|
|
+ Rt_r := Handle.PullHandle(&Device_r, c.Device_Sn, packetData)
|
|
|
+
|
|
|
+ // 返回
|
|
|
+ data, _ := json.Marshal(Rt_r)
|
|
|
+
|
|
|
+ Handle.PushHandle(&Device_r, c.Device_Sn, string(data))
|
|
|
|
|
|
}
|