123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- package server
- import (
- "bth-rs30-gateway/protocol"
- "errors"
- "fmt"
- "github.com/gogf/gf/encoding/gbinary"
- "github.com/gogf/gf/frame/g"
- "github.com/gogf/gf/net/gtcp"
- "github.com/gogf/gf/os/glog"
- "io"
- "net"
- "strings"
- "syscall"
- "time"
- )
- type Client struct {
- Id string
- srv *Server
- conn *gtcp.Conn
- sendChan chan []byte
- closeChan chan struct{}
- closeHandler func(id string, c *Client)
- isReg bool
- }
- func NewClient(s *Server, conn *gtcp.Conn) *Client {
- return &Client{
- srv: s,
- conn: conn,
- sendChan: make(chan []byte),
- closeChan: make(chan struct{}),
- }
- }
- func (c *Client) SetId(id string) {
- c.Id = id
- }
- func (c *Client) SendLoop() {
- for {
- select {
- case buf := <-c.sendChan:
- err := c.send(buf)
- if err != nil {
- glog.Errorf("指令发送失败:%s", err.Error())
- continue
- }
- timer := time.NewTimer(5 * time.Second)
- for {
- timer.Reset(5 * time.Second)
- select {
- case <-timer.C:
- glog.Error("接收指令超时")
- break
- default:
- if !c.isReg {
- id := gbinary.DecodeToString(buf)
- glog.Debugf("收到注册包!id:%s", id)
- c.SetId(id)
- c.isReg = true
- break
- }
- receiveBuf, err := c.conn.Recv(-1)
- if err != nil {
- c.readError(err)
- break
- }
- glog.Debugf("收到数据:%2X", receiveBuf)
- if err := c.decodeAndReport(receiveBuf); err != nil {
- glog.Debugf("处理数据失败:%s", err.Error())
- break
- }
- }
- break
- }
- }
- }
- }
- // 收到数据:01 03 04 00 FD 00 8A EA 64
- func (c *Client) decodeAndReport(buf []byte) error {
- length := len(buf)
- var crc crc
- crc.reset().pushBytes(buf[0 : length-2])
- checksum := uint16(buf[length-1])<<8 | uint16(buf[length-2])
- if checksum != crc.value() {
- return errors.New(fmt.Sprintf("modbus: response crc '%v' does not match expected '%v'", checksum, crc.value()))
- }
- if buf[1] == 0x03 && buf[2] == 0x06 {
- data := &protocol.Data{}
- data.Temperature = float32(gbinary.BeDecodeToUint16(buf[3:5])) * 0.1
- data.Humidly = float32(gbinary.BeDecodeToUint16(buf[5:7])) * 0.1
- if err := c.srv.ReportStatus(c.Id, data); err != nil {
- return err
- }
- }
- return nil
- }
- func (c *Client) readError(err error) {
- defer c.closeConnection()
- if err == io.EOF || isErrConnReset(err) {
- return
- }
- glog.Errorf("读取数据发生错误:%s", err.Error())
- }
- func (c *Client) closeConnection() {
- _ = c.conn.Close()
- c.conn = nil
- close(c.closeChan)
- c.SetId("")
- c.isReg = false
- if c.closeHandler != nil {
- c.closeHandler(c.Id, c)
- }
- }
- // isErrConnReset read: connection reset by peer
- func isErrConnReset(err error) bool {
- if ne, ok := err.(*net.OpError); ok {
- return strings.Contains(ne.Err.Error(), syscall.ECONNRESET.Error())
- }
- return false
- }
- func (c *Client) GetSendByte() {
- for {
- c.sendChan <- []byte{0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x0B}
- time.Sleep(time.Duration(g.Cfg().GetInt("Server.Frequency")) * time.Second)
- }
- }
- func (c *Client) send(buf []byte) error {
- if c.conn == nil {
- return nil
- }
- glog.Debugf("----->%2X", buf)
- err := c.conn.Send(buf)
- if err != nil {
- glog.Error(c.srv.ctx, err)
- c.closeConnection()
- return err
- }
- return nil
- }
|