|
@@ -2,13 +2,15 @@ package com.zhiyun.project.item.service;
|
|
|
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+
|
|
|
+import com.zhiyun.common.utils.http.HttpUtils;
|
|
|
import com.zhiyun.project.item.domain.entity.Device;
|
|
|
import com.zhiyun.project.item.domain.vo.CoordinateVo;
|
|
|
import com.zhiyun.project.item.domain.vo.DeviceVo;
|
|
|
import com.zhiyun.project.item.domain.vo.OptionsVo;
|
|
|
import com.zhiyun.project.item.mapper.DeviceMapper;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.time.Duration;
|
|
@@ -16,11 +18,15 @@ import java.time.LocalDateTime;
|
|
|
import java.util.List;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
+import java.net.*;
|
|
|
+import java.io.*;
|
|
|
@Service
|
|
|
public class DeviceService extends ServiceImpl<DeviceMapper, Device> {
|
|
|
|
|
|
public DeviceVo getDeviceData(Integer id){
|
|
|
|
|
|
+
|
|
|
+
|
|
|
Device device = baseMapper.selectById(id);
|
|
|
|
|
|
if(device == null)
|
|
@@ -89,23 +95,75 @@ public class DeviceService extends ServiceImpl<DeviceMapper, Device> {
|
|
|
device.setY(jsonObject.getDouble("y"));
|
|
|
device.setZ(jsonObject.getDouble("z"));
|
|
|
|
|
|
+ // 获取原始经纬度
|
|
|
+ double lat = device.getLat();
|
|
|
+ double lon = device.getLon();
|
|
|
+
|
|
|
+ // 调用坐标转换方法
|
|
|
+ String[] convertedCoords = convertCoordinates(lat, lon);
|
|
|
+ if (convertedCoords != null && convertedCoords.length == 2) {
|
|
|
+ // 更新 Device 对象中的 x, y 坐标
|
|
|
+ device.setLon(Double.parseDouble(convertedCoords[0])); // 转换后的经度
|
|
|
+ device.setLat(Double.parseDouble(convertedCoords[1])); // 转换后的纬度
|
|
|
+ } else {
|
|
|
+ System.out.println("坐标转换失败,无法更新设备坐标");
|
|
|
+ return -1; // 如果转换失败,返回失败标志
|
|
|
+ }
|
|
|
+
|
|
|
// 如果需要处理 latestTime 字段,可以设置为当前时间,或者从 JSON 中获取时间字段
|
|
|
// 这里暂时设置为当前时间
|
|
|
LocalDateTime now = LocalDateTime.now(); // 获取当前的 LocalDateTime
|
|
|
device.setLatestTime(now); // 设置到 Device 对象中
|
|
|
|
|
|
- //数据库操作判断
|
|
|
+ // 数据库操作判断
|
|
|
Device judgeObject = baseMapper.getDeviceById(device.getDeviceid());
|
|
|
if (judgeObject == null) {
|
|
|
+ // 如果设备不存在,插入新设备
|
|
|
return baseMapper.insert(device);
|
|
|
} else {
|
|
|
+ // 如果设备已存在,更新设备信息
|
|
|
device.setId(judgeObject.getId());
|
|
|
- System.out.println(device);
|
|
|
+ System.out.println("更新设备: " + device);
|
|
|
return baseMapper.updateById(device);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
public List<OptionsVo> getDeviceOptions() {
|
|
|
return baseMapper.getDeviceOptions();
|
|
|
}
|
|
|
+
|
|
|
+ private String[] convertCoordinates(double lat, double lon) {
|
|
|
+ // 高德API的Key
|
|
|
+ String apiKey = "3b37a2c8ae14c7d0382183bf94ddf654";
|
|
|
+ // 构造请求参数
|
|
|
+ String url = "https://restapi.amap.com/v3/assistant/coordinate/convert";
|
|
|
+ String param = "key=" + apiKey + "&locations=" + lon + "," + lat + "&coordsys=gps&output=JSON";
|
|
|
+
|
|
|
+ // 发送GET请求获取转换后的坐标
|
|
|
+ String response = HttpUtils.sendGet(url, param);
|
|
|
+ System.out.println("坐标转换响应: " + response);
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 解析API返回的JSON响应
|
|
|
+ JSONObject jsonResponse = JSONObject.parseObject(response);
|
|
|
+ if ("1".equals(jsonResponse.getString("status"))) {
|
|
|
+ // 请求成功,获取转换后的坐标
|
|
|
+ String convertedLocations = jsonResponse.getString("locations"); // 转换后的坐标
|
|
|
+ return convertedLocations.split(",");
|
|
|
+ } else {
|
|
|
+ // 请求失败,打印错误信息
|
|
|
+ String errorMessage = jsonResponse.getString("info");
|
|
|
+ String errorCode = jsonResponse.getString("infocode");
|
|
|
+ System.out.println("坐标转换失败: " + errorMessage + " (错误码: " + errorCode + ")");
|
|
|
+ return null; // 转换失败
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null; // 出现异常时返回 null
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|