153 lines
4.8 KiB
Go
153 lines
4.8 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"fmt"
|
|
"github.com/spf13/viper"
|
|
"joylink.club/bj-rtsts-server/bin/config"
|
|
"joylink.club/bj-rtsts-server/const/balise_const"
|
|
"joylink.club/bj-rtsts-server/third_party/message"
|
|
"joylink.club/bj-rtsts-server/third_party/udp"
|
|
"log/slog"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const config_name = "example"
|
|
|
|
var exampleConfig config.ExampleConfig
|
|
|
|
func initConfig() {
|
|
cnf := viper.New()
|
|
cnf.SetConfigName(config_name)
|
|
cnf.SetConfigType("yml")
|
|
cnf.AddConfigPath("./bin/config/")
|
|
//cnf.AddConfigPath("./config/")
|
|
cnf.AddConfigPath(".")
|
|
err := cnf.ReadInConfig()
|
|
if err != nil {
|
|
panic(fmt.Errorf("读取配置文件错误: %w", err))
|
|
}
|
|
err = cnf.Unmarshal(&exampleConfig)
|
|
if err != nil {
|
|
panic(fmt.Errorf("解析配置文件错误: %w", err))
|
|
}
|
|
slog.Info("成功加载配置", "config", exampleConfig)
|
|
}
|
|
|
|
var btmCli udp.UdpClient
|
|
|
|
func initBtmTest() {
|
|
ser := udp.NewServer(fmt.Sprintf("%v:%d", exampleConfig.Btm.LocalIp, exampleConfig.Btm.LocalPort), handleBtmVobcFrames)
|
|
ser.Listen()
|
|
btmCli = udp.NewClient(fmt.Sprintf("%v:%v", exampleConfig.Btm.RemoteIp, exampleConfig.Btm.RemotePort))
|
|
}
|
|
|
|
var accClient udp.UdpClient
|
|
|
|
func initAccTest() {
|
|
accClient = udp.NewClient(fmt.Sprintf("%v:%v", exampleConfig.Acc.RemoteIp, exampleConfig.Acc.RemotePort))
|
|
go testAcc(accClient, 0.54)
|
|
}
|
|
|
|
var speedClient udp.UdpClient
|
|
|
|
func initSpeedTest() {
|
|
speedClient = udp.NewClient(fmt.Sprintf("%v:%v", exampleConfig.Speed.RemoteIp, exampleConfig.Speed.RemotePort))
|
|
go testSpeed(speedClient, 30)
|
|
}
|
|
func main() {
|
|
|
|
initConfig()
|
|
initBtmTest()
|
|
//initAccTest()
|
|
//initSpeedTest()
|
|
|
|
select {}
|
|
}
|
|
|
|
var testUserBtmMsg = "90007F8181B60B10183280003FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
|
|
var freeBtmMsg = strings.Repeat("00", balise_const.UserTelegramByteLen)
|
|
|
|
func handleBtmVobcFrames(cfs []byte) {
|
|
frameType, dataText, err := message.BtmVobcDecode(cfs)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if frameType == message.COMMAND_TYPE {
|
|
idCommand := &message.BtmVobcIdCommand{}
|
|
idCommand.Decode(dataText)
|
|
|
|
//userMsg, _ := hex.DecodeString(testUserBtmMsg)
|
|
|
|
//msg := &message.BtmVobcMessage{FontTtl: 5, BtmStatus: 0x00, DecodeTime: 10, BackTtl: 4, BtmMsg: userMsg, ResponseTime: 10,
|
|
// VobcLifeNum: idCommand.VobcLifeNum, BaseBtmVobc: message.BaseBtmVobc{AutoIdFrame: idCommand.AutoIdFrame}}
|
|
//sendData := msg.Encode()
|
|
//fmt.Println("发送btm vobc len:", len(sendData), "报文:", hex.EncodeToString(sendData), "报文序列号:", msg.MsgSerial)
|
|
//btmCli.Send(sendData)
|
|
|
|
freeMsg, _ := hex.DecodeString(freeBtmMsg)
|
|
msg2 := &message.BtmVobcMsgFree{BtmStatus: 0x00, WorkTemperature: 10, Fun1: uint16(0), Fun2: uint16(0), Fun3: uint16(0), Fun4: uint16(0),
|
|
FreeMsg: freeMsg, RespTime: 20, VobcLifeNum: idCommand.VobcLifeNum, BaseBtmVobc: message.BaseBtmVobc{AutoIdFrame: idCommand.AutoIdFrame}}
|
|
sendData2 := msg2.Encode()
|
|
fmt.Println("发送btm vobc 空报文:", hex.EncodeToString(sendData2), "len:", len(sendData2), "报文序列号:", msg2.MsgSerial, "atoId=", idCommand.AutoIdFrame)
|
|
btmCli.Send(sendData2)
|
|
} else if frameType == message.REQUEST_TYPE {
|
|
req := &message.BtmVobcReq{}
|
|
req.Decode(dataText)
|
|
fmt.Println(req, "========================")
|
|
} else {
|
|
slog.Error("btm vobc 解析未知命令帧类型", strconv.FormatInt(int64(frameType), 16), frameType, "原始数据:", hex.EncodeToString(cfs), "长度:", len(cfs))
|
|
return
|
|
}
|
|
|
|
}
|
|
|
|
func testSpeed(client udp.UdpClient, speed float32) {
|
|
|
|
//client = udp.NewClient(fmt.Sprintf("%v:%v", exampleConfig.Speed.RemoteIp, exampleConfig.Speed.RemotePort))
|
|
ac := message.ElectricMachinery{Speed: speed, WheelDiameter: 800, IsBack: false}
|
|
var index int64
|
|
for {
|
|
data := ac.Encode()
|
|
//fmt.Println(hex.EncodeToString(data))
|
|
err := client.Send(data)
|
|
index++
|
|
if index%10 == 0 {
|
|
slog.Info(fmt.Sprintf("发送speed数据,时间戳:%v,发送次数:%v 数据:%v", time.Now().Unix(), index, hex.EncodeToString(data)))
|
|
ac = message.ElectricMachinery{Speed: 0, WheelDiameter: 800, IsBack: false}
|
|
client.Send(ac.Encode())
|
|
break
|
|
}
|
|
|
|
if err != nil {
|
|
slog.Error("发送数据失败", "err", err)
|
|
}
|
|
time.Sleep(time.Millisecond * 200)
|
|
}
|
|
}
|
|
|
|
// 加速度计
|
|
func testAcc(client udp.UdpClient, acc float32) {
|
|
|
|
//client = udp.NewClient(fmt.Sprintf("%v:%v", exampleConfig.Acc.RemoteIp, exampleConfig.Acc.RemotePort))
|
|
ac := message.Accelerometer{Acc: acc}
|
|
var index int64
|
|
for {
|
|
data := ac.Encode()
|
|
//fmt.Println(hex.EncodeToString(data))
|
|
err := client.Send(data)
|
|
index++
|
|
if index%10 == 0 {
|
|
slog.Info(fmt.Sprintf("发送acc数据,时间戳:%v,发送次数:%v 数据:%v", time.Now().Unix(), index, hex.EncodeToString(data)))
|
|
}
|
|
|
|
if err != nil {
|
|
slog.Error("发送数据失败", "err", err)
|
|
}
|
|
time.Sleep(time.Millisecond * 11)
|
|
}
|
|
|
|
}
|