liuxiulin 2 роки тому
батько
коміт
c8b60ddd0b
4 змінених файлів з 78 додано та 25 видалено
  1. 10 0
      main.go
  2. 9 2
      protocol/protocol.go
  3. 57 21
      server/client.go
  4. 2 2
      server/server.go

+ 10 - 0
main.go

@@ -84,6 +84,16 @@ func main() {
 	}); err != nil {
 		panic(err)
 	}
+	if err = gw.RegisterCommand("GetOffset", func(msg protocal.CloudSend) error {
+		glog.Debugf("指令:%s, 子设备Id:%s", msg.Data.Cmd, msg.SubDeviceId)
+		client := srv.GetClient(msg.SubDeviceId)
+		if client != nil {
+			client.GetOffset()
+		}
+		return nil
+	}); err != nil {
+		panic(err)
+	}
 
 	gproc.AddSigHandlerShutdown(func(sig os.Signal) {
 		gw.Close()

+ 9 - 2
protocol/protocol.go

@@ -5,7 +5,14 @@ type Data struct {
 	Humidly     float32 `json:"humidly"`     // 湿度
 }
 
+type OffsetData struct {
+	TemOffset      int     `json:"tem_offset"`       // 温度使能校准 1 开 2 关闭
+	TemOffsetValue float32 `json:"tem_offset_value"` // 温度校准值
+	HumOffset      int     `json:"hum_offset"`       // 湿度使能校准 1 开 2 关
+	HumOffsetValue float32 `json:"hum_offset_value"` // 湿度校准值
+}
+
 type SetOffSetParams struct {
-	Act   int `json:"act"`   // 操作类型:(1:使能温度校准2:关闭温度校准 3:使能湿度校准 4:关闭湿度校准)
-	Value int `json:"value"` // 偏移值
+	Act   int     `json:"act"`   // 操作类型:(1:使能温度校准2:关闭温度校准 3:使能湿度校准 4:关闭湿度校准)
+	Value float32 `json:"value"` // 偏移值
 }

+ 57 - 21
server/client.go

@@ -11,6 +11,7 @@ import (
 	"github.com/gogf/gf/os/glog"
 	"github.com/gogf/gf/util/gconv"
 	"io"
+	"math"
 	"net"
 	"strconv"
 	"strings"
@@ -53,13 +54,13 @@ func (c *Client) SendLoop() {
 			}
 			timer := time.NewTimer(5 * time.Second)
 			for {
-				timer.Reset(5 * time.Second)
 				select {
+				case <-c.closeChan:
+					return
 				case <-timer.C:
 					glog.Error("接收指令超时")
 					break
 				default:
-
 					receiveBuf, err := c.conn.Recv(-1)
 					if err != nil {
 						c.readError(err)
@@ -73,7 +74,7 @@ func (c *Client) SendLoop() {
 						if c.regHandler != nil {
 							c.regHandler(c.Id, c)
 						}
-						break
+						continue
 					}
 					glog.Debugf("收到数据:%2X", receiveBuf)
 					if err := c.decodeAndReport(receiveBuf); err != nil {
@@ -98,11 +99,34 @@ func (c *Client) decodeAndReport(buf []byte) error {
 	}
 
 	if buf[1] == 0x03 {
-		data := &protocol.Data{}
-		data.Temperature = gconv.Float32(caleTemperature(buf[3:5])) * 0.1
-		data.Humidly = gconv.Float32(gbinary.BeDecodeToUint16(buf[5:7])) * 0.1
-		if err := c.srv.ReportStatus(c.Id, data); err != nil {
-			return err
+		if buf[2] == 0x04 {
+			data := &protocol.Data{}
+			data.Temperature = gconv.Float32(caleTemperature(buf[3:5])) / 10
+			data.Humidly = gconv.Float32(gbinary.BeDecodeToUint16(buf[5:7])) / 10
+			if err := c.srv.ReportStatus(c.Id, data, "status"); err != nil {
+				return err
+			}
+		}
+		if buf[2] == 0x08 {
+			data := &protocol.OffsetData{
+				TemOffset: 2,
+				HumOffset: 2,
+			}
+			if gconv.Int(gbinary.BeDecodeToUint16(buf[3:5])) == 1 {
+				data.TemOffset = 1
+				value := gconv.Float32(gbinary.BeDecodeToUint16(buf[5:7])&0x7FFF) / 100
+				if gconv.Int(gbinary.BeDecodeToUint16(buf[5:7])&0x8000) == 0 {
+					value = -value
+				}
+				data.TemOffsetValue = value
+			}
+			if gconv.Int(gbinary.BeDecodeToUint16(buf[7:9])) == 1 {
+				data.HumOffset = 1
+				data.HumOffsetValue = gconv.Float32(gbinary.BeDecodeToUint16(buf[9:11])) / 100
+			}
+			if err := c.srv.ReportStatus(c.Id, data, "rs30_offset"); err != nil {
+				return err
+			}
 		}
 	}
 	return nil
@@ -118,7 +142,6 @@ 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)
@@ -156,7 +179,7 @@ func isErrConnReset(err error) bool {
 }
 
 // SetOffset 设置温湿度偏移值
-func (c *Client) SetOffset(act, value, slaveId int) error {
+func (c *Client) SetOffset(act int, value float32, slaveId int) error {
 	var sendBuf []byte
 	buffer := bytes.NewBuffer(sendBuf)
 	buffer.Write(gbinary.BeEncodeInt(slaveId))
@@ -165,41 +188,54 @@ func (c *Client) SetOffset(act, value, slaveId int) error {
 	var address []byte
 	if act == 1 || act == 3 {
 		funcByte = 0x10
-		address = []byte{0x00, 0x50}
-	}
-	if act == 2 || act == 4 {
+	} else if act == 2 || act == 4 {
 		funcByte = 0x06
+	}
+	if act == 1 || act == 2 {
+		address = []byte{0x00, 0x50}
+	} else if act == 3 || act == 4 {
 		address = []byte{0x00, 0x52}
 	}
 	buffer.WriteByte(funcByte)
 	buffer.Write(address)
 
-	if act == 1 || act == 3 {
+	if act == 1 {
 		number := []byte{0x00, 0x02}
 		buffer.Write(number)
 		buffer.Write([]byte{0x00, 0x01})
 		buffer.Write(dataBlock(value))
+	} else if act == 3 {
+		number := []byte{0x00, 0x02}
+		buffer.Write(number)
+		buffer.Write([]byte{0x00, 0x01})
+		buffer.Write(gbinary.BeEncodeUint16(uint16(value * 100)))
 	} else if act == 2 || act == 4 {
 		buffer.Write([]byte{0x00, 0x00})
 	}
-
 	var mCrc crc
 	checkSum := mCrc.reset().pushBytes(buffer.Bytes()).value()
 	buffer.Write([]byte{byte(checkSum), byte(checkSum >> 8)})
-	return c.send(buffer.Bytes())
+	//fmt.Printf("%2X", buffer.Bytes())
+	c.sendChan <- buffer.Bytes()
+	return nil
 }
 
-func dataBlock(value int) []byte {
+func dataBlock(value float32) []byte {
 	buffer := &bytes.Buffer{}
-	if value < 0 {
-		buffer.WriteByte(0xFF)
+	var a uint16
+	if value >= 0 {
+		a = uint16(value*100) | 0x8000
 	} else {
-		buffer.WriteByte(0x00)
+		a = uint16(math.Abs(float64(value))*100) | 0x0000
 	}
-	buffer.Write(gbinary.BeEncodeInt(value))
+	buffer.Write(gbinary.BeEncodeUint16(a))
 	return buffer.Bytes()
 }
 
+func (c *Client) GetOffset() {
+	c.sendChan <- []byte{0x01, 0x03, 0x00, 0x50, 0x00, 0x04, 0x44, 0x18}
+}
+
 func (c *Client) GetSendByte() {
 	for {
 		c.sendChan <- []byte{0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x0B}

+ 2 - 2
server/server.go

@@ -66,8 +66,8 @@ func (s *Server) onClientConnect(conn *gtcp.Conn) {
 	go client.GetSendByte()
 }
 
-func (s *Server) ReportStatus(subId string, data interface{}) error {
-	return s.gateWay.ReportStatus(subId, "status", data)
+func (s *Server) ReportStatus(subId string, data interface{}, cmd string) error {
+	return s.gateWay.ReportStatus(subId, cmd, data)
 }
 
 func (s *Server) GetClient(subId string) *Client {