Commit 31cdcd55 authored by sven's avatar sven

增加dtu数据接收

parent f6cf64b0
......@@ -168,3 +168,8 @@ oss:
OssInternalHost: "https://wensi-zhanlang.oss-cn-hangzhou-internal.aliyuncs.com"
EndPoint: "oss-cn-hangzhou.aliyuncs.com"
prefix: "qsh_wc/"
# dtu设备配置
dtuServer:
addr: 0.0.0.0:9804
enable: false
\ No newline at end of file
......@@ -168,3 +168,8 @@ oss:
OssInternalHost: "https://wensi-zhanlang.oss-cn-hangzhou-internal.aliyuncs.com"
EndPoint: "oss-cn-hangzhou.aliyuncs.com"
prefix: "qsh_wc/"
# dtu设备配置
dtuServer:
addr: 0.0.0.0:9804
enable: true
\ No newline at end of file
......@@ -168,3 +168,8 @@ oss:
OssInternalHost: "https://wensi-zhanlang.oss-cn-hangzhou-internal.aliyuncs.com"
EndPoint: "oss-cn-hangzhou.aliyuncs.com"
prefix: "qsh_wc/"
# dtu设备配置
dtuServer:
addr: 0.0.0.0:9804
enable: false
\ No newline at end of file
package main
import (
"fmt"
"github.com/flipped-aurora/gin-vue-admin/server/core"
"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/flipped-aurora/gin-vue-admin/server/initialize"
"github.com/flipped-aurora/gin-vue-admin/server/server"
"go.uber.org/zap"
)
......@@ -33,5 +35,14 @@ func main() {
db, _ := global.GVA_DB.DB()
defer db.Close()
}
// dtu服务器
if global.GVA_VP.GetBool("dtuServer.enable") {
server.OnDtuMessage(func(data server.YeWeiData) error {
fmt.Println(data)
// {9.300 860059054444117}
return nil
})
go server.InitDtuServer(global.GVA_VP.GetString("dtuServer.addr"))
}
core.RunWindowsServer()
}
package server
import (
"fmt"
"github.com/flipped-aurora/gin-vue-admin/server/global"
"log"
"net"
"regexp"
"strings"
)
type YeWeiData struct {
YeWei string // 液位高度,单位m
DeviceId string // 设备编号
}
type YeWeiCb func(data YeWeiData) error
var yeWeiCb YeWeiCb
func InitDtuServer(address string) {
server, err := net.Listen("tcp", address)
if err != nil {
log.Fatalln("dtu server init err:", err)
}
defer server.Close()
log.Println("Dtu server is running, listened:", address)
for {
conn, err := server.Accept()
if err != nil {
log.Println("dtu accept err:", err)
continue
}
go dtuOnMessage(conn)
}
}
// dtuOnMessage 处理dtu消息
func dtuOnMessage(conn net.Conn) {
defer conn.Close()
for {
data := make([]byte, 1024)
n, err := conn.Read(data)
if err != nil {
global.GVA_LOG.Error(fmt.Sprintf("dtu read data err: %s", err.Error()))
break
}
// 处理协议,解析数据
yeweiData := YeWeiData{}
strData := string(data[0:n])
reg, err := regexp.Compile("\\d*50000#SY6600")
yeweiData.DeviceId = strings.Replace(reg.FindString(strData), "50000#SY6600", "", 1)
if yeweiData.DeviceId == "" {
global.GVA_LOG.Error(fmt.Sprintf("协议解析失败 strData: %s", strData))
continue
}
reg, _ = regexp.Compile("0/0,0/0,0/0,0/0,0,0,.*,0,0,0,0,0,0,0,0,0,0,0,0,0,0,Protocol:NKCSBYE$")
value := strings.Replace(reg.FindString(strData), "0/0,0/0,0/0,0/0,0,0,", "", 1)
value = strings.Replace(value, ",0,0,0,0,0,0,0,0,0,0,0,0,0,0,Protocol:NKCSBYE", "", 1)
if values := strings.Split(value, ","); len(values) == 2 {
yeweiData.YeWei = values[1]
}
if yeweiData.YeWei == "" {
global.GVA_LOG.Error(fmt.Sprintf("协议解析失败 strData: %s", strData))
continue
}
// 调用业务处理
if yeWeiCb != nil {
err = yeWeiCb(yeweiData)
if err != nil {
global.GVA_LOG.Error(fmt.Sprintf("处理液位回调失败 strData: %s", strData))
}
}
}
}
// OnDtuMessage 注册回调函数
func OnDtuMessage(cb YeWeiCb) {
yeWeiCb = cb
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment