2023-07-26 17:02:53 +08:00
|
|
|
package dynamics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2023-07-27 17:12:38 +08:00
|
|
|
"fmt"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"joylink.club/bj-rtsts-server/config"
|
2023-07-26 17:02:53 +08:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2023-08-02 15:47:48 +08:00
|
|
|
func SendInitTrainReq(info *InitTrainInfo) (int, *[]byte, error) {
|
2023-07-27 17:12:38 +08:00
|
|
|
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"
|
2023-07-27 17:12:38 +08:00
|
|
|
url := "http://" + ip + port + uri
|
2023-07-26 17:02:53 +08:00
|
|
|
|
|
|
|
data, _ := json.Marshal(info)
|
2023-08-02 15:47:48 +08:00
|
|
|
resp, err := http.Post(url, "application/json", bytes.NewBuffer(data))
|
|
|
|
if err != nil {
|
|
|
|
s := err.Error()
|
|
|
|
println(s)
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
2023-07-27 17:12:38 +08:00
|
|
|
var buf []byte
|
2023-08-02 15:47:48 +08:00
|
|
|
_, err = resp.Body.Read(buf)
|
2023-07-27 17:12:38 +08:00
|
|
|
if err != nil {
|
|
|
|
return resp.StatusCode, nil, err
|
2023-07-26 17:02:53 +08:00
|
|
|
}
|
2023-07-27 17:12:38 +08:00
|
|
|
return resp.StatusCode, &buf, resp.Body.Close()
|
2023-07-26 17:02:53 +08:00
|
|
|
}
|