Jay
2024-11-25 ee9f604388a3e77d3f4654e326f3976552e7f532
提交 | 用户 | 时间
a6de49 1 package com.iailab.module.data.channel.opcua.controller.admin;
H 2
03e8ac 3 import com.iailab.framework.apilog.core.annotation.ApiAccessLog;
aecc49 4 import com.iailab.framework.common.pojo.CommonResult;
03e8ac 5 import com.iailab.framework.common.pojo.PageParam;
aecc49 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.opcua.collector.OpcUaCollector;
a6de49 11 import com.iailab.module.data.channel.opcua.entity.ChannelOPCUATagEntity;
H 12 import com.iailab.module.data.channel.opcua.service.ChannelOPCUATagService;
03e8ac 13 import com.iailab.module.data.channel.opcua.vo.OpcUaTagExportExcelVO;
J 14 import com.iailab.module.data.channel.opcua.vo.OpcUaTagImportExcelVO;
aecc49 15 import com.iailab.module.data.channel.opcua.vo.OpcUaTagPageReqVO;
L 16 import com.iailab.module.data.channel.opcua.vo.OpcUaTagRespVO;
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;
a477ef 23 import org.springframework.security.access.prepost.PreAuthorize;
03e8ac 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
aecc49 28 import javax.annotation.Resource;
03e8ac 29 import javax.servlet.http.HttpServletResponse;
aecc49 30 import javax.validation.Valid;
03e8ac 31 import java.io.IOException;
J 32 import java.util.Collections;
aecc49 33 import java.util.Date;
03e8ac 34 import java.util.List;
a6de49 35 import java.util.UUID;
aecc49 36
03e8ac 37 import static com.iailab.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
aecc49 38 import static com.iailab.framework.common.pojo.CommonResult.success;
a6de49 39
H 40 /**
41  * 操作opcua tag配置
2f03e2 42  *
aecc49 43  * @author lirm
L 44  * @Description
45  * @createTime 2024年08月26日
a6de49 46  */
H 47 @RestController
48 @RequestMapping("/data/channel/opcua/tag")
49 public class ChannelOPCUATagController {
50     @Resource
51     private ChannelOPCUATagService channelOpcuaTagService;
52
e8ad66 53     @Resource
D 54     private OpcUaCollector opcUaCollector;
55
a477ef 56     @PreAuthorize("@ss.hasPermission('data:channel-opcua:query')")
aecc49 57     @GetMapping("page")
L 58     public CommonResult<PageResult<OpcUaTagRespVO>> list(@Valid OpcUaTagPageReqVO reqVO) {
59         PageResult<ChannelOPCUATagEntity> page = channelOpcuaTagService.queryPage(reqVO);
2f03e2 60         PageResult<OpcUaTagRespVO> pageResult = BeanUtils.toBean(page, OpcUaTagRespVO.class);
61         pageResult.getList().forEach(item -> {
62             item.setDataValue(opcUaCollector.getTagValue(reqVO.getDeviceId(), item.getTagName()));
63             item.setDataTime(new Date());
64             item.setDataQuality(DataQualityEnum.getEumByValue(item.getDataValue()).getDesc());
65         });
66         return success(pageResult);
a6de49 67     }
aecc49 68
a477ef 69     @PreAuthorize("@ss.hasPermission('data:channel-opcua:query')")
a6de49 70     @GetMapping("/info/{id}")
aecc49 71     public CommonResult<ChannelOPCUATagEntity> info(@PathVariable("id") String id) {
L 72         ChannelOPCUATagEntity info = channelOpcuaTagService.info(id);
73         return success(info);
a6de49 74     }
aecc49 75
a477ef 76     @PreAuthorize("@ss.hasPermission('data:channel-opcua:create')")
77     @PostMapping("/create")
78     public CommonResult<Boolean> create(@RequestBody ChannelOPCUATagEntity channelOPCUATagEntity) {
aecc49 79         String id = UUID.randomUUID().toString();
L 80         channelOPCUATagEntity.setId(id);
81         channelOPCUATagEntity.setCreateTime(new Date());
82         channelOpcuaTagService.add(channelOPCUATagEntity);
83         return success(true);
a6de49 84     }
H 85
a477ef 86     @PreAuthorize("@ss.hasPermission('data:channel-opcua:update')")
aecc49 87     @PutMapping("/update")
L 88     public CommonResult<Boolean> update(@RequestBody ChannelOPCUATagEntity channelOPCUATagEntity) {
89         channelOPCUATagEntity.setUpdateTime(new Date());
a6de49 90         channelOpcuaTagService.update(channelOPCUATagEntity);
aecc49 91         return success(true);
a6de49 92     }
H 93
a477ef 94     @PreAuthorize("@ss.hasPermission('data:channel-opcua:delete')")
aecc49 95     @DeleteMapping("/delete")
L 96     public CommonResult<Boolean> delete(@RequestParam("id") String id) {
a6de49 97         channelOpcuaTagService.delete(id);
aecc49 98         return success(true);
a6de49 99     }
H 100
03e8ac 101     @GetMapping("/export")
J 102     @Operation(summary = "导出modbus tag列表")
103     @PreAuthorize("@ss.hasPermission('data:channel-opcua-tag:export')")
104     @ApiAccessLog(operateType = EXPORT)
105     public void exportPointList(@Validated OpcUaTagPageReqVO reqVO, HttpServletResponse response) throws IOException {
106         reqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
107         PageResult<ChannelOPCUATagEntity> page = channelOpcuaTagService.queryPage(reqVO);
108         List<OpcUaTagExportExcelVO> list = ConvertUtils.sourceToTarget(page.getList(), OpcUaTagExportExcelVO.class);
109         ExcelUtils.write(response, "tag列表.xls", "数据", OpcUaTagExportExcelVO.class, list, true);
110     }
111
112     @GetMapping("/get-import-template")
113     @Operation(summary = "获得tag导入模板")
114     public void importTemplate(HttpServletResponse response) throws IOException {
115         // 手动创建导出 demo
116         List<OpcUaTagImportExcelVO> list = Collections.singletonList(
f21253 117                 OpcUaTagImportExcelVO.builder().tagName("Tag名称").dataType("String").address("123").samplingRate(1000).enabled(IsEnableEnum.ENABLE.getCode())
03e8ac 118                         .build()
J 119         );
120         // 输出
2f03e2 121         ExcelUtils.write(response, "tag导入模板.xls", "tag列表", OpcUaTagImportExcelVO.class, list, true);
03e8ac 122     }
J 123
124     @PostMapping("/import")
125     @Operation(summary = "导入tag")
126     @Parameters({
127             @Parameter(name = "file", description = "Excel 文件", required = true),
128             @Parameter(name = "updateSupport", description = "是否支持更新,默认为 false", example = "true")
129     })
130     @PreAuthorize("@ss.hasPermission('data:channel-opcua-tag:import')")
131     public CommonResult<TagImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
132                                                      @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport,
133                                                      @RequestParam("device") String device) throws Exception {
134         List<OpcUaTagImportExcelVO> list = ExcelUtils.read(file, OpcUaTagImportExcelVO.class);
2f03e2 135         return success(channelOpcuaTagService.importOpcUaTagList(list, updateSupport, device));
03e8ac 136     }
a6de49 137 }