|
|
@@ -2,16 +2,26 @@ package com.usky.cdi.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.usky.cdi.service.config.mqtt.MqttGateway;
|
|
|
+import com.alibaba.nacos.shaded.com.google.gson.Gson;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
+import com.usky.cdi.domain.BaseBuildFacility;
|
|
|
+import com.usky.cdi.domain.DmpDevice;
|
|
|
+import com.usky.cdi.service.BaseBuildFacilityService;
|
|
|
+import com.usky.cdi.service.DmpDeviceInfoService;
|
|
|
+import com.usky.cdi.service.config.mqtt.MqttOutConfig;
|
|
|
import com.usky.cdi.service.util.SnowflakeIdGenerator;
|
|
|
import com.usky.cdi.service.vo.base.*;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Paths;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
-import java.util.Date;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* 基础类数据传输服务
|
|
|
@@ -25,8 +35,17 @@ import java.util.Date;
|
|
|
@ConditionalOnProperty(prefix = "mqtt", value = {"enabled"}, havingValue = "true")
|
|
|
public class BaseDataTransferService {
|
|
|
|
|
|
- @Autowired(required = false)
|
|
|
- private MqttGateway mqttGateway;
|
|
|
+ @Autowired
|
|
|
+ private BaseBuildFacilityService baseBuildFacilityService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DmpDeviceInfoService dmpDeviceInfoService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private MqttOutConfig.MqttGateway mqttGateway;
|
|
|
+
|
|
|
+ @Value("${config.engineeringID}")
|
|
|
+ private String engineeringID;
|
|
|
|
|
|
private final SnowflakeIdGenerator idGenerator;
|
|
|
private final SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
|
|
@@ -58,10 +77,6 @@ public class BaseDataTransferService {
|
|
|
* @return 是否发送成功
|
|
|
*/
|
|
|
public boolean sendEngineeringBase(EngineeringBaseVO vo) {
|
|
|
- if (mqttGateway == null) {
|
|
|
- log.warn("MQTT Gateway未初始化,无法发送消息");
|
|
|
- return false;
|
|
|
- }
|
|
|
try {
|
|
|
if (vo.getDataPacketID() == null) {
|
|
|
vo.setDataPacketID(generateDataPacketID());
|
|
|
@@ -91,10 +106,6 @@ public class BaseDataTransferService {
|
|
|
* @return 是否发送成功
|
|
|
*/
|
|
|
public boolean sendProtectiveUnit(ProtectiveUnitVO vo) {
|
|
|
- if (mqttGateway == null) {
|
|
|
- log.warn("MQTT Gateway未初始化,无法发送消息");
|
|
|
- return false;
|
|
|
- }
|
|
|
try {
|
|
|
if (vo.getDataPacketID() == null) {
|
|
|
vo.setDataPacketID(generateDataPacketID());
|
|
|
@@ -124,10 +135,6 @@ public class BaseDataTransferService {
|
|
|
* @return 是否发送成功
|
|
|
*/
|
|
|
public boolean sendFloorPlane(FloorPlaneVO vo) {
|
|
|
- if (mqttGateway == null) {
|
|
|
- log.warn("MQTT Gateway未初始化,无法发送消息");
|
|
|
- return false;
|
|
|
- }
|
|
|
try {
|
|
|
if (vo.getDataPacketID() == null) {
|
|
|
vo.setDataPacketID(generateDataPacketID());
|
|
|
@@ -136,27 +143,46 @@ public class BaseDataTransferService {
|
|
|
vo.setPublishTime(getCurrentTime());
|
|
|
}
|
|
|
|
|
|
+ String imagePath = "D://games/3492.jpg";
|
|
|
+ // 将图片文件读取为字节数组
|
|
|
+ byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));
|
|
|
+
|
|
|
// 检查文件大小(不超过5MB)
|
|
|
- if (vo.getFloorFile() != null && vo.getFloorFile().length > 5 * 1024 * 1024) {
|
|
|
+ if (vo.getFloorFile() != null && imageBytes.length > 5 * 1024 * 1024) {
|
|
|
log.error("楼层平面图文件大小超过5MB限制,FileID: {}", vo.getFloorFileID());
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ HashMap<String, Object> map = new HashMap<>();
|
|
|
+ map.put("dataPacketID", vo.getDataPacketID());
|
|
|
+ map.put("engineeringID", vo.getEngineeringID());
|
|
|
+ map.put("floor", vo.getFloor());
|
|
|
+ map.put("floorFileID", vo.getFloorFileID());
|
|
|
+ map.put("floorFileName", vo.getFloorFileName());
|
|
|
+ map.put("floorFileSuffix", vo.getFloorFileSuffix());
|
|
|
+ map.put("filePixWidth", vo.getFilePixWidth());
|
|
|
+ map.put("filePixHeight", vo.getFilePixHeight());
|
|
|
+ map.put("floorFile", imageBytes);
|
|
|
+ map.put("publishTime", vo.getPublishTime());
|
|
|
+ Gson gson = new Gson();
|
|
|
// 将字节数组转换为Base64编码
|
|
|
JSONObject jsonObject = (JSONObject) JSON.toJSON(vo);
|
|
|
+ vo.setFloorFile(imageBytes);
|
|
|
+// jsonObject.put("floorFile", imageBytes);
|
|
|
if (vo.getFloorFile() != null) {
|
|
|
// 使用Base64编码传输二进制数据
|
|
|
String base64File = java.util.Base64.getEncoder().encodeToString(vo.getFloorFile());
|
|
|
- jsonObject.put("floorFile", base64File);
|
|
|
+ jsonObject.put("floorFile", imageBytes);
|
|
|
}
|
|
|
|
|
|
String json = jsonObject.toJSONString();
|
|
|
+ System.out.println(gson.toJson(map));
|
|
|
String topic = "base/floorPlane";
|
|
|
|
|
|
log.info("发送楼层平面图信息,Topic: {}, FileID: {}, FileSize: {} bytes",
|
|
|
topic, vo.getFloorFileID(),
|
|
|
vo.getFloorFile() != null ? vo.getFloorFile().length : 0);
|
|
|
- mqttGateway.sendToMqtt(topic, json);
|
|
|
+ mqttGateway.sendToMqtt(topic, gson.toJson(map));
|
|
|
|
|
|
return true;
|
|
|
} catch (Exception e) {
|
|
|
@@ -165,6 +191,21 @@ public class BaseDataTransferService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据输入的键(key)从Map中匹配并返回对应的值
|
|
|
+ * @param key 要匹配的键(输入值)
|
|
|
+ * @param dataMap 存储键值对的Map
|
|
|
+ * @return 匹配到的值(若未匹配到返回null)
|
|
|
+ */
|
|
|
+ public static <K, V> V matchByKey(K key, Map<K, V> dataMap) {
|
|
|
+ // 判空处理(避免空指针)
|
|
|
+ if (key == null || dataMap == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 直接通过Map的get方法匹配
|
|
|
+ return dataMap.get(key);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 发送智能监管物联设施信息
|
|
|
* Topic: base/sensorInfo
|
|
|
@@ -172,24 +213,43 @@ public class BaseDataTransferService {
|
|
|
* @param vo 智能监管物联设施信息
|
|
|
* @return 是否发送成功
|
|
|
*/
|
|
|
- public boolean sendSensorInfo(SensorInfoVO vo) {
|
|
|
- if (mqttGateway == null) {
|
|
|
- log.warn("MQTT Gateway未初始化,无法发送消息");
|
|
|
- return false;
|
|
|
- }
|
|
|
+ public boolean sendSensorInfo(FacilityDeviceVO vo) {
|
|
|
try {
|
|
|
- if (vo.getDataPacketID() == null) {
|
|
|
- vo.setDataPacketID(generateDataPacketID());
|
|
|
- }
|
|
|
- if (vo.getPublishTime() == null) {
|
|
|
- vo.setPublishTime(getCurrentTime());
|
|
|
- }
|
|
|
+ Map<Integer, Integer> userIdToName = new HashMap<>();
|
|
|
+ userIdToName.put(702, 31);
|
|
|
+ userIdToName.put(703, 33);
|
|
|
+ userIdToName.put(704, 11);
|
|
|
+ userIdToName.put(707, 19);
|
|
|
+ userIdToName.put(708, 19);
|
|
|
+ userIdToName.put(709, 15);
|
|
|
+ userIdToName.put(710, 16);
|
|
|
+ userIdToName.put(711, 2);
|
|
|
+ userIdToName.put(712, 34);
|
|
|
+ userIdToName.put(713, 36);
|
|
|
+ userIdToName.put(714, 37);
|
|
|
|
|
|
- String json = JSON.toJSONString(vo);
|
|
|
+ HashMap<String, Object> map = new HashMap<>();
|
|
|
+ map.put("dataPacketID", generateDataPacketID());
|
|
|
+ map.put("engineeringID", Long.parseLong(engineeringID));
|
|
|
+ map.put("floor", "B2");
|
|
|
+ map.put("floorFileID", 1);
|
|
|
+ map.put("sensorID", Integer.parseInt(vo.getDeviceId()));
|
|
|
+ map.put("sensorNo", vo.getDeviceUuid());
|
|
|
+ map.put("sensorType", matchByKey(vo.getDeviceType(), userIdToName));
|
|
|
+ map.put("unitName", vo.getFacilityDesc());
|
|
|
+ map.put("monitorObjNo", vo.getFacilityNum());
|
|
|
+ map.put("monitorObj", vo.getFacilityName());
|
|
|
+ map.put("location", vo.getAddress());
|
|
|
+// map.put("xCoordinate", (int) Math.floor(Double.parseDouble(vo.getPlaneX())*4.34));
|
|
|
+// map.put("yCoordinate", (int) Math.floor(Double.parseDouble(vo.getPlaneY())*4.34));
|
|
|
+ map.put("xCoordinate", Integer.valueOf(vo.getPlaneX()));
|
|
|
+ map.put("yCoordinate", Integer.valueOf(vo.getPlaneY()));
|
|
|
+ map.put("publishTime", getCurrentTime());
|
|
|
+ Gson gson = new Gson();
|
|
|
String topic = "base/sensorInfo";
|
|
|
-
|
|
|
- log.info("发送智能监管物联设施信息,Topic: {}, SensorID: {}", topic, vo.getSensorID());
|
|
|
- mqttGateway.sendToMqtt(topic, json);
|
|
|
+ System.out.println(gson.toJson(map));
|
|
|
+// log.info("发送智能监管物联设施信息,Topic: {}, SensorID: {}", topic, vo.getSensorID());
|
|
|
+ mqttGateway.sendToMqtt(topic, gson.toJson(map));
|
|
|
|
|
|
return true;
|
|
|
} catch (Exception e) {
|
|
|
@@ -223,22 +283,42 @@ public class BaseDataTransferService {
|
|
|
/**
|
|
|
* 批量发送智能监管物联设施信息
|
|
|
*
|
|
|
- * @param sensors 物联设施列表
|
|
|
+ * @param tenantId 租户ID
|
|
|
* @return 成功发送的数量
|
|
|
*/
|
|
|
- public int batchSendSensorInfos(java.util.List<SensorInfoVO> sensors) {
|
|
|
- if (sensors == null || sensors.isEmpty()) {
|
|
|
- return 0;
|
|
|
+ public int batchSendSensorInfos(Integer tenantId) {
|
|
|
+ List<BaseBuildFacility> list = baseBuildFacilityService.facilityInfo(tenantId);
|
|
|
+ List<DmpDevice> list1 = dmpDeviceInfoService.deviceInfo(tenantId);
|
|
|
+ List<FacilityDeviceVO> list2 = new ArrayList<>();
|
|
|
+ if(CollectionUtils.isNotEmpty(list)&&CollectionUtils.isNotEmpty(list1)){
|
|
|
+ for (int j=0;j<list.size();j++) {
|
|
|
+ for (int k=0;k<list1.size();k++) {
|
|
|
+ if (list.get(j).getDeviceId().equals(list1.get(k).getDeviceId())){
|
|
|
+ FacilityDeviceVO facilityDeviceVO = new FacilityDeviceVO();
|
|
|
+ facilityDeviceVO.setFloor(list.get(j).getFloor());
|
|
|
+ facilityDeviceVO.setFacilityName(list.get(j).getFacilityName());
|
|
|
+ facilityDeviceVO.setFacilityNum(list.get(j).getFacilityNum());
|
|
|
+ facilityDeviceVO.setDeviceId(list.get(j).getDeviceId());
|
|
|
+ facilityDeviceVO.setAddress(list.get(j).getAddress());
|
|
|
+ facilityDeviceVO.setPlaneX(list.get(j).getPlaneX());
|
|
|
+ facilityDeviceVO.setPlaneY(list.get(j).getPlaneY());
|
|
|
+ facilityDeviceVO.setDeviceUuid(list1.get(k).getDeviceUuid());
|
|
|
+ facilityDeviceVO.setFacilityDesc(list.get(j).getFacilityDesc());
|
|
|
+ facilityDeviceVO.setDeviceType(list1.get(k).getDeviceType());
|
|
|
+ list2.add(facilityDeviceVO);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
int successCount = 0;
|
|
|
- for (SensorInfoVO sensor : sensors) {
|
|
|
- if (sendSensorInfo(sensor)) {
|
|
|
- successCount++;
|
|
|
+ if(CollectionUtils.isNotEmpty(list2)){
|
|
|
+ for (int i=0;i<list2.size();i++) {
|
|
|
+ if (sendSensorInfo(list2.get(i))) {
|
|
|
+ successCount++;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- log.info("批量发送智能监管物联设施信息,总数: {}, 成功: {}", sensors.size(), successCount);
|
|
|
+ log.info("批量发送智能监管物联设施信息,总数: {}, 成功: {}", list2.size(), successCount);
|
|
|
return successCount;
|
|
|
}
|
|
|
}
|