From 8f7f1762574086d4c037b148d5d981d71bf28e6b Mon Sep 17 00:00:00 2001
From: fish <gcslyp@gmail.com>
Date: Mon, 6 May 2019 21:03:40 +0800
Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=90=8C=E6=AD=A5=E6=9C=BA?=
 =?UTF-8?q?=E5=88=B6=201min=E4=B8=80=E6=AC=A1?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 loadconfig.go | 29 +++--------------------------
 main.go       | 13 +++++++++----
 tcpserver.go  | 12 +-----------
 worker.go     | 44 +++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 56 insertions(+), 42 deletions(-)

diff --git a/loadconfig.go b/loadconfig.go
index 0e3a927..86759ce 100644
--- a/loadconfig.go
+++ b/loadconfig.go
@@ -116,30 +116,7 @@ func init() {
 	check(err)
 	var device Device
 
-	//var serialPort = make(map[string]int)
-	//for k, i := range conf.SERIALPORTS_LIST {
-	//	log.Println(k)
-	//	log.Println(i)
-	//	if v, c := serialPort[k]; c {
-	//		log.Println("repeatd serial port:", v)
-	//		os.Exit(3)
-	//	} else {
-	//		serialPort[k] = 0
-	//	}
-	//}
-
-	//log.Println("begin print serialPort")
-	//for k, i := range serialPort {
-	//	log.Println(k)
-	//	log.Println(i)
-	//}
-
-	log.Println("slaveDevices:", conf.WIRELESSSLAVEDEVICES)
-
-	// for _,i := range conf.WIRELESSSLAVEDEVICES{
-	// log.Println(conf.WIRELESSSLAVEDEVICES[k].ID)
-	// log.Println(i.ID)
-	// }
+	// log.Println("slaveDevices:", conf.WIRELESSSLAVEDEVICES)
 
 	for _,v := range conf.LORANODES {
 			v.CURRENT_DATA = -1
@@ -217,9 +194,9 @@ func init() {
 	maledemoDevice.DATA["data2"] = 0
 	//3是男厕
 	DEVICEDATAS.DATA[demoDevice.VERSION+"_2"] = maledemoDevice
-	
 
-	dbdata_init()
+
+	go dbdata_init()
 
 }
 
diff --git a/main.go b/main.go
index 5788afe..0142154 100644
--- a/main.go
+++ b/main.go
@@ -15,12 +15,17 @@ func main() {
 	go serial_run()
 	c := cron.New()
 
-	send_data(CODE1001)
-	//获取数据防抖处理时间
+	// send_data(CODE1001)
+	//获取数据防抖处理时间配置
 	go getConf()
-	
+
+	// c.AddFunc("0, *, *, *, *, *", func() {
+		// send_data(CODE1001)
+	// })
+
+//一分钟一次 同步数据
 	c.AddFunc("0, *, *, *, *, *", func() {
-		send_data(CODE1001)
+		 yunhorn_hb_dpark_v1()
 	})
 
 	c.AddFunc("*/10, *, *, *, *, *", func() {
diff --git a/tcpserver.go b/tcpserver.go
index 780e227..cc4f9a6 100644
--- a/tcpserver.go
+++ b/tcpserver.go
@@ -71,15 +71,10 @@ func handleConn(conn net.Conn) {
 
 func readConn(conn net.Conn, readChan chan<- []byte, stopChan chan<- bool) {
 
-	// data_buf := make(chan []byte)
-
-	// Rdata := make([]byte, 0)
 	data := make([]byte, 256)
 	Rdata2 := make([]byte, 0)
 
 	for {
-		// data := make([]byte, config.SERVER_RECV_LEN)
-		//data2 := make([]byte,256)
 		reqLen, err := conn.Read(data)
 		if err != nil {
 			log.Println(err)
@@ -108,10 +103,10 @@ func readConn(conn net.Conn, readChan chan<- []byte, stopChan chan<- bool) {
         buffer.WriteString(s)
 
 				// log.Println(b)
-				//分隔符
 				// log.Println("$$$$$$$$$$$$$$$$$$$$$$$$$",i,reqLen)
 				// log.Println("$$$$$$$$$$$$$$$$$$$$$$$$$",data[reqLen])
 				end :=0
+				//254为分隔符
 				if b==254 && i>3{
 					addrs := data[i-4:i]
 					//尿布台数据,地址1位 数据2位
@@ -186,11 +181,6 @@ func readConn(conn net.Conn, readChan chan<- []byte, stopChan chan<- bool) {
 	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) {
 	for {
 		strData := <-writeChan
diff --git a/worker.go b/worker.go
index f98c79c..62d3e2d 100644
--- a/worker.go
+++ b/worker.go
@@ -7,6 +7,8 @@ import (
 	_ "math/rand"
 	"strconv"
 	"sync"
+	"encoding/json"
+	"time"
 )
 
 type Devicedatas struct {
@@ -105,7 +107,47 @@ func yunhorn_lora_l_v1(data_buf []byte) {
 	if conf.DEBUG {
 		log.Println(device)
 	}
-	// send_data(1005, device)
+}
+
+func yunhorn_hb_dpark_v1(){
+
+	log.Println("begin yunhorn_hb_dpark_v1")
+
+	// var datalist [len(conf.LORANODES)]LoraNode
+ datalist := make([]LoraNode,len(conf.LORANODES))
+
+	var R_data P1004_6
+	//心跳数据
+	R_data.CODE = 1009
+	//spao点的公厕
+	R_data.ID = "spaoDevs"
+	//尿布台
+	R_data.VERSION = "yunhorn_hb_dpark"
+	R_data.ONLINE = true
+	R_data.DATA = make(map[string]interface{})
+	R_data.DATA["data"]=datalist
+
+	for i,v := range conf.LORANODES {
+			loraNode,ok :=loraNodeMap.Load(v.ADR)
+			if !ok {
+				log.Println("hb load loraNode fail ", ok)
+				continue;
+			}
+			newLoraNode := LoraNode{}
+			newLoraNode.CURRENT_DATA = loraNode.(LoraNode).CURRENT_DATA
+			newLoraNode.TIME = loraNode.(LoraNode).TIME
+
+			newLoraNode.LOCATION_TYPE = ""
+			newLoraNode.LOCATION_CODE = ""
+			newLoraNode.LOCATION_DATA = loraNode.(LoraNode).LOCATION_DATA
+			newLoraNode.ADR = loraNode.(LoraNode).ADR
+			datalist[i]=newLoraNode
+	}
+	R_data.TIMESTAMP=time.Now().Unix()
+
+	data_bufTmp, _ := json.Marshal(R_data)
+	data_buf := string(data_bufTmp)
+	go post_to_server(data_buf)
 
 }