|
|
|
|
@ -1,9 +1,15 @@
|
|
|
|
|
package cn.iocoder.yudao.module.iot.controller.admin.recipedevicerecord;
|
|
|
|
|
|
|
|
|
|
import cn.iocoder.yudao.framework.common.util.opc.OpcUtils;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.DeviceRespVO;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.dal.dataobject.devicecontactmodel.DeviceContactModelDO;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.dal.dataobject.recipe.RecipeDO;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.dal.dataobject.recipeplandetail.RecipePlanDetailDO;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.service.device.DeviceService;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.service.devicecontactmodel.DeviceContactModelService;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.service.recipe.RecipeService;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.service.recipeplandetail.RecipePlanDetailService;
|
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
@ -16,17 +22,23 @@ import javax.validation.*;
|
|
|
|
|
import javax.servlet.http.*;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
|
|
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
|
|
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
|
|
|
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
|
|
|
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
|
|
|
|
|
|
|
|
|
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
|
|
|
|
|
|
|
|
|
|
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
|
|
|
|
|
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
|
|
|
|
|
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.DEVICE_NOT_EXISTS;
|
|
|
|
|
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.RECIPE_NOT_EXISTS;
|
|
|
|
|
|
|
|
|
|
import cn.iocoder.yudao.module.iot.controller.admin.recipedevicerecord.vo.*;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.dal.dataobject.recipedevicerecord.RecipeDeviceRecordDO;
|
|
|
|
|
@ -73,6 +85,9 @@ public class RecipeDeviceRecordController {
|
|
|
|
|
@Resource
|
|
|
|
|
private DeviceService deviceService;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private DeviceContactModelService deviceContactModelService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@PostMapping("/create")
|
|
|
|
|
@ -134,7 +149,6 @@ public class RecipeDeviceRecordController {
|
|
|
|
|
/**
|
|
|
|
|
* 批量创建设备点位采集记录和配方点位记录
|
|
|
|
|
* @param recipeId 配方ID
|
|
|
|
|
* @param pointList 点位列表 [{id: 1, refer: "参考值1"}, {id: 2, refer: "参考值2"}]
|
|
|
|
|
* @return 创建结果
|
|
|
|
|
* @throws JsonProcessingException JSON处理异常
|
|
|
|
|
*/
|
|
|
|
|
@ -152,28 +166,51 @@ public class RecipeDeviceRecordController {
|
|
|
|
|
// ========== 第一步:查询配方关联的点位属性信息 ==========
|
|
|
|
|
// 1.1 根据recipeId查询iot_recipe_device_attribute表记录
|
|
|
|
|
|
|
|
|
|
recipePlanDetailDO.setRecipeId(32L);
|
|
|
|
|
recipePlanDetailDO.setRecipeId(recipeId);
|
|
|
|
|
List<RecipeDeviceAttributeDO> attributeList = recipeDeviceAttributeService.getByRecipeId(recipePlanDetailDO.getRecipeId());
|
|
|
|
|
// if (CollectionUtils.isEmpty(attributeList)) {
|
|
|
|
|
// return success(false); // 无关联属性,直接返回
|
|
|
|
|
// }
|
|
|
|
|
Map<Long, Map<String, Object>> deviceDataMap = deviceService.createDeviceDataMap(103L);//recipeRespVO.getDeviceId()
|
|
|
|
|
|
|
|
|
|
for (RecipeDeviceAttributeDO attributeDO : attributeList) {
|
|
|
|
|
Map<String, Object> data = deviceDataMap.get(attributeDO.getAttributeId());
|
|
|
|
|
if (data != null ) {
|
|
|
|
|
// 创建
|
|
|
|
|
RecipeDeviceRecordDO recipeDeviceRecordDO = new RecipeDeviceRecordDO();
|
|
|
|
|
recipeDeviceRecordDO.setRecipeId(recipeId);
|
|
|
|
|
recipeDeviceRecordDO.setAttributeCode(attributeDO.getAttributeName());
|
|
|
|
|
recipeDeviceRecordDO.setDataType(attributeDO.getDataType());
|
|
|
|
|
recipeDeviceRecordDO.setDataUnit(attributeDO.getDataUnit());
|
|
|
|
|
if (data.get("addressValue") != null && data.get("addressValue").toString() != null) {
|
|
|
|
|
recipeDeviceRecordDO.setValue(data.get("addressValue").toString());
|
|
|
|
|
}
|
|
|
|
|
recipeDeviceRecordService.createRecipeDeviceRecord(BeanUtils.toBean(recipeDeviceRecordDO, RecipeDeviceRecordSaveReqVO.class));
|
|
|
|
|
|
|
|
|
|
//先删除在添加
|
|
|
|
|
List<RecipeDeviceRecordDO> recipeDeviceRecordDOS = recipeDeviceRecordService.getListByRecipeId(recipeId);
|
|
|
|
|
if (!recipeDeviceRecordDOS.isEmpty()){
|
|
|
|
|
recipeDeviceRecordService.deleteByIds(recipeDeviceRecordDOS);
|
|
|
|
|
}
|
|
|
|
|
RecipeDO recipe = recipeService.getRecipe(recipeId);
|
|
|
|
|
if (recipe == null){
|
|
|
|
|
throw exception(RECIPE_NOT_EXISTS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DeviceRespVO device = deviceService.getDevice(recipe.getMachineId());
|
|
|
|
|
if (device== null ){
|
|
|
|
|
throw exception(DEVICE_NOT_EXISTS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<Long, DeviceContactModelDO> deviceContactModelMap = new HashMap<>();
|
|
|
|
|
List<DeviceContactModelDO> deviceContactModelDOS = deviceContactModelService.selectListByDeviceId(device.getId());
|
|
|
|
|
if (!deviceContactModelDOS.isEmpty()){
|
|
|
|
|
deviceContactModelMap = deviceContactModelDOS.stream()
|
|
|
|
|
.collect(Collectors.toMap(
|
|
|
|
|
DeviceContactModelDO::getId,
|
|
|
|
|
Function.identity()
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OpcUtils.connect(device.getUrl(),device.getUsername(),device.getPassword(),10);
|
|
|
|
|
|
|
|
|
|
for (RecipeDeviceAttributeDO attributeDO : attributeList) {
|
|
|
|
|
DeviceContactModelDO deviceContactModelDO = deviceContactModelMap.get(attributeDO.getAttributeId());
|
|
|
|
|
if (deviceContactModelDO == null){
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
// 创建
|
|
|
|
|
RecipeDeviceRecordDO recipeDeviceRecordDO = new RecipeDeviceRecordDO();
|
|
|
|
|
recipeDeviceRecordDO.setRecipeId(recipeId);
|
|
|
|
|
recipeDeviceRecordDO.setAttributeCode(deviceContactModelDO.getAttributeName());
|
|
|
|
|
recipeDeviceRecordDO.setDataType(deviceContactModelDO.getDataType());
|
|
|
|
|
recipeDeviceRecordDO.setDataUnit(deviceContactModelDO.getDataUnit());
|
|
|
|
|
recipeDeviceRecordDO.setValue((String) OpcUtils.readValue(deviceContactModelDO.getAddress()));
|
|
|
|
|
|
|
|
|
|
recipeDeviceRecordService.createRecipeDeviceRecord(BeanUtils.toBean(recipeDeviceRecordDO, RecipeDeviceRecordSaveReqVO.class));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return success(true);
|
|
|
|
|
|