|
|
|
|
@ -104,56 +104,114 @@ public class EventsServiceImpl implements IEventsService {
|
|
|
|
|
} |
|
|
|
|
// 修复类型转换问题
|
|
|
|
|
List<?> rawDataList = datas.getDatas(); |
|
|
|
|
for (Object rawData : rawDataList) { |
|
|
|
|
// 安全地转换对象
|
|
|
|
|
SanShiFengDatas data = new SanShiFengDatas(); |
|
|
|
|
if (rawData instanceof SanShiFengDatas) { |
|
|
|
|
data = (SanShiFengDatas) rawData; |
|
|
|
|
} else { |
|
|
|
|
// 如果是 LinkedHashMap,则手动转换
|
|
|
|
|
JSONObject jsonObject = new JSONObject((HashMap<?, ?>) rawData); |
|
|
|
|
try { |
|
|
|
|
data = jsonObject.to(SanShiFengDatas.class); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error("mqtt数据上报异常", e); |
|
|
|
|
data.setName(jsonObject.getString("name")); |
|
|
|
|
data.setValue("-1"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
// 不使用消息队列,查询属于哪种设备类型,然后通过策略进行数据解析
|
|
|
|
|
log.info("设备SN:{},PLC名称:{},项目名称:{},时间:{},数据:{}", sn, plcName, projectName, time, data.toString()); |
|
|
|
|
// 获取点位参数名称
|
|
|
|
|
String name = data.getName(); |
|
|
|
|
// 获取点位值
|
|
|
|
|
BigDecimal value = new BigDecimal(0); |
|
|
|
|
if (rawDataList == null || rawDataList.isEmpty()) { |
|
|
|
|
log.warn("数据列表为空,SN: {}", sn); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
// 并行处理数据列表,避免阻塞
|
|
|
|
|
rawDataList.parallelStream().forEach(rawData -> { |
|
|
|
|
try { |
|
|
|
|
value = new BigDecimal(String.valueOf(data.getValue())); |
|
|
|
|
processDataItem(rawData, sn, plcName, projectName, time, buildingId); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
value = BigDecimal.ZERO; |
|
|
|
|
} |
|
|
|
|
// 直接更新collectionParamManage参数值
|
|
|
|
|
collectionParamManageService.updateCPMByOtherName(name, value, time, buildingId); |
|
|
|
|
// 查询device_install表,走之前的逻辑
|
|
|
|
|
CollectionParamsManageEntity collectionParamsManageEntity = collectionParamManageService.selectDeviceInstallByOtherName(name, buildingId); |
|
|
|
|
if (null != collectionParamsManageEntity && collectionParamsManageEntity.getDeviceInstallId() != null) { |
|
|
|
|
DeviceInstallEntity deviceInstallEntity = deviceInstallService.selectDeviceById(collectionParamsManageEntity.getDeviceInstallId()); |
|
|
|
|
if (deviceInstallEntity != null) { |
|
|
|
|
// 开始走策略判断
|
|
|
|
|
String deviceType = deviceInstallEntity.getDeviceType(); |
|
|
|
|
Device device = DeviceFactory.createDevice(deviceType); |
|
|
|
|
DeviceStrategy strategy = DeviceStrategyFactory.createStrategy(deviceType); |
|
|
|
|
if (null == strategy) { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
device.setStrategy(strategy); |
|
|
|
|
device.analysisMQTTReceiveData(time, deviceInstallEntity.getDeviceAddr(), String.valueOf(value), Constant.READ, deviceInstallEntity, collectionParamsManageEntity); |
|
|
|
|
} |
|
|
|
|
log.error("处理单个数据项失败: {}", rawData, e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
log.error("处理数据时发生错误: ", e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void processDataItem(Object rawData, String sn, String plcName, String projectName, String time, String buildingId) { |
|
|
|
|
// 安全地转换对象
|
|
|
|
|
SanShiFengDatas data = convertDataItem(rawData); |
|
|
|
|
if (data == null) { |
|
|
|
|
log.warn("数据转换失败,跳过处理"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
// 不使用消息队列,查询属于哪种设备类型,然后通过策略进行数据解析
|
|
|
|
|
log.info("设备SN:{},PLC名称:{},项目名称:{},时间:{},数据:{}", sn, plcName, projectName, time, data.toString()); |
|
|
|
|
// 获取点位参数名称
|
|
|
|
|
String name = data.getName(); |
|
|
|
|
// 获取点位值
|
|
|
|
|
BigDecimal value = new BigDecimal(0); |
|
|
|
|
try { |
|
|
|
|
value = new BigDecimal(String.valueOf(data.getValue())); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
value = BigDecimal.ZERO; |
|
|
|
|
} |
|
|
|
|
// 直接更新collectionParamManage参数值
|
|
|
|
|
collectionParamManageService.updateCPMByOtherName(name, value, time, buildingId); |
|
|
|
|
// 查询device_install表,走之前的逻辑
|
|
|
|
|
CollectionParamsManageEntity collectionParamsManageEntity = collectionParamManageService.selectDeviceInstallByOtherName(name, buildingId); |
|
|
|
|
if (null != collectionParamsManageEntity && collectionParamsManageEntity.getDeviceInstallId() != null) { |
|
|
|
|
DeviceInstallEntity deviceInstallEntity = deviceInstallService.selectDeviceById(collectionParamsManageEntity.getDeviceInstallId()); |
|
|
|
|
if (deviceInstallEntity != null) { |
|
|
|
|
// 开始走策略判断
|
|
|
|
|
String deviceType = deviceInstallEntity.getDeviceType(); |
|
|
|
|
Device device = DeviceFactory.createDevice(deviceType); |
|
|
|
|
DeviceStrategy strategy = DeviceStrategyFactory.createStrategy(deviceType); |
|
|
|
|
if (strategy != null) { |
|
|
|
|
device.setStrategy(strategy); |
|
|
|
|
device.analysisMQTTReceiveData(time, deviceInstallEntity.getDeviceAddr(), String.valueOf(value), Constant.READ, deviceInstallEntity, collectionParamsManageEntity); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private SanShiFengDatas convertDataItem(Object rawData) { |
|
|
|
|
if (rawData == null) { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
SanShiFengDatas data = new SanShiFengDatas(); |
|
|
|
|
try { |
|
|
|
|
if (rawData instanceof SanShiFengDatas) { |
|
|
|
|
data = (SanShiFengDatas) rawData; |
|
|
|
|
} else if (rawData instanceof HashMap) { |
|
|
|
|
JSONObject jsonObject = new JSONObject((HashMap<?, ?>) rawData); |
|
|
|
|
data = jsonObject.to(SanShiFengDatas.class); |
|
|
|
|
} else { |
|
|
|
|
log.warn("不支持的数据类型: {}", rawData.getClass().getName()); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error("数据转换异常", e); |
|
|
|
|
data.setName(getJsonValueAsString(rawData, "name")); |
|
|
|
|
data.setValue("-1"); |
|
|
|
|
} |
|
|
|
|
return data; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 从原始数据对象中获取指定键的字符串值 |
|
|
|
|
* |
|
|
|
|
* @param rawData 原始数据对象(通常为Map类型) |
|
|
|
|
* @param key 要获取的键名 |
|
|
|
|
* @return 键对应的字符串值,如果不存在或转换失败则返回null |
|
|
|
|
*/ |
|
|
|
|
private String getJsonValueAsString(Object rawData, String key) { |
|
|
|
|
if (rawData == null || StringUtils.isBlank(key)) { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
if (rawData instanceof HashMap) { |
|
|
|
|
HashMap<?, ?> map = (HashMap<?, ?>) rawData; |
|
|
|
|
Object value = map.get(key); |
|
|
|
|
return value != null ? String.valueOf(value) : null; |
|
|
|
|
} else if (rawData instanceof JSONObject) { |
|
|
|
|
JSONObject jsonObject = (JSONObject) rawData; |
|
|
|
|
return jsonObject.getString(key); |
|
|
|
|
} else { |
|
|
|
|
// 如果是其他类型,尝试使用反射或通用方式获取
|
|
|
|
|
log.warn("不支持的数据类型: {}", rawData.getClass().getName()); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error("获取JSON值时发生异常,key: {}", key, e); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|