完善tcp下解析lora传输来的数据
This commit is contained in:
parent
5505ba460e
commit
45307d6a06
384
router.go
384
router.go
|
@ -13,6 +13,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
"bytes"
|
||||||
)
|
)
|
||||||
|
|
||||||
var router = gin.Default()
|
var router = gin.Default()
|
||||||
|
@ -33,10 +34,14 @@ func gw_router() {
|
||||||
v1.GET("/satisfaction", pj_api)
|
v1.GET("/satisfaction", pj_api)
|
||||||
v1.GET("/seat/:id", seat_api)
|
v1.GET("/seat/:id", seat_api)
|
||||||
v1.POST("/push", push)
|
v1.POST("/push", push)
|
||||||
|
v1.POST("/pushv2", pushv2)
|
||||||
|
|
||||||
v1.GET("/getConf")
|
v1.GET("/getConf")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
router.Run(":10086")
|
router.Run(":10086")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,6 +73,10 @@ func getConf() {
|
||||||
log.Println("yunhorndebug.getConf.success:", dparkConf)
|
log.Println("yunhorndebug.getConf.success:", dparkConf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err!=nil{
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if dparkConf.ExtensionInTime == 0 && dparkConf.ExtensionOutTime == 0 {
|
if dparkConf.ExtensionInTime == 0 && dparkConf.ExtensionOutTime == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -83,7 +92,7 @@ func getConf() {
|
||||||
extensionInTime = dparkConf.ExtensionInTime
|
extensionInTime = dparkConf.ExtensionInTime
|
||||||
extensionOutTime = dparkConf.ExtensionOutTime
|
extensionOutTime = dparkConf.ExtensionOutTime
|
||||||
|
|
||||||
check(err)
|
// check(err)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,56 +116,70 @@ var dataMap sync.Map
|
||||||
//接收传感器push来的数据,解析并上传到云端
|
//接收传感器push来的数据,解析并上传到云端
|
||||||
func push(c *gin.Context) {
|
func push(c *gin.Context) {
|
||||||
|
|
||||||
nowtime := time.Now()
|
data := c.PostForm("d")
|
||||||
|
|
||||||
data := c.PostForm("d")
|
go dealData(data)
|
||||||
|
|
||||||
|
log.Println("data:", data)
|
||||||
|
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": data})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func dealData(data string) string{
|
||||||
|
|
||||||
|
nowtime := time.Now()
|
||||||
|
|
||||||
log.Println("yunhorndeug.push.data|", data)
|
log.Println("yunhorndeug.push.data|", data)
|
||||||
|
|
||||||
go pushCODE1007(data)
|
// go pushCODE1007(data)
|
||||||
|
|
||||||
alldata := strings.Split(data, "#")
|
alldata := strings.Split(data, "#")
|
||||||
|
|
||||||
if len(alldata) != 2 {
|
if len(alldata) != 2 {
|
||||||
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "ok"})
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "ok"})
|
||||||
return
|
return ""
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
valuedata := strings.Split(alldata[1], "*")
|
valuedata := strings.Split(alldata[1], "*")
|
||||||
|
|
||||||
if len(valuedata) < 1 {
|
if len(valuedata) < 1 {
|
||||||
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "ok"})
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "ok"})
|
||||||
return
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
dtype, error := strconv.Atoi(valuedata[0])
|
dtype, error := strconv.Atoi(valuedata[0])
|
||||||
if error != nil {
|
if error != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "errtype"})
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "errtype"})
|
||||||
return
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
var nowTmpData = "1#0"
|
var nowTmpData = "1#0"
|
||||||
|
|
||||||
if dtype == 1 {
|
if dtype == 1 {
|
||||||
if len(valuedata) != 3 {
|
if len(valuedata) != 3 {
|
||||||
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "valuedata not 3"})
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "valuedata not 3"})
|
||||||
return
|
return ""
|
||||||
}
|
}
|
||||||
addr, error := strconv.Atoi(alldata[0])
|
addr, error := strconv.Atoi(alldata[0])
|
||||||
if error != nil {
|
if error != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "erraddr"})
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "erraddr"})
|
||||||
return
|
return ""
|
||||||
}
|
}
|
||||||
cs2001, error := strconv.Atoi(valuedata[1])
|
cs2001, error := strconv.Atoi(valuedata[1])
|
||||||
if error != nil {
|
if error != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "errcs2001"})
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "errcs2001"})
|
||||||
return
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
tof, error := strconv.Atoi(valuedata[2])
|
tof, error := strconv.Atoi(valuedata[2])
|
||||||
if error != nil {
|
if error != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "errtof"})
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "errtof"})
|
||||||
return
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
if tof ==0{
|
||||||
|
log.Println("tof is zero")
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
if addr == 1 {
|
if addr == 1 {
|
||||||
|
@ -195,13 +218,13 @@ func push(c *gin.Context) {
|
||||||
|
|
||||||
if nowTmpData == "1#0" {
|
if nowTmpData == "1#0" {
|
||||||
if durationAsInt64 < extensionOutTime {
|
if durationAsInt64 < extensionOutTime {
|
||||||
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "not enough time"})
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "not enough time"})
|
||||||
return
|
return ""
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if durationAsInt64 < extensionInTime {
|
if durationAsInt64 < extensionInTime {
|
||||||
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "not enough time"})
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "not enough time"})
|
||||||
return
|
return ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
maledemoDevice.DATA["data1"] = 0
|
maledemoDevice.DATA["data1"] = 0
|
||||||
|
@ -250,13 +273,13 @@ func push(c *gin.Context) {
|
||||||
|
|
||||||
if nowTmpData == "1#0" {
|
if nowTmpData == "1#0" {
|
||||||
if durationAsInt64 < extensionOutTime {
|
if durationAsInt64 < extensionOutTime {
|
||||||
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "not enough time"})
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "not enough time"})
|
||||||
return
|
return ""
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if durationAsInt64 < extensionInTime {
|
if durationAsInt64 < extensionInTime {
|
||||||
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "not enough time"})
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "not enough time"})
|
||||||
return
|
return ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
maledemoDevice.DATA["data2"] = 0
|
maledemoDevice.DATA["data2"] = 0
|
||||||
|
@ -274,18 +297,18 @@ func push(c *gin.Context) {
|
||||||
} else if dtype == 2 {
|
} else if dtype == 2 {
|
||||||
//吸顶传感器 没有距离数据
|
//吸顶传感器 没有距离数据
|
||||||
if len(valuedata) != 2 {
|
if len(valuedata) != 2 {
|
||||||
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "valuedata not 2,type2"})
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "valuedata not 2,type2"})
|
||||||
return
|
return ""
|
||||||
}
|
}
|
||||||
addr, error := strconv.Atoi(alldata[0])
|
addr, error := strconv.Atoi(alldata[0])
|
||||||
if error != nil {
|
if error != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "erraddr"})
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "erraddr"})
|
||||||
return
|
return ""
|
||||||
}
|
}
|
||||||
cs2001, error := strconv.Atoi(valuedata[1])
|
cs2001, error := strconv.Atoi(valuedata[1])
|
||||||
if error != nil {
|
if error != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "errcs2001"})
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "errcs2001"})
|
||||||
return
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
if cs2001 == 1 {
|
if cs2001 == 1 {
|
||||||
|
@ -324,13 +347,13 @@ func push(c *gin.Context) {
|
||||||
|
|
||||||
if nowTmpData == "1#0" {
|
if nowTmpData == "1#0" {
|
||||||
if durationAsInt64 < extensionOutTime {
|
if durationAsInt64 < extensionOutTime {
|
||||||
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "not enough time"})
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "not enough time"})
|
||||||
return
|
return ""
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if durationAsInt64 < extensionInTime {
|
if durationAsInt64 < extensionInTime {
|
||||||
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "not enough time"})
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "not enough time"})
|
||||||
return
|
return ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -344,11 +367,296 @@ func push(c *gin.Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func pushv2(c *gin.Context){
|
||||||
|
data := c.PostForm("d")
|
||||||
|
|
||||||
|
go dealDatav2(data)
|
||||||
|
|
||||||
log.Println("data:", data)
|
log.Println("data:", data)
|
||||||
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": data})
|
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "code": 1})
|
||||||
|
|
||||||
|
}z
|
||||||
|
|
||||||
|
func dealDatav2(data string) string{
|
||||||
|
|
||||||
|
nowtime := time.Now()
|
||||||
|
|
||||||
|
log.Println("yunhorndeug.push.data|", data)
|
||||||
|
|
||||||
|
strs := []byte(data)
|
||||||
|
|
||||||
|
addr := string(strs[:3])
|
||||||
|
|
||||||
|
var readyData bytes.Buffer
|
||||||
|
//地址
|
||||||
|
readyData.WriteString(addr)
|
||||||
|
readyData.WriteString("#")
|
||||||
|
//类型
|
||||||
|
readyData.WriteString(string(strs[3:4]))
|
||||||
|
readyData.WriteString("*")
|
||||||
|
//红外人体感应
|
||||||
|
readyData.WriteString(string(strs[4:5]))
|
||||||
|
|
||||||
|
newdtype,err :=strconv.Atoi(string(strs[3:4]))
|
||||||
|
if err!=nil{
|
||||||
|
log.Println("type parse err",err)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
if newdtype==1{
|
||||||
|
readyData.WriteString("*")
|
||||||
|
readyData.WriteString(string(strs[5:6]))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
for i,v := range strs{
|
||||||
|
log.Println("strs,i,v",i,v-48)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("readyData:",readyData.String())
|
||||||
|
|
||||||
|
// go pushCODE1007(data)
|
||||||
|
|
||||||
|
alldata := strings.Split(readyData.String(), "#")
|
||||||
|
|
||||||
|
if len(alldata) != 2 {
|
||||||
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "ok"})
|
||||||
|
return ""
|
||||||
|
} else {
|
||||||
|
|
||||||
|
valuedata := strings.Split(alldata[1], "*")
|
||||||
|
|
||||||
|
if len(valuedata) < 1 {
|
||||||
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "ok"})
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
dtype, error := strconv.Atoi(valuedata[0])
|
||||||
|
if error != nil {
|
||||||
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "errtype"})
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
var nowTmpData = "1#0"
|
||||||
|
|
||||||
|
if dtype == 1 {
|
||||||
|
if len(valuedata) != 3 {
|
||||||
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "valuedata not 3"})
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
addr, error := strconv.Atoi(alldata[0])
|
||||||
|
if error != nil {
|
||||||
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "erraddr"})
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
cs2001, error := strconv.Atoi(valuedata[1])
|
||||||
|
if error != nil {
|
||||||
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "errcs2001"})
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
tof, error := strconv.Atoi(valuedata[2])
|
||||||
|
if error != nil {
|
||||||
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "errtof"})
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
if tof ==0{
|
||||||
|
log.Println("tof is zero")
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
if addr == 1 {
|
||||||
|
|
||||||
|
if cs2001 == 1 || tof ==1 {
|
||||||
|
nowTmpData = "1#1"
|
||||||
|
}
|
||||||
|
|
||||||
|
if oldData1 != nowTmpData {
|
||||||
|
dataMap.Store(addr, nowtime)
|
||||||
|
if oldData1 == "" {
|
||||||
|
//为空 直接改变状态
|
||||||
|
maledemoDevice.DATA["data1"] = 0
|
||||||
|
if nowTmpData == "1#1" {
|
||||||
|
maledemoDevice.DATA["data1"] = 1
|
||||||
|
}
|
||||||
|
changeData(2, maledemoDevice)
|
||||||
|
}
|
||||||
|
//存储状态
|
||||||
|
oldData1 = nowTmpData
|
||||||
|
|
||||||
|
} else {
|
||||||
|
oldDevDataTime, ok := dataMap.Load(addr)
|
||||||
|
if !ok {
|
||||||
|
log.Println("load time err", ok)
|
||||||
|
}
|
||||||
|
|
||||||
|
if ok {
|
||||||
|
if oldDevDataTime != nil {
|
||||||
|
//log.Println("t2.Sub(t1)", nowtime.Sub(oldDevDataTime.(time.Time)))
|
||||||
|
|
||||||
|
var duration = nowtime.Sub(oldDevDataTime.(time.Time)).Seconds()
|
||||||
|
var durationAsInt64 = int(duration)
|
||||||
|
|
||||||
|
log.Printf("yunhorndebug.origin.%s|%s|%d|%d|%d|%d|%d", oldData1, nowTmpData, addr, dtype, cs2001, tof, durationAsInt64)
|
||||||
|
|
||||||
|
if nowTmpData == "1#0" {
|
||||||
|
if durationAsInt64 < extensionOutTime {
|
||||||
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "not enough time"})
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if durationAsInt64 < extensionInTime {
|
||||||
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "not enough time"})
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
maledemoDevice.DATA["data1"] = 0
|
||||||
|
if nowTmpData == "1#1" {
|
||||||
|
maledemoDevice.DATA["data1"] = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
changeData(2, maledemoDevice)
|
||||||
|
dataMap.Delete(addr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if addr == 2 {
|
||||||
|
if cs2001 == 1 || tof == 1 {
|
||||||
|
nowTmpData = "1#1"
|
||||||
|
}
|
||||||
|
|
||||||
|
if oldData2 != nowTmpData {
|
||||||
|
dataMap.Store(addr, nowtime)
|
||||||
|
if oldData2 == "" {
|
||||||
|
//为空 直接改变状态
|
||||||
|
maledemoDevice.DATA["data2"] = 0
|
||||||
|
if nowTmpData == "1#1" {
|
||||||
|
maledemoDevice.DATA["data2"] = 1
|
||||||
|
}
|
||||||
|
changeData(2, maledemoDevice)
|
||||||
|
}
|
||||||
|
//存储状态
|
||||||
|
oldData2 = nowTmpData
|
||||||
|
|
||||||
|
} else {
|
||||||
|
oldDevDataTime, ok := dataMap.Load(addr)
|
||||||
|
if !ok {
|
||||||
|
log.Println("load time err", ok)
|
||||||
|
}
|
||||||
|
|
||||||
|
if ok {
|
||||||
|
if oldDevDataTime != nil {
|
||||||
|
//log.Println("t2.Sub(t1)", nowtime.Sub(oldDevDataTime.(time.Time)))
|
||||||
|
|
||||||
|
var duration = nowtime.Sub(oldDevDataTime.(time.Time)).Seconds()
|
||||||
|
var durationAsInt64 = int(duration)
|
||||||
|
|
||||||
|
log.Printf("yunhorndebug.origin.%s|%s|%d|%d|%d|%d|%d", oldData2, nowTmpData, addr, dtype, cs2001, tof, durationAsInt64)
|
||||||
|
|
||||||
|
if nowTmpData == "1#0" {
|
||||||
|
if durationAsInt64 < extensionOutTime {
|
||||||
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "not enough time"})
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if durationAsInt64 < extensionInTime {
|
||||||
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "not enough time"})
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
maledemoDevice.DATA["data2"] = 0
|
||||||
|
if nowTmpData == "1#1" {
|
||||||
|
maledemoDevice.DATA["data2"] = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
changeData(2, maledemoDevice)
|
||||||
|
dataMap.Delete(addr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if dtype == 2 {
|
||||||
|
//吸顶传感器 没有距离数据
|
||||||
|
if len(valuedata) != 2 {
|
||||||
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "valuedata not 2,type2"})
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
addr, error := strconv.Atoi(alldata[0])
|
||||||
|
if error != nil {
|
||||||
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "erraddr"})
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
cs2001, error := strconv.Atoi(valuedata[1])
|
||||||
|
if error != nil {
|
||||||
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "errcs2001"})
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
if cs2001 == 1 {
|
||||||
|
nowTmpData = "1#1"
|
||||||
|
}
|
||||||
|
|
||||||
|
if oldData3 != nowTmpData {
|
||||||
|
dataMap.Store(addr, nowtime)
|
||||||
|
if oldData3 == "" {
|
||||||
|
//为空 直接改变状态
|
||||||
|
femaledemoDevice.DATA["data1"] = 0
|
||||||
|
if nowTmpData == "1#1" {
|
||||||
|
femaledemoDevice.DATA["data1"] = 1
|
||||||
|
}
|
||||||
|
changeData(2, maledemoDevice)
|
||||||
|
}
|
||||||
|
//存储状态
|
||||||
|
oldData3 = nowTmpData
|
||||||
|
|
||||||
|
} else {
|
||||||
|
oldDevDataTime, ok := dataMap.Load(addr)
|
||||||
|
if !ok {
|
||||||
|
log.Println("load time err", ok)
|
||||||
|
}
|
||||||
|
|
||||||
|
if ok {
|
||||||
|
if oldDevDataTime != nil {
|
||||||
|
//log.Println("t2.Sub(t1)", nowtime.Sub(oldDevDataTime.(time.Time)))
|
||||||
|
|
||||||
|
var duration = nowtime.Sub(oldDevDataTime.(time.Time)).Seconds()
|
||||||
|
var durationAsInt64 = int(duration)
|
||||||
|
|
||||||
|
var tof = 0
|
||||||
|
|
||||||
|
log.Printf("yunhorndebug.origin.%s|%s|%d|%d|%d|%d|%d", oldData3, nowTmpData, addr, dtype, cs2001, tof, durationAsInt64)
|
||||||
|
|
||||||
|
if nowTmpData == "1#0" {
|
||||||
|
if durationAsInt64 < extensionOutTime {
|
||||||
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "not enough time"})
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if durationAsInt64 < extensionInTime {
|
||||||
|
// c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": "not enough time"})
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
femaledemoDevice.DATA["data1"] = 0
|
||||||
|
if nowTmpData == "1#1" {
|
||||||
|
femaledemoDevice.DATA["data1"] = 1
|
||||||
|
}
|
||||||
|
changeData(3, femaledemoDevice)
|
||||||
|
dataMap.Delete(addr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
//发送code1007数据到云端
|
//发送code1007数据到云端
|
||||||
|
|
155
tcpserver.go
155
tcpserver.go
|
@ -9,11 +9,14 @@ import (
|
||||||
//"strings"
|
//"strings"
|
||||||
"time"
|
"time"
|
||||||
// "go-study/socket/config"
|
// "go-study/socket/config"
|
||||||
|
"bytes"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func handleConn(conn net.Conn) {
|
func handleConn(conn net.Conn) {
|
||||||
|
|
||||||
log.Println("begin handleConn,cliendAddr:", conn.RemoteAddr())
|
log.Println("yunhorndeug.push.data.tcp.begin handleConn,cliendAddr:", conn.RemoteAddr())
|
||||||
|
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
|
@ -61,50 +64,146 @@ func handleConn(conn net.Conn) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func readConn(conn net.Conn, readChan chan<- []byte, stopChan chan<- bool) {
|
func readConn(conn net.Conn, readChan chan<- []byte, stopChan chan<- bool) {
|
||||||
data_buf := make(chan []byte)
|
|
||||||
Rdata := make([]byte, 0)
|
// data_buf := make(chan []byte)
|
||||||
|
|
||||||
|
// Rdata := make([]byte, 0)
|
||||||
data := make([]byte, 256)
|
data := make([]byte, 256)
|
||||||
|
Rdata2 := make([]byte, 0)
|
||||||
|
|
||||||
go func() {
|
// go func() {
|
||||||
for {
|
// for {
|
||||||
select {
|
// select {
|
||||||
case kkdata := <-data_buf: //如果有数据,下面打印。但是有可能ch一直没数据
|
// case kkdata := <-data_buf: //如果有数据,下面打印。但是有可能ch一直没数据
|
||||||
Rdata = append(Rdata, kkdata...)
|
// // log.Println("yunhorndeug.push.data.tcp|",kkdata)
|
||||||
//log.Println("kkdata:",kkdata)
|
//
|
||||||
case <-time.After(50 * time.Millisecond): //上面的ch如果一直没数据会阻塞,那么select也会检测其他case条件,检测到后3秒超时
|
// log.Println("###########################yunhorndeug.push.data.tcp.kkdata:",kkdata)
|
||||||
// log.Println("超时")
|
// Rdata = append(Rdata, kkdata...)
|
||||||
if len(Rdata) > 4 {
|
// Rdata2 = append(Rdata2,Rdata...)
|
||||||
readChan <- Rdata
|
// count := 0
|
||||||
Rdata = make([]byte, 0)
|
// for _,v := range Rdata{
|
||||||
}
|
// if v==0{
|
||||||
//strData := string(Rdata)
|
// break;
|
||||||
//Rdata = make([]byte,0)
|
// }
|
||||||
//log.Println("Received:", strData)
|
// count++
|
||||||
|
// }
|
||||||
//readChan <- Rdata
|
//
|
||||||
//Rdata = make([]byte,0)
|
// Rdata2 = make([]byte, count)
|
||||||
}
|
// for i,v := range Rdata{
|
||||||
}
|
// if v==0{
|
||||||
}()
|
// break;
|
||||||
|
// }
|
||||||
|
// Rdata2[i]=v
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// log.Println("---------------------------yunhorndeug.push.data.tcp.Rdata:",Rdata2)
|
||||||
|
// Rdata2 = make([]byte, 0)
|
||||||
|
// //log.Println("kkdata:",kkdata)
|
||||||
|
// case <-time.After(50 * time.Millisecond): //上面的ch如果一直没数据会阻塞,那么select也会检测其他case条件,检测到后3秒超时
|
||||||
|
// // log.Println("超时")
|
||||||
|
// if len(Rdata) > 4 {
|
||||||
|
// readChan <- Rdata
|
||||||
|
// Rdata = make([]byte, 0)
|
||||||
|
// }
|
||||||
|
// //strData := string(Rdata)
|
||||||
|
// //Rdata = make([]byte,0)
|
||||||
|
// //log.Println("Received:", strData)
|
||||||
|
//
|
||||||
|
// //readChan <- Rdata
|
||||||
|
// //Rdata = make([]byte,0)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// data := make([]byte, config.SERVER_RECV_LEN)
|
// data := make([]byte, config.SERVER_RECV_LEN)
|
||||||
//data2 := make([]byte,256)
|
//data2 := make([]byte,256)
|
||||||
_, err := conn.Read(data)
|
reqLen, err := conn.Read(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
// log.Println(DEVICEDATAS)
|
|
||||||
log.Println("read.data:%d,%s", Bytes2Bits(data[3:4])[0],conn.RemoteAddr())
|
|
||||||
|
|
||||||
data_buf <- data
|
buffer := new(bytes.Buffer)
|
||||||
|
continueCount := 0
|
||||||
|
log.Println("yunhorndeug.push.data.tcp.reqLen",reqLen)
|
||||||
|
for i, b := range data[:reqLen] {
|
||||||
|
|
||||||
|
if continueCount>0{
|
||||||
|
continueCount--
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
s := strconv.FormatInt(int64(b&0xff), 16)
|
||||||
|
if len(s) == 1 {
|
||||||
|
buffer.WriteString("0")
|
||||||
|
}
|
||||||
|
buffer.WriteString(s)
|
||||||
|
|
||||||
|
// log.Println(b)
|
||||||
|
//分隔符
|
||||||
|
end :=0
|
||||||
|
if b==254 && i>3{
|
||||||
|
addrs := data[i-4:i]
|
||||||
|
//尿布台数据,地址1位 数据2位
|
||||||
|
dataType := data[i+1]
|
||||||
|
values := make([]byte,0)
|
||||||
|
if data[i+1] == 1 {
|
||||||
|
continueCount = 3
|
||||||
|
end = 4
|
||||||
|
//类型为1 数据量为2
|
||||||
|
values = data[i+2:i+4]
|
||||||
|
}else if dataType == 2{
|
||||||
|
//类型为2 数据量位1
|
||||||
|
values = data[i+2:i+3]
|
||||||
|
continueCount = 2
|
||||||
|
end = 3
|
||||||
|
}
|
||||||
|
log.Println("yunhorndeug.push.data.tcp.addr",addrs)
|
||||||
|
log.Println("yunhorndeug.push.data.tcp.type",dataType)
|
||||||
|
log.Println("yunhorndeug.push.data.tcp.values",values)
|
||||||
|
// var buffer bytes.Buffer
|
||||||
|
// buffer.Write(addrs)
|
||||||
|
// buffer.Write(dataType)
|
||||||
|
// buffer.Write(values)
|
||||||
|
|
||||||
|
// go dealDatav3(data[i-4:i+end])
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// 转化为字符串
|
||||||
|
log.Println("yunhorndeug.push.data.tcp.16data:",buffer.String())
|
||||||
|
|
||||||
|
Rdata2 = make([]byte, reqLen)
|
||||||
|
for i,v := range data[:reqLen]{
|
||||||
|
Rdata2[i]=v
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("===========================yunhorndeug.push.data.tcp.origin.Rdata:",Rdata2)
|
||||||
|
|
||||||
|
// Rdata2 = make([]byte, count)
|
||||||
|
|
||||||
|
// log.Println("read.data:%d,%s", Bytes2Bits(data[3:4])[0],conn.RemoteAddr())
|
||||||
|
|
||||||
|
// data_buf <- data
|
||||||
|
|
||||||
|
data = make([]byte, 256)
|
||||||
}
|
}
|
||||||
|
|
||||||
stopChan <- true
|
stopChan <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//BytesCombine 多个[]byte数组合并成一个[]byte
|
||||||
|
func BytesCombine(pBytes ...[]byte) []byte {
|
||||||
|
return bytes.Join(pBytes, []byte(""))
|
||||||
|
}
|
||||||
|
|
||||||
func writeConn(conn net.Conn, writeChan <-chan string, stopChan chan<- bool) {
|
func writeConn(conn net.Conn, writeChan <-chan string, stopChan chan<- bool) {
|
||||||
for {
|
for {
|
||||||
strData := <-writeChan
|
strData := <-writeChan
|
||||||
|
|
Loading…
Reference in New Issue