海康服务模块补充 修改接口参数 #91
@ -0,0 +1,243 @@
|
|||||||
|
package cn.iocoder.yudao.module.hiking;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||||
|
import cn.iocoder.yudao.module.hiking.controller.admin.camera.vo.CameraSaveReqVO;
|
||||||
|
import cn.iocoder.yudao.module.hiking.controller.admin.region.vo.RegionSaveReqVO;
|
||||||
|
import cn.iocoder.yudao.module.hiking.dal.dataobject.camera.CameraDO;
|
||||||
|
import cn.iocoder.yudao.module.hiking.service.camera.CameraService;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.OffsetDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class GetCameraInfoUtil {
|
||||||
|
@Autowired
|
||||||
|
private RedisTemplate<String, String> redisTemplate;
|
||||||
|
@Resource
|
||||||
|
private CameraService cameraService;
|
||||||
|
private static final String ACCESS_TOKEN_KEY = "hikingAPI:access_token";
|
||||||
|
|
||||||
|
|
||||||
|
public String getAccessToken() {
|
||||||
|
// 检查 Redis 中是否有 access_token
|
||||||
|
String accessToken = redisTemplate.opsForValue().get(ACCESS_TOKEN_KEY);
|
||||||
|
if (!StringUtils.isEmpty(accessToken)) {
|
||||||
|
System.out.println("使用缓存的token:" + accessToken);
|
||||||
|
return accessToken; // 如果存在则直接返回
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果没有,调用 API 获取并存入 Redis
|
||||||
|
System.out.println("获取新的token");
|
||||||
|
String accessTokenUrlResp = IntegrationURL.getAccessTokenURL();
|
||||||
|
try {
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
JsonNode jsonNode = objectMapper.readTree(accessTokenUrlResp);
|
||||||
|
accessToken = jsonNode.get("access_token").asText();
|
||||||
|
|
||||||
|
// 将 access_token 存入 Redis,设置过期时间为 11 小时(有效期12小时)
|
||||||
|
redisTemplate.opsForValue().set(ACCESS_TOKEN_KEY, accessToken, 11, TimeUnit.HOURS);
|
||||||
|
} catch (Exception e) {
|
||||||
|
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return accessToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteAccessToken() {
|
||||||
|
redisTemplate.delete(ACCESS_TOKEN_KEY);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Boolean checkToken(String result) {
|
||||||
|
try {
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
JsonNode jsonNode = objectMapper.readTree(result);
|
||||||
|
String code = jsonNode.path("code").asText();
|
||||||
|
if ("0x02401007".equals(code)) {
|
||||||
|
System.out.println("token失效,重新获取");
|
||||||
|
deleteAccessToken();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/getCameraListToDB")
|
||||||
|
public void getCameraListToDB() {
|
||||||
|
try {
|
||||||
|
// 总记录数
|
||||||
|
int total = 0;
|
||||||
|
JsonNode jsonNode;
|
||||||
|
JsonNode cameraDataList;
|
||||||
|
// 查询有几条记录
|
||||||
|
// String cameraListPageURL = IntegrationURL.getCameraResourceURL(1, 1, getAccessToken());
|
||||||
|
// if (!checkToken(cameraListPageURL)) {
|
||||||
|
// cameraListPageURL = IntegrationURL.getCameraResourceURL(1, 1, getAccessToken());
|
||||||
|
// }
|
||||||
|
String cameraListPageURL = " {\n" +
|
||||||
|
" \"code\": \"0\", \n" +
|
||||||
|
" \"msg\": \"success\", \n" +
|
||||||
|
" \"data\": {\n" +
|
||||||
|
" \"total\": 13, \n" +
|
||||||
|
" \"pageNo\": 1, \n" +
|
||||||
|
" \"pageSize\": 1, \n" +
|
||||||
|
" \"list\": [\n" +
|
||||||
|
" {\n" +
|
||||||
|
" \"altitude\": null, \n" +
|
||||||
|
" \"cameraIndexCode\": \"eddf8458f74d42e9bf4ecfc752dba146\", \n" +
|
||||||
|
" \"cameraName\": \"3层吉米后厨入口\", \n" +
|
||||||
|
" \"cameraType\": 0, \n" +
|
||||||
|
" \"cameraTypeName\": \"枪机\", \n" +
|
||||||
|
" \"capabilitySet\": \"io,event_io,event_ias,event_rule,event_heat,record,net,event_face,vss,ptz,status,maintenance,event_device\", \n" +
|
||||||
|
" \"capabilitySetName\": null, \n" +
|
||||||
|
" \"intelligentSet\": null, \n" +
|
||||||
|
" \"intelligentSetName\": null, \n" +
|
||||||
|
" \"channelNo\": \"33\", \n" +
|
||||||
|
" \"channelType\": \"digital\", \n" +
|
||||||
|
" \"channelTypeName\": \"数字通道\", \n" +
|
||||||
|
" \"createTime\": \"2018-09-15T11:14:27.812+08:00\", \n" +
|
||||||
|
" \"encodeDevIndexCode\": \"1d3d5c26e6174cf1aa452f57cac91879\", \n" +
|
||||||
|
" \"encodeDevResourceType\": null, \n" +
|
||||||
|
" \"encodeDevResourceTypeName\": null, \n" +
|
||||||
|
" \"gbIndexCode\": null, \n" +
|
||||||
|
" \"installLocation\": null, \n" +
|
||||||
|
" \"keyBoardCode\": null, \n" +
|
||||||
|
" \"latitude\": null, \n" +
|
||||||
|
" \"longitude\": null, \n" +
|
||||||
|
" \"pixel\": null, \n" +
|
||||||
|
" \"ptz\": null, \n" +
|
||||||
|
" \"ptzName\": null, \n" +
|
||||||
|
" \"ptzController\": null, \n" +
|
||||||
|
" \"ptzControllerName\": null, \n" +
|
||||||
|
" \"recordLocation\": null, \n" +
|
||||||
|
" \"recordLocationName\": null, \n" +
|
||||||
|
" \"regionIndexCode\": \"2feadc43-ffef-464b-a2e2-b146a02de5ba\", \n" +
|
||||||
|
" \"status\": null, \n" +
|
||||||
|
" \"statusName\": null, \n" +
|
||||||
|
" \"transType\": 1, \n" +
|
||||||
|
" \"transTypeName\": \"TCP\", \n" +
|
||||||
|
" \"treatyType\": null, \n" +
|
||||||
|
" \"treatyTypeName\": null, \n" +
|
||||||
|
" \"viewshed\": null, \n" +
|
||||||
|
" \"updateTime\": \"2018-09-15T11:19:48.973+08:00\"\n" +
|
||||||
|
" }\n" +
|
||||||
|
" ]\n" +
|
||||||
|
" }\n" +
|
||||||
|
"}\n";
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
jsonNode = objectMapper.readTree(cameraListPageURL);
|
||||||
|
// total = jsonNode.path("data").path("total").asInt();
|
||||||
|
total = 1;
|
||||||
|
|
||||||
|
JsonNode cameraNode;
|
||||||
|
// 查询所有记录的cameraIndexCode并保存
|
||||||
|
for (int i = 1; i <= (total / 100) + 1; i++) {
|
||||||
|
System.out.println("开始第" + i + "次查询");
|
||||||
|
// cameraListPageURL = IntegrationURL.getCameraResourceURL(i, 100, getAccessToken());
|
||||||
|
jsonNode = objectMapper.readTree(cameraListPageURL);
|
||||||
|
cameraDataList = jsonNode.path("data").path("list");
|
||||||
|
System.out.println(cameraDataList);
|
||||||
|
Iterator<JsonNode> elements2 = cameraDataList.elements();
|
||||||
|
while (elements2.hasNext()) {
|
||||||
|
CameraDO cameraDO = new CameraDO();
|
||||||
|
cameraNode = elements2.next();
|
||||||
|
System.out.println("cameraNode:"+cameraNode);
|
||||||
|
cameraDO.setAltitude(cameraNode.path("altitude").asText());
|
||||||
|
cameraDO.setCameraIndexCode(cameraNode.path("cameraIndexCode").asText());
|
||||||
|
cameraDO.setCameraName(cameraNode.path("cameraName").asText());
|
||||||
|
cameraDO.setCameraType(cameraNode.path("cameraType").asInt());
|
||||||
|
cameraDO.setCameraTypeName(cameraNode.path("cameraTypeName").asText());
|
||||||
|
cameraDO.setCapabilitySet(cameraNode.path("capabilitySet").asText());
|
||||||
|
cameraDO.setCapabilitySetName(cameraNode.path("capabilitySetName").asText());
|
||||||
|
cameraDO.setIntelligentSet(cameraNode.path("intelligentSet").asText());
|
||||||
|
cameraDO.setIntelligentSetName(cameraNode.path("intelligentSetName").asText());
|
||||||
|
cameraDO.setChannelNo(cameraNode.path("channelNo").asText());
|
||||||
|
cameraDO.setChannelType(cameraNode.path("channelType").asText());
|
||||||
|
cameraDO.setChannelTypeName(cameraNode.path("channelTypeName").asText());
|
||||||
|
cameraDO.setCreateTime(dateParse(cameraNode.path("createTime").asText()));
|
||||||
|
cameraDO.setEncodeDevIndexCode(cameraNode.path("encodeDevIndexCode").asText());
|
||||||
|
cameraDO.setEncodeDevResourceType(cameraNode.path("encodeDevResourceType").asText());
|
||||||
|
cameraDO.setEncodeDevResourceTypeName(cameraNode.path("encodeDevResourceTypeName").asText());
|
||||||
|
cameraDO.setGbIndexCode(cameraNode.path("gbIndexCode").asText());
|
||||||
|
cameraDO.setInstallLocation(cameraNode.path("installLocation").asText());
|
||||||
|
cameraDO.setKeyBoardCode(cameraNode.path("keyBoardCode").asText());
|
||||||
|
cameraDO.setLatitude(cameraNode.path("latitude").asText());
|
||||||
|
cameraDO.setLongitude(cameraNode.path("longitude").asText());
|
||||||
|
cameraDO.setPixel(cameraNode.path("pixel").asText());
|
||||||
|
cameraDO.setPtz(cameraNode.path("ptz").asText());
|
||||||
|
cameraDO.setPtzName(cameraNode.path("ptzName").asText());
|
||||||
|
cameraDO.setPtzController(cameraNode.path("ptzController").asText());
|
||||||
|
cameraDO.setPtzControllerName(cameraNode.path("ptzControllerName").asText());
|
||||||
|
cameraDO.setRecordLocation(cameraNode.path("recordLocation").asText());
|
||||||
|
cameraDO.setRecordLocationName(cameraNode.path("recordLocationName").asText());
|
||||||
|
cameraDO.setRegionIndexCode(cameraNode.path("regionIndexCode").asText());
|
||||||
|
cameraDO.setStatus(cameraNode.path("status").asText());
|
||||||
|
cameraDO.setStatusName(cameraNode.path("statusName").asText());
|
||||||
|
cameraDO.setTransType(cameraNode.path("transType").asInt());
|
||||||
|
cameraDO.setTransTypeName(cameraNode.path("transTypeName").asText());
|
||||||
|
cameraDO.setTreatyType(cameraNode.path("treatyType").asText());
|
||||||
|
cameraDO.setTreatyTypeName(cameraNode.path("treatyTypeName").asText());
|
||||||
|
cameraDO.setViewshed(cameraNode.path("viewshed").asText());
|
||||||
|
cameraDO.setUpdateTime(dateParse(cameraNode.path("updateTime").asText()));
|
||||||
|
|
||||||
|
// 查询数据库中是否已有数据
|
||||||
|
CameraDO dbCameraData = cameraService.selectByCameraIndexCode(cameraDO.getCameraIndexCode());
|
||||||
|
System.out.println("dbCameraData: " + dbCameraData);
|
||||||
|
System.out.println(cameraDO);
|
||||||
|
CameraSaveReqVO cameraSaveReqVO = BeanUtils.toBean(cameraDO, CameraSaveReqVO.class);
|
||||||
|
// 如果数据库中有记录且数据不一致,执行更新操作
|
||||||
|
if (dbCameraData != null && !cameraDO.getUpdateTime().equals(dbCameraData.getUpdateTime())) {
|
||||||
|
// 进行更新操作
|
||||||
|
cameraService.updateCameraCheckByCameraIndexCode(cameraSaveReqVO);
|
||||||
|
System.out.println("更新了" + cameraDO.getCameraIndexCode() + "的数据");
|
||||||
|
} else if (dbCameraData == null) {
|
||||||
|
// 如果数据库中没有记录,则插入新数据
|
||||||
|
cameraService.createCamera(cameraSaveReqVO);
|
||||||
|
System.out.println("插入了" + cameraDO.getCameraIndexCode() + "的数据");
|
||||||
|
} else {
|
||||||
|
System.out.println("无需更新" + cameraDO.getCameraIndexCode() + "的数据");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (
|
||||||
|
Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private LocalDateTime dateParse(String dateStr) {
|
||||||
|
String dateString = dateStr;
|
||||||
|
// 通过正则调整时区格式
|
||||||
|
// 这次确保只有一位数字时区后缀 +0800 被替换为 +08:00
|
||||||
|
if (dateString.matches(".*\\+\\d{4}$")) {
|
||||||
|
dateString = dateString.replaceAll("(\\+\\d{2})(\\d{2})$", "$1:$2");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 定义一个日期格式化器,包括时区信息
|
||||||
|
DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
|
||||||
|
|
||||||
|
// 解析带有时区的字符串
|
||||||
|
OffsetDateTime offsetDateTime = OffsetDateTime.parse(dateString, formatter);
|
||||||
|
|
||||||
|
// 转换为 LocalDateTime
|
||||||
|
LocalDateTime localDateTime = offsetDateTime.toLocalDateTime();
|
||||||
|
|
||||||
|
return localDateTime;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user