Bladeren bron

添加主动上报

liuxiulin 2 jaren geleden
bovenliggende
commit
6cfe991f17

+ 12 - 4
main.go

@@ -31,10 +31,7 @@ func main() {
 	if _, err = gw.Authentication(); err != nil {
 		panic(err)
 	}
-	// 通用指令回调
-	gw.SetReportCommandCallback(func(deviceCode, subId string) error {
-		return nil
-	})
+
 	go gw.Connect()
 
 	srv := server.NewServer(
@@ -49,6 +46,17 @@ func main() {
 		}
 	}()
 
+	gw.SetReportCommandCallback(func(deviceCode, subId string) error {
+		client := srv.GetClient(subId)
+		if client != nil {
+			if err = client.ReportActivePower(); err != nil {
+				glog.Errorf("上报数据发生错误:%s", err.Error())
+				return err
+			}
+		}
+		return nil
+	})
+
 	gproc.AddSigHandlerShutdown(func(sig os.Signal) {
 		gw.Close()
 		srv.Stop()

+ 11 - 10
protocol/dlt645_0x33323435.go

@@ -2,6 +2,7 @@ package protocol
 
 import (
 	"encoding/hex"
+	"fmt"
 	"github.com/gogf/gf/os/glog"
 	"strconv"
 )
@@ -50,7 +51,7 @@ func (e *Dlt_0x33323435) Decode(ctx *PacketContext, dataByte []byte) (data Data,
 	e.DeviceID = hex.EncodeToString(bytea)
 	//正向总电能每个字节-33,1-4,分别为,小数位,个位,百位,万位
 	byteb := make([]byte, 6)
-	for i := 0; i < 2; i++ {
+	for i := 0; i < 6; i++ {
 		byteb[i] = dataByte[14+i] - 0x33
 	}
 
@@ -65,14 +66,14 @@ func (e *Dlt_0x33323435) Decode(ctx *PacketContext, dataByte []byte) (data Data,
 }
 
 func (e *Dlt_0x33323435) stringToVoltageBlock(s string) error {
-	a0, _ := strconv.ParseFloat(s[0:2], 64)
-	a1, _ := strconv.ParseFloat(s[2:4], 64)
-	b0, _ := strconv.ParseFloat(s[4:6], 64)
-	b1, _ := strconv.ParseFloat(s[6:8], 64)
-	c0, _ := strconv.ParseFloat(s[8:10], 64)
-	c1, _ := strconv.ParseFloat(s[10:12], 64)
-	e.VoltageA = a0/10 + a1*10
-	e.VoltageB = b0/10 + b1*10
-	e.VoltageC = c0/10 + c1*10
+	a0, _ := strconv.ParseFloat(s[0:2], 32)
+	a1, _ := strconv.ParseFloat(s[2:4], 32)
+	b0, _ := strconv.ParseFloat(s[4:6], 32)
+	b1, _ := strconv.ParseFloat(s[6:8], 32)
+	c0, _ := strconv.ParseFloat(s[8:10], 32)
+	c1, _ := strconv.ParseFloat(s[10:12], 32)
+	e.VoltageA, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", a0/10+a1*10), 64)
+	e.VoltageB, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", b0/10+b1*10), 64)
+	e.VoltageC, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", c0/10+c1*10), 64)
 	return nil
 }

+ 6 - 4
protocol/dlt645_0x33323535.go

@@ -2,6 +2,7 @@ package protocol
 
 import (
 	"encoding/hex"
+	"fmt"
 	"github.com/gogf/gf/os/glog"
 	"strconv"
 )
@@ -49,7 +50,7 @@ func (e *Dlt_0x33323535) Decode(ctx *PacketContext, dataByte []byte) (data Data,
 	e.DeviceID = hex.EncodeToString(bytea)
 	//正向总电能每个字节-33,1-4,分别为,小数位,个位,百位,万位
 	byteb := make([]byte, 9)
-	for i := 0; i < 2; i++ {
+	for i := 0; i < 9; i++ {
 		byteb[i] = dataByte[14+i] - 0x33
 	}
 
@@ -73,8 +74,9 @@ func (e *Dlt_0x33323535) stringToCurrentBlock(s string) error {
 	c0, _ := strconv.ParseFloat(s[12:14], 64)
 	c1, _ := strconv.ParseFloat(s[14:16], 64)
 	c2, _ := strconv.ParseFloat(s[16:18], 64)
-	e.CurrentA = a0/1000 + a1/10 + a2*10
-	e.CurrentB = b0/1000 + b1/10 + b2*10
-	e.CurrentC = c0/1000 + c1/10 + c2*10
+
+	e.CurrentA, _ = strconv.ParseFloat(fmt.Sprintf("%.4f", a0/1000+a1/10+a2*10), 64)
+	e.CurrentB, _ = strconv.ParseFloat(fmt.Sprintf("%.4f", b0/1000+b1/10+b2*10), 64)
+	e.CurrentC, _ = strconv.ParseFloat(fmt.Sprintf("%.4f", c0/1000+c1/10+c2*10), 64)
 	return nil
 }

+ 2 - 1
protocol/dlt645_0x33333433.go

@@ -2,6 +2,7 @@ package protocol
 
 import (
 	"encoding/hex"
+	"fmt"
 	"github.com/gogf/gf/os/glog"
 	"strconv"
 )
@@ -65,6 +66,6 @@ func stringToWP(s string) (float64, error) {
 	a1, _ := strconv.ParseFloat(s[2:4], 64)
 	a2, _ := strconv.ParseFloat(s[4:6], 64)
 	a3, _ := strconv.ParseFloat(s[6:8], 64)
-	res := a0/100 + a1 + a2*100 + a3*10000
+	res, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", a0/100+a1+a2*100+a3*10000), 64)
 	return res, nil
 }

+ 1 - 0
protocol/dlt645_0x33333533.go

@@ -64,6 +64,7 @@ func stringToReactiveEnergy(s string) (float64, error) {
 	a1, _ := strconv.ParseFloat(s[2:4], 64)
 	a2, _ := strconv.ParseFloat(s[4:6], 64)
 	a3, _ := strconv.ParseFloat(s[6:8], 64)
+
 	res := a0/100 + a1 + a2*100 + a3*10000
 	return res, nil
 }

+ 3 - 1
protocol/dlt645_0x33333635.go

@@ -2,6 +2,7 @@ package protocol
 
 import (
 	"encoding/hex"
+	"fmt"
 	"github.com/gogf/gf/os/glog"
 	"strconv"
 )
@@ -68,6 +69,7 @@ func stringToActivePower(s string) (float64, error) {
 	a0, _ := strconv.ParseFloat(s[0:2], 64)
 	a1, _ := strconv.ParseFloat(s[2:4], 64)
 	a2, _ := strconv.ParseFloat(s[4:6], 64)
-	res := a0/10000 + a1/100 + a2
+
+	res, _ := strconv.ParseFloat(fmt.Sprintf("%.4f", a0/10000+a1/100+a2), 64)
 	return res, nil
 }

+ 2 - 1
protocol/dlt645_0x33333735.go

@@ -2,6 +2,7 @@ package protocol
 
 import (
 	"encoding/hex"
+	"fmt"
 	"github.com/gogf/gf/os/glog"
 	"strconv"
 )
@@ -63,6 +64,6 @@ func stringToReactivePower(s string) (float64, error) {
 	a0, _ := strconv.ParseFloat(s[0:2], 64)
 	a1, _ := strconv.ParseFloat(s[2:4], 64)
 	a2, _ := strconv.ParseFloat(s[4:6], 64)
-	res := a0/10000 + a1/100 + a2
+	res, _ := strconv.ParseFloat(fmt.Sprintf("%.4f", a0/10000+a1/100+a2), 64)
 	return res, nil
 }

+ 2 - 1
protocol/dlt645_0x33333835.go

@@ -2,6 +2,7 @@ package protocol
 
 import (
 	"encoding/hex"
+	"fmt"
 	"github.com/gogf/gf/os/glog"
 	"strconv"
 )
@@ -63,6 +64,6 @@ func stringToApparentPower(s string) (float64, error) {
 	a0, _ := strconv.ParseFloat(s[0:2], 64)
 	a1, _ := strconv.ParseFloat(s[2:4], 64)
 	a2, _ := strconv.ParseFloat(s[4:6], 64)
-	res := a0/10000 + a1/100 + a2
+	res, _ := strconv.ParseFloat(fmt.Sprintf("%.4f", a0/10000+a1/100+a2), 64)
 	return res, nil
 }

+ 2 - 1
protocol/dlt645_0x33333935.go

@@ -2,6 +2,7 @@ package protocol
 
 import (
 	"encoding/hex"
+	"fmt"
 	"github.com/gogf/gf/os/glog"
 	"strconv"
 )
@@ -62,6 +63,6 @@ func (e *Dlt_0x33333935) Decode(ctx *PacketContext, dataByte []byte) (data Data,
 func stringToPowerFactor(s string) (float64, error) {
 	a0, _ := strconv.ParseFloat(s[0:2], 64)
 	a1, _ := strconv.ParseFloat(s[2:4], 64)
-	res := a0/10000 + a1/10
+	res, _ := strconv.ParseFloat(fmt.Sprintf("%.4f", a0/10000+a1/10), 64)
 	return res, nil
 }

+ 2 - 1
protocol/dlt645_0x33343435.go

@@ -2,6 +2,7 @@ package protocol
 
 import (
 	"encoding/hex"
+	"fmt"
 	"github.com/gogf/gf/os/glog"
 	"strconv"
 )
@@ -63,6 +64,6 @@ func (e *Dlt_0x33343435) Decode(ctx *PacketContext, dataByte []byte) (data Data,
 func stringToVoltage(s string) (float64, error) {
 	a0, _ := strconv.ParseFloat(s[0:2], 64)
 	a1, _ := strconv.ParseFloat(s[2:4], 64)
-	res := a0/10 + a1*10
+	res, _ := strconv.ParseFloat(fmt.Sprintf("%.4f", a0/10+a1*10), 64)
 	return res, nil
 }

+ 2 - 1
protocol/dlt645_0x33343535.go

@@ -2,6 +2,7 @@ package protocol
 
 import (
 	"encoding/hex"
+	"fmt"
 	"github.com/gogf/gf/os/glog"
 	"strconv"
 )
@@ -64,6 +65,6 @@ func stringToCurrent(s string) (float64, error) {
 	a0, _ := strconv.ParseFloat(s[0:2], 64)
 	a1, _ := strconv.ParseFloat(s[2:4], 64)
 	a2, _ := strconv.ParseFloat(s[4:6], 64)
-	res := a0/1000 + a1/10 + a2*10
+	res, _ := strconv.ParseFloat(fmt.Sprintf("%.4f", a0/1000+a1/10+a2*10), 64)
 	return res, nil
 }

+ 9 - 1
server/client.go

@@ -162,7 +162,6 @@ func (c *Client) GetActivePower() {
 		}
 		time.Sleep(time.Duration(g.Cfg().GetInt("Server.PowerFrequency")) * time.Second)
 	}
-
 }
 
 func (c *Client) writeSensChan(buf []byte) error {
@@ -241,3 +240,12 @@ func (c *Client) SendGetAddress() error {
 		}
 	}
 }
+
+func (c *Client) ReportActivePower() error {
+	entity := protocol.Dlt_0x33333433{}
+	sendByte, _ := entity.Encode(c.packetContext)
+	if err := c.writeSensChan(sendByte); err != nil {
+		return err
+	}
+	return nil
+}

+ 8 - 0
server/server.go

@@ -87,3 +87,11 @@ func (s *Server) onClientConnect(conn *gtcp.Conn) {
 func (s *Server) ReportStatus(subId string, data interface{}, code string) error {
 	return s.gateWay.ReportStatus(subId, code, data)
 }
+
+func (s *Server) GetClient(subId string) *Client {
+	client := s.clients.Get(subId)
+	if client != nil {
+		return client.(*Client)
+	}
+	return nil
+}