2023-07-26 17:02:53 +08:00
|
|
|
package dynamics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2023-07-27 17:12:38 +08:00
|
|
|
"fmt"
|
2023-07-26 17:02:53 +08:00
|
|
|
"net/http"
|
2023-08-09 15:34:19 +08:00
|
|
|
"time"
|
2023-08-16 09:29:28 +08:00
|
|
|
|
|
|
|
"joylink.club/bj-rtsts-server/config"
|
2023-07-26 17:02:53 +08:00
|
|
|
)
|
|
|
|
|
2023-08-02 15:47:48 +08:00
|
|
|
func SendInitTrainReq(info *InitTrainInfo) (int, *[]byte, error) {
|
2023-08-07 14:08:02 +08:00
|
|
|
baseUrl := getUrlBase()
|
|
|
|
uri := "/api/aerodynamics/init/train/"
|
|
|
|
url := baseUrl + uri
|
|
|
|
data, _ := json.Marshal(info)
|
2023-08-09 15:34:19 +08:00
|
|
|
client := http.Client{
|
|
|
|
Timeout: time.Second * 5,
|
|
|
|
}
|
|
|
|
resp, err := client.Post(url, "application/json", bytes.NewBuffer(data))
|
2023-08-07 14:08:02 +08:00
|
|
|
if err != nil {
|
|
|
|
s := err.Error()
|
|
|
|
println(s)
|
|
|
|
return 0, nil, err
|
2023-07-27 17:12:38 +08:00
|
|
|
}
|
2023-08-07 14:08:02 +08:00
|
|
|
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
|
|
|
|
2023-08-16 09:29:28 +08:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2023-08-07 14:08:02 +08:00
|
|
|
func SendSimulationStartReq(base *LineBaseInfo) (int, *[]byte, error) {
|
|
|
|
baseUrl := getUrlBase()
|
|
|
|
uri := "/api/start/"
|
|
|
|
url := baseUrl + uri
|
|
|
|
data, _ := json.Marshal(base)
|
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
|
|
|
}
|
2023-08-07 14:08:02 +08:00
|
|
|
|
|
|
|
func SendSimulationEndReq() (int, *[]byte, error) {
|
|
|
|
baseUrl := getUrlBase()
|
2023-08-09 18:02:08 +08:00
|
|
|
uri := "/api/end/"
|
2023-08-07 14:08:02 +08:00
|
|
|
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
|
|
|
|
}
|