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

82 lines
1.7 KiB
Go

package dynamics
import (
"bytes"
"encoding/json"
"fmt"
"joylink.club/bj-rtsts-server/config"
"net/http"
)
func SendInitTrainReq(info *InitTrainInfo) (int, *[]byte, error) {
baseUrl := getUrlBase()
uri := "/api/aerodynamics/init/train/"
url := baseUrl + uri
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
}
return resp.StatusCode, &buf, resp.Body.Close()
}
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
}
return resp.StatusCode, &buf, resp.Body.Close()
}
func SendSimulationEndReq() (int, *[]byte, error) {
baseUrl := getUrlBase()
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
}