|
|
|
package com.zhonglai.luhui.device.protocol.mnplc.purification;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.ruoyi.common.utils.GsonConstructor;
|
|
|
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
|
|
|
import com.zhonglai.luhui.device.analysis.comm.dto.DeviceSensorData;
|
|
|
|
import com.zhonglai.luhui.device.analysis.comm.dto.thingsmodels.ThingsModelDataTypeEnum;
|
|
|
|
import com.zhonglai.luhui.device.analysis.comm.dto.thingsmodels.ThingsModelItemBase;
|
|
|
|
import com.zhonglai.luhui.device.analysis.comm.factory.Topic;
|
|
|
|
import com.zhonglai.luhui.device.analysis.comm.util.DateUtils;
|
|
|
|
import com.zhonglai.luhui.device.domain.IotThingsModel;
|
|
|
|
import com.zhonglai.luhui.device.protocol.factory.dto.*;
|
|
|
|
import com.zhonglai.luhui.device.protocol.factory.purification.ProtocolPurificationFactory;
|
|
|
|
import com.zhonglai.luhui.device.protocol.factory.service.IotThingsModelService;
|
|
|
|
import org.apache.commons.lang3.EnumUtils;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 默认的清洗服务
|
|
|
|
*/
|
|
|
|
public class MNPLCProtocolPurificationFactoryImpl implements ProtocolPurificationFactory {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ProtocolPurificationModel purification(Integer product_id,Topic topic, AnalysisResult analysisResult ) {
|
|
|
|
JsonObject protocolParserModel = analysisResult.getJsonObject();
|
|
|
|
if(null != protocolParserModel && protocolParserModel.size()!=0)
|
|
|
|
{
|
|
|
|
ProtocolPurificationModel protocolPurificationModel = new ProtocolPurificationModel();
|
|
|
|
ParserDeviceHostDto parserDeviceHostDto = analysisHost(product_id,topic,protocolParserModel,protocolPurificationModel.getDeviceSensorDataList());
|
|
|
|
protocolPurificationModel.setParserDeviceHostDto(parserDeviceHostDto);
|
|
|
|
|
|
|
|
List<ParserDeviceInfoDto> parserDeviceInfoDtoList = analysisDeviceInfo(parserDeviceHostDto.getUpdateTime(),product_id,topic,protocolParserModel,protocolPurificationModel.getDeviceSensorDataList());
|
|
|
|
protocolPurificationModel.setParserDeviceInfoDtoList(parserDeviceInfoDtoList);
|
|
|
|
return protocolPurificationModel;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private ParserDeviceHostDto analysisHost(Integer product_id,Topic topic, JsonObject data,List<DeviceSensorData> deviceSensorDataList )
|
|
|
|
{
|
|
|
|
int time = DateUtils.getNowTimeMilly();
|
|
|
|
if(data.has("0"))
|
|
|
|
{
|
|
|
|
JsonObject jsonObjectData = data.get("0").getAsJsonObject();
|
|
|
|
JsonObject summary = null;
|
|
|
|
//解析设备摘要
|
|
|
|
if(jsonObjectData.has("summary") && null != jsonObjectData.get("summary") && jsonObjectData.get("summary").isJsonObject())
|
|
|
|
{
|
|
|
|
summary = jsonObjectData.get("summary").getAsJsonObject();
|
|
|
|
jsonObjectData.remove("summary");
|
|
|
|
}
|
|
|
|
|
|
|
|
String online = null;
|
|
|
|
//解析在线状态
|
|
|
|
if(jsonObjectData.has("online") && null != jsonObjectData.get("online"))
|
|
|
|
{
|
|
|
|
online = jsonObjectData.get("online").getAsString();
|
|
|
|
jsonObjectData.remove("online");
|
|
|
|
}
|
|
|
|
|
|
|
|
//信号
|
|
|
|
Integer rssi = null;
|
|
|
|
if(jsonObjectData.has("rssi") && null != jsonObjectData.get("rssi"))
|
|
|
|
{
|
|
|
|
rssi = jsonObjectData.get("rssi").getAsInt();
|
|
|
|
jsonObjectData.remove("rssi");
|
|
|
|
}
|
|
|
|
|
|
|
|
ParserDeviceHostDto parserDeviceHostDto = null;
|
|
|
|
if(null != jsonObjectData && jsonObjectData.size() != 0)
|
|
|
|
{
|
|
|
|
parserDeviceHostDto = analysisJsonData(time,"0",product_id,topic,jsonObjectData).pushDeviceSensorData(deviceSensorDataList).toParserDeviceHostDto(topic.getClientid(),time);
|
|
|
|
}
|
|
|
|
if(null == parserDeviceHostDto)
|
|
|
|
{
|
|
|
|
parserDeviceHostDto = new ParserDeviceHostDto();
|
|
|
|
parserDeviceHostDto.setId(topic.getClientid());
|
|
|
|
}
|
|
|
|
if(null != summary && summary.size() != 0)
|
|
|
|
{
|
|
|
|
parserDeviceHostDto.setSummary(summary);
|
|
|
|
}
|
|
|
|
parserDeviceHostDto.setOnline(online);
|
|
|
|
parserDeviceHostDto.setRssi(rssi);
|
|
|
|
parserDeviceHostDto.setUpdateTime(time);
|
|
|
|
return parserDeviceHostDto;
|
|
|
|
}
|
|
|
|
ParserDeviceHostDto parserDeviceHostDto = new ParserDeviceHostDto();
|
|
|
|
parserDeviceHostDto.setId(topic.getClientid());
|
|
|
|
parserDeviceHostDto.setUpdateTime(time);
|
|
|
|
return parserDeviceHostDto;
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<ParserDeviceInfoDto> analysisDeviceInfo(Integer time,Integer product_id,Topic topic, JsonObject data,List<DeviceSensorData> deviceSensorDataList)
|
|
|
|
{
|
|
|
|
List<ParserDeviceInfoDto> list = new ArrayList<>();
|
|
|
|
|
|
|
|
for (String key:data.keySet())
|
|
|
|
{
|
|
|
|
if(!key.equals("0") && data.get(key).isJsonObject())
|
|
|
|
{
|
|
|
|
ParserDeviceInfoDto parserDeviceInfoDto = analysisJsonData(time,key,product_id,topic,data.get(key).getAsJsonObject()).pushDeviceSensorData(deviceSensorDataList).toParserDeviceInfoDto(topic.getClientid()+"_"+key,time);
|
|
|
|
list.add(parserDeviceInfoDto);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
private AnalysisDto analysisJsonData(Integer time,String sensorNumber,Integer product_id,Topic topic, JsonObject jsonObjectData )
|
|
|
|
{
|
|
|
|
if(null != jsonObjectData && jsonObjectData.size() !=0)
|
|
|
|
{
|
|
|
|
AnalysisDto analysisDto = new AnalysisDto();
|
|
|
|
|
|
|
|
JsonObject things_model_value = new JsonObject(); //存放数据
|
|
|
|
analysisDto.setThings_model_value(things_model_value);
|
|
|
|
|
|
|
|
JsonObject things_model_config = new JsonObject(); //存放配置
|
|
|
|
analysisDto.setThings_model_config(things_model_config);
|
|
|
|
for (String key: jsonObjectData.keySet())
|
|
|
|
{
|
|
|
|
IotThingsModel thingsModel = SpringUtils.getBean(IotThingsModelService.class).getIotThingsModel(product_id, key);
|
|
|
|
|
|
|
|
JsonElement jsonElement = jsonObjectData.get(key);
|
|
|
|
if(null != jsonElement && !jsonElement.isJsonNull() )
|
|
|
|
{
|
|
|
|
ThingsModelItemBase thingsModelItemBase = getThingsModelItemBase(thingsModel,jsonElement);
|
|
|
|
if(null == thingsModelItemBase || !thingsModelItemBase.checkValue())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (thingsModel.getIs_config())
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
things_model_value.add(key,GsonConstructor.get().fromJson(JSONObject.toJSONString(thingsModelItemBase),JsonObject.class));
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
things_model_config.add(key,GsonConstructor.get().fromJson(JSONObject.toJSONString(thingsModelItemBase),JsonObject.class));
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
things_model_config.add(key,GsonConstructor.get().fromJson(JSONObject.toJSONString(thingsModelItemBase),JsonObject.class));
|
|
|
|
things_model_value.add(key,GsonConstructor.get().fromJson(JSONObject.toJSONString(thingsModelItemBase),JsonObject.class));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
things_model_value.add(key,GsonConstructor.get().fromJson(JSONObject.toJSONString(thingsModelItemBase),JsonObject.class));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//记录数据日志
|
|
|
|
if(1==thingsModelItemBase.getIs_save_log() )
|
|
|
|
{
|
|
|
|
List<DeviceSensorData> deviceSensorDataList = analysisDto.getDeviceSensorDataList();
|
|
|
|
if(null == deviceSensorDataList)
|
|
|
|
{
|
|
|
|
deviceSensorDataList = new ArrayList<>();
|
|
|
|
analysisDto.setDeviceSensorDataList(deviceSensorDataList);
|
|
|
|
}
|
|
|
|
deviceSensorDataList.add(getDeviceSensorData(time,sensorNumber,topic,thingsModel,thingsModelItemBase));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return analysisDto;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private ThingsModelItemBase getThingsModelItemBase(IotThingsModel thingsModel, JsonElement jsonElement) {
|
|
|
|
String dataType = thingsModel.getData_type();
|
|
|
|
ThingsModelDataTypeEnum thingsModelDataTypeEnum;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (dataType == null) {
|
|
|
|
thingsModelDataTypeEnum = ThingsModelDataTypeEnum.STRING;
|
|
|
|
} else {
|
|
|
|
// 统一转大写再校验
|
|
|
|
String upperType = dataType.toUpperCase();
|
|
|
|
if (EnumUtils.isValidEnum(ThingsModelDataTypeEnum.class, upperType)) {
|
|
|
|
thingsModelDataTypeEnum = Enum.valueOf(ThingsModelDataTypeEnum.class, upperType);
|
|
|
|
} else {
|
|
|
|
thingsModelDataTypeEnum = ThingsModelDataTypeEnum.STRING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IllegalArgumentException | NullPointerException e) {
|
|
|
|
// 容错处理,回退到 STRING
|
|
|
|
thingsModelDataTypeEnum = ThingsModelDataTypeEnum.STRING;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ThingsModelItemBase.newhingsModel(thingsModelDataTypeEnum, thingsModel, jsonElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private DeviceSensorData getDeviceSensorData(Integer time,String sensorNumber,Topic topic,IotThingsModel thingsModel,ThingsModelItemBase thingsModelItemBase)
|
|
|
|
{
|
|
|
|
DeviceSensorData sensorData = new DeviceSensorData();
|
|
|
|
sensorData.setDataType(thingsModel.getIdentifier());
|
|
|
|
sensorData.setDataValue(thingsModelItemBase.getSaveView());
|
|
|
|
sensorData.setCreatTime(time);
|
|
|
|
sensorData.setDeviceModel(topic.getUsername());
|
|
|
|
if("0".equals(sensorNumber))
|
|
|
|
{
|
|
|
|
sensorData.setDeviceInfoId(topic.getClientid());
|
|
|
|
}else {
|
|
|
|
sensorData.setDeviceInfoId(topic.getClientid()+"_"+sensorNumber);
|
|
|
|
}
|
|
|
|
return sensorData;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} |
...
|
...
|
|