123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- 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()
- logs.Println("============TcpServer==============")
- logs.Println("TCPServer_Port:" + fmt.Sprintf(":%d", conf.TCPServer_Port))
- server := TcpServerLib.NewServer(fmt.Sprintf(":%d", conf.TCPServer_Port))
- server.OnNewClient(OnNewClient)
- server.OnClientConnectionClosed(OnClientConnectionClosed)
- server.OnNewMessage(OnNewMessage)
- go server.Listen()
- go Polling()
- }
- func Polling() {
- for true {
- for i := SessionS.Front(); i != nil; i = i.Next() {
- t := i.Value.(*TcpServerLib.Session)
- if len(t.Device_Sn) == 0 {
- t.Verify_num += 1
- if t.Verify_num > 10 {
- t.Close()
- SessionS.Remove(i)
- logs.Println("Polling 删除", t.Conn().LocalAddr().String())
- }
- }
- }
- time.Sleep(time.Second * 1)
- }
- }
- func OnNewClient(c *TcpServerLib.Session) {
- logs.Println("OnNewClient")
- SessionS.PushBack(c)
- //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
- }
- return Reason
- }
- 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))
- }
|