添加delete log 机制,添加上传指令返回结果
This commit is contained in:
parent
a2249d1102
commit
68ad6e1fee
|
@ -149,7 +149,7 @@
|
|||
"device":"yunhorn_kgl_c16_v1_1"
|
||||
},
|
||||
{
|
||||
"adr":"0c0a2017",
|
||||
"adr":"0c2d1319",
|
||||
"location_type":"changing_desk",
|
||||
"location_code":"spao",
|
||||
"location_data":"data12",
|
||||
|
|
|
@ -114,7 +114,7 @@ websocketurl: ["192.168.3.216:8080", "/echo"]
|
|||
post_to_server: "https://smartoilets.cn/ydc/statis/push" #数据收集服务器
|
||||
debug: true #是否打印debug log
|
||||
openserial: false #是否采集串口数据
|
||||
open_tcp_server: true #是否开启TCP端口接收无线数据
|
||||
open_tcp_server: false #是否开启TCP端口接收无线数据
|
||||
check_serial: true
|
||||
dpark_location: "spaoDevs"
|
||||
tcp_port: 10090
|
||||
|
@ -122,7 +122,8 @@ api_port: 10086 #本地web api port
|
|||
pro_category: 1
|
||||
syn_data: 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_keepAlive: 30 #mqtt心跳时间间隔 单位是s
|
||||
mqtt_timeout: 10 #mqtt超时时间 单位s
|
||||
|
@ -132,4 +133,5 @@ mqtt_username: "dpark"
|
|||
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
|
||||
|
|
|
@ -51,6 +51,7 @@ type Conf struct {
|
|||
SYNDATA bool `json:"syn_data"`
|
||||
SYNEXTENSIONTIME bool `json:"syn_extension_Time"`
|
||||
RECORD_LOG_PATH string `json:"record_log_path"`
|
||||
RECORD_LOG_RETENTION_TIME int `json:"record_log_retention_time"`
|
||||
MQTT_OPEN bool `json:"mqtt_open"`
|
||||
MQTT_BROKER string `json:"mqtt_broker"`
|
||||
MQTT_USERNAME string `json:"mqtt_username"`
|
||||
|
|
71
main.go
71
main.go
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
|
@ -97,6 +98,10 @@ func main() {
|
|||
}
|
||||
})
|
||||
|
||||
c.AddFunc("0, *, *, *, *, *", func() {
|
||||
ClearLog()
|
||||
})
|
||||
|
||||
// c.AddFunc("*/10, *, *, *, *, *", func() {
|
||||
// devices := []Device{}
|
||||
// log.Println(string(build_data(1001,devices)))
|
||||
|
@ -154,3 +159,69 @@ func check(err error) {
|
|||
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)
|
||||
}
|
||||
}
|
||||
|
|
24
router.go
24
router.go
|
@ -230,8 +230,24 @@ func dealDatav3(data []byte, loragwip string) string {
|
|||
|
||||
datatype := data[5]
|
||||
|
||||
if datatype == 0 {
|
||||
|
||||
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-吸顶
|
||||
if datatype == 1 || datatype == 2 {
|
||||
|
||||
//过滤无效数据 adr
|
||||
|
||||
|
@ -631,6 +647,12 @@ func get_renliu_api(c *gin.Context) {
|
|||
|
||||
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)
|
||||
if conf.DEBUG {
|
||||
log.Println(rl_data)
|
||||
|
|
24
tcpserver.go
24
tcpserver.go
|
@ -11,10 +11,8 @@ import (
|
|||
// "go-study/socket/config"
|
||||
"bytes"
|
||||
"strconv"
|
||||
|
||||
)
|
||||
|
||||
|
||||
/**
|
||||
* 从loragw的组中获取到范围内的所有node,给符合addr的node发送command
|
||||
**/
|
||||
|
@ -26,13 +24,13 @@ func sendCommand(addr string,command int){
|
|||
byteAddr, ok := sensorMap.Load(addr)
|
||||
if !ok {
|
||||
log.Println("send command fail,invalid addr|", addr)
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
loraNode, ok := loraNodeMap.Load(addr)
|
||||
if !ok {
|
||||
log.Println("send command fail,invalid addr|", addr)
|
||||
return;
|
||||
return
|
||||
}
|
||||
if command == 1 {
|
||||
commands = []byte{0x01}
|
||||
|
@ -40,7 +38,7 @@ func sendCommand(addr string,command int){
|
|||
commands = []byte{0x02}
|
||||
} else {
|
||||
log.Println("send command fail,invalid command|", command)
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
// strconv.Itoa
|
||||
|
@ -57,13 +55,12 @@ func sendCommand(addr string,command int){
|
|||
_, err := loraconn.(net.Conn).Write(buffer.Bytes())
|
||||
if err != nil {
|
||||
log.Println("yunhorn.loragw.write msg error!", err)
|
||||
return;
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func handleConn(conn net.Conn) {
|
||||
|
||||
if connTest == nil {
|
||||
|
@ -146,7 +143,7 @@ func readConn(conn net.Conn, readChan chan<- []byte, stopChan chan<- bool) {
|
|||
|
||||
if continueCount > 0 {
|
||||
continueCount--
|
||||
continue;
|
||||
continue
|
||||
}
|
||||
|
||||
if b != 254 {
|
||||
|
@ -165,9 +162,9 @@ func readConn(conn net.Conn, readChan chan<- []byte, stopChan chan<- bool) {
|
|||
//尿布台数据,地址1位 数据2位
|
||||
dataType := data[i+1]
|
||||
|
||||
//0是通用类型
|
||||
//0是通用类型 命令交互
|
||||
if dataType != 0 && dataType != 1 && dataType != 2 && dataType != 3 && dataType != 4 {
|
||||
continue;
|
||||
continue
|
||||
}
|
||||
|
||||
values := make([]byte, 0)
|
||||
|
@ -175,6 +172,7 @@ func readConn(conn net.Conn, readChan chan<- []byte, stopChan chan<- bool) {
|
|||
if dataType == 0 {
|
||||
continueCount = 2
|
||||
end = 3
|
||||
values = data[i+2 : i+3]
|
||||
} else if dataType == 1 {
|
||||
|
||||
continueCount = 3
|
||||
|
@ -276,15 +274,14 @@ func readConn(conn net.Conn, readChan chan<- []byte, stopChan chan<- bool) {
|
|||
if !loraNode.(LoraNode).UPDATE_TIME.IsZero() {
|
||||
var duration = now.Sub(loraNode.(LoraNode).UPDATE_TIME).Seconds()
|
||||
var durationAsInt64 = int(duration)
|
||||
if(durationAsInt64<=1){
|
||||
if durationAsInt64 <= 1 {
|
||||
log.Println("repeat.sensor.data:", addrs)
|
||||
continue;
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
sensorMap.Store(buffer.String(), addrs)
|
||||
|
||||
|
||||
buffer = new(bytes.Buffer)
|
||||
|
||||
if conf.RECORD_LOG_PATH != "" {
|
||||
|
@ -296,7 +293,6 @@ func readConn(conn net.Conn, readChan chan<- []byte, stopChan chan<- bool) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
// 转化为字符串
|
||||
// log.Println("yunhorndeug.push.data.tcp.16data:",buffer.String())
|
||||
|
||||
|
|
21
worker.go
21
worker.go
|
@ -1,16 +1,16 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"github.com/tarm/serial"
|
||||
"log"
|
||||
"os"
|
||||
_ "math/rand"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
"encoding/json"
|
||||
"time"
|
||||
"bytes"
|
||||
)
|
||||
|
||||
type Devicedatas struct {
|
||||
|
@ -28,7 +28,6 @@ type Device struct {
|
|||
|
||||
type baseMessage struct {
|
||||
Ip string `json:"ip"`
|
||||
|
||||
}
|
||||
|
||||
//处理通过tcp发送过来的数据
|
||||
|
@ -96,7 +95,6 @@ func record_log_channel(){
|
|||
|
||||
addrs := data_buf[:4]
|
||||
|
||||
|
||||
buffer := new(bytes.Buffer)
|
||||
|
||||
for i := 0; i < len(addrs); i++ {
|
||||
|
@ -104,19 +102,18 @@ func record_log_channel(){
|
|||
buffer.WriteString(s)
|
||||
}
|
||||
|
||||
|
||||
adr := buffer.String()
|
||||
|
||||
// log.Println("addrs and data",adr,data_buf)
|
||||
|
||||
now := time.Now()
|
||||
|
||||
oldTime,ok := nodeUpdateMap.Load(adr);
|
||||
if(ok){
|
||||
oldTime, ok := nodeUpdateMap.Load(adr)
|
||||
if ok {
|
||||
//have data
|
||||
var duration = now.Sub(oldTime.(time.Time)).Seconds()
|
||||
var durationAsInt64 = int(duration)
|
||||
if(durationAsInt64>1){
|
||||
if durationAsInt64 > 1 {
|
||||
record_log(data_buf)
|
||||
}
|
||||
//same data
|
||||
|
@ -129,7 +126,6 @@ func record_log_channel(){
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
//打印原始数据进log
|
||||
func record_log(data_buf []byte) {
|
||||
time := today
|
||||
|
@ -148,7 +144,6 @@ func record_log(data_buf []byte){
|
|||
record.Println(data_buf)
|
||||
}
|
||||
|
||||
|
||||
func yunhorn_lora_l_v1(data_buf []byte) {
|
||||
|
||||
// for _, i := range conf.RS485DEVICES {
|
||||
|
@ -205,7 +200,7 @@ func yunhorn_hb_dpark_v1(){
|
|||
loraNode, ok := loraNodeMap.Load(v.ADR)
|
||||
if !ok {
|
||||
log.Println("hb load loraNode fail ", ok)
|
||||
continue;
|
||||
continue
|
||||
}
|
||||
newLoraNode := LoraNode{}
|
||||
newLoraNode.CURRENT_DATA = loraNode.(LoraNode).CURRENT_DATA
|
||||
|
@ -254,7 +249,6 @@ func yunhorn_ys_l_v1(data_buf []byte) {
|
|||
demoDevice.DATA["data"+strconv.Itoa(2)] = 0
|
||||
demoDevice.DATA["data"+strconv.Itoa(3)] = 0
|
||||
|
||||
|
||||
if data_int0[0] == 0 && data_int1[0] == 0 && data_type[0] == 0 {
|
||||
demoDevice.DATA["data"+strconv.Itoa(8)] = data_int[0]
|
||||
var device Device
|
||||
|
@ -279,7 +273,6 @@ func yunhorn_ys_l_v1(data_buf []byte) {
|
|||
device.ONLINE = demoDevice.ONLINE
|
||||
device.DATA = make(map[string]interface{})
|
||||
|
||||
|
||||
if data_int[0] == 0 {
|
||||
device.DATA["data7"] = 1
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue