123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- package server
- import (
- "dlt645-server/protocol"
- "github.com/gogf/gf/net/gtcp"
- "github.com/gogf/gf/os/glog"
- "io"
- "net"
- "strings"
- "syscall"
- "time"
- )
- type Client struct {
- Id string
- Address []byte
- srv *Server
- conn *gtcp.Conn
- sendChan chan []byte
- closeChan chan struct{}
- closeHandler func(id string, c *Client)
- lastHeartBeat time.Time
- gatewayId uint16
- 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) SendLoop(ctx *protocol.PacketContext, data *protocol.Data) {
- defer c.srv.grWG.Done()
- for {
- select {
- case buf := <-c.sendChan:
- err := c.send(buf)
- if err != nil {
- if err != nil {
- glog.Errorf("指令发送失败:%s", err.Error())
- }
- continue
- }
- timer := time.NewTimer(5 * time.Second)
- for {
- select {
- case <-timer.C:
- glog.Errorf("读取超时:%s", err.Error())
- continue
- default:
- receiveBuf, err := c.conn.Recv(-1)
- if err != nil {
- c.readError(err)
- continue
- }
- if len(buf) > 0 {
- result, err := c.srv.message.Decode(ctx, receiveBuf, data)
- if err != nil {
- glog.Errorf("解析报文失败:%s", err.Error())
- }
- if !c.isReg {
- c.SetId(ctx.GetId())
- c.isReg = true
- continue
- }
- var reportData interface{}
- switch result.DataType {
- case protocol.IsPower:
- reportData = protocol.PowerData{
- ActivePower: result.ActivePower,
- }
- case protocol.IsVData:
- reportData = protocol.VIData{
- AV: result.AV,
- BV: result.BV,
- CV: result.CV,
- }
- case protocol.IsIData:
- reportData = protocol.IData{
- AI2: result.AI,
- BI2: result.BI,
- CI2: result.CI,
- }
- default:
- continue
- }
- if err := c.srv.ReportStatus(c.Id, reportData); err != nil {
- glog.Errorf("数据上报发送错误:%s", err.Error())
- continue
- }
- }
- }
- }
- }
- }
- }
- func (c *Client) SetId(id string) {
- c.Id = id
- }
- func (c *Client) SetAddress(address []byte) {
- c.Address = address
- }
- 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) send(buf []byte) error {
- if c.conn == nil {
- return nil
- }
- err := c.conn.Send(buf)
- if err != nil {
- glog.Error(err)
- c.closeConnection()
- return err
- }
- glog.Debugf("指令发送成功:%2X", buf)
- return nil
- }
- func (c *Client) GetActivePower(ctx *protocol.PacketContext) {
- defer c.srv.grWG.Done()
- for {
- entity := protocol.Dlt_0x33333433{}
- sendByte, _ := entity.Encode(ctx)
- c.sendChan <- sendByte
- time.Sleep(10 * time.Second)
- }
- }
- func (c *Client) GetAV(ctx *protocol.PacketContext) {
- defer c.srv.grWG.Done()
- for {
- entity := protocol.Dlt_0x33343435{}
- sendByte, _ := entity.Encode(ctx)
- c.sendChan <- sendByte
- time.Sleep(10 * time.Second)
- }
- }
- func (c *Client) GetAI(ctx *protocol.PacketContext) {
- defer c.srv.grWG.Done()
- for {
- entity := protocol.Dlt_0x33343535{}
- sendByte, _ := entity.Encode(ctx)
- c.sendChan <- sendByte
- time.Sleep(10 * time.Second)
- }
- }
- func (c *Client) GetBV(ctx *protocol.PacketContext, bvChan, biChan chan struct{}) {
- defer c.srv.grWG.Done()
- for {
- entity := protocol.Dlt_0x33353435{}
- sendByte, _ := entity.Encode(ctx)
- c.sendChan <- sendByte
- time.Sleep(10 * time.Second)
- }
- }
- func (c *Client) GetBI(ctx *protocol.PacketContext, biChan, cvChan chan struct{}) {
- defer c.srv.grWG.Done()
- for {
- entity := protocol.Dlt_0x33353535{}
- sendByte, _ := entity.Encode(ctx)
- c.sendChan <- sendByte
- time.Sleep(10 * time.Second)
- }
- }
- func (c *Client) GetCV(ctx *protocol.PacketContext, cvChan, ciChan chan struct{}) {
- defer c.srv.grWG.Done()
- for {
- <-cvChan
- entity := protocol.Dlt_0x33363435{}
- sendByte, _ := entity.Encode(ctx)
- c.sendChan <- sendByte
- time.Sleep(10 * time.Second)
- }
- }
- func (c *Client) GetCI(ctx *protocol.PacketContext, ciChan, powerChan chan struct{}) {
- defer c.srv.grWG.Done()
- for {
- <-ciChan
- entity := protocol.Dlt_0x33363535{}
- sendByte, _ := entity.Encode(ctx)
- c.sendChan <- sendByte
- time.Sleep(10 * time.Second)
- }
- }
- func (c *Client) SendGetAddress(ctx *protocol.PacketContext) []byte {
- entity := new(protocol.Dlt_0x93)
- sendBuf, _ := entity.Encode(ctx)
- return sendBuf
- }
|