2024-05-20 15:22:58 +08:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2024-06-18 17:10:21 +08:00
|
|
|
|
"encoding/binary"
|
2024-05-27 14:59:17 +08:00
|
|
|
|
"encoding/hex"
|
2024-05-20 15:22:58 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
"joylink.club/bj-rtsts-server/bin/config"
|
2024-05-29 11:48:04 +08:00
|
|
|
|
"joylink.club/bj-rtsts-server/const/balise_const"
|
2024-05-20 15:22:58 +08:00
|
|
|
|
"joylink.club/bj-rtsts-server/third_party/message"
|
2024-06-18 17:10:21 +08:00
|
|
|
|
"joylink.club/bj-rtsts-server/third_party/tcp"
|
2024-05-20 15:22:58 +08:00
|
|
|
|
"joylink.club/bj-rtsts-server/third_party/udp"
|
|
|
|
|
"log/slog"
|
2024-05-29 11:48:04 +08:00
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2024-05-20 15:22:58 +08:00
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const config_name = "example"
|
|
|
|
|
|
|
|
|
|
var exampleConfig config.ExampleConfig
|
|
|
|
|
|
|
|
|
|
func initConfig() {
|
|
|
|
|
cnf := viper.New()
|
|
|
|
|
cnf.SetConfigName(config_name)
|
|
|
|
|
cnf.SetConfigType("yml")
|
2024-06-18 17:10:21 +08:00
|
|
|
|
//cnf.AddConfigPath("./bin/config/")
|
|
|
|
|
//cnf.AddConfigPath("./")
|
2024-05-20 15:22:58 +08:00
|
|
|
|
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)
|
|
|
|
|
}
|
2024-05-27 14:59:17 +08:00
|
|
|
|
|
2024-05-29 11:48:04 +08:00
|
|
|
|
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)
|
|
|
|
|
}
|
2024-06-18 17:10:21 +08:00
|
|
|
|
|
|
|
|
|
var trainpcClient *tcp.TcpClient
|
|
|
|
|
|
|
|
|
|
func initTrainPc() {
|
|
|
|
|
addr := fmt.Sprintf("%v:%v", exampleConfig.Trainpc.RemoteIp, exampleConfig.Trainpc.RemotePort)
|
|
|
|
|
|
|
|
|
|
client2, err := tcp.StartTcpClient(addr, trainPcDataHandle, trainPcConnErr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("仿真列车pc连接失败", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
trainpcClient = client2
|
2024-06-27 10:37:52 +08:00
|
|
|
|
time.Sleep(time.Millisecond * 1500)
|
|
|
|
|
createOrRemoveTrain()
|
2024-06-18 17:10:21 +08:00
|
|
|
|
circleSendTrainActive()
|
|
|
|
|
circleSendTrainMockData()
|
2024-07-04 09:26:37 +08:00
|
|
|
|
//go circleSendTrainSpeedPlace()
|
|
|
|
|
//sendBtm()
|
2024-06-18 17:10:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-04 09:26:37 +08:00
|
|
|
|
const (
|
|
|
|
|
e1 = 0x1e
|
|
|
|
|
aa = 0x17
|
|
|
|
|
)
|
|
|
|
|
|
2024-06-27 10:37:52 +08:00
|
|
|
|
func createOrRemoveTrain() {
|
|
|
|
|
msgs := make([]*message.TrainPcSimBaseMessage, 0)
|
|
|
|
|
//msgs = append(msgs, &message.TrainPcSimBaseMessage{Data: []byte{0x00}, Type: message.RECIVE_TRAIN_CREATE_REMOVE})
|
|
|
|
|
msgs = append(msgs, &message.TrainPcSimBaseMessage{Data: []byte{0x01}, Type: message.RECIVE_TRAIN_CREATE_REMOVE})
|
|
|
|
|
for _, msg := range msgs {
|
|
|
|
|
data := msg.Encode()
|
|
|
|
|
fmt.Println(fmt.Sprintf("发送列车模拟数据:%v,命令码:%v", hex.EncodeToString(data), msg.Data[0]))
|
|
|
|
|
trainpcClient.Send(data)
|
|
|
|
|
time.Sleep(time.Millisecond * 1000)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-18 17:10:21 +08:00
|
|
|
|
func circleSendTrainActive() {
|
2024-06-27 10:37:52 +08:00
|
|
|
|
msgs := make([]*message.TrainPcSimBaseMessage, 0)
|
|
|
|
|
msgs = append(msgs, &message.TrainPcSimBaseMessage{Data: []byte{}, Type: message.SENDER_TRAIN_TC_ACTIVE})
|
|
|
|
|
//msgs = append(msgs, &message.TrainPcSimBaseMessage{Data: []byte{}, Type: message.SENDER_TRAIN_HAND_KEY_FORWARD})
|
|
|
|
|
|
|
|
|
|
for _, msg := range msgs {
|
|
|
|
|
|
|
|
|
|
actData := msg.Encode()
|
|
|
|
|
fmt.Println(fmt.Sprintf("发送列车驾驶室激活:%v", hex.EncodeToString(actData)))
|
|
|
|
|
trainpcClient.Send(actData)
|
|
|
|
|
time.Sleep(time.Millisecond * 1000)
|
|
|
|
|
}
|
2024-06-18 17:10:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func circleSendTrainMockData() {
|
2024-06-27 10:37:52 +08:00
|
|
|
|
msgs := make([]*message.TrainPcSimBaseMessage, 0)
|
|
|
|
|
msgs = append(msgs, &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_OUTR_INFO, Data: []byte{byte(0), 1}})
|
|
|
|
|
//msgs = append(msgs, &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_OUTR_INFO, Data: []byte{byte(16), 1}})
|
2024-07-04 09:26:37 +08:00
|
|
|
|
msgs = append(msgs, &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_OUTR_INFO, Data: []byte{byte(3), 0}})
|
|
|
|
|
//msgs = append(msgs, &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_OUTR_INFO, Data: []byte{byte(3), 0}})
|
|
|
|
|
msgs = append(msgs, &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_OUTR_INFO, Data: []byte{byte(16), 1}})
|
|
|
|
|
msgs = append(msgs, &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_OUTR_INFO, Data: []byte{byte(45), 1}})
|
|
|
|
|
msgs = append(msgs, &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_OUTR_INFO, Data: []byte{byte(46), 1}})
|
2024-06-27 10:37:52 +08:00
|
|
|
|
/* for i := 3; i <= 50; i++ {
|
|
|
|
|
if i != 2 {
|
|
|
|
|
msgs = append(msgs, &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_OUTR_INFO, Data: []byte{byte(i), 0}})
|
|
|
|
|
}
|
|
|
|
|
}*/
|
|
|
|
|
//msgs = append(msgs, &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_OUTR_INFO, Data: []byte{14, 0}})
|
|
|
|
|
//msgs = append(msgs, &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_OUTR_INFO, Data: []byte{1, 1}})
|
|
|
|
|
/*msgs = append(msgs, &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_OUTR_INFO, Data: []byte{1, 1}})
|
|
|
|
|
msgs = append(msgs, &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_OUTR_INFO, Data: []byte{2, 0}})
|
|
|
|
|
msgs = append(msgs, &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_OUTR_INFO, Data: []byte{12, 1}})
|
|
|
|
|
msgs = append(msgs, &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_OUTR_INFO, Data: []byte{16, 1}})
|
|
|
|
|
msgs = append(msgs, &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_OUTR_INFO, Data: []byte{20, 1}})
|
|
|
|
|
msgs = append(msgs, &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_OUTR_INFO, Data: []byte{3, 1}})
|
|
|
|
|
msgs = append(msgs, &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_OUTR_INFO, Data: []byte{4, 1}})
|
|
|
|
|
msgs = append(msgs, &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_OUTR_INFO, Data: []byte{6, 1}})
|
|
|
|
|
msgs = append(msgs, &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_OUTR_INFO, Data: []byte{7, 1}})
|
|
|
|
|
msgs = append(msgs, &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_OUTR_INFO, Data: []byte{30, 1}})
|
|
|
|
|
msgs = append(msgs, &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_OUTR_INFO, Data: []byte{22, 0}})
|
|
|
|
|
msgs = append(msgs, &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_OUTR_INFO, Data: []byte{23, 0}})*/
|
|
|
|
|
//msgs = append(msgs, &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_OUTR_INFO, Data: []byte{3, 0}})
|
|
|
|
|
|
|
|
|
|
for _, msg := range msgs {
|
|
|
|
|
data := msg.Encode()
|
|
|
|
|
fmt.Println(fmt.Sprintf("发送列车模拟数据:%v,命令码:%v", hex.EncodeToString(data), msg.Data[0]))
|
|
|
|
|
trainpcClient.Send(data)
|
|
|
|
|
time.Sleep(time.Millisecond * 1000)
|
|
|
|
|
}
|
2024-06-18 17:10:21 +08:00
|
|
|
|
}
|
2024-07-04 09:26:37 +08:00
|
|
|
|
func sendBtm() {
|
|
|
|
|
source := "eb3305d543eb3211000005ea000000010000082b7989eb321100003573000000010000257ce38eeb3211000005d90000000100002731a483eb3211000005c8000000010000322d6b8aeb32110000356200000001000033976df1eb3179000005fb000000010000060390007f8182fd8b10183280003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc02edeb31790000358400000001000007b990007f819ac20b10183280003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffcefe4eb31790000070d00000001000008f690007f8183868b10183280003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc72a9eb3179000035950000000100000a3390007f819aca8b10183280003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc5487eb31790000071e000000010000279b90007f81838f0b10183280003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffcddd1eb3179000005fb000000010000060390007f8182fd8b10183280003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc02edeb31790000358400000001000007b990007f819ac20b10183280003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffcefe4eb31790000070d00000001000008f690007f8183868b10183280003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc72a9eb3179000035950000000100000a3390007f819aca8b10183280003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc5487eb31790000071e000000010000279b90007f81838f0b10183280003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffcddd1"
|
|
|
|
|
data, _ := hex.DecodeString(source)
|
|
|
|
|
trainpcClient.Send(data)
|
|
|
|
|
}
|
2024-06-18 17:10:21 +08:00
|
|
|
|
func circleSendTrainSpeedPlace() {
|
|
|
|
|
for {
|
|
|
|
|
data := make([]byte, 0)
|
2024-06-27 10:37:52 +08:00
|
|
|
|
data = binary.BigEndian.AppendUint16(data, uint16(2))
|
|
|
|
|
//data = binary.BigEndian.AppendUint16(data, uint16(1))
|
|
|
|
|
//data = binary.BigEndian.AppendUint16(data, uint16(0))
|
|
|
|
|
//data = binary.BigEndian.AppendUint16(data, uint16(0))
|
|
|
|
|
//data = binary.BigEndian.AppendUint32(data, uint32(940))
|
|
|
|
|
//data = binary.BigEndian.AppendUint32(data, uint32(940))
|
|
|
|
|
data = binary.BigEndian.AppendUint32(data, uint32(0))
|
|
|
|
|
data = binary.BigEndian.AppendUint32(data, uint32(0))
|
|
|
|
|
data = binary.BigEndian.AppendUint32(data, uint32(940))
|
|
|
|
|
data = binary.BigEndian.AppendUint32(data, uint32(940))
|
2024-06-18 17:10:21 +08:00
|
|
|
|
now := time.Now().UTC()
|
|
|
|
|
sec := now.Unix()
|
2024-06-27 10:37:52 +08:00
|
|
|
|
data = binary.BigEndian.AppendUint32(data, uint32(27797))
|
2024-06-18 17:10:21 +08:00
|
|
|
|
data = binary.BigEndian.AppendUint16(data, uint16(now.UnixMilli()-sec*1000))
|
2024-06-27 10:37:52 +08:00
|
|
|
|
ss := "eb011d000200000000000000000000000000000000000017af0299ffff"
|
|
|
|
|
data, _ = hex.DecodeString(ss)
|
|
|
|
|
//bm := &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_LOCATION_INFO, Data: data}
|
|
|
|
|
//dataCode := bm.Encode()
|
|
|
|
|
//fmt.Println(fmt.Sprintf("发送列车位置信息:%v", hex.EncodeToString(data)))
|
|
|
|
|
trainpcClient.Send(data)
|
|
|
|
|
//dd := &message.TrainPcSimBaseMessage{Type: message.SENDER_TRAIN_OUTR_INFO, Data: []byte{3, 0}}
|
|
|
|
|
//trainpcClient.Send(dd.Encode())
|
2024-06-18 17:10:21 +08:00
|
|
|
|
time.Sleep(time.Millisecond * 80)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
func trainPcDataHandle(n int, data []byte) {
|
2024-07-04 09:26:37 +08:00
|
|
|
|
hexData := hex.EncodeToString(data[:n])
|
|
|
|
|
slog.Info(fmt.Sprintf("列车pc仿真接口长度:%v,实际长度:%v,接受数据:%v", len(data), n, hexData))
|
|
|
|
|
//source := "eb3211000005ea000000010000082b7989eb321100003573000000010000257ce38eeb3211000005d90000000100002731a483eb3211000005c8000000010000322d6b8aeb32110000356200000001000033976df1eb3179000005fb000000010000060390007f8182fd8b10183280003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc02edeb31790000358400000001000007b990007f819ac20b10183280003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffcefe4eb31790000070d00000001000008f690007f8183868b10183280003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc72a9eb3179000035950000000100000a3390007f819aca8b10183280003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc5487eb31790000071e000000010000279b90007f81838f0b10183280003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffcddd1eb3179000005fb000000010000060390007f8182fd8b10183280003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc02edeb31790000358400000001000007b990007f819ac20b10183280003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffcefe4eb31790000070d00000001000008f690007f8183868b10183280003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc72a9eb3179000035950000000100000a3390007f819aca8b10183280003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc5487eb31790000071e000000010000279b90007f81838f0b10183280003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffcddd1"
|
|
|
|
|
//data2, _ := hex.DecodeString(source)
|
|
|
|
|
//trainpcClient.Send(data2)
|
|
|
|
|
|
2024-06-18 17:10:21 +08:00
|
|
|
|
}
|
|
|
|
|
func trainPcConnErr(err error) {
|
|
|
|
|
|
|
|
|
|
}
|
2024-05-20 15:22:58 +08:00
|
|
|
|
func main() {
|
2024-05-27 14:59:17 +08:00
|
|
|
|
|
2024-05-20 15:22:58 +08:00
|
|
|
|
initConfig()
|
2024-06-18 17:10:21 +08:00
|
|
|
|
//initBtmTest()
|
2024-06-27 10:37:52 +08:00
|
|
|
|
initTrainPc()
|
2024-05-29 11:48:04 +08:00
|
|
|
|
//initAccTest()
|
2024-06-27 10:37:52 +08:00
|
|
|
|
//initSpeedTest()
|
2024-05-27 14:59:17 +08:00
|
|
|
|
|
2024-06-06 17:57:30 +08:00
|
|
|
|
for {
|
|
|
|
|
}
|
2024-05-20 15:22:58 +08:00
|
|
|
|
}
|
2024-05-27 14:59:17 +08:00
|
|
|
|
|
2024-05-29 11:48:04 +08:00
|
|
|
|
var testUserBtmMsg = "90007F8181B60B10183280003FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
|
|
|
|
|
var freeBtmMsg = strings.Repeat("00", balise_const.UserTelegramByteLen)
|
2024-05-27 14:59:17 +08:00
|
|
|
|
|
2024-06-06 17:57:30 +08:00
|
|
|
|
func sendPacket(lifeNum uint32, autoId byte) {
|
|
|
|
|
userMsg, _ := hex.DecodeString(testUserBtmMsg)
|
|
|
|
|
|
2024-06-18 17:10:21 +08:00
|
|
|
|
msg := &message.BtmVobcMessage{FontTtl: 2, BtmStatus: 0x00, DecodeTime: 10, BackTtl: 2, BtmMsg: userMsg, ResponseTime: 10,
|
2024-06-06 17:57:30 +08:00
|
|
|
|
VobcLifeNum: lifeNum, BaseBtmVobc: message.BaseBtmVobc{AutoIdFrame: autoId}, MsgSerial: message.GetAutoMessageId()}
|
|
|
|
|
sendData := msg.Encode()
|
|
|
|
|
fmt.Println("发送btm vobc len:", len(sendData), "报文:", hex.EncodeToString(sendData), "报文序列号:", msg.MsgSerial)
|
2024-06-18 17:10:21 +08:00
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
|
err := btmCli.Send(sendData)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(fmt.Sprintf("发送失败%v", err.Error()))
|
|
|
|
|
}
|
2024-06-06 17:57:30 +08:00
|
|
|
|
|
|
|
|
|
/*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: lifeNum, BaseBtmVobc: message.BaseBtmVobc{AutoIdFrame: autoId}, MsgSerial: message.GetAutoMessageId()}
|
|
|
|
|
sendData2 := msg2.Encode()
|
|
|
|
|
fmt.Println("发送btm vobc 空报文11111:", hex.EncodeToString(sendData2), "len:", len(sendData2), "报文序列号:", msg2.MsgSerial, "atoId=", autoId, "报文序列号:", msg2.MsgSerial)
|
|
|
|
|
btmCli.Send(sendData2)*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func sendPacketFree(lifeNum uint32, autoId byte, msgs byte) {
|
|
|
|
|
freeMsg, _ := hex.DecodeString(freeBtmMsg)
|
|
|
|
|
newMsg := msgs + 1
|
|
|
|
|
if newMsg > 255 {
|
|
|
|
|
newMsg = 1
|
|
|
|
|
}
|
|
|
|
|
msg2 := &message.BtmVobcMsgFree{BtmStatus: 0x00, WorkTemperature: 10, Fun1: uint16(0), Fun2: uint16(0), Fun3: uint16(0), Fun4: uint16(0),
|
2024-06-18 17:10:21 +08:00
|
|
|
|
//FreeMsg: freeMsg, RespTime: 20, VobcLifeNum: lifeNum, BaseBtmVobc: message.BaseBtmVobc{AutoIdFrame: autoId}, MsgSerial: newMsg}
|
|
|
|
|
FreeMsg: freeMsg, RespTime: 20, VobcLifeNum: lifeNum, BaseBtmVobc: message.BaseBtmVobc{AutoIdFrame: autoId}, MsgSerial: message.GetAutoMessageId()}
|
2024-06-06 17:57:30 +08:00
|
|
|
|
sendData2 := msg2.Encode()
|
|
|
|
|
fmt.Println("发送btm vobc 空报文:", hex.EncodeToString(sendData2), "len:", len(sendData2), "报文序列号:", msg2.MsgSerial, "atoId=", autoId, "报文序列号:", msg2.MsgSerial)
|
2024-06-18 17:10:21 +08:00
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
|
err := btmCli.Send(sendData2)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(fmt.Sprintf("发送失败%v", err.Error()))
|
|
|
|
|
}
|
2024-06-06 17:57:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-18 17:10:21 +08:00
|
|
|
|
func RequestFramePackets(req *message.BtmVobcReq, vobcLife uint32) {
|
|
|
|
|
//fmt.Println(fmt.Sprintf("接受 请求帧 frameStatus:%v,messageType:%v,lifeNum:%v,序列号:%v", req.FrameStatus, req.MessageType, req.VobcLifeNum, req.MessageSerial))
|
2024-06-06 17:57:30 +08:00
|
|
|
|
if req.FrameStatus == message.REQ_FRAME_STATUS_BOOT && req.MessageType == message.REQ_PACKETS_TYPE_BOOT {
|
2024-06-18 17:10:21 +08:00
|
|
|
|
fmt.Println("000000000000000000000000000")
|
|
|
|
|
sendPacketFree(vobcLife, req.AutoIdFrame, req.MessageSerial)
|
2024-06-06 17:57:30 +08:00
|
|
|
|
} else if req.FrameStatus == message.REQ_FRAME_STATUS_OK {
|
|
|
|
|
//帧正确,删除之前发送的数据
|
|
|
|
|
fmt.Println("11111111111111111111")
|
|
|
|
|
|
|
|
|
|
} else if req.FrameStatus == message.REQ_FRAME_STATUS_ERROR {
|
|
|
|
|
//帧不正确 重新发送2次,如果2次后仍然不正确,则删除之前发送的数据
|
|
|
|
|
fmt.Println("22222222222222222")
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-18 17:10:21 +08:00
|
|
|
|
|
|
|
|
|
var btmReceiveTime = time.Now().UnixMilli()
|
|
|
|
|
var vobcNumLife uint32 = 0
|
|
|
|
|
|
2024-05-29 11:48:04 +08:00
|
|
|
|
func handleBtmVobcFrames(cfs []byte) {
|
2024-06-18 17:10:21 +08:00
|
|
|
|
|
|
|
|
|
fmt.Println(fmt.Sprintf("收到源数据:%v ,请求帧时间:%v ,vobcLife:%v", hex.EncodeToString(cfs), time.Now().UnixMilli()-btmReceiveTime, vobcNumLife))
|
2024-05-29 11:48:04 +08:00
|
|
|
|
frameType, dataText, err := message.BtmVobcDecode(cfs)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if frameType == message.COMMAND_TYPE {
|
|
|
|
|
idCommand := &message.BtmVobcIdCommand{}
|
|
|
|
|
idCommand.Decode(dataText)
|
2024-06-18 17:10:21 +08:00
|
|
|
|
if vobcNumLife <= 0 {
|
|
|
|
|
vobcNumLife = idCommand.VobcLifeNum
|
|
|
|
|
}
|
|
|
|
|
sendPacketFree(vobcNumLife, idCommand.AutoIdFrame, idCommand.AutoIdFrame)
|
|
|
|
|
//sendPacket(vobcNumLife, idCommand.AutoIdFrame)
|
2024-05-29 11:48:04 +08:00
|
|
|
|
} else if frameType == message.REQUEST_TYPE {
|
2024-06-18 17:10:21 +08:00
|
|
|
|
if vobcNumLife <= 0 {
|
|
|
|
|
return
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Println(fmt.Sprintf("准备发送vobcLife:%v", vobcNumLife))
|
|
|
|
|
}
|
2024-05-29 11:48:04 +08:00
|
|
|
|
req := &message.BtmVobcReq{}
|
|
|
|
|
req.Decode(dataText)
|
2024-06-18 17:10:21 +08:00
|
|
|
|
fmt.Println(fmt.Sprintf("接受 请求帧 frameStatus:%v,messageType:%v,lifeNum:%v,序列号:%v", req.FrameStatus, req.MessageType, req.VobcLifeNum, req.MessageSerial))
|
|
|
|
|
if time.Now().UnixMilli()-btmReceiveTime > 20*1000 {
|
|
|
|
|
idCommand := &message.BtmVobcIdCommand{}
|
|
|
|
|
idCommand.Decode(dataText)
|
|
|
|
|
sendPacket(vobcNumLife, idCommand.AutoIdFrame)
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
RequestFramePackets(req, vobcNumLife)
|
|
|
|
|
}
|
2024-05-27 14:59:17 +08:00
|
|
|
|
} else {
|
2024-06-06 17:57:30 +08:00
|
|
|
|
slog.Error(fmt.Sprintf("btm vobc 解析未知命令帧类型:0x%v,原始数据:%v,长度:%v", strconv.FormatInt(int64(frameType), 16), hex.EncodeToString(cfs), len(cfs)))
|
2024-05-29 11:48:04 +08:00
|
|
|
|
return
|
2024-05-27 14:59:17 +08:00
|
|
|
|
}
|
2024-05-29 11:48:04 +08:00
|
|
|
|
|
2024-05-27 14:59:17 +08:00
|
|
|
|
}
|
2024-05-29 11:48:04 +08:00
|
|
|
|
|
2024-05-27 14:59:17 +08:00
|
|
|
|
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}
|
2024-05-20 15:22:58 +08:00
|
|
|
|
var index int64
|
|
|
|
|
for {
|
2024-05-27 14:59:17 +08:00
|
|
|
|
data := ac.Encode()
|
|
|
|
|
//fmt.Println(hex.EncodeToString(data))
|
|
|
|
|
err := client.Send(data)
|
2024-05-20 15:22:58 +08:00
|
|
|
|
index++
|
|
|
|
|
if index%10 == 0 {
|
2024-05-27 14:59:17 +08:00
|
|
|
|
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
|
2024-05-20 15:22:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error("发送数据失败", "err", err)
|
|
|
|
|
}
|
|
|
|
|
time.Sleep(time.Millisecond * 200)
|
|
|
|
|
}
|
2024-05-27 14:59:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 加速度计
|
|
|
|
|
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)
|
|
|
|
|
}
|
2024-05-29 11:48:04 +08:00
|
|
|
|
time.Sleep(time.Millisecond * 11)
|
2024-05-27 14:59:17 +08:00
|
|
|
|
}
|
2024-05-20 15:22:58 +08:00
|
|
|
|
|
|
|
|
|
}
|