添加delete log 机制,添加上传指令返回结果
This commit is contained in:
parent
a2249d1102
commit
68ad6e1fee
|
@ -149,7 +149,7 @@
|
||||||
"device":"yunhorn_kgl_c16_v1_1"
|
"device":"yunhorn_kgl_c16_v1_1"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"adr":"0c0a2017",
|
"adr":"0c2d1319",
|
||||||
"location_type":"changing_desk",
|
"location_type":"changing_desk",
|
||||||
"location_code":"spao",
|
"location_code":"spao",
|
||||||
"location_data":"data12",
|
"location_data":"data12",
|
||||||
|
|
|
@ -114,7 +114,7 @@ websocketurl: ["192.168.3.216:8080", "/echo"]
|
||||||
post_to_server: "https://smartoilets.cn/ydc/statis/push" #数据收集服务器
|
post_to_server: "https://smartoilets.cn/ydc/statis/push" #数据收集服务器
|
||||||
debug: true #是否打印debug log
|
debug: true #是否打印debug log
|
||||||
openserial: false #是否采集串口数据
|
openserial: false #是否采集串口数据
|
||||||
open_tcp_server: true #是否开启TCP端口接收无线数据
|
open_tcp_server: false #是否开启TCP端口接收无线数据
|
||||||
check_serial: true
|
check_serial: true
|
||||||
dpark_location: "spaoDevs"
|
dpark_location: "spaoDevs"
|
||||||
tcp_port: 10090
|
tcp_port: 10090
|
||||||
|
@ -122,7 +122,8 @@ api_port: 10086 #本地web api port
|
||||||
pro_category: 1
|
pro_category: 1
|
||||||
syn_data: false
|
syn_data: false
|
||||||
syn_extension_Time: false
|
syn_extension_Time: false
|
||||||
record_log_path: "" #/usr/local/dpark #无线原始数据收集的log目录
|
record_log_path: "./logs/" #/usr/local/dpark #无线原始数据收集的log目录
|
||||||
|
record_log_retention_time: 10 #default 60 day
|
||||||
mqtt_open: false
|
mqtt_open: false
|
||||||
mqtt_keepAlive: 30 #mqtt心跳时间间隔 单位是s
|
mqtt_keepAlive: 30 #mqtt心跳时间间隔 单位是s
|
||||||
mqtt_timeout: 10 #mqtt超时时间 单位s
|
mqtt_timeout: 10 #mqtt超时时间 单位s
|
||||||
|
@ -132,4 +133,5 @@ mqtt_username: "dpark"
|
||||||
mqtt_password: "Jev@nwd0409"
|
mqtt_password: "Jev@nwd0409"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#2019/08/16 06:50:00 Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub
|
#2019/08/16 06:50:00 Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub
|
||||||
|
|
|
@ -41,23 +41,24 @@ type Conf struct {
|
||||||
//无线 设备
|
//无线 设备
|
||||||
WIRELESSSLAVEDEVICES []WirelessSlaveDevice `json:"wireless_slave_devices"`
|
WIRELESSSLAVEDEVICES []WirelessSlaveDevice `json:"wireless_slave_devices"`
|
||||||
//是否启动tcpserver
|
//是否启动tcpserver
|
||||||
OPENTCPSETVER bool `json:"open_tcp_server"`
|
OPENTCPSETVER bool `json:"open_tcp_server"`
|
||||||
CHECKSERIAL bool `json:"check_serial"`
|
CHECKSERIAL bool `json:"check_serial"`
|
||||||
LORANODES []LoraNode `json:"lora_nodes"`
|
LORANODES []LoraNode `json:"lora_nodes"`
|
||||||
DPARKLOCATION string `json:"dpark_location"`
|
DPARKLOCATION string `json:"dpark_location"`
|
||||||
TCPPORT int `json:"tcp_port"`
|
TCPPORT int `json:"tcp_port"`
|
||||||
APIPORT int `json:"api_port"`
|
APIPORT int `json:"api_port"`
|
||||||
PROCATEGORY int `json:"pro_category"`
|
PROCATEGORY int `json:"pro_category"`
|
||||||
SYNDATA bool `json:"syn_data"`
|
SYNDATA bool `json:"syn_data"`
|
||||||
SYNEXTENSIONTIME bool `json:"syn_extension_Time"`
|
SYNEXTENSIONTIME bool `json:"syn_extension_Time"`
|
||||||
RECORD_LOG_PATH string `json:"record_log_path"`
|
RECORD_LOG_PATH string `json:"record_log_path"`
|
||||||
MQTT_OPEN bool `json:"mqtt_open"`
|
RECORD_LOG_RETENTION_TIME int `json:"record_log_retention_time"`
|
||||||
MQTT_BROKER string `json:"mqtt_broker"`
|
MQTT_OPEN bool `json:"mqtt_open"`
|
||||||
MQTT_USERNAME string `json:"mqtt_username"`
|
MQTT_BROKER string `json:"mqtt_broker"`
|
||||||
MQTT_PASSWORD string `json:"mqtt_password"`
|
MQTT_USERNAME string `json:"mqtt_username"`
|
||||||
MQTT_KEEPALIVE int `json:"mqtt_keepAlive"`
|
MQTT_PASSWORD string `json:"mqtt_password"`
|
||||||
MQTT_TOPIC string `json:"mqtt_topic"`
|
MQTT_KEEPALIVE int `json:"mqtt_keepAlive"`
|
||||||
MQTT_TIMEOUT int `json:"mqtt_timeout"`
|
MQTT_TOPIC string `json:"mqtt_topic"`
|
||||||
|
MQTT_TIMEOUT int `json:"mqtt_timeout"`
|
||||||
}
|
}
|
||||||
|
|
||||||
//无线 下位机
|
//无线 下位机
|
||||||
|
|
71
main.go
71
main.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
@ -97,6 +98,10 @@ func main() {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
c.AddFunc("0, *, *, *, *, *", func() {
|
||||||
|
ClearLog()
|
||||||
|
})
|
||||||
|
|
||||||
// c.AddFunc("*/10, *, *, *, *, *", func() {
|
// c.AddFunc("*/10, *, *, *, *, *", func() {
|
||||||
// devices := []Device{}
|
// devices := []Device{}
|
||||||
// log.Println(string(build_data(1001,devices)))
|
// log.Println(string(build_data(1001,devices)))
|
||||||
|
@ -154,3 +159,69 @@ func check(err error) {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetAllFile(pathname string) error {
|
||||||
|
// log.Println("begin for folder ", pathname)
|
||||||
|
rd, err := ioutil.ReadDir(pathname)
|
||||||
|
|
||||||
|
sep := string(os.PathSeparator)
|
||||||
|
|
||||||
|
nowtime := time.Now()
|
||||||
|
|
||||||
|
for _, fi := range rd {
|
||||||
|
//",update time:" + fi.ModTime()
|
||||||
|
// log.Printf("file is dir %b name : %s ", fi.IsDir(), fi.Name())
|
||||||
|
|
||||||
|
var duration = nowtime.Sub(fi.ModTime()).Hours()
|
||||||
|
var durationAsInt64 = int(duration) / 24
|
||||||
|
|
||||||
|
if fi.IsDir() {
|
||||||
|
// log.Println("begin in folder", pathname+sep+fi.Name(), fi.ModTime(), durationAsInt64)
|
||||||
|
|
||||||
|
if durationAsInt64 > conf.RECORD_LOG_RETENTION_TIME {
|
||||||
|
log.Println("begin read folder ", pathname+sep+fi.Name())
|
||||||
|
rd2, err2 := ioutil.ReadDir(pathname + sep + fi.Name())
|
||||||
|
if err2 != nil {
|
||||||
|
log.Println("readir fail:", err2)
|
||||||
|
}
|
||||||
|
if len(rd2) == 0 {
|
||||||
|
err3 := os.Remove(pathname + sep + fi.Name())
|
||||||
|
if err3 != nil {
|
||||||
|
log.Println("remove folder fail", pathname+sep+fi.Name(), err3)
|
||||||
|
}
|
||||||
|
// log.Println("delete folder name : %s %d", fi.Name(), len(rd2))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
GetAllFile(pathname + fi.Name() + sep)
|
||||||
|
} else {
|
||||||
|
//RECORD_LOG_RETENTION_TIME conf
|
||||||
|
// var duration = nowtime.Sub(loraNode.(LoraNode).TIME).Seconds()
|
||||||
|
// var durationAsInt64 = int(duration)
|
||||||
|
|
||||||
|
log.Println("file name:", fi.Name(), fi.ModTime(), durationAsInt64)
|
||||||
|
|
||||||
|
if durationAsInt64 > conf.RECORD_LOG_RETENTION_TIME {
|
||||||
|
log.Println("delete file name :", fi.Name())
|
||||||
|
err3 := os.Remove(pathname + sep + fi.Name())
|
||||||
|
if err3 != nil {
|
||||||
|
log.Println("remove file fail", pathname+sep+fi.Name(), err3)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func ClearLog() {
|
||||||
|
folder := conf.RECORD_LOG_PATH
|
||||||
|
log.Println("logfile task begin ", folder)
|
||||||
|
if folder != "" && conf.RECORD_LOG_RETENTION_TIME > 0 {
|
||||||
|
err2 := os.MkdirAll(folder, os.ModePerm)
|
||||||
|
if err2 != nil {
|
||||||
|
log.Println(err2)
|
||||||
|
}
|
||||||
|
GetAllFile(folder)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
26
router.go
26
router.go
|
@ -230,8 +230,24 @@ func dealDatav3(data []byte, loragwip string) string {
|
||||||
|
|
||||||
datatype := data[5]
|
datatype := data[5]
|
||||||
|
|
||||||
//dpark 1-尿布台 2-吸顶
|
if datatype == 0 {
|
||||||
if datatype == 1 || datatype == 2 {
|
|
||||||
|
var R_data P1004_6
|
||||||
|
R_data.CODE = 1008
|
||||||
|
R_data.ID = adr
|
||||||
|
//尿布台
|
||||||
|
R_data.VERSION = "sts_hps_3ir"
|
||||||
|
R_data.ONLINE = true
|
||||||
|
R_data.DATA = make(map[string]interface{})
|
||||||
|
R_data.DATA["restart"] = data[6]
|
||||||
|
|
||||||
|
data_bufTmp, _ := json.Marshal(R_data)
|
||||||
|
data_buf := string(data_bufTmp)
|
||||||
|
go post_to_server(data_buf)
|
||||||
|
|
||||||
|
} else if datatype == 1 || datatype == 2 {
|
||||||
|
|
||||||
|
//dpark 1-尿布台 2-吸顶
|
||||||
|
|
||||||
//过滤无效数据 adr
|
//过滤无效数据 adr
|
||||||
|
|
||||||
|
@ -631,6 +647,12 @@ func get_renliu_api(c *gin.Context) {
|
||||||
|
|
||||||
var rl_data EventNotificationAlert
|
var rl_data EventNotificationAlert
|
||||||
|
|
||||||
|
var debugInt = 2
|
||||||
|
if debugInt > 1 {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": rl_data})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
err := c.Bind(&rl_data)
|
err := c.Bind(&rl_data)
|
||||||
if conf.DEBUG {
|
if conf.DEBUG {
|
||||||
log.Println(rl_data)
|
log.Println(rl_data)
|
||||||
|
|
372
tcpserver.go
372
tcpserver.go
|
@ -11,62 +11,59 @@ import (
|
||||||
// "go-study/socket/config"
|
// "go-study/socket/config"
|
||||||
"bytes"
|
"bytes"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从loragw的组中获取到范围内的所有node,给符合addr的node发送command
|
* 从loragw的组中获取到范围内的所有node,给符合addr的node发送command
|
||||||
**/
|
**/
|
||||||
|
|
||||||
func sendCommand(addr string,command int){
|
func sendCommand(addr string, command int) {
|
||||||
log.Println("begin sendCommand")
|
log.Println("begin sendCommand")
|
||||||
log.Println("yunhorn.loragw.sendCommand|",addr,command)
|
log.Println("yunhorn.loragw.sendCommand|", addr, command)
|
||||||
commands := []byte{0x01}
|
commands := []byte{0x01}
|
||||||
byteAddr,ok := sensorMap.Load(addr)
|
byteAddr, ok := sensorMap.Load(addr)
|
||||||
if !ok{
|
if !ok {
|
||||||
log.Println("send command fail,invalid addr|",addr)
|
log.Println("send command fail,invalid addr|", addr)
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
loraNode,ok := loraNodeMap.Load(addr)
|
loraNode, ok := loraNodeMap.Load(addr)
|
||||||
if !ok{
|
if !ok {
|
||||||
log.Println("send command fail,invalid addr|",addr)
|
log.Println("send command fail,invalid addr|", addr)
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
if command == 1{
|
if command == 1 {
|
||||||
commands = []byte{0x01}
|
commands = []byte{0x01}
|
||||||
}else if command == 2{
|
} else if command == 2 {
|
||||||
commands = []byte{0x02}
|
commands = []byte{0x02}
|
||||||
}else{
|
} else {
|
||||||
log.Println("send command fail,invalid command|",command)
|
log.Println("send command fail,invalid command|", command)
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// strconv.Itoa
|
// strconv.Itoa
|
||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
buffer.Write(byteAddr.([]byte))
|
buffer.Write(byteAddr.([]byte))
|
||||||
buffer.Write(commands)
|
buffer.Write(commands)
|
||||||
|
|
||||||
// log.Println("loragws ",loraNode.(LoraNode).LORA_GWS)
|
// log.Println("loragws ",loraNode.(LoraNode).LORA_GWS)
|
||||||
|
|
||||||
for _,loragwip := range loraNode.(LoraNode).LORA_GWS{
|
for _, loragwip := range loraNode.(LoraNode).LORA_GWS {
|
||||||
loraconn,ok := loragwMap.Load(loragwip)
|
loraconn, ok := loragwMap.Load(loragwip)
|
||||||
if ok{
|
if ok {
|
||||||
log.Println("yunhorn.loragw.begin push tcp command ",buffer.Bytes())
|
log.Println("yunhorn.loragw.begin push tcp command ", buffer.Bytes())
|
||||||
_, err := loraconn.(net.Conn).Write(buffer.Bytes())
|
_, err := loraconn.(net.Conn).Write(buffer.Bytes())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("yunhorn.loragw.write msg error!", err)
|
log.Println("yunhorn.loragw.write msg error!", err)
|
||||||
return;
|
return
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func handleConn(conn net.Conn) {
|
func handleConn(conn net.Conn) {
|
||||||
|
|
||||||
if connTest==nil{
|
if connTest == nil {
|
||||||
connTest = conn
|
connTest = conn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,175 +131,174 @@ func readConn(conn net.Conn, readChan chan<- []byte, stopChan chan<- bool) {
|
||||||
buffer := new(bytes.Buffer)
|
buffer := new(bytes.Buffer)
|
||||||
continueCount := 0
|
continueCount := 0
|
||||||
// log.Println("yunhorndeug.push.data.tcp.reqLen|data",reqLen,data)
|
// log.Println("yunhorndeug.push.data.tcp.reqLen|data",reqLen,data)
|
||||||
log.Println("$$$$$$$$$$$$$$$$$$$$$$$$$",data[:reqLen])
|
log.Println("$$$$$$$$$$$$$$$$$$$$$$$$$", data[:reqLen])
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
// log.Println("yunhorndeug.push.data.tcp.begin handleConn,cliendAddr:", conn.RemoteAddr())
|
// log.Println("yunhorndeug.push.data.tcp.begin handleConn,cliendAddr:", conn.RemoteAddr())
|
||||||
|
|
||||||
for i, b := range data[:reqLen] {
|
for i, b := range data[:reqLen] {
|
||||||
|
|
||||||
if reqLen<7{
|
if reqLen < 7 {
|
||||||
break
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if continueCount > 0 {
|
||||||
|
continueCount--
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if b != 254 {
|
||||||
|
s := strconv.FormatInt(int64(b&0xff), 16)
|
||||||
|
if len(s) == 1 {
|
||||||
|
buffer.WriteString("0")
|
||||||
|
}
|
||||||
|
buffer.WriteString(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
end := 0
|
||||||
|
//254为分隔符
|
||||||
|
if b == 254 && i > 3 {
|
||||||
|
addrs := data[i-4 : i]
|
||||||
|
|
||||||
|
//尿布台数据,地址1位 数据2位
|
||||||
|
dataType := data[i+1]
|
||||||
|
|
||||||
|
//0是通用类型 命令交互
|
||||||
|
if dataType != 0 && dataType != 1 && dataType != 2 && dataType != 3 && dataType != 4 {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if continueCount>0{
|
values := make([]byte, 0)
|
||||||
continueCount--
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if b!=254{
|
if dataType == 0 {
|
||||||
s := strconv.FormatInt(int64(b&0xff), 16)
|
continueCount = 2
|
||||||
if len(s) == 1 {
|
end = 3
|
||||||
buffer.WriteString("0")
|
values = data[i+2 : i+3]
|
||||||
}
|
} else if dataType == 1 {
|
||||||
buffer.WriteString(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
end :=0
|
continueCount = 3
|
||||||
//254为分隔符
|
end = 4
|
||||||
if b==254 && i>3{
|
|
||||||
addrs := data[i-4:i]
|
|
||||||
|
|
||||||
//尿布台数据,地址1位 数据2位
|
if i+4 > reqLen {
|
||||||
dataType := data[i+1]
|
log.Println("yunhorndeug.push.data.tcp.fq", buffer.String(), addrs, dataType)
|
||||||
|
|
||||||
//0是通用类型
|
|
||||||
if dataType!=0 && dataType!=1 && dataType!=2 && dataType!=3 && dataType!=4{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
values := make([]byte,0)
|
|
||||||
|
|
||||||
if dataType ==0{
|
|
||||||
continueCount = 2
|
|
||||||
end = 3
|
|
||||||
}else if dataType == 1 {
|
|
||||||
|
|
||||||
continueCount = 3
|
|
||||||
end = 4
|
|
||||||
|
|
||||||
if i+4>reqLen{
|
|
||||||
log.Println("yunhorndeug.push.data.tcp.fq",buffer.String(),addrs,dataType)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
//类型为1 数据量为2
|
|
||||||
values = data[i+2:i+4]
|
|
||||||
|
|
||||||
}else if dataType == 2{
|
|
||||||
//类型为2 数据量位1
|
|
||||||
|
|
||||||
continueCount = 2
|
|
||||||
end = 3
|
|
||||||
|
|
||||||
if i+3 >reqLen{
|
|
||||||
log.Println("yunhorndeug.push.data.tcp.fq",buffer.String(),addrs,dataType)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
values = data[i+2:i+3]
|
|
||||||
}else if dataType ==4 {
|
|
||||||
//空气质量
|
|
||||||
continueCount = 19
|
|
||||||
end = 20
|
|
||||||
if i+end >reqLen{
|
|
||||||
log.Println("yunhorndeug.push.data.tcp.fq",buffer.String(),addrs,dataType)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
values = data[i+2:i+end]
|
|
||||||
// log.Println("pm25data1",data[i+15])
|
|
||||||
// log.Println("pm25data2",data[i+16])
|
|
||||||
// nh3data := uint32(data[i+7])*256+uint32(data[i+8])
|
|
||||||
// pm25data := uint32(data[i+15])*256+uint32(data[i+16])
|
|
||||||
// log.Println("nh3data",data[i+7])
|
|
||||||
// log.Println("pm25data",pm25data)
|
|
||||||
//8E 3C 00 00 47 46
|
|
||||||
// var close []byte = []byte{0x8E,0x3C,0x00,0x00,0x43,0x46}
|
|
||||||
// var open []byte = []byte{0x8E,0x3C,0x00,0x00,0x4F,0x46}
|
|
||||||
// var writedata []byte =[]byte{}
|
|
||||||
//i+7 氨气
|
|
||||||
//i+14 tvoc 甲醛
|
|
||||||
// if pm25data>100 || data[i+7]>=3 || data[i+14]>=2 {
|
|
||||||
// writedata = open
|
|
||||||
// }else{
|
|
||||||
// writedata = close
|
|
||||||
// }
|
|
||||||
// v, err := connTest.Write(writedata)
|
|
||||||
// log.Println("send.msg.conn.RemoteAddr:",connTest.RemoteAddr(),writedata)
|
|
||||||
// log.Println("send.msg.conn.v",v)
|
|
||||||
// if err != nil {
|
|
||||||
// log.Println("write msg error!", err)
|
|
||||||
// }
|
|
||||||
}else if dataType ==3{
|
|
||||||
continueCount = 2
|
|
||||||
end = 3
|
|
||||||
|
|
||||||
if i+3 >reqLen{
|
|
||||||
log.Println("yunhorndeug.push.data.tcp.fq",buffer.String(),addrs,dataType)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
values = data[i+2:i+end]
|
|
||||||
// values = data[i+2]
|
|
||||||
|
|
||||||
//28 3C 00 00 47 46
|
|
||||||
// var green []byte = []byte{0x28,0x3C,0x00,0x00,0x47,0x46}
|
|
||||||
// var red []byte = []byte{0x28,0x3C,0x00,0x00,0x52,0x46}
|
|
||||||
// var yellow []byte = []byte{0x28,0x3C,0x00,0x00,0x59,0x46}
|
|
||||||
// var writedata []byte =[]byte{}
|
|
||||||
// writedata = green
|
|
||||||
// if values == 2{
|
|
||||||
// writedata = red
|
|
||||||
// }else if values == 1{
|
|
||||||
// writedata = green
|
|
||||||
// }else if values == 4{
|
|
||||||
// writedata = yellow
|
|
||||||
// }
|
|
||||||
// log.Println("send.msg.conn.RemoteAddr|writedata",connTest.RemoteAddr(),writedata)
|
|
||||||
// v, err := connTest.Write(writedata)
|
|
||||||
// log.Println("send.msg.conn.v",v)
|
|
||||||
// if err != nil {
|
|
||||||
// log.Println("write msg error!", err)
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println("yunhorndeug.push.data.tcp:",buffer.String(),addrs,dataType,values)
|
|
||||||
|
|
||||||
loraNode,ok := loraNodeMap.Load(buffer.String())
|
|
||||||
if !ok{
|
|
||||||
log.Println("yunhorndeug.push.data.tcp.dev not exist:",buffer.String(),dataType,values)
|
|
||||||
buffer = new(bytes.Buffer)
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if !loraNode.(LoraNode).UPDATE_TIME.IsZero() {
|
//类型为1 数据量为2
|
||||||
var duration = now.Sub(loraNode.(LoraNode).UPDATE_TIME).Seconds()
|
values = data[i+2 : i+4]
|
||||||
var durationAsInt64 = int(duration)
|
|
||||||
if(durationAsInt64<=1){
|
} else if dataType == 2 {
|
||||||
log.Println("repeat.sensor.data:",addrs)
|
//类型为2 数据量位1
|
||||||
continue;
|
|
||||||
}
|
continueCount = 2
|
||||||
|
end = 3
|
||||||
|
|
||||||
|
if i+3 > reqLen {
|
||||||
|
log.Println("yunhorndeug.push.data.tcp.fq", buffer.String(), addrs, dataType)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
values = data[i+2 : i+3]
|
||||||
|
} else if dataType == 4 {
|
||||||
|
//空气质量
|
||||||
|
continueCount = 19
|
||||||
|
end = 20
|
||||||
|
if i+end > reqLen {
|
||||||
|
log.Println("yunhorndeug.push.data.tcp.fq", buffer.String(), addrs, dataType)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
values = data[i+2 : i+end]
|
||||||
|
// log.Println("pm25data1",data[i+15])
|
||||||
|
// log.Println("pm25data2",data[i+16])
|
||||||
|
// nh3data := uint32(data[i+7])*256+uint32(data[i+8])
|
||||||
|
// pm25data := uint32(data[i+15])*256+uint32(data[i+16])
|
||||||
|
// log.Println("nh3data",data[i+7])
|
||||||
|
// log.Println("pm25data",pm25data)
|
||||||
|
//8E 3C 00 00 47 46
|
||||||
|
// var close []byte = []byte{0x8E,0x3C,0x00,0x00,0x43,0x46}
|
||||||
|
// var open []byte = []byte{0x8E,0x3C,0x00,0x00,0x4F,0x46}
|
||||||
|
// var writedata []byte =[]byte{}
|
||||||
|
//i+7 氨气
|
||||||
|
//i+14 tvoc 甲醛
|
||||||
|
// if pm25data>100 || data[i+7]>=3 || data[i+14]>=2 {
|
||||||
|
// writedata = open
|
||||||
|
// }else{
|
||||||
|
// writedata = close
|
||||||
|
// }
|
||||||
|
// v, err := connTest.Write(writedata)
|
||||||
|
// log.Println("send.msg.conn.RemoteAddr:",connTest.RemoteAddr(),writedata)
|
||||||
|
// log.Println("send.msg.conn.v",v)
|
||||||
|
// if err != nil {
|
||||||
|
// log.Println("write msg error!", err)
|
||||||
|
// }
|
||||||
|
} else if dataType == 3 {
|
||||||
|
continueCount = 2
|
||||||
|
end = 3
|
||||||
|
|
||||||
|
if i+3 > reqLen {
|
||||||
|
log.Println("yunhorndeug.push.data.tcp.fq", buffer.String(), addrs, dataType)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
sensorMap.Store(buffer.String(),addrs)
|
values = data[i+2 : i+end]
|
||||||
|
// values = data[i+2]
|
||||||
|
|
||||||
buffer = new(bytes.Buffer)
|
|
||||||
|
|
||||||
if conf.RECORD_LOG_PATH!=""{
|
|
||||||
go write_log_channel(data[i-4:i+end])
|
|
||||||
}
|
|
||||||
|
|
||||||
go dealDatav3(data[i-4:i+end],conn.RemoteAddr().String())
|
|
||||||
|
|
||||||
|
//28 3C 00 00 47 46
|
||||||
|
// var green []byte = []byte{0x28,0x3C,0x00,0x00,0x47,0x46}
|
||||||
|
// var red []byte = []byte{0x28,0x3C,0x00,0x00,0x52,0x46}
|
||||||
|
// var yellow []byte = []byte{0x28,0x3C,0x00,0x00,0x59,0x46}
|
||||||
|
// var writedata []byte =[]byte{}
|
||||||
|
// writedata = green
|
||||||
|
// if values == 2{
|
||||||
|
// writedata = red
|
||||||
|
// }else if values == 1{
|
||||||
|
// writedata = green
|
||||||
|
// }else if values == 4{
|
||||||
|
// writedata = yellow
|
||||||
|
// }
|
||||||
|
// log.Println("send.msg.conn.RemoteAddr|writedata",connTest.RemoteAddr(),writedata)
|
||||||
|
// v, err := connTest.Write(writedata)
|
||||||
|
// log.Println("send.msg.conn.v",v)
|
||||||
|
// if err != nil {
|
||||||
|
// log.Println("write msg error!", err)
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
log.Println("yunhorndeug.push.data.tcp:", buffer.String(), addrs, dataType, values)
|
||||||
|
|
||||||
// 转化为字符串
|
loraNode, ok := loraNodeMap.Load(buffer.String())
|
||||||
// log.Println("yunhorndeug.push.data.tcp.16data:",buffer.String())
|
if !ok {
|
||||||
|
log.Println("yunhorndeug.push.data.tcp.dev not exist:", buffer.String(), dataType, values)
|
||||||
|
buffer = new(bytes.Buffer)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if !loraNode.(LoraNode).UPDATE_TIME.IsZero() {
|
||||||
|
var duration = now.Sub(loraNode.(LoraNode).UPDATE_TIME).Seconds()
|
||||||
|
var durationAsInt64 = int(duration)
|
||||||
|
if durationAsInt64 <= 1 {
|
||||||
|
log.Println("repeat.sensor.data:", addrs)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sensorMap.Store(buffer.String(), addrs)
|
||||||
|
|
||||||
|
buffer = new(bytes.Buffer)
|
||||||
|
|
||||||
|
if conf.RECORD_LOG_PATH != "" {
|
||||||
|
go write_log_channel(data[i-4 : i+end])
|
||||||
|
}
|
||||||
|
|
||||||
|
go dealDatav3(data[i-4:i+end], conn.RemoteAddr().String())
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 转化为字符串
|
||||||
|
// log.Println("yunhorndeug.push.data.tcp.16data:",buffer.String())
|
||||||
|
|
||||||
Rdata2 = make([]byte, reqLen)
|
Rdata2 = make([]byte, reqLen)
|
||||||
for i,v := range data[:reqLen]{
|
for i, v := range data[:reqLen] {
|
||||||
Rdata2[i]=v
|
Rdata2[i] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
// log.Println("===========================yunhorndeug.push.data.tcp.origin.Rdata:",Rdata2)
|
// log.Println("===========================yunhorndeug.push.data.tcp.origin.Rdata:",Rdata2)
|
||||||
|
|
113
worker.go
113
worker.go
|
@ -1,16 +1,16 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"encoding/json"
|
||||||
"github.com/tarm/serial"
|
"github.com/tarm/serial"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
_ "math/rand"
|
_ "math/rand"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"encoding/json"
|
|
||||||
"time"
|
"time"
|
||||||
"bytes"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Devicedatas struct {
|
type Devicedatas struct {
|
||||||
|
@ -26,9 +26,8 @@ type Device struct {
|
||||||
DATA map[string]interface{} `json:"data"`
|
DATA map[string]interface{} `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type baseMessage struct{
|
type baseMessage struct {
|
||||||
Ip string `json:"ip"`
|
Ip string `json:"ip"`
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//处理通过tcp发送过来的数据
|
//处理通过tcp发送过来的数据
|
||||||
|
@ -84,71 +83,67 @@ func yunhorn_xdy_l_v1(data_buf []byte) {
|
||||||
}
|
}
|
||||||
|
|
||||||
//wirte data to log channel
|
//wirte data to log channel
|
||||||
func write_log_channel(data []byte){
|
func write_log_channel(data []byte) {
|
||||||
logch <- data
|
logch <- data
|
||||||
}
|
}
|
||||||
|
|
||||||
//get data from log channel and write file
|
//get data from log channel and write file
|
||||||
func record_log_channel(){
|
func record_log_channel() {
|
||||||
|
|
||||||
for {
|
for {
|
||||||
data_buf := <- logch
|
data_buf := <-logch
|
||||||
|
|
||||||
addrs := data_buf[:4]
|
addrs := data_buf[:4]
|
||||||
|
|
||||||
|
|
||||||
buffer := new(bytes.Buffer)
|
buffer := new(bytes.Buffer)
|
||||||
|
|
||||||
for i:=0;i<len(addrs);i++{
|
for i := 0; i < len(addrs); i++ {
|
||||||
s := strconv.FormatInt(int64(addrs[i]&0xff), 16)
|
s := strconv.FormatInt(int64(addrs[i]&0xff), 16)
|
||||||
buffer.WriteString(s)
|
buffer.WriteString(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
adr := buffer.String()
|
adr := buffer.String()
|
||||||
|
|
||||||
// log.Println("addrs and data",adr,data_buf)
|
// log.Println("addrs and data",adr,data_buf)
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
|
||||||
oldTime,ok := nodeUpdateMap.Load(adr);
|
oldTime, ok := nodeUpdateMap.Load(adr)
|
||||||
if(ok){
|
if ok {
|
||||||
//have data
|
//have data
|
||||||
var duration = now.Sub(oldTime.(time.Time)).Seconds()
|
var duration = now.Sub(oldTime.(time.Time)).Seconds()
|
||||||
var durationAsInt64 = int(duration)
|
var durationAsInt64 = int(duration)
|
||||||
if(durationAsInt64>1){
|
if durationAsInt64 > 1 {
|
||||||
record_log(data_buf)
|
record_log(data_buf)
|
||||||
}
|
}
|
||||||
//same data
|
//same data
|
||||||
}else{
|
} else {
|
||||||
//not data
|
//not data
|
||||||
// log.Println("save adr:",adr)
|
// log.Println("save adr:",adr)
|
||||||
nodeUpdateMap.Store(adr,now)
|
nodeUpdateMap.Store(adr, now)
|
||||||
record_log(data_buf)
|
record_log(data_buf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//打印原始数据进log
|
//打印原始数据进log
|
||||||
func record_log(data_buf []byte){
|
func record_log(data_buf []byte) {
|
||||||
time := today
|
time := today
|
||||||
folder := conf.RECORD_LOG_PATH+"/"+time
|
folder := conf.RECORD_LOG_PATH + "/" + time
|
||||||
err2 :=os.MkdirAll(folder,os.ModePerm)
|
err2 := os.MkdirAll(folder, os.ModePerm)
|
||||||
if err2!=nil{
|
if err2 != nil {
|
||||||
log.Println(err2)
|
log.Println(err2)
|
||||||
}
|
}
|
||||||
f, err := os.OpenFile(folder+"/"+time+".log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
f, err := os.OpenFile(folder+"/"+time+".log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||||
if err != nil{
|
if err != nil {
|
||||||
log.Fatalln("HHHHH")
|
log.Fatalln("HHHHH")
|
||||||
}
|
}
|
||||||
record := log.New(f,"",log.Llongfile)
|
record := log.New(f, "", log.Llongfile)
|
||||||
//debugLog.SetPrefix("[Debug]")
|
//debugLog.SetPrefix("[Debug]")
|
||||||
record.SetFlags(log.Ldate | log.Ltime)
|
record.SetFlags(log.Ldate | log.Ltime)
|
||||||
record.Println(data_buf)
|
record.Println(data_buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func yunhorn_lora_l_v1(data_buf []byte) {
|
func yunhorn_lora_l_v1(data_buf []byte) {
|
||||||
|
|
||||||
// for _, i := range conf.RS485DEVICES {
|
// for _, i := range conf.RS485DEVICES {
|
||||||
|
@ -183,12 +178,12 @@ func yunhorn_lora_l_v1(data_buf []byte) {
|
||||||
}
|
}
|
||||||
|
|
||||||
//发送心跳包
|
//发送心跳包
|
||||||
func yunhorn_hb_dpark_v1(){
|
func yunhorn_hb_dpark_v1() {
|
||||||
|
|
||||||
log.Println("begin yunhorn_hb_dpark_v1")
|
log.Println("begin yunhorn_hb_dpark_v1")
|
||||||
|
|
||||||
// var datalist [len(conf.LORANODES)]LoraNode
|
// var datalist [len(conf.LORANODES)]LoraNode
|
||||||
datalist := make([]LoraNode,len(conf.LORANODES))
|
datalist := make([]LoraNode, len(conf.LORANODES))
|
||||||
|
|
||||||
var R_data P1004_6
|
var R_data P1004_6
|
||||||
//心跳数据
|
//心跳数据
|
||||||
|
@ -199,25 +194,25 @@ func yunhorn_hb_dpark_v1(){
|
||||||
R_data.VERSION = "yunhorn_hb_dpark"
|
R_data.VERSION = "yunhorn_hb_dpark"
|
||||||
R_data.ONLINE = true
|
R_data.ONLINE = true
|
||||||
R_data.DATA = make(map[string]interface{})
|
R_data.DATA = make(map[string]interface{})
|
||||||
R_data.DATA["data"]=datalist
|
R_data.DATA["data"] = datalist
|
||||||
|
|
||||||
for i,v := range conf.LORANODES {
|
for i, v := range conf.LORANODES {
|
||||||
loraNode,ok :=loraNodeMap.Load(v.ADR)
|
loraNode, ok := loraNodeMap.Load(v.ADR)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Println("hb load loraNode fail ", ok)
|
log.Println("hb load loraNode fail ", ok)
|
||||||
continue;
|
continue
|
||||||
}
|
}
|
||||||
newLoraNode := LoraNode{}
|
newLoraNode := LoraNode{}
|
||||||
newLoraNode.CURRENT_DATA = loraNode.(LoraNode).CURRENT_DATA
|
newLoraNode.CURRENT_DATA = loraNode.(LoraNode).CURRENT_DATA
|
||||||
newLoraNode.TIME = loraNode.(LoraNode).TIME
|
newLoraNode.TIME = loraNode.(LoraNode).TIME
|
||||||
|
|
||||||
newLoraNode.LOCATION_TYPE = ""
|
newLoraNode.LOCATION_TYPE = ""
|
||||||
newLoraNode.LOCATION_CODE = ""
|
newLoraNode.LOCATION_CODE = ""
|
||||||
newLoraNode.LOCATION_DATA = loraNode.(LoraNode).LOCATION_DATA
|
newLoraNode.LOCATION_DATA = loraNode.(LoraNode).LOCATION_DATA
|
||||||
newLoraNode.ADR = loraNode.(LoraNode).ADR
|
newLoraNode.ADR = loraNode.(LoraNode).ADR
|
||||||
datalist[i]=newLoraNode
|
datalist[i] = newLoraNode
|
||||||
}
|
}
|
||||||
R_data.TIMESTAMP=time.Now().Unix()
|
R_data.TIMESTAMP = time.Now().Unix()
|
||||||
|
|
||||||
data_bufTmp, _ := json.Marshal(R_data)
|
data_bufTmp, _ := json.Marshal(R_data)
|
||||||
data_buf := string(data_bufTmp)
|
data_buf := string(data_bufTmp)
|
||||||
|
@ -254,7 +249,6 @@ func yunhorn_ys_l_v1(data_buf []byte) {
|
||||||
demoDevice.DATA["data"+strconv.Itoa(2)] = 0
|
demoDevice.DATA["data"+strconv.Itoa(2)] = 0
|
||||||
demoDevice.DATA["data"+strconv.Itoa(3)] = 0
|
demoDevice.DATA["data"+strconv.Itoa(3)] = 0
|
||||||
|
|
||||||
|
|
||||||
if data_int0[0] == 0 && data_int1[0] == 0 && data_type[0] == 0 {
|
if data_int0[0] == 0 && data_int1[0] == 0 && data_type[0] == 0 {
|
||||||
demoDevice.DATA["data"+strconv.Itoa(8)] = data_int[0]
|
demoDevice.DATA["data"+strconv.Itoa(8)] = data_int[0]
|
||||||
var device Device
|
var device Device
|
||||||
|
@ -267,9 +261,9 @@ func yunhorn_ys_l_v1(data_buf []byte) {
|
||||||
device.Unlock()
|
device.Unlock()
|
||||||
DEVICEDATAS.DATA[demoDevice.VERSION+"_1"].DATA["data8"] = data_int[0]
|
DEVICEDATAS.DATA[demoDevice.VERSION+"_1"].DATA["data8"] = data_int[0]
|
||||||
} else {
|
} else {
|
||||||
if data_int[0] == 0{
|
if data_int[0] == 0 {
|
||||||
demoDevice.DATA["data"+strconv.Itoa(7)] = 1
|
demoDevice.DATA["data"+strconv.Itoa(7)] = 1
|
||||||
}else{
|
} else {
|
||||||
demoDevice.DATA["data"+strconv.Itoa(7)] = 0
|
demoDevice.DATA["data"+strconv.Itoa(7)] = 0
|
||||||
}
|
}
|
||||||
var device Device
|
var device Device
|
||||||
|
@ -279,19 +273,18 @@ func yunhorn_ys_l_v1(data_buf []byte) {
|
||||||
device.ONLINE = demoDevice.ONLINE
|
device.ONLINE = demoDevice.ONLINE
|
||||||
device.DATA = make(map[string]interface{})
|
device.DATA = make(map[string]interface{})
|
||||||
|
|
||||||
|
if data_int[0] == 0 {
|
||||||
if data_int[0] == 0{
|
device.DATA["data7"] = 1
|
||||||
device.DATA["data7"] = 1
|
} else {
|
||||||
}else{
|
device.DATA["data7"] = 0
|
||||||
device.DATA["data7"] = 0
|
}
|
||||||
}
|
|
||||||
|
|
||||||
//device.DATA["data7"] = data_int[0]
|
//device.DATA["data7"] = data_int[0]
|
||||||
device.Unlock()
|
device.Unlock()
|
||||||
|
|
||||||
DEVICEDATAS.DATA[demoDevice.VERSION+"_1"].DATA["data7"] = data_int[0]
|
DEVICEDATAS.DATA[demoDevice.VERSION+"_1"].DATA["data7"] = data_int[0]
|
||||||
|
|
||||||
// DEVICEDATAS.DATA[demoDevice.VERSION+"_1"].DATA["data7"] = device.DATA["data7"]
|
// DEVICEDATAS.DATA[demoDevice.VERSION+"_1"].DATA["data7"] = device.DATA["data7"]
|
||||||
|
|
||||||
}
|
}
|
||||||
device.DATA = demoDevice.DATA
|
device.DATA = demoDevice.DATA
|
||||||
|
|
Loading…
Reference in New Issue