|
@@ -1,10 +1,12 @@
|
|
|
package com.usky.iot.service.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
//import com.usky.backend.client.DeviceDataClient;
|
|
|
-import com.usky.backend.domain.request.DeviceDataInfoVO;
|
|
|
+import com.usky.iot.service.config.mqtt.MqttOutConfig;
|
|
|
+import com.usky.iot.service.vo.DeviceDataInfoVO;
|
|
|
import com.usky.common.core.exception.BusinessException;
|
|
|
import com.usky.common.mybatis.core.AbstractCrudService;
|
|
|
import com.usky.common.security.utils.SecurityUtils;
|
|
@@ -15,12 +17,11 @@ import com.usky.backend.domain.request.DmpDeviceDataRequestVO;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import java.time.Instant;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.ZoneId;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
@@ -44,12 +45,15 @@ public class DmpDataInfoServiceImpl extends AbstractCrudService<DmpDataInfoMappe
|
|
|
// @Autowired
|
|
|
// private DeviceDataClient deviceDataClient;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private MqttOutConfig.MqttGateway mqttGateway;
|
|
|
+
|
|
|
@Autowired
|
|
|
private DmpDeviceTypeService dmpDeviceTypeService;
|
|
|
|
|
|
@Override
|
|
|
public void data(DmpDeviceDataRequestVO dmpDeviceDataRequestVO){
|
|
|
- List<DeviceDataInfoVO> dataInfo = new ArrayList<>();
|
|
|
+ DeviceDataInfoVO dataInfo = new DeviceDataInfoVO();
|
|
|
|
|
|
LambdaQueryWrapper<DmpDeviceInfo> dmpDeviceQuery = Wrappers.lambdaQuery();
|
|
|
dmpDeviceQuery.eq(DmpDeviceInfo::getDeleteFlag,0)
|
|
@@ -84,31 +88,38 @@ public class DmpDataInfoServiceImpl extends AbstractCrudService<DmpDataInfoMappe
|
|
|
.eq(DmpProductAttribute::getProductId,list.get(0).getId());
|
|
|
List<DmpProductAttribute> list1 = dmpProductAttributeService.list(queryWrapper1);
|
|
|
if(CollectionUtils.isNotEmpty(list1)){
|
|
|
- for(int i=0;i<list1.size();i++){
|
|
|
Map<String,Float> metrics = dmpDeviceDataRequestVO.getMetrics();
|
|
|
if(metrics.size()>0){
|
|
|
+ Map<String,Object> mp = new HashMap<>();
|
|
|
for(Map.Entry<String,Float> map:metrics.entrySet()){
|
|
|
+ boolean find = false;
|
|
|
String key = map.getKey();
|
|
|
float value = map.getValue();
|
|
|
- if(key.equals(list1.get(i).getAttributeCode())){ //属性名需和产品注册信息一致
|
|
|
- DeviceDataInfoVO deviceDataInfoVO = new DeviceDataInfoVO();
|
|
|
- deviceDataInfoVO.setDeviceId(dmpDeviceDataRequestVO.getDeviceId());
|
|
|
- deviceDataInfoVO.setDeviceType(list.get(0).getDeviceType());
|
|
|
- deviceDataInfoVO.setTypeAbbrevia(shortName);
|
|
|
- deviceDataInfoVO.setName(key);
|
|
|
- deviceDataInfoVO.setValue(value);
|
|
|
- deviceDataInfoVO.setTimestamp(LocalDateTime.ofInstant(Instant.ofEpochSecond(dmpDeviceDataRequestVO.getTimestamp()), ZoneId.systemDefault()));
|
|
|
- dataInfo.add(deviceDataInfoVO);
|
|
|
+ for(int i=0;i<list1.size();i++){
|
|
|
+ if(key.equals(list1.get(i).getAttributeCode())){ //属性名需和产品注册信息一致
|
|
|
+ mp.put(key,value);
|
|
|
+ find = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(find == false){
|
|
|
+ throw new BusinessException("对应这个产品下面的设备属性"+key+" 在产品属性表中不存在");
|
|
|
}
|
|
|
}
|
|
|
+ dataInfo.setMetrics(mp);
|
|
|
+ dataInfo.setDevice_id(dmpDeviceDataRequestVO.getDeviceId());
|
|
|
+ dataInfo.setProduct_id(list.get(0).getId().toString());
|
|
|
+ dataInfo.setTimestamp(dmpDeviceDataRequestVO.getTimestamp());
|
|
|
+ dataInfo.setDevice_type(list.get(0).getDeviceType()+"-"+shortName);
|
|
|
}
|
|
|
- }
|
|
|
+ }else{
|
|
|
+ throw new BusinessException("查找产品属性表失败,"+dmpDeviceDataRequestVO.getProductCode()+" 该产品对应的属性在产品信息表没有信息");
|
|
|
}
|
|
|
}else{
|
|
|
throw new BusinessException("查找产品信息失败,"+dmpDeviceDataRequestVO.getProductCode()+" 在产品信息表中不存在");
|
|
|
}
|
|
|
- if(CollectionUtils.isNotEmpty(dataInfo)){
|
|
|
-
|
|
|
+ if(dataInfo != null){
|
|
|
+ mqttGateway.sendToMqtt("data-collector", JSONArray.toJSON(dataInfo).toString());
|
|
|
+ System.out.println(JSONArray.toJSON(dataInfo).toString());
|
|
|
}
|
|
|
}
|
|
|
|