add send
This commit is contained in:
@ -10,10 +10,21 @@ import com.sczx.sync.service.ElectronicFenceService;
|
|||||||
import com.sczx.sync.service.ThirdPartyForwardService;
|
import com.sczx.sync.service.ThirdPartyForwardService;
|
||||||
import com.sczx.sync.utils.Point;
|
import com.sczx.sync.utils.Point;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.http.HttpEntity;
|
||||||
|
import org.apache.http.client.config.RequestConfig;
|
||||||
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
|
import org.apache.http.client.methods.HttpPost;
|
||||||
|
import org.apache.http.entity.StringEntity;
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
import org.apache.http.impl.client.HttpClients;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import com.sczx.sync.utils.RedisUtil;
|
import com.sczx.sync.utils.RedisUtil;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
@ -33,8 +44,6 @@ public class ElectronicFenceServiceImpl implements ElectronicFenceService {
|
|||||||
@Value("${iot-url}")
|
@Value("${iot-url}")
|
||||||
private String URL;
|
private String URL;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ThirdPartyForwardService thirdPartyForwardService;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void checkCarPositionInFence(DeviceData deviceLocation) {
|
public void checkCarPositionInFence(DeviceData deviceLocation) {
|
||||||
@ -155,7 +164,7 @@ public class ElectronicFenceServiceImpl implements ElectronicFenceService {
|
|||||||
commandDTO.setClientld(clientId);
|
commandDTO.setClientld(clientId);
|
||||||
commandDTO.setCommand(3);
|
commandDTO.setCommand(3);
|
||||||
commandDTO.setProviderld(Integer.parseInt(iotBrands));
|
commandDTO.setProviderld(Integer.parseInt(iotBrands));
|
||||||
String response = thirdPartyForwardService.forwardData(URL+"/device/8304", JSON.toJSONString(commandDTO));
|
String response = forwardData(URL+"/device/8304", JSON.toJSONString(commandDTO));
|
||||||
log.info("发送通电指令给车辆: {} , 响应: {}" + clientId ,response);
|
log.info("发送通电指令给车辆: {} , 响应: {}" + clientId ,response);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -172,7 +181,70 @@ public class ElectronicFenceServiceImpl implements ElectronicFenceService {
|
|||||||
commandDTO.setProviderld(Integer.parseInt(iotBrands));
|
commandDTO.setProviderld(Integer.parseInt(iotBrands));
|
||||||
|
|
||||||
|
|
||||||
String response = thirdPartyForwardService.forwardData(URL+"/device/8304", JSON.toJSONString(commandDTO));
|
String response = forwardData(URL+"/device/8304", JSON.toJSONString(commandDTO));
|
||||||
log.info("发送断电指令给车辆: {} , 响应: {}" + clientId ,response);
|
log.info("发送断电指令给车辆: {} , 响应: {}" + clientId ,response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private String forwardData(String url,String data) {
|
||||||
|
CloseableHttpClient httpClient = null;
|
||||||
|
CloseableHttpResponse response = null;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 创建HTTP客户端
|
||||||
|
httpClient = HttpClients.createDefault();
|
||||||
|
// 创建POST请求
|
||||||
|
HttpPost httpPost = new HttpPost(url);
|
||||||
|
|
||||||
|
// 设置请求头
|
||||||
|
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
|
||||||
|
httpPost.setHeader("User-Agent", "SczxSync/1.0");
|
||||||
|
|
||||||
|
|
||||||
|
StringEntity entity = new StringEntity(JSON.toJSONString(data), StandardCharsets.UTF_8);
|
||||||
|
httpPost.setEntity(entity);
|
||||||
|
|
||||||
|
// 设置超时配置
|
||||||
|
RequestConfig requestConfig = RequestConfig.custom()
|
||||||
|
.setConnectTimeout(30000)
|
||||||
|
.setSocketTimeout(30000)
|
||||||
|
.build();
|
||||||
|
httpPost.setConfig(requestConfig);
|
||||||
|
|
||||||
|
log.info("开始转发数据到第三方平台: {}, 数据: {}", url, data);
|
||||||
|
|
||||||
|
// 执行请求
|
||||||
|
response = httpClient.execute(httpPost);
|
||||||
|
HttpEntity responseEntity = response.getEntity();
|
||||||
|
String responseBody = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
|
if (statusCode == 200) {
|
||||||
|
log.info("数据转发成功,响应: {}", responseBody);
|
||||||
|
return responseBody;
|
||||||
|
} else {
|
||||||
|
log.error("数据转发失败,状态码: {}, 响应: {}", statusCode, responseBody);
|
||||||
|
throw new RuntimeException("第三方平台响应异常,状态码: " + statusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("数据转发异常", e);
|
||||||
|
throw new RuntimeException("数据转发异常: " + e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
// 关闭资源
|
||||||
|
try {
|
||||||
|
if (response != null) {
|
||||||
|
response.close();
|
||||||
|
}
|
||||||
|
if (httpClient != null) {
|
||||||
|
httpClient.close();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("关闭HTTP客户端异常", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user