From cca97cc6945ffd6e0d75c7a635d9a9d20b2c94b8 Mon Sep 17 00:00:00 2001 From: fish Date: Fri, 26 Apr 2019 17:02:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84tcp=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=8E=A5=E6=94=B6=E4=BB=A5=E5=8F=8A=E6=9B=B4=E6=96=B0=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/gateway.conf | 60 +++++-- loadconfig.go | 22 +++ router.go | 401 +++++++++++++++++----------------------------- tcpserver.go | 23 +-- 4 files changed, 233 insertions(+), 273 deletions(-) diff --git a/conf/gateway.conf b/conf/gateway.conf index c2573a1..7c0f7fc 100644 --- a/conf/gateway.conf +++ b/conf/gateway.conf @@ -5,19 +5,15 @@ "location": [113.960347, 22.54143], "单位": "深圳市云兴科技", "config": [ - { - "id": "8E146028-E1B1-4AC9-8F47-01376A22CB35", - "version": "yunhorn_kgl_c8_v1", - "data": [ - ["女厕位", 1] - ] - }, { "id": "7BB22E6A-ED0D-4161-8370-6B722D3683B7", - "version": "yunhorn_kgl_c8_v1", + "version": "yunhorn_kgl_c16_v1", "data": [ ["男厕位", 1], - ["男厕位", 2] + ["男厕位", 2], + ["女厕位", 1], + ["女厕位", 2], + ["男厕位", 5] ] } ] @@ -48,11 +44,53 @@ "id":"018B5704-55BE-40CA-9D3E-90EF157C2489" } ], + "lora_nodes":[ + { + "adr":"0c521618", + "location_type":"changing_desk", + "location_code":"spao", + "location_data":"data1", + "current_data":0, + "device":"yunhorn_kgl_c16_v1_1" + }, + { + "adr":"0c3b4f14", + "location_type":"changing_desk", + "location_code":"spao", + "location_data":"data2", + "current_data":0, + "device":"yunhorn_kgl_c16_v1_1" + }, + { + "adr":"0c451d14", + "location_type":"changing_desk", + "location_code":"spao", + "location_data":"data3", + "current_data":0, + "device":"yunhorn_kgl_c16_v1_1" + }, + { + "adr":"0c471714", + "location_type":"changing_desk", + "location_code":"spao", + "location_data":"data4", + "current_data":0, + "device":"yunhorn_kgl_c16_v1_1" + }, + { + "adr":"0c093e17", + "location_type":"changing_desk", + "location_code":"spao", + "location_data":"data5", + "current_data":0, + "device":"yunhorn_kgl_c16_v1_1" + } + ], "devices_list": [ ["4E5D52DD-EB81-43C0-A2B6-04E432412EBA", "loraport", "yunhorn_kgl_l_v1", "1"], ["deb03e28-ddcb-4e5d-aa31-d870a1f0f09b", "loraport", "yunhorn_kgl_c8_v1", "1"], - ["7BB22E6A-ED0D-4161-8370-6B722D3683B7", "loraport", "yunhorn_kgl_c8_v1", "2"], - ["8E146028-E1B1-4AC9-8F47-01376A22CB35", "loraport", "yunhorn_kgl_c8_v1", "3"] + ["7BB22E6A-ED0D-4161-8370-6B722D3683B7", "loraport", "yunhorn_kgl_c16_v1", "1"], + ["8E146028-E1B1-4AC9-8F47-01376A22CB35", "loraport", "yunhorn_kgl_c8_v1", "2"] ], "serialports_list": { "loraport": { diff --git a/loadconfig.go b/loadconfig.go index e93df7f..43a28e5 100644 --- a/loadconfig.go +++ b/loadconfig.go @@ -8,6 +8,7 @@ import ( "os" "strconv" "time" + "sync" ) type Traffic struct { @@ -29,6 +30,7 @@ type Conf struct { DEBUG bool `json:"debug"` OPENSERIAL bool `json:"openserial"` WIRELESSSLAVEDEVICES []WirelessSlaveDevice `json:"wireless_slave_devices"` + LORANODES []LoraNode `json:"lora_nodes"` } //无线 下位机 @@ -71,6 +73,17 @@ type Serialconf struct { NOTE string `json:"note"` } +type LoraNode struct{ + ADR string `json:"adr"` + LOCATION_TYPE string `json:"location_type"` + LOCATION_CODE string `json:"location_code"` + LOCATION_DATA string `json:"location_data"` + TIME time.Time `json:"time"` + CURRENT_DATA int `json:"current_data"` + CHECK_TIME bool `json:"check_time"` + DEVICE string `json:"device"` +} + var conf Conf var DEVICEDATAS Devicedatas var demoDevice Device @@ -80,6 +93,8 @@ var maledemoDevice Device var db, _ = sql.Open("sqlite3", "./db/gateway.db") +var loraNodeMap sync.Map + // 加载配置文件 func init() { @@ -99,6 +114,13 @@ func init() { // log.Println(i.ID) // } + for _,v := range conf.LORANODES { + v.CURRENT_DATA = -1 + // log.Println("&&&&&&&&&&&&&&&&&&&",v) + v.TIME = time.Now() + loraNodeMap.Store(v.ADR,v) + } + DEVICEDATAS.Lock() DEVICEDATAS.DATA = make(map[string]Device) //初始化设备,目前conf.DEVICES_LIST diff --git a/router.go b/router.go index cfe6e3e..09bfb9e 100644 --- a/router.go +++ b/router.go @@ -371,291 +371,175 @@ func dealData(data string) string{ return "" } + func pushv2(c *gin.Context){ data := c.PostForm("d") - go dealDatav2(data) + // go dealDatav2(data) log.Println("data:", data) c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "code": 1}) -}z +} -func dealDatav2(data string) string{ +func dealDatav3(data []byte) string{ nowtime := time.Now() log.Println("yunhorndeug.push.data|", data) - strs := []byte(data) + strs := data - addr := string(strs[:3]) + buffer := new(bytes.Buffer) + for _, b := range data[:4] { + s := strconv.FormatInt(int64(b&0xff), 16) + if len(s) == 1 { + buffer.WriteString("0") + } + buffer.WriteString(s) + } + + adr := buffer.String() var readyData bytes.Buffer + + datatype := data[5] + + cs2001 := data[6] + + datatypeStr := strconv.FormatInt(int64(data[5]&0xff), 16) + + cs2001Str := strconv.FormatInt(int64(data[6]&0xff), 16) + //地址 - readyData.WriteString(addr) + readyData.WriteString(buffer.String()) readyData.WriteString("#") //类型 - readyData.WriteString(string(strs[3:4])) + readyData.WriteString(datatypeStr) readyData.WriteString("*") //红外人体感应 - readyData.WriteString(string(strs[4:5])) + readyData.WriteString(cs2001Str) - newdtype,err :=strconv.Atoi(string(strs[3:4])) - if err!=nil{ - log.Println("type parse err",err) - return "" - } + newdtype := strs[5] + + tmp := []byte{0, 0} + tof := tmp[0] if newdtype==1{ + tof = data[7] + tofTmp := strconv.FormatInt(int64(data[7]&0xff), 10) readyData.WriteString("*") - readyData.WriteString(string(strs[5:6])) + readyData.WriteString(tofTmp) } - - for i,v := range strs{ - log.Println("strs,i,v",i,v-48) - } - - log.Println("readyData:",readyData.String()) + // log.Println("readyData:",readyData.String()) // go pushCODE1007(data) - alldata := strings.Split(readyData.String(), "#") + //默认当前无人占用 + current := 0 - 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 "" + if datatype == 1 { + //尿布台类型 + if cs2001==1 || tof<130{ + //有人占用 + current = 1 } - - 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) - } - } - } + }else if datatype==2{ + //吸顶类型 + if cs2001 ==1 { + //有人占用 + current = 1 } } + + // 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 + 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) + } + + + }else{ + // 非首次 + if loraNode.(LoraNode).CHECK_TIME { + + var duration = nowtime.Sub(loraNode.(LoraNode).TIME).Seconds() + var durationAsInt64 = int(duration) + + log.Println("yunhorndeug.push.data.durationAsInt64:",durationAsInt64) + if current == 0 { + if durationAsInt64 < extensionOutTime { + return "" + } + }else{ + if durationAsInt64 < extensionInTime { + return "" + } + } + + //改变状态 + // 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 + + loraNodeMap.Store(adr,newLoraNode) + } + } + + // log.Println("current:",current) + // log.Println("loraNode:",loraNode) + // log.Println("loraNode.time.is nil",(loraNode==nil)) + return "" } @@ -680,6 +564,17 @@ func changeData(num int, device Device) { DEVICEDATAS.DATA[demoDevice.VERSION+"_"+strconv.Itoa(num)] = device } +func changeDataV2(loraNode LoraNode){ + // log.Println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@") + // log.Println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@00000:",loraNode) + // log.Println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@11111:",DEVICEDATAS.DATA[loraNode.DEVICE]) + DEVICEDATAS.RLock() + DEVICEDATAS.DATA[loraNode.DEVICE].DATA[loraNode.LOCATION_DATA] = loraNode.CURRENT_DATA + DEVICEDATAS.RUnlock() + log.Println("yunhorndeug.push.data.@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@22222:",DEVICEDATAS.DATA[loraNode.DEVICE]) + // log.Println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@") +} + type EventNotificationAlert struct { IPADDRESS string `xml:"ipAddress" json:"ipAddress"` PROTOCOLTYPE string `xml:"protocolType" json:"protocolType"` @@ -873,9 +768,9 @@ func seat_api(c *gin.Context) { // log.Println("data:", DEVICEDATAS.DATA[version+"_"+num].DATA) //} - log.Println("============================devicedatas.data:", DEVICEDATAS.DATA[version+"_"+num].DATA) + // log.Println("============================devicedatas.data:", DEVICEDATAS.DATA[version+"_"+num].DATA) if DEVICEDATAS.DATA[version+"_"+num].DATA["data"+strconv.Itoa(k+1)] != nil { - log.Println("==========================do.data:", DEVICEDATAS.DATA[version+"_"+num].DATA["data"+strconv.Itoa(k+1)]) + // log.Println("==========================do.data:", DEVICEDATAS.DATA[version+"_"+num].DATA["data"+strconv.Itoa(k+1)]) j = append(j, DEVICEDATAS.DATA[version+"_"+num].DATA["data"+strconv.Itoa(k+1)]) } } diff --git a/tcpserver.go b/tcpserver.go index 2593653..e0d4e45 100644 --- a/tcpserver.go +++ b/tcpserver.go @@ -129,9 +129,13 @@ func readConn(conn net.Conn, readChan chan<- []byte, stopChan chan<- bool) { buffer := new(bytes.Buffer) continueCount := 0 - log.Println("yunhorndeug.push.data.tcp.reqLen",reqLen) + // log.Println("yunhorndeug.push.data.tcp.reqLen",reqLen) for i, b := range data[:reqLen] { + if reqLen<7{ + break + } + if continueCount>0{ continueCount-- continue; @@ -162,23 +166,24 @@ func readConn(conn net.Conn, readChan chan<- []byte, stopChan chan<- bool) { 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) + + log.Println("yunhorndeug.push.data.tcp:",buffer.String(),addrs,dataType,values) + // 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]) - + go dealDatav3(data[i-4:i+end]) + } } - - // 转化为字符串 - log.Println("yunhorndeug.push.data.tcp.16data:",buffer.String()) + // log.Println("yunhorndeug.push.data.tcp.16data:",buffer.String()) Rdata2 = make([]byte, reqLen) for i,v := range data[:reqLen]{