delete old iot device
parent
101a3084a0
commit
7450017d0c
@ -1,152 +0,0 @@
|
||||
package cn.iocoder.yudao.module.iot.controller.admin.device;
|
||||
|
||||
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.DevicePageReqVO;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.DeviceRespVO;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.DeviceSaveReqVO;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.device.DeviceAttributeDO;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.device.DeviceDO;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.gateway.GatewayDO;
|
||||
import cn.iocoder.yudao.module.iot.service.device.DeviceService;
|
||||
import cn.iocoder.yudao.module.iot.service.gateway.GatewayService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.validation.Valid;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
|
||||
@Tag(name = "管理后台 - 物联设备")
|
||||
@RestController
|
||||
@RequestMapping("/iot/device")
|
||||
@Validated
|
||||
public class DeviceController {
|
||||
|
||||
@Resource
|
||||
private DeviceService deviceService;
|
||||
@Resource
|
||||
private GatewayService gatewayService;
|
||||
|
||||
@PostMapping("/create")
|
||||
@Operation(summary = "创建物联设备")
|
||||
@PreAuthorize("@ss.hasPermission('iot:device:create')")
|
||||
public CommonResult<Long> createDevice(@Valid @RequestBody DeviceSaveReqVO createReqVO) {
|
||||
return success(deviceService.createDevice(createReqVO));
|
||||
}
|
||||
|
||||
@PutMapping("/update")
|
||||
@Operation(summary = "更新物联设备")
|
||||
@PreAuthorize("@ss.hasPermission('iot:device:update')")
|
||||
public CommonResult<Boolean> updateDevice(@Valid @RequestBody DeviceSaveReqVO updateReqVO) {
|
||||
deviceService.updateDevice(updateReqVO);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete")
|
||||
@Operation(summary = "删除物联设备")
|
||||
@Parameter(name = "id", description = "编号", required = true)
|
||||
@PreAuthorize("@ss.hasPermission('iot:device:delete')")
|
||||
public CommonResult<Boolean> deleteDevice(@RequestParam("id") Long id) {
|
||||
deviceService.deleteDevice(id);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@GetMapping("/get")
|
||||
@Operation(summary = "获得物联设备")
|
||||
@Parameter(name = "id", description = "编号", required = true, example = "1024")
|
||||
@PreAuthorize("@ss.hasPermission('iot:device:query')")
|
||||
public CommonResult<DeviceRespVO> getDevice(@RequestParam("id") Long id) {
|
||||
DeviceDO device = deviceService.getDevice(id);
|
||||
return success(BeanUtils.toBean(device, DeviceRespVO.class));
|
||||
}
|
||||
|
||||
@GetMapping("/page")
|
||||
@Operation(summary = "获得物联设备分页")
|
||||
@PreAuthorize("@ss.hasPermission('iot:device:query')")
|
||||
public CommonResult<PageResult<DeviceRespVO>> getDevicePage(@Valid DevicePageReqVO pageReqVO) {
|
||||
PageResult<DeviceDO> pageResult = deviceService.getDevicePage(pageReqVO);
|
||||
return success(BeanUtils.toBean(pageResult, DeviceRespVO.class));
|
||||
}
|
||||
|
||||
@GetMapping("/export-excel")
|
||||
@Operation(summary = "导出物联设备 Excel")
|
||||
@PreAuthorize("@ss.hasPermission('iot:device:export')")
|
||||
@ApiAccessLog(operateType = EXPORT)
|
||||
public void exportDeviceExcel(@Valid DevicePageReqVO pageReqVO,
|
||||
HttpServletResponse response) throws IOException {
|
||||
pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
|
||||
List<DeviceDO> list = deviceService.getDevicePage(pageReqVO).getList();
|
||||
// 导出 Excel
|
||||
ExcelUtils.write(response, "物联设备.xls", "数据", DeviceRespVO.class,
|
||||
BeanUtils.toBean(list, DeviceRespVO.class));
|
||||
}
|
||||
|
||||
@GetMapping("/pushDevice")
|
||||
@Operation(summary = "下发物联设备配置到网关")
|
||||
@Parameter(name = "id", description = "编号", required = true, example = "1024")
|
||||
@PreAuthorize("@ss.hasPermission('iot:device:update')")
|
||||
public CommonResult<String> pushDevice(@RequestParam("id") Long id) {
|
||||
DeviceDO device = deviceService.getDevice(id);
|
||||
GatewayDO gatewayDO = gatewayService.getGateway(device.getGatewayId());
|
||||
return deviceService.pushDevice(device, gatewayDO);
|
||||
}
|
||||
// ==================== 子表(设备属性) ====================
|
||||
|
||||
@GetMapping("/device-attribute/page")
|
||||
@Operation(summary = "获得设备属性分页")
|
||||
@Parameter(name = "deviceId", description = "设备id")
|
||||
@PreAuthorize("@ss.hasPermission('iot:device:query')")
|
||||
public CommonResult<PageResult<DeviceAttributeDO>> getDeviceAttributePage(PageParam pageReqVO,
|
||||
@RequestParam("deviceId") Long deviceId) {
|
||||
return success(deviceService.getDeviceAttributePage(pageReqVO, deviceId));
|
||||
}
|
||||
|
||||
@PostMapping("/device-attribute/create")
|
||||
@Operation(summary = "创建设备属性")
|
||||
@PreAuthorize("@ss.hasPermission('iot:device:create')")
|
||||
public CommonResult<Long> createDeviceAttribute(@Valid @RequestBody DeviceAttributeDO deviceAttribute) {
|
||||
DeviceDO deviceDO = deviceService.getDevice(deviceAttribute.getDeviceId());
|
||||
deviceAttribute.setDeviceCode(deviceDO.getDeviceCode());
|
||||
return success(deviceService.createDeviceAttribute(deviceAttribute));
|
||||
}
|
||||
|
||||
@PutMapping("/device-attribute/update")
|
||||
@Operation(summary = "更新设备属性")
|
||||
@PreAuthorize("@ss.hasPermission('iot:device:update')")
|
||||
public CommonResult<Boolean> updateDeviceAttribute(@Valid @RequestBody DeviceAttributeDO deviceAttribute) {
|
||||
deviceService.updateDeviceAttribute(deviceAttribute);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@DeleteMapping("/device-attribute/delete")
|
||||
@Parameter(name = "id", description = "编号", required = true)
|
||||
@Operation(summary = "删除设备属性")
|
||||
@PreAuthorize("@ss.hasPermission('iot:device:delete')")
|
||||
public CommonResult<Boolean> deleteDeviceAttribute(@RequestParam("id") Long id) {
|
||||
deviceService.deleteDeviceAttribute(id);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@GetMapping("/device-attribute/get")
|
||||
@Operation(summary = "获得设备属性")
|
||||
@Parameter(name = "id", description = "编号", required = true)
|
||||
@PreAuthorize("@ss.hasPermission('iot:device:query')")
|
||||
public CommonResult<DeviceAttributeDO> getDeviceAttribute(@RequestParam("id") Long id) {
|
||||
return success(deviceService.getDeviceAttribute(id));
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,33 +0,0 @@
|
||||
package cn.iocoder.yudao.module.iot.dal.mysql.device;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.device.DeviceAttributeDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 设备属性 Mapper
|
||||
*
|
||||
* @author 内蒙必硕
|
||||
*/
|
||||
@Mapper
|
||||
public interface DeviceAttributeMapper extends BaseMapperX<DeviceAttributeDO> {
|
||||
|
||||
default PageResult<DeviceAttributeDO> selectPage(PageParam reqVO, Long deviceId) {
|
||||
return selectPage(reqVO, new LambdaQueryWrapperX<DeviceAttributeDO>()
|
||||
.eq(DeviceAttributeDO::getDeviceId, deviceId)
|
||||
.orderByDesc(DeviceAttributeDO::getId));
|
||||
}
|
||||
|
||||
default int deleteByDeviceId(Long deviceId) {
|
||||
return delete(DeviceAttributeDO::getDeviceId, deviceId);
|
||||
}
|
||||
|
||||
default List<DeviceAttributeDO> selectByDeviceId(Long deviceId) {
|
||||
return selectList(DeviceAttributeDO::getDeviceId, deviceId);
|
||||
}
|
||||
}
|
||||
@ -1,67 +0,0 @@
|
||||
package cn.iocoder.yudao.module.iot.dal.mysql.device;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.DevicePageReqVO;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.device.DeviceDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 物联设备 Mapper
|
||||
*
|
||||
* @author 内蒙必硕
|
||||
*/
|
||||
@Mapper
|
||||
public interface DeviceMapper extends BaseMapperX<DeviceDO> {
|
||||
|
||||
default PageResult<DeviceDO> selectPage(DevicePageReqVO reqVO) {
|
||||
return selectPage(reqVO, new LambdaQueryWrapperX<DeviceDO>()
|
||||
.eqIfPresent(DeviceDO::getDeviceConfigId, reqVO.getDeviceConfigId())
|
||||
.eqIfPresent(DeviceDO::getDeviceCode, reqVO.getDeviceCode())
|
||||
.likeIfPresent(DeviceDO::getDeviceName, reqVO.getDeviceName())
|
||||
.eqIfPresent(DeviceDO::getDeviceType, reqVO.getDeviceType())
|
||||
.eqIfPresent(DeviceDO::getSiemensSeries, reqVO.getSiemensSeries())
|
||||
.eqIfPresent(DeviceDO::getSiemensConnectParam, reqVO.getSiemensConnectParam())
|
||||
.eqIfPresent(DeviceDO::getReadCronType, reqVO.getReadCronType())
|
||||
.eqIfPresent(DeviceDO::getReadRepeatValue, reqVO.getReadRepeatValue())
|
||||
.eqIfPresent(DeviceDO::getReadRepeatUnit, reqVO.getReadRepeatUnit())
|
||||
.eqIfPresent(DeviceDO::getReadCron, reqVO.getReadCron())
|
||||
.eqIfPresent(DeviceDO::getWriteCronType, reqVO.getWriteCronType())
|
||||
.eqIfPresent(DeviceDO::getWriteRepeatValue, reqVO.getWriteRepeatValue())
|
||||
.eqIfPresent(DeviceDO::getWriteRepeatUnit, reqVO.getWriteRepeatUnit())
|
||||
.eqIfPresent(DeviceDO::getWriteCron, reqVO.getWriteCron())
|
||||
.eqIfPresent(DeviceDO::getLocalPersistent, reqVO.getLocalPersistent())
|
||||
.eqIfPresent(DeviceDO::getUploadRate, reqVO.getUploadRate())
|
||||
.eqIfPresent(DeviceDO::getRateCount, reqVO.getRateCount())
|
||||
.eqIfPresent(DeviceDO::getModbusProtocol, reqVO.getModbusProtocol())
|
||||
.eqIfPresent(DeviceDO::getModbusPattern, reqVO.getModbusPattern())
|
||||
.eqIfPresent(DeviceDO::getPortName, reqVO.getPortName())
|
||||
.eqIfPresent(DeviceDO::getModbusConnectParam, reqVO.getModbusConnectParam())
|
||||
.eqIfPresent(DeviceDO::getModbusReadAddrGap, reqVO.getModbusReadAddrGap())
|
||||
.eqIfPresent(DeviceDO::getIsUpload, reqVO.getIsUpload())
|
||||
.eqIfPresent(DeviceDO::getGatewayId, reqVO.getGatewayId())
|
||||
.eqIfPresent(DeviceDO::getOrgId, reqVO.getOrgId())
|
||||
.eqIfPresent(DeviceDO::getRemark, reqVO.getRemark())
|
||||
.eqIfPresent(DeviceDO::getIsEnable, reqVO.getIsEnable())
|
||||
.betweenIfPresent(DeviceDO::getCreateTime, reqVO.getCreateTime())
|
||||
.orderByDesc(DeviceDO::getId));
|
||||
}
|
||||
|
||||
default PageResult<DeviceDO> selectPage(PageParam reqVO, Long gatewayId) {
|
||||
return selectPage(reqVO, new LambdaQueryWrapperX<DeviceDO>()
|
||||
.eq(DeviceDO::getGatewayId, gatewayId)
|
||||
.orderByDesc(DeviceDO::getId));
|
||||
}
|
||||
|
||||
default int deleteByGatewayId(Long gatewayId) {
|
||||
return delete(DeviceDO::getGatewayId, gatewayId);
|
||||
}
|
||||
|
||||
default List<DeviceDO> selectByGatewayId(Long gatewayId) {
|
||||
return selectList(DeviceDO::getGatewayId, gatewayId);
|
||||
}
|
||||
}
|
||||
@ -1,102 +0,0 @@
|
||||
package cn.iocoder.yudao.module.iot.service.device;
|
||||
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.DevicePageReqVO;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.DeviceSaveReqVO;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.device.DeviceAttributeDO;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.device.DeviceDO;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.gateway.GatewayDO;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
/**
|
||||
* 物联设备 Service 接口
|
||||
*
|
||||
* @author 内蒙必硕
|
||||
*/
|
||||
public interface DeviceService {
|
||||
|
||||
/**
|
||||
* 创建物联设备
|
||||
*
|
||||
* @param createReqVO 创建信息
|
||||
* @return 编号
|
||||
*/
|
||||
Long createDevice(@Valid DeviceSaveReqVO createReqVO);
|
||||
|
||||
/**
|
||||
* 更新物联设备
|
||||
*
|
||||
* @param updateReqVO 更新信息
|
||||
*/
|
||||
void updateDevice(@Valid DeviceSaveReqVO updateReqVO);
|
||||
|
||||
/**
|
||||
* 删除物联设备
|
||||
*
|
||||
* @param id 编号
|
||||
*/
|
||||
void deleteDevice(Long id);
|
||||
|
||||
/**
|
||||
* 获得物联设备
|
||||
*
|
||||
* @param id 编号
|
||||
* @return 物联设备
|
||||
*/
|
||||
DeviceDO getDevice(Long id);
|
||||
|
||||
/**
|
||||
* 获得物联设备分页
|
||||
*
|
||||
* @param pageReqVO 分页查询
|
||||
* @return 物联设备分页
|
||||
*/
|
||||
PageResult<DeviceDO> getDevicePage(DevicePageReqVO pageReqVO);
|
||||
|
||||
CommonResult<String> pushDevice(DeviceDO deviceDO, GatewayDO gatewayDO);
|
||||
// ==================== 子表(设备属性) ====================
|
||||
|
||||
/**
|
||||
* 获得设备属性分页
|
||||
*
|
||||
* @param pageReqVO 分页查询
|
||||
* @param deviceId 设备id
|
||||
* @return 设备属性分页
|
||||
*/
|
||||
PageResult<DeviceAttributeDO> getDeviceAttributePage(PageParam pageReqVO, Long deviceId);
|
||||
|
||||
/**
|
||||
* 创建设备属性
|
||||
*
|
||||
* @param deviceAttribute 创建信息
|
||||
* @return 编号
|
||||
*/
|
||||
Long createDeviceAttribute(@Valid DeviceAttributeDO deviceAttribute);
|
||||
|
||||
/**
|
||||
* 更新设备属性
|
||||
*
|
||||
* @param deviceAttribute 更新信息
|
||||
*/
|
||||
void updateDeviceAttribute(@Valid DeviceAttributeDO deviceAttribute);
|
||||
|
||||
/**
|
||||
* 删除设备属性
|
||||
*
|
||||
* @param id 编号
|
||||
*/
|
||||
void deleteDeviceAttribute(Long id);
|
||||
|
||||
/**
|
||||
* 获得设备属性
|
||||
*
|
||||
* @param id 编号
|
||||
* @return 设备属性
|
||||
*/
|
||||
DeviceAttributeDO getDeviceAttribute(Long id);
|
||||
|
||||
}
|
||||
@ -1,189 +0,0 @@
|
||||
package cn.iocoder.yudao.module.iot.service.device;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.DevicePageReqVO;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.DeviceSaveReqVO;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.device.DeviceAttributeDO;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.device.DeviceDO;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.gateway.GatewayDO;
|
||||
import cn.iocoder.yudao.module.iot.dal.mysql.device.DeviceAttributeMapper;
|
||||
import cn.iocoder.yudao.module.iot.dal.mysql.device.DeviceMapper;
|
||||
import cn.iocoder.yudao.module.iot.framework.constant.DeviceTypeEnum;
|
||||
import cn.iocoder.yudao.module.iot.framework.gateway.service.impl.TheGatewayServiceImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.DEVICE_ATTRIBUTE_NOT_EXISTS;
|
||||
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.DEVICE_NOT_EXISTS;
|
||||
|
||||
/**
|
||||
* 物联设备 Service 实现类
|
||||
*
|
||||
* @author 内蒙必硕
|
||||
*/
|
||||
@Service
|
||||
@Validated
|
||||
public class DeviceServiceImpl implements DeviceService {
|
||||
|
||||
@Resource
|
||||
private DeviceMapper deviceMapper;
|
||||
@Resource
|
||||
private DeviceAttributeMapper deviceAttributeMapper;
|
||||
|
||||
@Resource
|
||||
private TheGatewayServiceImpl theGatewayService;
|
||||
|
||||
@Override
|
||||
public Long createDevice(DeviceSaveReqVO createReqVO) {
|
||||
// 插入
|
||||
DeviceDO device = BeanUtils.toBean(createReqVO, DeviceDO.class);
|
||||
deviceMapper.insert(device);
|
||||
// 返回
|
||||
return device.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateDevice(DeviceSaveReqVO updateReqVO) {
|
||||
// 校验存在
|
||||
validateDeviceExists(updateReqVO.getId());
|
||||
// 更新
|
||||
DeviceDO updateObj = BeanUtils.toBean(updateReqVO, DeviceDO.class);
|
||||
deviceMapper.updateById(updateObj);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void deleteDevice(Long id) {
|
||||
// 校验存在
|
||||
validateDeviceExists(id);
|
||||
// 删除
|
||||
deviceMapper.deleteById(id);
|
||||
|
||||
// 删除子表
|
||||
deleteDeviceAttributeByDeviceId(id);
|
||||
}
|
||||
|
||||
private void validateDeviceExists(Long id) {
|
||||
if (deviceMapper.selectById(id) == null) {
|
||||
throw exception(DEVICE_NOT_EXISTS);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public DeviceDO getDevice(Long id) {
|
||||
return deviceMapper.selectById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<DeviceDO> getDevicePage(DevicePageReqVO pageReqVO) {
|
||||
return deviceMapper.selectPage(pageReqVO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<String> pushDevice(DeviceDO deviceDO, GatewayDO gatewayDO) {
|
||||
if (DeviceTypeEnum.isSiemens(deviceDO.getDeviceType())) {
|
||||
return siemensAdd(deviceDO, gatewayDO);
|
||||
} else if (DeviceTypeEnum.isModbus(deviceDO.getDeviceType())) {
|
||||
return modbusAdd(deviceDO, gatewayDO);
|
||||
} else return CommonResult.error(500, "未知类型设备!");
|
||||
}
|
||||
|
||||
public CommonResult<String> siemensAdd(DeviceDO deviceDO, GatewayDO gatewayDO) {
|
||||
if (gatewayDO != null && deviceDO != null &&
|
||||
deviceDO.getDeviceCode() != null && gatewayDO.getGatewayCode() != null) {
|
||||
try {
|
||||
//清除原本的西门子设备和点位数据
|
||||
theGatewayService.deleteSiemensEntity(gatewayDO, deviceDO);
|
||||
//新增西门子设备
|
||||
theGatewayService.addSiemensEntity(gatewayDO, deviceDO);
|
||||
//下发完成西门子设备后 下发对应设备的点位
|
||||
theGatewayService.addDeviceSiemensEntity(gatewayDO, deviceDO.getOrgMachineId(), deviceDO);
|
||||
//修改下发标识
|
||||
deviceDO.setIsUpload("1");
|
||||
deviceMapper.updateById(deviceDO);
|
||||
//启用设备
|
||||
//todo 启用有点问题
|
||||
theGatewayService.changeStatus(gatewayDO, deviceDO.getDeviceCode(), "siemens");
|
||||
return theGatewayService.compare(gatewayDO, deviceDO, "siemens");
|
||||
} catch (Exception e) {
|
||||
e.getLocalizedMessage();
|
||||
}
|
||||
}
|
||||
return CommonResult.error(500, "设备或网关不存在!");
|
||||
}
|
||||
|
||||
/*modbus设备下发*/
|
||||
public CommonResult<String> modbusAdd(DeviceDO deviceDO, GatewayDO gatewayDO) {
|
||||
if (deviceDO.getDeviceCode() != null && gatewayDO.getGatewayCode() != null) {
|
||||
try {
|
||||
//清除原本的Modbus设备和点位数据
|
||||
theGatewayService.deleteModbusEntity(gatewayDO, deviceDO.getDeviceCode());
|
||||
//新增Modbus
|
||||
theGatewayService.addModbusEntity(gatewayDO, deviceDO);
|
||||
//下发完成Modbus设备后 下发对应设备的点位
|
||||
theGatewayService.addDeviceModbusEntity(gatewayDO, deviceDO);
|
||||
//修改下发标识
|
||||
deviceDO.setIsUpload("1");
|
||||
deviceMapper.updateById(deviceDO);
|
||||
//启用设备
|
||||
theGatewayService.changeStatus(gatewayDO, deviceDO.getDeviceCode(), "modbus");
|
||||
return theGatewayService.compare(gatewayDO, deviceDO, "modbus");
|
||||
|
||||
} catch (Exception e) {
|
||||
e.getLocalizedMessage();
|
||||
}
|
||||
}
|
||||
return CommonResult.error(500, "设备或网关不存在!");
|
||||
}
|
||||
// ==================== 子表(设备属性) ====================
|
||||
|
||||
@Override
|
||||
public PageResult<DeviceAttributeDO> getDeviceAttributePage(PageParam pageReqVO, Long deviceId) {
|
||||
return deviceAttributeMapper.selectPage(pageReqVO, deviceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long createDeviceAttribute(DeviceAttributeDO deviceAttribute) {
|
||||
deviceAttributeMapper.insert(deviceAttribute);
|
||||
return deviceAttribute.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateDeviceAttribute(DeviceAttributeDO deviceAttribute) {
|
||||
// 校验存在
|
||||
validateDeviceAttributeExists(deviceAttribute.getId());
|
||||
// 更新
|
||||
deviceAttributeMapper.updateById(deviceAttribute);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteDeviceAttribute(Long id) {
|
||||
// 校验存在
|
||||
validateDeviceAttributeExists(id);
|
||||
// 删除
|
||||
deviceAttributeMapper.deleteById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DeviceAttributeDO getDeviceAttribute(Long id) {
|
||||
return deviceAttributeMapper.selectById(id);
|
||||
}
|
||||
|
||||
private void validateDeviceAttributeExists(Long id) {
|
||||
if (deviceAttributeMapper.selectById(id) == null) {
|
||||
throw exception(DEVICE_ATTRIBUTE_NOT_EXISTS);
|
||||
}
|
||||
}
|
||||
|
||||
private void deleteDeviceAttributeByDeviceId(Long deviceId) {
|
||||
deviceAttributeMapper.deleteByDeviceId(deviceId);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,231 +0,0 @@
|
||||
package cn.iocoder.yudao.module.iot.service.device;
|
||||
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.device.DeviceDO;
|
||||
import cn.iocoder.yudao.module.iot.dal.mysql.device.DeviceMapper;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.*;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.*;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.*;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.*;
|
||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* {@link DeviceServiceImpl} 的单元测试类
|
||||
*
|
||||
* @author 内蒙必硕
|
||||
*/
|
||||
@Import(DeviceServiceImpl.class)
|
||||
public class DeviceServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private DeviceServiceImpl deviceService;
|
||||
|
||||
@Resource
|
||||
private DeviceMapper deviceMapper;
|
||||
|
||||
@Test
|
||||
public void testCreateDevice_success() {
|
||||
// 准备参数
|
||||
DeviceSaveReqVO createReqVO = randomPojo(DeviceSaveReqVO.class).setId(null);
|
||||
|
||||
// 调用
|
||||
Long deviceId = deviceService.createDevice(createReqVO);
|
||||
// 断言
|
||||
assertNotNull(deviceId);
|
||||
// 校验记录的属性是否正确
|
||||
DeviceDO device = deviceMapper.selectById(deviceId);
|
||||
assertPojoEquals(createReqVO, device, "id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateDevice_success() {
|
||||
// mock 数据
|
||||
DeviceDO dbDevice = randomPojo(DeviceDO.class);
|
||||
deviceMapper.insert(dbDevice);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
DeviceSaveReqVO updateReqVO = randomPojo(DeviceSaveReqVO.class, o -> {
|
||||
o.setId(dbDevice.getId()); // 设置更新的 ID
|
||||
});
|
||||
|
||||
// 调用
|
||||
deviceService.updateDevice(updateReqVO);
|
||||
// 校验是否更新正确
|
||||
DeviceDO device = deviceMapper.selectById(updateReqVO.getId()); // 获取最新的
|
||||
assertPojoEquals(updateReqVO, device);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateDevice_notExists() {
|
||||
// 准备参数
|
||||
DeviceSaveReqVO updateReqVO = randomPojo(DeviceSaveReqVO.class);
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> deviceService.updateDevice(updateReqVO), DEVICE_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteDevice_success() {
|
||||
// mock 数据
|
||||
DeviceDO dbDevice = randomPojo(DeviceDO.class);
|
||||
deviceMapper.insert(dbDevice);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
Long id = dbDevice.getId();
|
||||
|
||||
// 调用
|
||||
deviceService.deleteDevice(id);
|
||||
// 校验数据不存在了
|
||||
assertNull(deviceMapper.selectById(id));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteDevice_notExists() {
|
||||
// 准备参数
|
||||
Long id = randomLongId();
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> deviceService.deleteDevice(id), DEVICE_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解
|
||||
public void testGetDevicePage() {
|
||||
// mock 数据
|
||||
DeviceDO dbDevice = randomPojo(DeviceDO.class, o -> { // 等会查询到
|
||||
o.setDeviceConfigId(null);
|
||||
o.setDeviceCode(null);
|
||||
o.setDeviceName(null);
|
||||
o.setDeviceType(null);
|
||||
o.setSiemensSeries(null);
|
||||
o.setSiemensConnectParam(null);
|
||||
o.setReadCronType(null);
|
||||
o.setReadRepeatValue(null);
|
||||
o.setReadRepeatUnit(null);
|
||||
o.setReadCron(null);
|
||||
o.setWriteCronType(null);
|
||||
o.setWriteRepeatValue(null);
|
||||
o.setWriteRepeatUnit(null);
|
||||
o.setWriteCron(null);
|
||||
o.setLocalPersistent(null);
|
||||
o.setUploadRate(null);
|
||||
o.setRateCount(null);
|
||||
o.setModbusProtocol(null);
|
||||
o.setModbusPattern(null);
|
||||
o.setPortName(null);
|
||||
o.setModbusConnectParam(null);
|
||||
o.setModbusReadAddrGap(null);
|
||||
o.setIsUpload(null);
|
||||
o.setGatewayId(null);
|
||||
o.setOrgId(null);
|
||||
o.setRemark(null);
|
||||
o.setIsEnable(null);
|
||||
o.setCreateTime(null);
|
||||
});
|
||||
deviceMapper.insert(dbDevice);
|
||||
// 测试 deviceConfigId 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setDeviceConfigId(null)));
|
||||
// 测试 deviceCode 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setDeviceCode(null)));
|
||||
// 测试 deviceName 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setDeviceName(null)));
|
||||
// 测试 deviceType 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setDeviceType(null)));
|
||||
// 测试 siemensSeries 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setSiemensSeries(null)));
|
||||
// 测试 siemensConnectParam 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setSiemensConnectParam(null)));
|
||||
// 测试 readCronType 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setReadCronType(null)));
|
||||
// 测试 readRepeatValue 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setReadRepeatValue(null)));
|
||||
// 测试 readRepeatUnit 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setReadRepeatUnit(null)));
|
||||
// 测试 readCron 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setReadCron(null)));
|
||||
// 测试 writeCronType 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setWriteCronType(null)));
|
||||
// 测试 writeRepeatValue 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setWriteRepeatValue(null)));
|
||||
// 测试 writeRepeatUnit 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setWriteRepeatUnit(null)));
|
||||
// 测试 writeCron 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setWriteCron(null)));
|
||||
// 测试 localPersistent 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setLocalPersistent(null)));
|
||||
// 测试 uploadRate 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setUploadRate(null)));
|
||||
// 测试 rateCount 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setRateCount(null)));
|
||||
// 测试 modbusProtocol 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setModbusProtocol(null)));
|
||||
// 测试 modbusPattern 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setModbusPattern(null)));
|
||||
// 测试 portName 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setPortName(null)));
|
||||
// 测试 modbusConnectParam 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setModbusConnectParam(null)));
|
||||
// 测试 modbusReadAddrGap 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setModbusReadAddrGap(null)));
|
||||
// 测试 isUpload 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setIsUpload(null)));
|
||||
// 测试 gatewayId 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setGatewayId(null)));
|
||||
// 测试 orgId 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setOrgId(null)));
|
||||
// 测试 remark 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setRemark(null)));
|
||||
// 测试 isEnable 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setIsEnable(null)));
|
||||
// 测试 createTime 不匹配
|
||||
deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setCreateTime(null)));
|
||||
// 准备参数
|
||||
DevicePageReqVO reqVO = new DevicePageReqVO();
|
||||
reqVO.setDeviceConfigId(null);
|
||||
reqVO.setDeviceCode(null);
|
||||
reqVO.setDeviceName(null);
|
||||
reqVO.setDeviceType(null);
|
||||
reqVO.setSiemensSeries(null);
|
||||
reqVO.setSiemensConnectParam(null);
|
||||
reqVO.setReadCronType(null);
|
||||
reqVO.setReadRepeatValue(null);
|
||||
reqVO.setReadRepeatUnit(null);
|
||||
reqVO.setReadCron(null);
|
||||
reqVO.setWriteCronType(null);
|
||||
reqVO.setWriteRepeatValue(null);
|
||||
reqVO.setWriteRepeatUnit(null);
|
||||
reqVO.setWriteCron(null);
|
||||
reqVO.setLocalPersistent(null);
|
||||
reqVO.setUploadRate(null);
|
||||
reqVO.setRateCount(null);
|
||||
reqVO.setModbusProtocol(null);
|
||||
reqVO.setModbusPattern(null);
|
||||
reqVO.setPortName(null);
|
||||
reqVO.setModbusConnectParam(null);
|
||||
reqVO.setModbusReadAddrGap(null);
|
||||
reqVO.setIsUpload(null);
|
||||
reqVO.setGatewayId(null);
|
||||
reqVO.setOrgId(null);
|
||||
reqVO.setRemark(null);
|
||||
reqVO.setIsEnable(null);
|
||||
reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
|
||||
|
||||
// 调用
|
||||
PageResult<DeviceDO> pageResult = deviceService.getDevicePage(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
assertPojoEquals(dbDevice, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue