client.go 5.9 KB

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