Jay
2024-11-25 ee9f604388a3e77d3f4654e326f3976552e7f532
提交 | 用户 | 时间
a6de49 1 package com.iailab.module.data.channel.modbus.controller.admin;
H 2
03e8ac 3 import com.iailab.framework.apilog.core.annotation.ApiAccessLog;
c7f709 4 import com.iailab.framework.common.pojo.CommonResult;
03e8ac 5 import com.iailab.framework.common.pojo.PageParam;
c7f709 6 import com.iailab.framework.common.pojo.PageResult;
L 7 import com.iailab.framework.common.util.object.BeanUtils;
03e8ac 8 import com.iailab.framework.common.util.object.ConvertUtils;
J 9 import com.iailab.framework.excel.core.util.ExcelUtils;
e8ad66 10 import com.iailab.module.data.channel.modbus.collector.ModBusCollector;
c7f709 11 import com.iailab.module.data.channel.modbus.entity.ChannelModBusTagEntity;
a6de49 12 import com.iailab.module.data.channel.modbus.service.ChannelModbusTagService;
03e8ac 13 import com.iailab.module.data.channel.modbus.vo.ModBusTagExportExcelVO;
J 14 import com.iailab.module.data.channel.modbus.vo.ModBusTagImportExcelVO;
c7f709 15 import com.iailab.module.data.channel.modbus.vo.ModBusTagPageReqVO;
L 16 import com.iailab.module.data.channel.modbus.vo.ModBusTagRespVO;
03e8ac 17 import com.iailab.module.data.channel.tag.vo.TagImportRespVO;
2f03e2 18 import com.iailab.module.data.common.enums.DataQualityEnum;
f21253 19 import com.iailab.module.data.common.enums.IsEnableEnum;
03e8ac 20 import io.swagger.v3.oas.annotations.Operation;
J 21 import io.swagger.v3.oas.annotations.Parameter;
22 import io.swagger.v3.oas.annotations.Parameters;
23 import org.springframework.security.access.prepost.PreAuthorize;
24 import org.springframework.validation.annotation.Validated;
a6de49 25 import org.springframework.web.bind.annotation.*;
03e8ac 26 import org.springframework.web.multipart.MultipartFile;
a6de49 27
c7f709 28 import javax.annotation.Resource;
03e8ac 29 import javax.servlet.http.HttpServletResponse;
c7f709 30 import javax.validation.Valid;
03e8ac 31 import java.io.IOException;
J 32 import java.util.Collections;
c7f709 33 import java.util.Date;
03e8ac 34 import java.util.List;
a6de49 35 import java.util.UUID;
H 36
03e8ac 37 import static com.iailab.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
c7f709 38 import static com.iailab.framework.common.pojo.CommonResult.success;
L 39
a6de49 40 /**
c7f709 41  * @author lirm
L 42  * @Description
43  * @createTime 2024年08月27日
a6de49 44  */
H 45 @RestController
46 @RequestMapping("/data/channel/modbus/tag")
47 public class ChannelModbusTagController {
48     @Resource
49     private ChannelModbusTagService channelModbusTagService;
50
e8ad66 51     @Resource
D 52     private ModBusCollector modBusCollector;
53
c7f709 54     @GetMapping("/page")
L 55     public CommonResult<PageResult<ModBusTagRespVO>> list(@Valid ModBusTagPageReqVO reqVO) {
56         PageResult<ChannelModBusTagEntity> page = channelModbusTagService.queryPage(reqVO);
2f03e2 57         PageResult<ModBusTagRespVO> pageResult = BeanUtils.toBean(page, ModBusTagRespVO.class);
58         pageResult.getList().forEach(item -> {
59             item.setDataValue(modBusCollector.getTagValue(reqVO.getDeviceId(), item.getTagName()));
60             item.setDataTime(new Date());
61             item.setDataQuality(DataQualityEnum.getEumByValue(item.getDataValue()).getDesc());
62         });
63         return success(pageResult);
a6de49 64     }
2f03e2 65
a6de49 66     /**
c7f709 67      * 根据id查询设备详情
a6de49 68      *
H 69      * @param id
70      */
71     @GetMapping("/info/{id}")
c7f709 72     public CommonResult<ChannelModBusTagEntity> info(@PathVariable("id") String id) {
L 73         ChannelModBusTagEntity info = channelModbusTagService.info(id);
74         return success(info);
a6de49 75     }
H 76
77     /**
c7f709 78      * 添加设备
a6de49 79      *
H 80      * @param channelModBusTagEntity
81      */
a477ef 82     @PostMapping("/create")
83     public CommonResult<Boolean> create(@RequestBody ChannelModBusTagEntity channelModBusTagEntity) {
c7f709 84         String id = UUID.randomUUID().toString();
L 85         channelModBusTagEntity.setId(id);
86         channelModBusTagEntity.setCreateTime(new Date());
87         channelModbusTagService.add(channelModBusTagEntity);
88         return success(true);
a6de49 89     }
H 90
91     /**
c7f709 92      * 修改设备
a6de49 93      *
c7f709 94      * @param channelModBusTagEntity
a6de49 95      */
c7f709 96     @PutMapping("/update")
L 97     public CommonResult<Boolean> update(@RequestBody ChannelModBusTagEntity channelModBusTagEntity) {
98         channelModBusTagEntity.setUpdateTime(new Date());
99         channelModbusTagService.update(channelModBusTagEntity);
100         return success(true);
a6de49 101     }
H 102
c7f709 103     /**
L 104      * 删除设备
105      *
106      * @param id
107      */
108     @DeleteMapping("/delete")
109     public CommonResult<Boolean> delete(@RequestParam("id") String id) {
110         channelModbusTagService.delete(id);
111         return success(true);
112     }
03e8ac 113
J 114     @GetMapping("/export")
115     @Operation(summary = "导出modbus tag列表")
116     @PreAuthorize("@ss.hasPermission('data:channel-modbus-tag:export')")
117     @ApiAccessLog(operateType = EXPORT)
118     public void exportPointList(@Validated ModBusTagPageReqVO reqVO, HttpServletResponse response) throws IOException {
119         reqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
120         PageResult<ChannelModBusTagEntity> page = channelModbusTagService.queryPage(reqVO);
121
122         List<ModBusTagExportExcelVO> list = ConvertUtils.sourceToTarget(page.getList(), ModBusTagExportExcelVO.class);
123         ExcelUtils.write(response, "tag列表.xls", "数据", ModBusTagExportExcelVO.class, list, true);
124     }
125
126
127     @GetMapping("/get-import-template")
128     @Operation(summary = "获得tag导入模板")
129     public void importTemplate(HttpServletResponse response) throws IOException {
130         // 手动创建导出 demo
131         List<ModBusTagImportExcelVO> list = Collections.singletonList(
132                 ModBusTagImportExcelVO.builder().tagName("Tag名称").tagDesc("Tag描述").dataType("String").
f21253 133                         address("123").format("1").samplingRate(1000).enabled(IsEnableEnum.ENABLE.getCode())
03e8ac 134                         .build()
J 135         );
136         // 输出
2f03e2 137         ExcelUtils.write(response, "tag导入模板.xls", "tag列表", ModBusTagImportExcelVO.class, list, true);
03e8ac 138     }
J 139
140     @PostMapping("/import")
141     @Operation(summary = "导入tag")
142     @Parameters({
143             @Parameter(name = "file", description = "Excel 文件", required = true),
144             @Parameter(name = "updateSupport", description = "是否支持更新,默认为 false", example = "true")
145     })
146     @PreAuthorize("@ss.hasPermission('data:channel-modbus-tag:import')")
147     public CommonResult<TagImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
148                                                      @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport,
149                                                      @RequestParam("device") String device) throws Exception {
150         List<ModBusTagImportExcelVO> list = ExcelUtils.read(file, ModBusTagImportExcelVO.class);
2f03e2 151         return success(channelModbusTagService.importModBusTagList(list, updateSupport, device));
03e8ac 152     }
a6de49 153 }