|
@@ -2,6 +2,7 @@ package server
|
|
|
|
|
|
import (
|
|
|
"dlt645-server/protocol"
|
|
|
+ "github.com/gogf/gf/frame/g"
|
|
|
"github.com/gogf/gf/net/gtcp"
|
|
|
"github.com/gogf/gf/os/glog"
|
|
|
"io"
|
|
@@ -26,7 +27,7 @@ type Client struct {
|
|
|
}
|
|
|
|
|
|
func (c *Client) ReadLoop(ctx *protocol.PacketContext) {
|
|
|
- //defer c.srv.grWG.Done()
|
|
|
+ defer c.srv.grWG.Done()
|
|
|
for {
|
|
|
buf, err := c.conn.Recv(-1)
|
|
|
if err != nil {
|
|
@@ -34,19 +35,38 @@ func (c *Client) ReadLoop(ctx *protocol.PacketContext) {
|
|
|
return
|
|
|
}
|
|
|
if len(buf) > 0 {
|
|
|
-
|
|
|
result, err := c.srv.message.Decode(ctx, buf)
|
|
|
if err != nil {
|
|
|
glog.Errorf("解析报文失败:%s", err.Error())
|
|
|
}
|
|
|
- //if c.Id == "" && ctx.GetId() != "" {
|
|
|
- c.SetId("220714031227")
|
|
|
- //}
|
|
|
- if c.Id != "" {
|
|
|
- if err := c.srv.ReportStatus(c.Id, result); err != nil {
|
|
|
+ if !c.isReg {
|
|
|
+ c.SetId(ctx.GetId())
|
|
|
+ c.isReg = true
|
|
|
+ }
|
|
|
+
|
|
|
+ if ctx.ReportPower {
|
|
|
+ data := new(protocol.PowerData)
|
|
|
+ data.ActivePower = result.ActivePower
|
|
|
+ if err := c.srv.ReportStatus(c.Id, data); err != nil {
|
|
|
+ c.readError(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ ctx.SetReportPower(false)
|
|
|
+ }
|
|
|
+
|
|
|
+ if ctx.ReportVI {
|
|
|
+ data := new(protocol.VIData)
|
|
|
+ data.AV = ctx.GetVIData().AV
|
|
|
+ data.BV = ctx.GetVIData().BV
|
|
|
+ data.CV = ctx.GetVIData().CV
|
|
|
+ data.AI = ctx.GetVIData().AI
|
|
|
+ data.BI = ctx.GetVIData().BI
|
|
|
+ data.CI = ctx.GetVIData().CI
|
|
|
+ if err := c.srv.ReportStatus(c.Id, data); err != nil {
|
|
|
c.readError(err)
|
|
|
return
|
|
|
}
|
|
|
+ ctx.SetReportVI(false)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -101,23 +121,128 @@ func (c *Client) send(buf []byte) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (c *Client) Send0X3433(ctx *protocol.PacketContext) {
|
|
|
- //defer c.srv.grWG.Done()
|
|
|
- entity := protocol.Dlt_0x33333433{}
|
|
|
+func (c *Client) GetActivePower(ctx *protocol.PacketContext, powerChan chan struct{}) {
|
|
|
+ defer c.srv.grWG.Done()
|
|
|
for {
|
|
|
+ <-powerChan
|
|
|
+ entity := protocol.Dlt_0x33333433{}
|
|
|
if ctx.GetReceiveAddress() != nil {
|
|
|
sendByte, _ := entity.Encode(ctx)
|
|
|
err := c.send(sendByte)
|
|
|
if err != nil {
|
|
|
glog.Debugf("指令发送失败:%s", err.Error())
|
|
|
}
|
|
|
- time.Sleep(10 * time.Second)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
-func (c *Client) SendGetAddress() {
|
|
|
+func (c *Client) GetAV(ctx *protocol.PacketContext, avChan, aiChan chan struct{}) {
|
|
|
+ defer c.srv.grWG.Done()
|
|
|
+ for {
|
|
|
+ <-avChan
|
|
|
+ entity := protocol.Dlt_0x33343435{}
|
|
|
+ if ctx.GetReceiveAddress() != nil {
|
|
|
+ sendByte, _ := entity.Encode(ctx)
|
|
|
+ err := c.send(sendByte)
|
|
|
+ if err != nil {
|
|
|
+ glog.Debugf("指令发送失败:%s", err.Error())
|
|
|
+ }
|
|
|
+ }
|
|
|
+ time.Sleep(1 * time.Second)
|
|
|
+ aiChan <- struct{}{}
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (c *Client) GetAI(ctx *protocol.PacketContext, aiChan, bvChan chan struct{}) {
|
|
|
+ defer c.srv.grWG.Done()
|
|
|
+ for {
|
|
|
+ <-aiChan
|
|
|
+ entity := protocol.Dlt_0x33343535{}
|
|
|
+ if ctx.GetReceiveAddress() != nil {
|
|
|
+ sendByte, _ := entity.Encode(ctx)
|
|
|
+ err := c.send(sendByte)
|
|
|
+ if err != nil {
|
|
|
+ glog.Debugf("指令发送失败:%s", err.Error())
|
|
|
+ }
|
|
|
+ }
|
|
|
+ time.Sleep(1 * time.Second)
|
|
|
+ bvChan <- struct{}{}
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (c *Client) GetBV(ctx *protocol.PacketContext, bvChan, biChan chan struct{}) {
|
|
|
+ defer c.srv.grWG.Done()
|
|
|
+ for {
|
|
|
+ <-bvChan
|
|
|
+ entity := protocol.Dlt_0x33353435{}
|
|
|
+ if ctx.GetReceiveAddress() != nil {
|
|
|
+ sendByte, _ := entity.Encode(ctx)
|
|
|
+ err := c.send(sendByte)
|
|
|
+ if err != nil {
|
|
|
+ glog.Debugf("指令发送失败:%s", err.Error())
|
|
|
+ }
|
|
|
+ }
|
|
|
+ time.Sleep(1 * time.Second)
|
|
|
+ biChan <- struct{}{}
|
|
|
+ }
|
|
|
+}
|
|
|
+func (c *Client) GetBI(ctx *protocol.PacketContext, biChan, cvChan chan struct{}) {
|
|
|
+ defer c.srv.grWG.Done()
|
|
|
+ for {
|
|
|
+ <-biChan
|
|
|
+ entity := protocol.Dlt_0x33353535{}
|
|
|
+ if ctx.GetReceiveAddress() != nil {
|
|
|
+ sendByte, _ := entity.Encode(ctx)
|
|
|
+ err := c.send(sendByte)
|
|
|
+ if err != nil {
|
|
|
+ glog.Debugf("指令发送失败:%s", err.Error())
|
|
|
+ }
|
|
|
+ }
|
|
|
+ time.Sleep(1 * time.Second)
|
|
|
+ cvChan <- struct{}{}
|
|
|
+ }
|
|
|
+}
|
|
|
+func (c *Client) GetCV(ctx *protocol.PacketContext, cvChan, ciChan chan struct{}) {
|
|
|
+ defer c.srv.grWG.Done()
|
|
|
+ for {
|
|
|
+ <-cvChan
|
|
|
+ entity := protocol.Dlt_0x33363435{}
|
|
|
+ if ctx.GetReceiveAddress() != nil {
|
|
|
+ sendByte, _ := entity.Encode(ctx)
|
|
|
+ err := c.send(sendByte)
|
|
|
+ if err != nil {
|
|
|
+ glog.Debugf("指令发送失败:%s", err.Error())
|
|
|
+ }
|
|
|
+ }
|
|
|
+ time.Sleep(1 * time.Second)
|
|
|
+ ciChan <- struct{}{}
|
|
|
+ }
|
|
|
+}
|
|
|
+func (c *Client) GetCI(ctx *protocol.PacketContext, ciChan, powerChan chan struct{}) {
|
|
|
+ defer c.srv.grWG.Done()
|
|
|
+ for {
|
|
|
+ <-ciChan
|
|
|
+ entity := protocol.Dlt_0x33363535{}
|
|
|
+ if ctx.GetReceiveAddress() != nil {
|
|
|
+ sendByte, _ := entity.Encode(ctx)
|
|
|
+ err := c.send(sendByte)
|
|
|
+ if err != nil {
|
|
|
+ glog.Debugf("指令发送失败:%s", err.Error())
|
|
|
+ }
|
|
|
+ }
|
|
|
+ time.Sleep(time.Duration(g.Cfg().GetInt("Server.PowerFrequency")-5) * time.Second)
|
|
|
+ powerChan <- struct{}{}
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (c *Client) SendGetAddress(ctx *protocol.PacketContext, addressChan, avChan chan struct{}) {
|
|
|
+ defer c.srv.grWG.Done()
|
|
|
+ <-addressChan
|
|
|
bytea := []byte{0x68, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0x68, 0x13, 0x00, 0xDF, 0x16}
|
|
|
- c.send(bytea)
|
|
|
+ _ = c.send(bytea)
|
|
|
+ for {
|
|
|
+ time.Sleep(time.Duration(g.Cfg().GetInt("Server.VIFrequency")) * time.Second)
|
|
|
+ avChan <- struct{}{}
|
|
|
+ }
|
|
|
}
|