|
@@ -1,139 +1,234 @@
|
|
|
package com.sooka.sponest.songhuahu.exchange.controller;
|
|
|
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.ruoyi.common.core.domain.R;
|
|
|
-import com.ruoyi.common.core.exception.file.FileException;
|
|
|
-import com.ruoyi.common.core.utils.file.FilePrefixUtils;
|
|
|
-import com.ruoyi.common.security.utils.SecurityUtils;
|
|
|
-import com.ruoyi.system.api.RemoteConfigService;
|
|
|
-import com.ruoyi.system.api.RemoteFileService;
|
|
|
-import com.ruoyi.system.api.domain.SysFile;
|
|
|
-import com.sooka.sponest.songhuahu.exchange.domian.DataExchangeEntity;
|
|
|
-import com.sooka.sponest.songhuahu.exchange.domian.LoginBody;
|
|
|
-import com.sooka.sponest.songhuahu.remoteapi.service.center.auth.RemoteAuthBaseService;
|
|
|
-import com.sooka.sponest.songhuahu.remoteapi.service.center.event.RemoteEventBaseService;
|
|
|
+import com.ruoyi.common.core.web.domain.AjaxResult;
|
|
|
+import com.sooka.sponest.songhuahu.constant.Constants;
|
|
|
+import com.sooka.sponest.songhuahu.exchange.domian.GPSEquipment;
|
|
|
+import com.sooka.sponest.songhuahu.exchange.domian.ShipSaveDTO;
|
|
|
+import com.sooka.sponest.songhuahu.redis.RedisCache;
|
|
|
+import com.sooka.sponest.songhuahu.util.MD5Encryptor;
|
|
|
import com.sooka.sponest.songhuahu.util.RestUtil;
|
|
|
-import com.sooka.sponest.songhuahu.util.spring.SpringUtils;
|
|
|
+import com.sooka.sponest.songhuahu.util.URLEncodeExampleUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestBody;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
-import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
-import javax.annotation.Resource;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+import static com.ruoyi.common.core.web.domain.AjaxResult.error;
|
|
|
+import static com.ruoyi.common.core.web.domain.AjaxResult.success;
|
|
|
|
|
|
@Slf4j
|
|
|
@Controller
|
|
|
@RequestMapping("/data/exchange")
|
|
|
public class ExchangeController {
|
|
|
|
|
|
- @Resource
|
|
|
- private RemoteAuthBaseService remoteAuthBaseService;
|
|
|
- @Resource
|
|
|
- private RemoteEventBaseService remoteEventBaseService;
|
|
|
- @Resource
|
|
|
- private RemoteConfigService remoteConfigService;
|
|
|
+ @Autowired
|
|
|
+ private RedisCache redisCache;
|
|
|
+
|
|
|
+ private final List<String> keys = Arrays.asList("gps_address", "app_key", "app_secret", "account", "password");
|
|
|
+
|
|
|
+ private final String ip = "http://127.0.0.1";
|
|
|
+
|
|
|
+ private final String port = "10001";
|
|
|
|
|
|
- // @Value("${}")
|
|
|
- private String url;
|
|
|
|
|
|
/**
|
|
|
- * 获取秘钥接口
|
|
|
+ * 获取访问令牌
|
|
|
*/
|
|
|
- @PostMapping("/getRemoteSecretKey")
|
|
|
- @ResponseBody
|
|
|
- public R<?> getRemoteSecretKey() {
|
|
|
- R<?> result = remoteAuthBaseService.getRemoteSecretKey();
|
|
|
- log.info("result:{}", result.getData());
|
|
|
- if (result.getCode() == 200) {
|
|
|
- return R.ok(result.getData().toString(), "获取成功");
|
|
|
- } else {
|
|
|
- return R.fail("获取失败");
|
|
|
+ public AjaxResult getToken() {
|
|
|
+ Date date = new Date();
|
|
|
+ String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
|
|
|
+ String dateStr = format.replace(" ", "%20");
|
|
|
+ Map<String, String> configMap = getStringStringMap();
|
|
|
+ String password = MD5Encryptor.md5EncryptToLowercase(configMap.get("password").toString());
|
|
|
+ String sign = MD5Encryptor.md5EncryptToUppercase(configMap.get("app_secret") +
|
|
|
+ "account" + configMap.get("account") +
|
|
|
+ "app_key" + configMap.get("app_key") +
|
|
|
+ "formatjsonmethodwicare.user.access_token" +
|
|
|
+ "password" + password +
|
|
|
+ "sign_methodmd5" +
|
|
|
+ "timestamp" + dateStr +
|
|
|
+ "type2v2.0" +
|
|
|
+ configMap.get("app_secret"));
|
|
|
+ String apiUrl = configMap.get("gps_address") +
|
|
|
+ "account=" + configMap.get("account") +
|
|
|
+ "&app_key=" + configMap.get("app_key") +
|
|
|
+ "&format=json&method=wicare.user.access_token&" +
|
|
|
+ "password=" + password +
|
|
|
+ "&sign_method=md5&" +
|
|
|
+ "timestamp=" + dateStr +
|
|
|
+ "&type=2&v=2.0&" +
|
|
|
+ "sign=" + sign;
|
|
|
+ try {
|
|
|
+ // 创建 URL 对象
|
|
|
+ URL url = new URL(apiUrl);
|
|
|
+ // 打开连接
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
+ // 设置请求方法
|
|
|
+ connection.setRequestMethod("GET");
|
|
|
+ // 获取响应状态码
|
|
|
+ int responseCode = connection.getResponseCode();
|
|
|
+ if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
|
+ // 读取响应内容
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
+ String line;
|
|
|
+ StringBuilder response = new StringBuilder();
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ response.append(line);
|
|
|
+ }
|
|
|
+ reader.close();
|
|
|
+ // 存入redis中
|
|
|
+ redisCache.setCacheObject("access_token", JSONObject.parseObject(response.toString()).get("access_token"), Constants.GPS_ACCESS_TOKEN_EXPIRATION, TimeUnit.MINUTES);
|
|
|
+ return success(JSONObject.parseObject(response.toString()).get("access_token"));
|
|
|
+ } else {
|
|
|
+ log.error("获取访问令牌接口返回错误代码:{}", responseCode);
|
|
|
+ }
|
|
|
+ // 关闭连接
|
|
|
+ connection.disconnect();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取访问令牌接口失败:{}", e);
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
+ return error("获取访问令牌接口失败");
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 获取token
|
|
|
- */
|
|
|
- @PostMapping("/getToken")
|
|
|
- @ResponseBody
|
|
|
- public R<?> getToken(@RequestBody LoginBody form) {
|
|
|
- R<?> result = remoteAuthBaseService.loginRemote(form);
|
|
|
- log.info("result:{}", result.getData());
|
|
|
- if (result.getCode() == 200) {
|
|
|
- Map<String, Object> json = (Map<String, Object>) result.getData();
|
|
|
- return R.ok(json.get("access_token").toString(), "获取成功");
|
|
|
+
|
|
|
+ @PostMapping("/getEquipment")
|
|
|
+ public void getEquipment() {
|
|
|
+ log.info("获取单个设备或多个设备的信息开始");
|
|
|
+ AjaxResult result;
|
|
|
+ Object obj;
|
|
|
+ if (!redisCache.hasKey("access_token")) {
|
|
|
+ result = getToken();
|
|
|
+ obj = result.get("data");
|
|
|
} else {
|
|
|
- return R.fail("获取失败");
|
|
|
+ obj = redisCache.getCacheObject("access_token");
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- @PostMapping("/straightPushData")
|
|
|
- @ResponseBody
|
|
|
- public R<?> straightPushData(@RequestBody DataExchangeEntity dataExchangeEntity) {
|
|
|
- log.info("dataExchangeEntity:{}", dataExchangeEntity);
|
|
|
- List<String> pictureList = new ArrayList<>();
|
|
|
- if (StringUtils.isNotEmpty(SecurityUtils.getToken()) && !CollectionUtils.isEmpty(dataExchangeEntity.getEventPicture())) {
|
|
|
- log.info("token:{}", StringUtils.isNotEmpty(SecurityUtils.getToken()));
|
|
|
- for (String picture : dataExchangeEntity.getEventPicture()) {
|
|
|
- if (!"image".equals(FilePrefixUtils.getUrlSuffix(picture))) {
|
|
|
- log.error("事件图片必须为图片格式---{}", dataExchangeEntity);
|
|
|
- return R.fail("事件图片必须为图片格式");
|
|
|
- } else {
|
|
|
- MultipartFile multipartFile = FilePrefixUtils.urlToMultipartFile(picture, System.currentTimeMillis() + ".jpg");
|
|
|
- log.info("multipartFile:{}", multipartFile);
|
|
|
- try {
|
|
|
- SysFile sysFile = SpringUtils.getBean(RemoteFileService.class).upload(multipartFile).getData();
|
|
|
- log.info("sysFile:{}", sysFile);
|
|
|
- pictureList.add(sysFile.getUrl());
|
|
|
- } catch (FileException e) {
|
|
|
- log.info("上传附件失败");
|
|
|
- }
|
|
|
|
|
|
+ Date date = new Date();
|
|
|
+ String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
|
|
|
+ Map<String, String> configMap = getStringStringMap();
|
|
|
+ String dateStr = format.replace(" ", "%20");
|
|
|
+ String sign = MD5Encryptor.md5EncryptToUppercase(configMap.get("app_secret") +
|
|
|
+ "access_token" + obj +
|
|
|
+ "app_key" + configMap.get("app_key") +
|
|
|
+ "dev_key59346d400236ab95e95193f35f3df6a4" +
|
|
|
+ "fieldsdid,activeGpsDataformatjsonlimit-1mapBAIDU" +
|
|
|
+ "methodwicare.device.listpagedidpage_no1sign_methodmd5sortsdid" +
|
|
|
+ "timestamp" + dateStr +
|
|
|
+ "v2.0" +
|
|
|
+ configMap.get("app_secret"));
|
|
|
+ log.info("singStr:{}", configMap.get("app_secret") +
|
|
|
+ "access_token" + obj +
|
|
|
+ "app_key" + configMap.get("app_key") +
|
|
|
+ "dev_key59346d400236ab95e95193f35f3df6a4" +
|
|
|
+ "fieldsdid,activeGpsDataformatjsonlimit-1mapBAIDU" +
|
|
|
+ "methodwicare.device.listpagedidpage_no1sign_methodmd5sortsdid" +
|
|
|
+ "timestamp" + dateStr +
|
|
|
+ "v2.0" +
|
|
|
+ configMap.get("app_secret"));
|
|
|
+ log.info("sign:{}", sign);
|
|
|
+ String apiUrl = configMap.get("gps_address") +
|
|
|
+ "timestamp=" + dateStr +
|
|
|
+ "&format=json&" +
|
|
|
+ "app_key=" +
|
|
|
+ configMap.get("app_key") +
|
|
|
+ "&v=2.0&sign_method=md5&method=wicare.device.list&dev_key=59346d400236ab95e95193f35f3df6a4&access_token=" + URLEncodeExampleUtil.URLEncodeExample(obj) +
|
|
|
+ "&map=BAIDU&fields=did,activeGpsData&sorts=did&page=did&page_no=1&limit=-1&sign=" +
|
|
|
+ sign;
|
|
|
+ log.info("apiUrl:{}", apiUrl);
|
|
|
+ log.info("URLEncodeExampleUtil.URLEncodeExample(obj):{}", URLEncodeExampleUtil.URLEncodeExample(obj));
|
|
|
+ try {
|
|
|
+ // 创建 URL 对象
|
|
|
+ URL url = new URL(apiUrl);
|
|
|
+ // 打开连接
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
+ // 设置请求方法
|
|
|
+ connection.setRequestMethod("GET");
|
|
|
+ // 获取响应状态码
|
|
|
+ int responseCode = connection.getResponseCode();
|
|
|
+ log.info("connection.getResponseMessage():{}", connection.getResponseMessage());
|
|
|
+ log.info("connection:{}", connection);
|
|
|
+ if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
|
+ // 读取响应内容
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
+ String line;
|
|
|
+ StringBuilder response = new StringBuilder();
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ response.append(line);
|
|
|
+ }
|
|
|
+ log.info("获取单个设备或多个设备的信息返回信息:{}", response);
|
|
|
+ reader.close();
|
|
|
+ List<GPSEquipment> objectList = JSONArray.parseArray(JSONObject.parseObject(response.toString()).getString("data").toString(), GPSEquipment.class);
|
|
|
+ List<ShipSaveDTO> saveList = new ArrayList<>();
|
|
|
+ for (GPSEquipment var : objectList) {
|
|
|
+ ShipSaveDTO saveEntity = new ShipSaveDTO();
|
|
|
+ saveEntity.setDid(var.getDid());
|
|
|
+ saveEntity.setLongitude(String.valueOf(var.getActiveGpsData().getLon()));
|
|
|
+ saveEntity.setLatitude(String.valueOf(var.getActiveGpsData().getLat()));
|
|
|
+ saveEntity.setGpsFlag(var.getActiveGpsData().getGpsFlag());
|
|
|
+ saveEntity.setLon(var.getActiveGpsData().get_lon());
|
|
|
+ saveEntity.setLat(var.getActiveGpsData().get_lat());
|
|
|
+ saveEntity.setSignal(var.getActiveGpsData().getSignal());
|
|
|
+ saveEntity.setSpeed(var.getActiveGpsData().getSpeed());
|
|
|
+ saveEntity.setDirect(var.getActiveGpsData().getDirect());
|
|
|
+ saveEntity.setMileage(var.getActiveGpsData().getMileage());
|
|
|
+ saveEntity.setBattery(var.getActiveGpsData().getBattery());
|
|
|
+ saveEntity.setPower(var.getActiveGpsData().getPower());
|
|
|
+ saveEntity.setStatus(String.join(",", var.getActiveGpsData().getStatus().stream().map(String::valueOf).collect(Collectors.toList())));
|
|
|
+ saveEntity.setAlerts(String.join(",", var.getActiveGpsData().getAlerts().stream().map(String::valueOf).collect(Collectors.toList())));
|
|
|
+ saveEntity.setGpsTime(var.getActiveGpsData().getGpsTime());
|
|
|
+ saveEntity.setRcvTime(var.getActiveGpsData().getRcvTime());
|
|
|
+ saveList.add(saveEntity);
|
|
|
+ }
|
|
|
+ if (!CollectionUtils.isEmpty(saveList)) {
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("list", saveList);
|
|
|
+ JSONObject json = RestUtil.post(ip + ":" + port + "/shipMapping/remote/saveCruiseShipMapping", jsonObject);
|
|
|
+ System.out.println("result" + json);
|
|
|
}
|
|
|
- }
|
|
|
- dataExchangeEntity.setPictureUrlList(pictureList);
|
|
|
- R<?> result = remoteEventBaseService.insert3rdSystemEvent(dataExchangeEntity);
|
|
|
- log.info("insert3rdSystemEvent:{}", result);
|
|
|
- if (result.getCode() == 200) {
|
|
|
- return R.ok(null, "推送成功");
|
|
|
} else {
|
|
|
- return R.fail(result.getMsg());
|
|
|
+ log.error("获取单个设备或多个设备的信息返回错误代码:{}", responseCode);
|
|
|
}
|
|
|
- } else {
|
|
|
- return R.fail("推送失败");
|
|
|
+ // 关闭连接
|
|
|
+ connection.disconnect();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取单个设备或多个设备的信息接口失败:{}", e);
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
+ log.info("获取单个设备或多个设备的信息结束");
|
|
|
}
|
|
|
|
|
|
|
|
|
- @PostMapping("/receiveEventData")
|
|
|
- @ResponseBody
|
|
|
- public R<?> receiveEventData(@RequestBody DataExchangeEntity dataExchangeEntity) {
|
|
|
- System.out.println(dataExchangeEntity);
|
|
|
- R<String> appUrl = remoteConfigService.remotegetConfigKey("appUrl");
|
|
|
- List<String> pictureList = new ArrayList<>();
|
|
|
- for (String picture : dataExchangeEntity.getEventPicture()) {
|
|
|
- pictureList.add(appUrl.getData().toString() + picture);
|
|
|
- }
|
|
|
- dataExchangeEntity.setEventPicture(pictureList);
|
|
|
- // TODO: 2024/1/9 0009 待修改
|
|
|
+ /**
|
|
|
+ * 根据参数编号获取详细信息
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Map<String, String> getStringStringMap() {
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
- jsonObject.put("", dataExchangeEntity);
|
|
|
- try {
|
|
|
- JSONObject result = RestUtil.post(url, jsonObject);
|
|
|
- log.info("远程调用119:{}", result);
|
|
|
- } catch (Exception e) {
|
|
|
- log.info("远程调用119报错");
|
|
|
+ jsonObject.put("keys", keys);
|
|
|
+ JSONObject result = RestUtil.post(ip + ":" + port + "/system/config/remote/configKey", jsonObject);
|
|
|
+ if (result.get("code").equals(200)) {
|
|
|
+ Map<String, String> configMap = (Map<String, String>) result.get("data");
|
|
|
+ return configMap;
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
}
|
|
|
- return R.ok(null, "接收成功");
|
|
|
}
|
|
|
|
|
|
+
|
|
|
}
|