update record_log
This commit is contained in:
parent
75177c54a9
commit
9a94dddb26
|
@ -106,10 +106,16 @@ var today string
|
||||||
//var femaledemoDevice Device
|
//var femaledemoDevice Device
|
||||||
//var maledemoDevice Device
|
//var maledemoDevice Device
|
||||||
|
|
||||||
|
var nodeUpdateMap sync.Map
|
||||||
|
var logch = make(chan []byte)
|
||||||
|
|
||||||
|
//log channel
|
||||||
|
var logch = make(chan []byte)
|
||||||
|
|
||||||
|
var loraNodeMap sync.Map
|
||||||
|
|
||||||
var db, _ = sql.Open("sqlite3", "./db/gateway.db")
|
var db, _ = sql.Open("sqlite3", "./db/gateway.db")
|
||||||
|
|
||||||
var loraNodeMap sync.Map
|
|
||||||
|
|
||||||
// 加载配置文件
|
// 加载配置文件
|
||||||
func init() {
|
func init() {
|
||||||
|
|
9
main.go
9
main.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
@ -16,7 +17,6 @@ func main() {
|
||||||
go serial_run()
|
go serial_run()
|
||||||
c := cron.New()
|
c := cron.New()
|
||||||
|
|
||||||
|
|
||||||
//获取数据防抖处理时间配置
|
//获取数据防抖处理时间配置
|
||||||
if conf.SYNEXTENSIONTIME {
|
if conf.SYNEXTENSIONTIME {
|
||||||
go getConf()
|
go getConf()
|
||||||
|
@ -52,9 +52,16 @@ func main() {
|
||||||
c.AddFunc("0, */10, *, *, *, *", savedata_cron)
|
c.AddFunc("0, */10, *, *, *, *", savedata_cron)
|
||||||
c.AddFunc("0, 0, 0, *, *, *", insertdata)
|
c.AddFunc("0, 0, 0, *, *, *", insertdata)
|
||||||
c.AddFunc("0, 0, *, *, *, *", get_weather)
|
c.AddFunc("0, 0, *, *, *, *", get_weather)
|
||||||
|
c.AddFunc("0, *, *, *, *, *", func(){
|
||||||
|
t := time.Now()
|
||||||
|
today = t.Format("2006-01-02")
|
||||||
|
})
|
||||||
c.Start()
|
c.Start()
|
||||||
|
|
||||||
if conf.OPENTCPSETVER {
|
if conf.OPENTCPSETVER {
|
||||||
|
|
||||||
|
go record_log_channel()
|
||||||
|
|
||||||
tcp_port := strconv.Itoa(conf.TCPPORT)
|
tcp_port := strconv.Itoa(conf.TCPPORT)
|
||||||
|
|
||||||
listener, err := net.Listen("tcp", "0.0.0.0:"+tcp_port)
|
listener, err := net.Listen("tcp", "0.0.0.0:"+tcp_port)
|
||||||
|
|
|
@ -58,6 +58,8 @@ var dparkConf DparkConf
|
||||||
var connTest net.Conn
|
var connTest net.Conn
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//获取数据防抖配置时间
|
//获取数据防抖配置时间
|
||||||
func getConf() {
|
func getConf() {
|
||||||
resp, err := http.Get("https://smartoilets.cn/socketServer/dpark/getConf")
|
resp, err := http.Get("https://smartoilets.cn/socketServer/dpark/getConf")
|
||||||
|
@ -94,7 +96,7 @@ func getConf() {
|
||||||
if dparkConf.ExtensionOutTime < 0 {
|
if dparkConf.ExtensionOutTime < 0 {
|
||||||
dparkConf.ExtensionOutTime = 0
|
dparkConf.ExtensionOutTime = 0
|
||||||
}
|
}
|
||||||
|
// time.
|
||||||
extensionInTime = dparkConf.ExtensionInTime
|
extensionInTime = dparkConf.ExtensionInTime
|
||||||
extensionOutTime = dparkConf.ExtensionOutTime
|
extensionOutTime = dparkConf.ExtensionOutTime
|
||||||
|
|
||||||
|
@ -102,8 +104,8 @@ func getConf() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//距离有效判断
|
//距离有效判断time.
|
||||||
var reactTof = 1650
|
var reactTof = 1650time.
|
||||||
|
|
||||||
//数据防抖 进入时间
|
//数据防抖 进入时间
|
||||||
var extensionInTime = 6
|
var extensionInTime = 6
|
||||||
|
|
|
@ -117,7 +117,9 @@ func readConn(conn net.Conn, readChan chan<- []byte, stopChan chan<- bool) {
|
||||||
//尿布台数据,地址1位 数据2位
|
//尿布台数据,地址1位 数据2位
|
||||||
dataType := data[i+1]
|
dataType := data[i+1]
|
||||||
values := make([]byte,0)
|
values := make([]byte,0)
|
||||||
if data[i+1] == 1 {
|
|
||||||
|
|
||||||
|
if dataType == 1 {
|
||||||
|
|
||||||
continueCount = 3
|
continueCount = 3
|
||||||
end = 4
|
end = 4
|
||||||
|
@ -208,7 +210,6 @@ func readConn(conn net.Conn, readChan chan<- []byte, stopChan chan<- bool) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("write msg error!", err)
|
log.Println("write msg error!", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("yunhorndeug.push.data.tcp:",buffer.String(),addrs,dataType,values)
|
log.Println("yunhorndeug.push.data.tcp:",buffer.String(),addrs,dataType,values)
|
||||||
|
@ -222,7 +223,7 @@ func readConn(conn net.Conn, readChan chan<- []byte, stopChan chan<- bool) {
|
||||||
|
|
||||||
buffer = new(bytes.Buffer)
|
buffer = new(bytes.Buffer)
|
||||||
|
|
||||||
go record_log(data[i-4:i+end])
|
go write_log_channel(data[i-4:i+end])
|
||||||
|
|
||||||
go dealDatav3(data[i-4:i+end])
|
go dealDatav3(data[i-4:i+end])
|
||||||
|
|
||||||
|
|
57
worker.go
57
worker.go
|
@ -10,6 +10,8 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"time"
|
"time"
|
||||||
|
"strconv"
|
||||||
|
"bytes"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Devicedatas struct {
|
type Devicedatas struct {
|
||||||
|
@ -77,18 +79,64 @@ func yunhorn_xdy_l_v1(data_buf []byte) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//wirte data to log channel
|
||||||
|
func write_log_channel(data []byte){
|
||||||
|
logch <- data
|
||||||
|
}
|
||||||
|
|
||||||
|
//get data from log channel and write file
|
||||||
|
func record_log_channel(){
|
||||||
|
|
||||||
|
for {
|
||||||
|
data_buf := <- logch
|
||||||
|
|
||||||
|
addrs := data_buf[:4]
|
||||||
|
|
||||||
|
|
||||||
|
buffer := new(bytes.Buffer)
|
||||||
|
|
||||||
|
for i:=0;i<len(addrs);i++{
|
||||||
|
s := strconv.FormatInt(int64(addrs[i]&0xff), 16)
|
||||||
|
buffer.WriteString(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
adr := buffer.String()
|
||||||
|
|
||||||
|
// log.Println("addrs and data",adr,data_buf)
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
|
oldTime,ok := nodeUpdateMap.Load(adr);
|
||||||
|
if(ok){
|
||||||
|
//have data
|
||||||
|
var duration = now.Sub(oldTime.(time.Time)).Seconds()
|
||||||
|
var durationAsInt64 = int(duration)
|
||||||
|
if(durationAsInt64>1){
|
||||||
|
record_log(data_buf)
|
||||||
|
}
|
||||||
|
//same data
|
||||||
|
}else{
|
||||||
|
//not data
|
||||||
|
// log.Println("save adr:",adr)
|
||||||
|
nodeUpdateMap.Store(adr,now)
|
||||||
|
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, 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]")
|
||||||
|
@ -96,6 +144,7 @@ func record_log(data_buf []byte){
|
||||||
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 {
|
||||||
|
|
Loading…
Reference in New Issue