109 lines
2.3 KiB
Go
109 lines
2.3 KiB
Go
package dynamics
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"joylink.club/bj-rtsts-server/config"
|
|
)
|
|
|
|
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()
|
|
}
|
|
|
|
func SendRemoveTrainReq(info *InitTrainInfo) (int, *[]byte, error) {
|
|
baseUrl := getUrlBase()
|
|
uri := "/api/aerodynamics/remove/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()
|
|
}
|
|
|
|
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
|
|
}
|