2
This commit is contained in:
parent
a9060efb87
commit
e9fdf5bac2
3
main.go
3
main.go
@ -26,7 +26,7 @@ func main() {
|
||||
|
||||
go startWebServer()
|
||||
|
||||
addr := "0.0.0.0:10002"
|
||||
addr := "0.0.0.0:10004"
|
||||
listener, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
logger.Fatalf("监听端口错误: %v", err)
|
||||
@ -128,6 +128,7 @@ func handleConnection(sensor *SensorComm) {
|
||||
}
|
||||
|
||||
logger.Printf("接收数据长度: %d", n)
|
||||
logger.Printf("Raw Data: %x\n",buffer[:n])
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
@ -71,25 +71,37 @@ func (sc *SensorComm) sendQuery() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// 发送整点重置命令,在查询成功后执行
|
||||
// 发送整点重置命令,在重置前先查询保存数据
|
||||
func (sc *SensorComm) resetHourly() bool {
|
||||
// 检查是否在过去5分钟内已经重置过
|
||||
if time.Since(sc.lastResetTime) < 5*time.Minute {
|
||||
logger.Printf("最近5分钟内已重置过雨量,跳过本次重置")
|
||||
return false
|
||||
}
|
||||
if time.Since(sc.lastResetTime) < 5*time.Minute {
|
||||
logger.Printf("最近5分钟内已重置过雨量,跳过本次重置")
|
||||
return false
|
||||
}
|
||||
|
||||
// 直接发送重置命令
|
||||
_, err := sc.conn.Write(sc.resetCmd)
|
||||
if err != nil {
|
||||
logger.Printf("发送重置命令错误: %v", err)
|
||||
return false
|
||||
}
|
||||
sc.querySuccess = false
|
||||
_, err := sc.conn.Write(sc.queryCmd)
|
||||
if err != nil {
|
||||
logger.Printf("重置前查询命令错误: %v", err)
|
||||
// 继续执行重置,不因查询失败而中断重置
|
||||
} else {
|
||||
logger.Printf("重置前发送查询命令: %s", time.Now().Format("15:04:05"))
|
||||
|
||||
logger.Printf("发送雨量重置命令: %s", time.Now().Format("15:04:05"))
|
||||
sc.lastResetTime = time.Now()
|
||||
return true
|
||||
// 等待一小段时间确保查询完成并且数据被处理
|
||||
time.Sleep(2000 * time.Millisecond)
|
||||
}
|
||||
|
||||
// 发送重置命令
|
||||
_, err = sc.conn.Write(sc.resetCmd)
|
||||
if err != nil {
|
||||
logger.Printf("发送重置命令错误: %v", err)
|
||||
return false
|
||||
}
|
||||
|
||||
logger.Printf("发送雨量重置命令: %s", time.Now().Format("15:04:05"))
|
||||
sc.lastResetTime = time.Now()
|
||||
return true
|
||||
}
|
||||
|
||||
// 处理接收到的数据
|
||||
func (sc *SensorComm) handleData(data []byte) *SensorData {
|
||||
sensorData := parseData(data)
|
||||
|
||||
BIN
tcp_server
BIN
tcp_server
Binary file not shown.
4
web.go
4
web.go
@ -21,8 +21,8 @@ func startWebServer() {
|
||||
http.HandleFunc("/", handleIndex)
|
||||
http.HandleFunc("/refresh-data", handleRefresh)
|
||||
|
||||
logger.Printf("启动Web服务器: http://0.0.0.0:10001")
|
||||
if err := http.ListenAndServe(":10001", nil); err != nil {
|
||||
logger.Printf("启动Web服务器: http://0.0.0.0:10003")
|
||||
if err := http.ListenAndServe(":10003", nil); err != nil {
|
||||
logger.Fatalf("启动Web服务器失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user