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