|
|
|
package com.zhonglai.luhui.device.protocol.factory.purification;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.ruoyi.common.utils.DateUtils;
|
|
|
|
import com.ruoyi.common.utils.GsonConstructor;
|
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
|
import com.zhonglai.luhui.device.analysis.comm.dto.DeviceSensorData;
|
|
|
|
import com.zhonglai.luhui.device.analysis.comm.dto.thingsmodels.ThingsModelBase;
|
|
|
|
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.domain.IotThingsModel;
|
|
|
|
import com.zhonglai.luhui.device.protocol.factory.dto.AnalysisDto;
|
|
|
|
import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceHostDto;
|
|
|
|
import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceInfoDto;
|
|
|
|
import com.zhonglai.luhui.device.protocol.factory.dto.ProtocolPurificationModel;
|
|
|
|
import com.zhonglai.luhui.device.protocol.factory.service.IotThingsModelService;
|
|
|
|
import com.zhonglai.luhui.device.protocol.factory.service.PersistenceDBService;
|
|
|
|
import org.apache.commons.lang3.EnumUtils;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
@Service("default_purification")
|
|
|
|
public class DefaultProtocolPurificationFactoryImpl implements ProtocolPurificationFactory{
|
|
|
|
@Autowired
|
|
|
|
private IotThingsModelService iotThingsModelService;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ProtocolPurificationModel purification(Integer product_id,Topic topic, JsonObject protocolParserModel ) {
|
|
|
|
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(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 )
|
|
|
|
{
|
|
|
|
if(data.has("0"))
|
|
|
|
{
|
|
|
|
JsonObject jsonObjectData = GsonConstructor.get().fromJson(GsonConstructor.get().toJson(data.get("0")),JsonObject.class);
|
|
|
|
JsonObject summary = null;
|
|
|
|
//解析设备摘要
|
|
|
|
if(jsonObjectData.has("summary") && null != jsonObjectData.get("summary") && jsonObjectData.get("summary").isJsonObject())
|
|
|
|
{
|
|
|
|
summary = jsonObjectData.get("summary").getAsJsonObject();
|
|
|
|
jsonObjectData.remove("summary");
|
|
|
|
}
|
|
|
|
ParserDeviceHostDto parserDeviceHostDto = null;
|
|
|
|
if(null != jsonObjectData && jsonObjectData.size() != 0)
|
|
|
|
{
|
|
|
|
parserDeviceHostDto = analysisJsonData(product_id,topic,jsonObjectData).pushDeviceSensorData(deviceSensorDataList).toParserDeviceHostDto(topic.getClientid());
|
|
|
|
}
|
|
|
|
|
|
|
|
if(null != summary && summary.size() != 0)
|
|
|
|
{
|
|
|
|
if(null == parserDeviceHostDto)
|
|
|
|
{
|
|
|
|
parserDeviceHostDto = new ParserDeviceHostDto();
|
|
|
|
parserDeviceHostDto.setId(topic.getClientid());
|
|
|
|
}
|
|
|
|
parserDeviceHostDto.setSummary(summary);
|
|
|
|
}
|
|
|
|
return parserDeviceHostDto;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<ParserDeviceInfoDto> analysisDeviceInfo(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(product_id,topic,data.get(key).getAsJsonObject()).pushDeviceSensorData(deviceSensorDataList).toParserDeviceInfoDto(topic.getClientid()+"_"+key);
|
|
|
|
list.add(parserDeviceInfoDto);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
private AnalysisDto analysisJsonData(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 = iotThingsModelService.getIotThingsModel(product_id, key);
|
|
|
|
|
|
|
|
JsonElement jsonElement = jsonObjectData.get(key);
|
|
|
|
if(null != jsonElement && !jsonElement.isJsonNull() )
|
|
|
|
{
|
|
|
|
ThingsModelItemBase thingsModelItemBase = getThingsModelItemBase(thingsModel,jsonElement);
|
|
|
|
if(!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(topic,thingsModel,thingsModelItemBase));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return analysisDto;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private ThingsModelItemBase getThingsModelItemBase(IotThingsModel thingsModel,JsonElement jsonElement)
|
|
|
|
{
|
|
|
|
String data_type = thingsModel.getData_type().toUpperCase();
|
|
|
|
if(!EnumUtils.isValidEnum(ThingsModelDataTypeEnum.class,data_type))
|
|
|
|
{
|
|
|
|
data_type = ThingsModelDataTypeEnum.STRING.name();
|
|
|
|
}
|
|
|
|
return ThingsModelItemBase.newhingsModelItem(Enum.valueOf(ThingsModelDataTypeEnum.class,data_type),thingsModel, jsonElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
private DeviceSensorData getDeviceSensorData(Topic topic,IotThingsModel thingsModel,ThingsModelItemBase thingsModelItemBase)
|
|
|
|
{
|
|
|
|
DeviceSensorData sensorData = new DeviceSensorData();
|
|
|
|
sensorData.setDataType(thingsModel.getIdentifier());
|
|
|
|
sensorData.setDataValue(thingsModelItemBase.getSaveView());
|
|
|
|
sensorData.setCreatTime(com.zhonglai.luhui.device.analysis.comm.util.DateUtils.getNowTimeMilly());
|
|
|
|
sensorData.setDeviceModel(topic.getUsername());
|
|
|
|
sensorData.setDeviceInfoId(topic.getClientid());
|
|
|
|
return sensorData;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} |
...
|
...
|
|