现场加速度计测试连接

This commit is contained in:
tiger_zhou 2024-05-20 15:22:58 +08:00
parent 11a14b2ef6
commit 06f8445a98
3 changed files with 73 additions and 0 deletions

61
bin/acc_conn-example.go Normal file
View File

@ -0,0 +1,61 @@
package main
import (
"fmt"
"github.com/spf13/viper"
"joylink.club/bj-rtsts-server/bin/config"
"joylink.club/bj-rtsts-server/third_party/message"
"joylink.club/bj-rtsts-server/third_party/udp"
"log/slog"
"time"
)
const config_name = "example"
var exampleConfig config.ExampleConfig
func initConfig() {
cnf := viper.New()
cnf.SetConfigName(config_name)
cnf.SetConfigType("yml")
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)
}
func main() {
initConfig()
testAcc()
}
func testAcc() {
var client udp.UdpClient
defer func() {
if client != nil {
client.Close()
}
slog.Info("连接关闭")
}()
client = udp.NewClient(fmt.Sprintf("%v:%v", exampleConfig.Acc.RemoteIp, exampleConfig.Acc.RemotePort))
ac := message.Accelerometer{Acc: 1}
var index int64
for {
err := client.Send(ac.Encode())
index++
if index%10 == 0 {
slog.Info(fmt.Sprintf("发送数据,时间戳:%v,发送次数:%v", time.Now().Unix(), index))
}
if err != nil {
slog.Error("发送数据失败", "err", err)
}
time.Sleep(time.Millisecond * 200)
}
}

3
bin/config/example.yml Normal file
View File

@ -0,0 +1,3 @@
acc:
remoteIp: "127.0.0.1"
remotePort: 1234

View File

@ -0,0 +1,9 @@
package config
type ExampleConfig struct {
Acc acc
}
type acc struct {
RemoteIp string
RemotePort int
}