Browse Source

同步相机物联网ID

develop
xiepufeng 8 months ago
parent
commit
c1649e479b
  1. 82
      zc-business/src/main/java/com/zc/business/controller/VideoController.java
  2. 60
      zc-business/src/main/java/com/zc/business/enums/CameraDirection.java

82
zc-business/src/main/java/com/zc/business/controller/VideoController.java

@ -7,7 +7,10 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.zc.business.constant.DeviceTypeConstants;
import com.zc.business.domain.DcDevice;
import com.zc.business.enums.CameraDirection;
import com.zc.business.enums.LaneDirection;
import com.zc.business.service.IDcDeviceService;
import com.zc.business.service.IMiddleDatabaseService;
import com.zc.common.core.httpclient.OkHttp;
@ -22,10 +25,13 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
import static com.ruoyi.common.constant.Constants.HTTP;
@ -63,6 +69,80 @@ public class VideoController extends BaseController {
@Resource
private IMiddleDatabaseService middleDatabaseService;
// 组织机构id
private final static String CAM_DEPT_ID = "1301730";
@PostConstruct
public void init() {
// synchronizeIotIds();
}
/**
* 同步物联网ID
* 该方法用于查询未同步物联网ID的摄像头设备信息并从外部API获取这些设备的详细信息然后更新它们的物联网ID
* 该过程主要涉及查询未初始化物联网ID的摄像头构建设备IP与设备映射从API获取设备详细信息以及批量更新设备的物联网ID
*/
public void synchronizeIotIds() {
// 查询未同步物联ID的摄像头设备信息
LambdaQueryWrapper<DcDevice> query = new LambdaQueryWrapper<>();
query.eq(DcDevice::getDeviceType, DeviceTypeConstants.CAMERA)
.isNull(DcDevice::getIotDeviceId);
List<DcDevice> uninitializedCameras = iDcDeviceService.list(query);
if (uninitializedCameras.isEmpty()) {
return; // 若无待同步设备,则直接返回
}
// 将未初始化的摄像头列表转换为Map,以设备的桩号、方向和子类型拼接作为键,设备本身作为值
Map<String, DcDevice> cameraMap = new HashMap<>();
uninitializedCameras.forEach(dcDevice -> {
String otherConfig = dcDevice.getOtherConfig();
if (otherConfig == null) return;
JSONObject otherConfigJson = JSONObject.parseObject(otherConfig);
String ptzCtrl = otherConfigJson.getString("ptzCtrl");
String key = dcDevice.getStakeMark() + "|" + dcDevice.getDirection() + "|" + ptzCtrl;
cameraMap.put(key, dcDevice);
});
try {
// 获取部门下的摄像头详细信息
JSONObject camApiResponse = getCamByDept(CAM_DEPT_ID);
if (camApiResponse == null || !camApiResponse.containsKey("data")) {
return; // 如果获取信息失败或不含有效数据,直接返回
}
JSONArray camDataArray = camApiResponse.getJSONArray("data");
// 遍历并更新具备匹配IP的摄像头设备物联ID
List<DcDevice> devicesToUpdate = new ArrayList<>();
camDataArray.forEach(item -> {
JSONObject cameraInfo = (JSONObject) item;
// 桩号
String pileNum = cameraInfo.getString("pileNum");
// 方向
Integer camOrientation = cameraInfo.getInteger("camOrientation");
LaneDirection laneDirection = CameraDirection.fromCode(camOrientation).toLaneDirection();
// 是否有云台控制 0 有(球机) 1 ⽆(枪机)
String ptzCtrl = cameraInfo.getString("ptzCtrl");
String key = pileNum + "|" + laneDirection.getValue() + "|" + ptzCtrl;
if (cameraMap.containsKey(key)) {
DcDevice dcDevice = cameraMap.get(key);
dcDevice.setIotDeviceId(cameraInfo.getString("camId"));
devicesToUpdate.add(dcDevice);
}
});
// 批量更新摄像头设备的物联ID
iDcDeviceService.updateBatchById(devicesToUpdate);
} catch (HttpException | IOException e) {
logger.error("获取摄像头信息失败!", e);
}
}
/**
* 查询附近相机
*/
@ -292,7 +372,7 @@ public class VideoController extends BaseController {
public Object nearCamListPileNum(@ApiParam(value = "桩号", name = "pileNum", required = true) String pileNum) throws HttpException, IOException {
// 获取济菏运管中心相机信息
JSONObject camInfo = getCamByDept("1301730");
JSONObject camInfo = getCamByDept(CAM_DEPT_ID);
if (!camInfo.containsKey("data")) {
return camInfo;

60
zc-business/src/main/java/com/zc/business/enums/CameraDirection.java

@ -0,0 +1,60 @@
package com.zc.business.enums;
/**
* 摄像头方向
*/
public enum CameraDirection {
/**
* 摄像头上行
*/
UPWARD(0, "上行"),
/**
* 摄像头下行
*/
DOWN(1, "下行"),
/**
* 摄像头上下行双向
*/
BIDIRECTIONAL(2, "上下行(双向)");
private final int code;
private final String description;
CameraDirection(int code, String description) {
this.code = code;
this.description = description;
}
public int getCode() {
return code;
}
public String getDescription() {
return description;
}
// 可选:根据code获取枚举值
public static CameraDirection fromCode(int code) {
for (CameraDirection direction : values()) {
if (direction.getCode() == code) {
return direction;
}
}
throw new IllegalArgumentException("无效的摄像机方向码: " + code);
}
public LaneDirection toLaneDirection() {
switch (this) {
case UPWARD:
return LaneDirection.UPWARD;
case DOWN:
return LaneDirection.DOWNWARD;
case BIDIRECTIONAL:
// 假设摄像机双向就映射为车道双向
return LaneDirection.BIDIRECTIONAL;
default:
throw new IllegalStateException("未知的CameraDirection: " + this);
}
}
}
Loading…
Cancel
Save