|
@@ -2,6 +2,8 @@ package server
|
|
|
|
|
|
import (
|
|
|
"dlt645-server/protocol"
|
|
|
+ "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"
|
|
@@ -35,71 +37,67 @@ func NewClient(s *Server, conn *gtcp.Conn) *Client {
|
|
|
|
|
|
func (c *Client) SendLoop(ctx *protocol.PacketContext, data *protocol.Data) {
|
|
|
defer c.srv.grWG.Done()
|
|
|
-
|
|
|
for {
|
|
|
+ glog.Info("Waiting..............")
|
|
|
select {
|
|
|
case buf := <-c.sendChan:
|
|
|
err := c.send(buf)
|
|
|
if err != nil {
|
|
|
- if err != nil {
|
|
|
- glog.Errorf("指令发送失败:%s", err.Error())
|
|
|
- }
|
|
|
+ glog.Errorf("指令发送失败:%s", err.Error())
|
|
|
continue
|
|
|
}
|
|
|
timer := time.NewTimer(5 * time.Second)
|
|
|
for {
|
|
|
+ timer.Reset(5 * time.Second)
|
|
|
select {
|
|
|
case <-timer.C:
|
|
|
- glog.Errorf("读取超时:%s", err.Error())
|
|
|
- continue
|
|
|
+ glog.Errorf("接收指令超时")
|
|
|
+ break
|
|
|
default:
|
|
|
receiveBuf, err := c.conn.Recv(-1)
|
|
|
if err != nil {
|
|
|
c.readError(err)
|
|
|
- continue
|
|
|
+ break
|
|
|
}
|
|
|
- if len(buf) > 0 {
|
|
|
- result, err := c.srv.message.Decode(ctx, receiveBuf, data)
|
|
|
+ if len(receiveBuf) > 0 {
|
|
|
+ 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
|
|
|
+ break
|
|
|
}
|
|
|
var reportData interface{}
|
|
|
- switch result.DataType {
|
|
|
+ switch data.DataType {
|
|
|
case protocol.IsPower:
|
|
|
reportData = protocol.PowerData{
|
|
|
- ActivePower: result.ActivePower,
|
|
|
+ ActivePower: data.ActivePower,
|
|
|
}
|
|
|
case protocol.IsVData:
|
|
|
reportData = protocol.VIData{
|
|
|
- AV: result.AV,
|
|
|
- BV: result.BV,
|
|
|
- CV: result.CV,
|
|
|
+ AV: data.AV,
|
|
|
+ BV: data.BV,
|
|
|
+ CV: data.CV,
|
|
|
}
|
|
|
case protocol.IsIData:
|
|
|
reportData = protocol.IData{
|
|
|
- AI2: result.AI,
|
|
|
- BI2: result.BI,
|
|
|
- CI2: result.CI,
|
|
|
+ AI: data.AI,
|
|
|
+ BI: data.BI,
|
|
|
+ CI: data.CI,
|
|
|
}
|
|
|
default:
|
|
|
- continue
|
|
|
+ break
|
|
|
}
|
|
|
- if err := c.srv.ReportStatus(c.Id, reportData); err != nil {
|
|
|
- glog.Errorf("数据上报发送错误:%s", err.Error())
|
|
|
- continue
|
|
|
+ if reportData != nil {
|
|
|
+ if err := c.srv.ReportStatus(c.Id, reportData); err != nil {
|
|
|
+ glog.Errorf("数据上报发送错误:%s", err.Error())
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+ break
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
func (c *Client) SetId(id string) {
|
|
@@ -157,73 +155,67 @@ func (c *Client) GetActivePower(ctx *protocol.PacketContext) {
|
|
|
entity := protocol.Dlt_0x33333433{}
|
|
|
sendByte, _ := entity.Encode(ctx)
|
|
|
c.sendChan <- sendByte
|
|
|
- time.Sleep(10 * time.Second)
|
|
|
+ time.Sleep(time.Duration(g.Cfg().GetInt("Server.PowerFrequency")) * time.Second)
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
-func (c *Client) GetAV(ctx *protocol.PacketContext) {
|
|
|
+func (c *Client) GetVBlock(ctx *protocol.PacketContext) {
|
|
|
defer c.srv.grWG.Done()
|
|
|
for {
|
|
|
- entity := protocol.Dlt_0x33343435{}
|
|
|
+ entity := protocol.Dlt_0x33323435{}
|
|
|
sendByte, _ := entity.Encode(ctx)
|
|
|
c.sendChan <- sendByte
|
|
|
- time.Sleep(10 * time.Second)
|
|
|
+ time.Sleep(time.Duration(g.Cfg().GetInt("Server.VIFrequency")) * 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{}) {
|
|
|
+func (c *Client) GetIBlock(ctx *protocol.PacketContext) {
|
|
|
defer c.srv.grWG.Done()
|
|
|
for {
|
|
|
- entity := protocol.Dlt_0x33353435{}
|
|
|
+ entity := protocol.Dlt_0x33323535{}
|
|
|
sendByte, _ := entity.Encode(ctx)
|
|
|
c.sendChan <- sendByte
|
|
|
- time.Sleep(10 * time.Second)
|
|
|
+ time.Sleep(time.Duration(g.Cfg().GetInt("Server.VIFrequency")) * 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 {
|
|
|
+func (c *Client) SendGetAddress(ctx *protocol.PacketContext, data *protocol.Data) error {
|
|
|
entity := new(protocol.Dlt_0x93)
|
|
|
sendBuf, _ := entity.Encode(ctx)
|
|
|
- return sendBuf
|
|
|
+ err := c.send(sendBuf)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ timer := time.NewTimer(10 * 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 !c.isReg {
|
|
|
+ c.SetId(gbinary.DecodeToString(receiveBuf))
|
|
|
+ c.isReg = true
|
|
|
+ break
|
|
|
+ }
|
|
|
+ if len(receiveBuf) > 0 {
|
|
|
+ err = c.srv.message.Decode(ctx, receiveBuf, data)
|
|
|
+ if err != nil {
|
|
|
+ glog.Errorf("解析报文失败:%s", err.Error())
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ctx.GetReceiveAddress() != nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|