rts-sim-testing-service/dynamics/http.go

40 lines
875 B
Go
Raw Normal View History

2023-07-26 17:02:53 +08:00
package dynamics
import (
"bytes"
"encoding/json"
"fmt"
"go.uber.org/zap"
"joylink.club/bj-rtsts-server/config"
2023-07-26 17:02:53 +08:00
"net/http"
)
func SendInitTrainReq(info *InitTrainInfo) (int, *[]byte, error) {
defer func() {
if r := recover(); r != nil {
zap.S().Error("发送列车初始化请求失败", r)
}
}()
ip := config.Config.Dynamics.Ip
var port string
if config.Config.Dynamics.HttpPort != 0 {
port = fmt.Sprintf(":%d", config.Config.Dynamics.HttpPort)
}
2023-07-26 17:02:53 +08:00
uri := "/api/aerodynamics/init/train"
url := "http://" + ip + port + uri
2023-07-26 17:02:53 +08:00
data, _ := json.Marshal(info)
resp, err := http.Post(url, "application/json", bytes.NewBuffer(data))
if err != nil {
s := err.Error()
println(s)
return 0, nil, err
}
var buf []byte
_, err = resp.Body.Read(buf)
if err != nil {
return resp.StatusCode, nil, err
2023-07-26 17:02:53 +08:00
}
return resp.StatusCode, &buf, resp.Body.Close()
2023-07-26 17:02:53 +08:00
}