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

86 lines
1.8 KiB
Go
Raw Normal View History

2023-07-26 17:02:53 +08:00
package dynamics
import (
"bytes"
"encoding/json"
"fmt"
"joylink.club/bj-rtsts-server/config"
2023-07-26 17:02:53 +08:00
"net/http"
"time"
2023-07-26 17:02:53 +08:00
)
func SendInitTrainReq(info *InitTrainInfo) (int, *[]byte, error) {
baseUrl := getUrlBase()
uri := "/api/aerodynamics/init/train/"
url := baseUrl + uri
data, _ := json.Marshal(info)
client := http.Client{
Timeout: time.Second * 5,
}
resp, err := client.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
}
return resp.StatusCode, &buf, resp.Body.Close()
}
2023-07-26 17:02:53 +08:00
func SendSimulationStartReq(base *LineBaseInfo) (int, *[]byte, error) {
baseUrl := getUrlBase()
uri := "/api/start/"
url := baseUrl + uri
data, _ := json.Marshal(base)
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
}
func SendSimulationEndReq() (int, *[]byte, error) {
baseUrl := getUrlBase()
2023-08-09 18:02:08 +08:00
uri := "/api/end/"
url := baseUrl + uri
resp, err := http.Post(url, "application/json", nil)
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
}
return resp.StatusCode, &buf, resp.Body.Close()
}
var (
urlBase string
)
func getUrlBase() string {
if urlBase == "" {
ip := config.Config.Dynamics.Ip
var port string
if config.Config.Dynamics.HttpPort != 0 {
port = fmt.Sprintf(":%d", config.Config.Dynamics.HttpPort)
}
urlBase = "http://" + ip + port
}
return urlBase
}