client.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package server
  2. import (
  3. "dlt645-server/protocol"
  4. "github.com/gogf/gf/net/gtcp"
  5. "github.com/gogf/gf/os/glog"
  6. "io"
  7. "net"
  8. "strings"
  9. "syscall"
  10. "time"
  11. )
  12. type Client struct {
  13. Id string
  14. Address []byte
  15. srv *Server
  16. conn *gtcp.Conn
  17. sendChan chan []byte
  18. closeChan chan struct{}
  19. closeHandler func(id string, c *Client)
  20. lastHeartBeat time.Time
  21. gatewayId uint16
  22. isReg bool
  23. }
  24. func NewClient(s *Server, conn *gtcp.Conn) *Client {
  25. return &Client{
  26. srv: s,
  27. conn: conn,
  28. sendChan: make(chan []byte),
  29. closeChan: make(chan struct{}),
  30. }
  31. }
  32. func (c *Client) SendLoop(ctx *protocol.PacketContext, data *protocol.Data) {
  33. defer c.srv.grWG.Done()
  34. for {
  35. select {
  36. case buf := <-c.sendChan:
  37. err := c.send(buf)
  38. if err != nil {
  39. if err != nil {
  40. glog.Errorf("指令发送失败:%s", err.Error())
  41. }
  42. continue
  43. }
  44. timer := time.NewTimer(5 * time.Second)
  45. for {
  46. select {
  47. case <-timer.C:
  48. glog.Errorf("读取超时:%s", err.Error())
  49. continue
  50. default:
  51. receiveBuf, err := c.conn.Recv(-1)
  52. if err != nil {
  53. c.readError(err)
  54. continue
  55. }
  56. if len(buf) > 0 {
  57. result, err := c.srv.message.Decode(ctx, receiveBuf, data)
  58. if err != nil {
  59. glog.Errorf("解析报文失败:%s", err.Error())
  60. }
  61. if !c.isReg {
  62. c.SetId(ctx.GetId())
  63. c.isReg = true
  64. continue
  65. }
  66. var reportData interface{}
  67. switch result.DataType {
  68. case protocol.IsPower:
  69. reportData = protocol.PowerData{
  70. ActivePower: result.ActivePower,
  71. }
  72. case protocol.IsVData:
  73. reportData = protocol.VIData{
  74. AV: result.AV,
  75. BV: result.BV,
  76. CV: result.CV,
  77. }
  78. case protocol.IsIData:
  79. reportData = protocol.IData{
  80. AI2: result.AI,
  81. BI2: result.BI,
  82. CI2: result.CI,
  83. }
  84. default:
  85. continue
  86. }
  87. if err := c.srv.ReportStatus(c.Id, reportData); err != nil {
  88. glog.Errorf("数据上报发送错误:%s", err.Error())
  89. continue
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. func (c *Client) SetId(id string) {
  98. c.Id = id
  99. }
  100. func (c *Client) SetAddress(address []byte) {
  101. c.Address = address
  102. }
  103. func (c *Client) readError(err error) {
  104. defer c.closeConnection()
  105. if err == io.EOF || isErrConnReset(err) {
  106. return
  107. }
  108. glog.Errorf("读取数据发生错误:%s", err.Error())
  109. }
  110. func (c *Client) closeConnection() {
  111. _ = c.conn.Close()
  112. c.conn = nil
  113. close(c.closeChan)
  114. c.SetId("")
  115. c.isReg = false
  116. if c.closeHandler != nil {
  117. c.closeHandler(c.Id, c)
  118. }
  119. }
  120. // isErrConnReset read: connection reset by peer
  121. func isErrConnReset(err error) bool {
  122. if ne, ok := err.(*net.OpError); ok {
  123. return strings.Contains(ne.Err.Error(), syscall.ECONNRESET.Error())
  124. }
  125. return false
  126. }
  127. func (c *Client) send(buf []byte) error {
  128. if c.conn == nil {
  129. return nil
  130. }
  131. err := c.conn.Send(buf)
  132. if err != nil {
  133. glog.Error(err)
  134. c.closeConnection()
  135. return err
  136. }
  137. glog.Debugf("指令发送成功:%2X", buf)
  138. return nil
  139. }
  140. func (c *Client) GetActivePower(ctx *protocol.PacketContext) {
  141. defer c.srv.grWG.Done()
  142. for {
  143. entity := protocol.Dlt_0x33333433{}
  144. sendByte, _ := entity.Encode(ctx)
  145. c.sendChan <- sendByte
  146. time.Sleep(10 * time.Second)
  147. }
  148. }
  149. func (c *Client) GetAV(ctx *protocol.PacketContext) {
  150. defer c.srv.grWG.Done()
  151. for {
  152. entity := protocol.Dlt_0x33343435{}
  153. sendByte, _ := entity.Encode(ctx)
  154. c.sendChan <- sendByte
  155. time.Sleep(10 * time.Second)
  156. }
  157. }
  158. func (c *Client) GetAI(ctx *protocol.PacketContext) {
  159. defer c.srv.grWG.Done()
  160. for {
  161. entity := protocol.Dlt_0x33343535{}
  162. sendByte, _ := entity.Encode(ctx)
  163. c.sendChan <- sendByte
  164. time.Sleep(10 * time.Second)
  165. }
  166. }
  167. func (c *Client) GetBV(ctx *protocol.PacketContext, bvChan, biChan chan struct{}) {
  168. defer c.srv.grWG.Done()
  169. for {
  170. entity := protocol.Dlt_0x33353435{}
  171. sendByte, _ := entity.Encode(ctx)
  172. c.sendChan <- sendByte
  173. time.Sleep(10 * time.Second)
  174. }
  175. }
  176. func (c *Client) GetBI(ctx *protocol.PacketContext, biChan, cvChan chan struct{}) {
  177. defer c.srv.grWG.Done()
  178. for {
  179. entity := protocol.Dlt_0x33353535{}
  180. sendByte, _ := entity.Encode(ctx)
  181. c.sendChan <- sendByte
  182. time.Sleep(10 * time.Second)
  183. }
  184. }
  185. func (c *Client) GetCV(ctx *protocol.PacketContext, cvChan, ciChan chan struct{}) {
  186. defer c.srv.grWG.Done()
  187. for {
  188. <-cvChan
  189. entity := protocol.Dlt_0x33363435{}
  190. sendByte, _ := entity.Encode(ctx)
  191. c.sendChan <- sendByte
  192. time.Sleep(10 * time.Second)
  193. }
  194. }
  195. func (c *Client) GetCI(ctx *protocol.PacketContext, ciChan, powerChan chan struct{}) {
  196. defer c.srv.grWG.Done()
  197. for {
  198. <-ciChan
  199. entity := protocol.Dlt_0x33363535{}
  200. sendByte, _ := entity.Encode(ctx)
  201. c.sendChan <- sendByte
  202. time.Sleep(10 * time.Second)
  203. }
  204. }
  205. func (c *Client) SendGetAddress(ctx *protocol.PacketContext) []byte {
  206. entity := new(protocol.Dlt_0x93)
  207. sendBuf, _ := entity.Encode(ctx)
  208. return sendBuf
  209. }