diff --git a/conf/gateway.conf b/conf/gateway.conf index fd0d3db..c3b5a4b 100644 --- a/conf/gateway.conf +++ b/conf/gateway.conf @@ -210,6 +210,6 @@ "post_to_server": "https://smartoilets.cn/socketServer/statis/push", "debug": true, "openserial":false, - "open_tcp_server":false, + "open_tcp_server":true, "check_serial":true } diff --git a/router.go b/router.go index c9ce9f6..9517362 100644 --- a/router.go +++ b/router.go @@ -14,6 +14,7 @@ import ( "sync" "time" "bytes" + "net" ) var router = gin.Default() @@ -35,6 +36,10 @@ func gw_router() { v1.GET("/seat/:id", seat_api) v1.POST("/push", push) v1.POST("/pushv2", pushv2) + v1.GET("/pushtest",pushTest) + v1.GET("/pushtest2",pushTest2) + v1.GET("/pushtest3",pushTest3) + v1.GET("/getConf") @@ -54,6 +59,44 @@ type DparkConf struct { var dparkConf DparkConf +var connTest net.Conn + +func pushTest(c *gin.Context){ + // + var heartbeat []byte = []byte{0x8E,0x3C,0x00,0x00,0x52,0x46} + v, err := connTest.Write(heartbeat) + log.Println("send.msg.conn.RemoteAddr",connTest.RemoteAddr()) + log.Println("send.msg.conn.v",v) + if err != nil { + log.Println("write msg error!", err) + } + return +} + +func pushTest2(c *gin.Context){ + // + var heartbeat []byte = []byte{0x8E,0x3C,0x00,0x00,0x47,0x46} + v, err := connTest.Write(heartbeat) + log.Println("send.msg.conn.RemoteAddr",connTest.RemoteAddr()) + log.Println("send.msg.conn.v",v) + if err != nil { + log.Println("write msg error!", err) + } + return +} + +func pushTest3(c *gin.Context){ + // + var heartbeat []byte = []byte{0x8E,0x3C,0x00,0x00,0x59,0x46} + v, err := connTest.Write(heartbeat) + log.Println("send.msg.conn.RemoteAddr",connTest.RemoteAddr()) + log.Println("send.msg.conn.v",v) + if err != nil { + log.Println("write msg error!", err) + } + return +} + //获取数据防抖配置时间 func getConf() { resp, err := http.Get("https://smartoilets.cn/socketServer/dpark/getConf") @@ -131,263 +174,6 @@ func push(c *gin.Context) { } -func dealData(data string) string{ - - nowtime := time.Now() - - log.Println("yunhorndeug.push.data|", data) - -// go pushCODE1007(data) - - alldata := strings.Split(data, "#") - - 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 < 1650 { - 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 < 1650 { - 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 "" -} - - -func pushv2(c *gin.Context){ - data := c.PostForm("d") - - // go dealDatav2(data) - - log.Println("data:", data) - c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "code": 1}) - -} - func dealDatav3(data []byte) string{ nowtime := time.Now() @@ -411,181 +197,197 @@ func dealDatav3(data []byte) string{ datatype := data[5] - cs2001 := data[6] + //dpark 1-尿布台 2-吸顶 + if datatype==1 || datatype==2{ - var R_data P1004_6 - R_data.CODE = 1008 - R_data.ID = adr - //尿布台 - R_data.VERSION = "yunhorn_2_v1" - R_data.ONLINE = true - R_data.DATA = make(map[string]interface{}) - R_data.DATA["cs2001"]=cs2001 + //过滤无效数据 adr + cs2001 := data[6] - datatypeStr := strconv.FormatInt(int64(data[5]&0xff), 16) + var R_data P1004_6 + R_data.CODE = 1008 + R_data.ID = adr + //尿布台 + R_data.VERSION = "yunhorn_2_v1" + R_data.ONLINE = true + R_data.DATA = make(map[string]interface{}) + R_data.DATA["cs2001"]=cs2001 - cs2001Str := strconv.FormatInt(int64(data[6]&0xff), 16) + datatypeStr := strconv.FormatInt(int64(data[5]&0xff), 16) - //地址 - readyData.WriteString(buffer.String()) - readyData.WriteString("#") - //类型 - readyData.WriteString(datatypeStr) - readyData.WriteString("*") - //红外人体感应 - readyData.WriteString(cs2001Str) + cs2001Str := strconv.FormatInt(int64(data[6]&0xff), 16) - newdtype := strs[5] - - tmp := []byte{0, 0} - tof := tmp[0] - - if newdtype==1{ - tof = data[7] - R_data.VERSION = "yunhorn_1_v1" - R_data.DATA["tof"]=tof - tofTmp := strconv.FormatInt(int64(data[7]&0xff), 10) + //地址 + readyData.WriteString(buffer.String()) + readyData.WriteString("#") + //类型 + readyData.WriteString(datatypeStr) readyData.WriteString("*") - readyData.WriteString(tofTmp) - } - R_data.TIMESTAMP=time.Now().Unix() + //红外人体感应 + readyData.WriteString(cs2001Str) - data_bufTmp, _ := json.Marshal(R_data) - data_buf := string(data_bufTmp) - go post_to_server(data_buf) + newdtype := strs[5] - // log.Println("readyData:",readyData.String()) + tmp := []byte{0, 0} + tof := tmp[0] -// go pushCODE1007(data) - - //默认当前无人占用 - current := 0 - - if datatype == 1 { - //尿布台类型 - if cs2001==1 || tof<130{ - //有人占用 - current = 1 + if newdtype==1{ + tof = data[7] + R_data.VERSION = "yunhorn_1_v1" + R_data.DATA["tof"]=tof + tofTmp := strconv.FormatInt(int64(data[7]&0xff), 10) + readyData.WriteString("*") + readyData.WriteString(tofTmp) } - }else if datatype==2{ - //吸顶类型 - if cs2001 ==1 { - //有人占用 - current = 1 - } - } + R_data.TIMESTAMP=time.Now().Unix() - // log.Println("loraNodeMap:",loraNodeMap) - loraNode, ok := loraNodeMap.Load(adr) - if !ok { - log.Println("load loraNode success ", ok) - return "" - } - // log.Println("load loraNode:",loraNode) + data_bufTmp, _ := json.Marshal(R_data) + data_buf := string(data_bufTmp) + go post_to_server(data_buf) - //log.Println("yunhorndeug.push.data.cuurent_data,current",loraNode.(LoraNode).CURRENT_DATA,current) - if loraNode.(LoraNode).CURRENT_DATA != current{ - //不相同,直接改变状态,更新时间 + // log.Println("readyData:",readyData.String()) - // loraNode.(LoraNode).CURRENT_DATA = current - // loraNode.(LoraNode).TIME = time.Now() - // loraNode.(LoraNode).CHECK_TIME = true + // go pushCODE1007(data) - newLoraNode := LoraNode{} - newLoraNode.CURRENT_DATA = current - newLoraNode.TIME = time.Now() - newLoraNode.CHECK_TIME=true + //默认当前无人占用 + current := 0 - newLoraNode.LOCATION_TYPE = loraNode.(LoraNode).LOCATION_TYPE - newLoraNode.LOCATION_CODE = loraNode.(LoraNode).LOCATION_CODE - newLoraNode.LOCATION_DATA = loraNode.(LoraNode).LOCATION_DATA - newLoraNode.DEVICE = loraNode.(LoraNode).DEVICE - - loraNodeMap.Store(adr,newLoraNode) - - //初次启动 - if loraNode.(LoraNode).CURRENT_DATA == -1{ - changeDataV2(newLoraNode) + if datatype == 1 { + //尿布台类型 + if cs2001==1 || tof<130{ + //有人占用 + current = 1 } + }else if datatype==2{ + //吸顶类型 + if cs2001 ==1 { + //有人占用 + current = 1 + } + } - if (datatype==2 && (extensionInTime2<=0 || extensionOutTime2<=0)){ + // log.Println("loraNodeMap:",loraNodeMap) + loraNode, ok := loraNodeMap.Load(adr) + if !ok { + log.Println("load loraNode success ", ok) + return "" + } + // log.Println("load loraNode:",loraNode) + + //log.Println("yunhorndeug.push.data.cuurent_data,current",loraNode.(LoraNode).CURRENT_DATA,current) + if loraNode.(LoraNode).CURRENT_DATA != current{ + //不相同,直接改变状态,更新时间 + + // loraNode.(LoraNode).CURRENT_DATA = current + // loraNode.(LoraNode).TIME = time.Now() + // loraNode.(LoraNode).CHECK_TIME = true + + newLoraNode := LoraNode{} newLoraNode.CURRENT_DATA = current - changeDataV2(newLoraNode) - } + newLoraNode.TIME = time.Now() + newLoraNode.CHECK_TIME=true + + newLoraNode.LOCATION_TYPE = loraNode.(LoraNode).LOCATION_TYPE + newLoraNode.LOCATION_CODE = loraNode.(LoraNode).LOCATION_CODE + newLoraNode.LOCATION_DATA = loraNode.(LoraNode).LOCATION_DATA + newLoraNode.DEVICE = loraNode.(LoraNode).DEVICE + + loraNodeMap.Store(adr,newLoraNode) + + //初次启动 + if loraNode.(LoraNode).CURRENT_DATA == -1{ + changeDataV2(newLoraNode) + } + + if (datatype==2 && (extensionInTime2<=0 || extensionOutTime2<=0)){ + newLoraNode.CURRENT_DATA = current + changeDataV2(newLoraNode) + } - }else{ - // 非首次 - if loraNode.(LoraNode).CHECK_TIME { + }else{ + // 非首次 + if loraNode.(LoraNode).CHECK_TIME { - var duration = nowtime.Sub(loraNode.(LoraNode).TIME).Seconds() - var durationAsInt64 = int(duration) + var duration = nowtime.Sub(loraNode.(LoraNode).TIME).Seconds() + var durationAsInt64 = int(duration) - log.Println("yunhorndeug.push.data.durationAsInt64:",durationAsInt64) - if current == 0 { - //类型2 吸顶 + log.Println("yunhorndeug.push.data.durationAsInt64:",durationAsInt64) + if current == 0 { + //类型2 吸顶 + if datatype==2{ + if durationAsInt64 < extensionOutTime2 && extensionOutTime2>0 { + return "" + } + }else{ + if durationAsInt64 < extensionOutTime && extensionOutTime>0 { + return "" + } + } + + }else{ + //类型2 吸顶 if datatype==2{ - if durationAsInt64 < extensionOutTime2 && extensionOutTime2>0 { + if durationAsInt64 < extensionInTime2 && extensionInTime2>0 { return "" } }else{ - if durationAsInt64 < extensionOutTime && extensionOutTime>0 { + if durationAsInt64 < extensionInTime && extensionInTime>0 { return "" } } - }else{ - //类型2 吸顶 - if datatype==2{ - if durationAsInt64 < extensionInTime2 && extensionInTime2>0 { - return "" - } - }else{ - if durationAsInt64 < extensionInTime && extensionInTime>0 { - return "" - } } - } + //改变状态 + // loraNode.(LoraNode).CHECK_TIME = false - //改变状态 - // loraNode.(LoraNode).CHECK_TIME = false - - newLoraNode := LoraNode{} - newLoraNode.CURRENT_DATA = current - newLoraNode.TIME = loraNode.(LoraNode).TIME - newLoraNode.CHECK_TIME= false - - newLoraNode.LOCATION_TYPE = loraNode.(LoraNode).LOCATION_TYPE - newLoraNode.LOCATION_CODE = loraNode.(LoraNode).LOCATION_CODE - newLoraNode.LOCATION_DATA = loraNode.(LoraNode).LOCATION_DATA - newLoraNode.DEVICE = loraNode.(LoraNode).DEVICE - loraNodeMap.Store(adr,newLoraNode) - - changeDataV2(newLoraNode) - }else{ - // loraNode.(LoraNode).CHECK_TIME = true - - newLoraNode := LoraNode{} - newLoraNode.CURRENT_DATA = loraNode.(LoraNode).CURRENT_DATA - newLoraNode.TIME = time.Now() - newLoraNode.CHECK_TIME= true - - newLoraNode.LOCATION_TYPE = loraNode.(LoraNode).LOCATION_TYPE - newLoraNode.LOCATION_CODE = loraNode.(LoraNode).LOCATION_CODE - newLoraNode.LOCATION_DATA = loraNode.(LoraNode).LOCATION_DATA - newLoraNode.DEVICE = loraNode.(LoraNode).DEVICE - - if (datatype==2 && (extensionInTime2<=0 || extensionOutTime2<=0)){ + newLoraNode := LoraNode{} newLoraNode.CURRENT_DATA = current + newLoraNode.TIME = loraNode.(LoraNode).TIME + newLoraNode.CHECK_TIME= false + + newLoraNode.LOCATION_TYPE = loraNode.(LoraNode).LOCATION_TYPE + newLoraNode.LOCATION_CODE = loraNode.(LoraNode).LOCATION_CODE + newLoraNode.LOCATION_DATA = loraNode.(LoraNode).LOCATION_DATA + newLoraNode.DEVICE = loraNode.(LoraNode).DEVICE + loraNodeMap.Store(adr,newLoraNode) + changeDataV2(newLoraNode) + }else{ + // loraNode.(LoraNode).CHECK_TIME = true + + newLoraNode := LoraNode{} + newLoraNode.CURRENT_DATA = loraNode.(LoraNode).CURRENT_DATA + newLoraNode.TIME = time.Now() + newLoraNode.CHECK_TIME= true + + newLoraNode.LOCATION_TYPE = loraNode.(LoraNode).LOCATION_TYPE + newLoraNode.LOCATION_CODE = loraNode.(LoraNode).LOCATION_CODE + newLoraNode.LOCATION_DATA = loraNode.(LoraNode).LOCATION_DATA + newLoraNode.DEVICE = loraNode.(LoraNode).DEVICE + + if (datatype==2 && (extensionInTime2<=0 || extensionOutTime2<=0)){ + newLoraNode.CURRENT_DATA = current + changeDataV2(newLoraNode) + } + + loraNodeMap.Store(adr,newLoraNode) + + } - - loraNodeMap.Store(adr,newLoraNode) - - } + + }else if datatype==3{ + //空气质量 + for _, b := range data[5:] { + s := strconv.FormatInt(int64(b&0xff), 16) + if len(s) == 1 { + buffer.WriteString("0") + } + buffer.WriteString(s) + } + log.Println("aq.data:",buffer.String()) } // log.Println("current:",current) diff --git a/tcpserver.go b/tcpserver.go index b794b32..780e227 100644 --- a/tcpserver.go +++ b/tcpserver.go @@ -16,6 +16,10 @@ import ( func handleConn(conn net.Conn) { + if connTest==nil{ + connTest = conn + } + log.Println("yunhorndeug.push.data.tcp.begin handleConn,cliendAddr:", conn.RemoteAddr()) defer conn.Close() @@ -73,51 +77,6 @@ func readConn(conn net.Conn, readChan chan<- []byte, stopChan chan<- bool) { data := make([]byte, 256) Rdata2 := make([]byte, 0) - // go func() { - // for { - // select { - // case kkdata := <-data_buf: //如果有数据,下面打印。但是有可能ch一直没数据 - // // log.Println("yunhorndeug.push.data.tcp|",kkdata) - // - // log.Println("###########################yunhorndeug.push.data.tcp.kkdata:",kkdata) - // Rdata = append(Rdata, kkdata...) - // Rdata2 = append(Rdata2,Rdata...) - // count := 0 - // for _,v := range Rdata{ - // if v==0{ - // break; - // } - // count++ - // } - // - // 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 { // data := make([]byte, config.SERVER_RECV_LEN) //data2 := make([]byte,256) @@ -182,6 +141,15 @@ func readConn(conn net.Conn, readChan chan<- []byte, stopChan chan<- bool) { continue } values = data[i+2:i+3] + }else if dataType ==3 { + //空气质量 + continueCount = 18 + end = 19 + if i+end >reqLen{ + log.Println("yunhorndeug.push.data.tcp.fq",buffer.String(),addrs,dataType) + continue + } + values = data[i+2:i+end] } log.Println("yunhorndeug.push.data.tcp:",buffer.String(),addrs,dataType,values) @@ -191,11 +159,6 @@ func readConn(conn net.Conn, readChan chan<- []byte, stopChan chan<- bool) { buffer = new(bytes.Buffer) - // var buffer bytes.Buffer - // buffer.Write(addrs) - // buffer.Write(dataType) - // buffer.Write(values) - go dealDatav3(data[i-4:i+end]) }