dengzedong
2024-11-06 db184afd0c5bf3359b44eb0251fa5b07386eb3ff
提交 | 用户 | 时间
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;
849c3b 8 import com.iailab.framework.common.util.object.ConvertUtils;
D 9 import com.iailab.framework.excel.core.util.ExcelUtils;
e8ad66 10 import com.iailab.module.data.channel.opcua.collector.OpcUaCollector;
03e8ac 11 import com.iailab.framework.common.util.object.ConvertUtils;
J 12 import com.iailab.framework.excel.core.util.ExcelUtils;
a6de49 13 import com.iailab.module.data.channel.opcua.entity.ChannelOPCUATagEntity;
H 14 import com.iailab.module.data.channel.opcua.service.ChannelOPCUATagService;
03e8ac 15 import com.iailab.module.data.channel.opcua.vo.OpcUaTagExportExcelVO;
J 16 import com.iailab.module.data.channel.opcua.vo.OpcUaTagImportExcelVO;
aecc49 17 import com.iailab.module.data.channel.opcua.vo.OpcUaTagPageReqVO;
L 18 import com.iailab.module.data.channel.opcua.vo.OpcUaTagRespVO;
03e8ac 19 import com.iailab.module.data.channel.tag.vo.TagImportRespVO;
J 20 import io.swagger.v3.oas.annotations.Operation;
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;
e8ad66 34 import java.util.List;
a6de49 35 import java.util.UUID;
e8ad66 36 import java.util.stream.Collectors;
aecc49 37
03e8ac 38 import static com.iailab.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
aecc49 39 import static com.iailab.framework.common.pojo.CommonResult.success;
a6de49 40
H 41 /**
42  * 操作opcua tag配置
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) {
e8ad66 59
aecc49 60         PageResult<ChannelOPCUATagEntity> page = channelOpcuaTagService.queryPage(reqVO);
e8ad66 61         PageResult<OpcUaTagRespVO> pageResultVO = new PageResult<>();
D 62         pageResultVO.setTotal(page.getTotal());
63
64         List<OpcUaTagRespVO> vos = page.getList().stream().map(entity -> {
65
66             OpcUaTagRespVO vo = BeanUtils.toBean(entity,OpcUaTagRespVO.class);
67             try{
68                 vo.setDataValue( Double.parseDouble(opcUaCollector.getTagValue(reqVO.getDeviceId(),entity.getTagName())));
69             }catch (Exception e){
70                 e.printStackTrace();
71             }
72             return vo;
73         }).collect(Collectors.toList());
74
75         pageResultVO.setList(vos);
76
77         return success(pageResultVO);
a6de49 78     }
aecc49 79
a477ef 80     @PreAuthorize("@ss.hasPermission('data:channel-opcua:query')")
a6de49 81     @GetMapping("/info/{id}")
aecc49 82     public CommonResult<ChannelOPCUATagEntity> info(@PathVariable("id") String id) {
L 83         ChannelOPCUATagEntity info = channelOpcuaTagService.info(id);
84         return success(info);
a6de49 85     }
aecc49 86
a477ef 87     @PreAuthorize("@ss.hasPermission('data:channel-opcua:create')")
88     @PostMapping("/create")
89     public CommonResult<Boolean> create(@RequestBody ChannelOPCUATagEntity channelOPCUATagEntity) {
aecc49 90         String id = UUID.randomUUID().toString();
L 91         channelOPCUATagEntity.setId(id);
92         channelOPCUATagEntity.setCreateTime(new Date());
93         channelOpcuaTagService.add(channelOPCUATagEntity);
94         return success(true);
a6de49 95     }
H 96
a477ef 97     @PreAuthorize("@ss.hasPermission('data:channel-opcua:update')")
aecc49 98     @PutMapping("/update")
L 99     public CommonResult<Boolean> update(@RequestBody ChannelOPCUATagEntity channelOPCUATagEntity) {
100         channelOPCUATagEntity.setUpdateTime(new Date());
a6de49 101         channelOpcuaTagService.update(channelOPCUATagEntity);
aecc49 102         return success(true);
a6de49 103     }
H 104
a477ef 105     @PreAuthorize("@ss.hasPermission('data:channel-opcua:delete')")
aecc49 106     @DeleteMapping("/delete")
L 107     public CommonResult<Boolean> delete(@RequestParam("id") String id) {
a6de49 108         channelOpcuaTagService.delete(id);
aecc49 109         return success(true);
a6de49 110     }
H 111
03e8ac 112     @GetMapping("/export")
J 113     @Operation(summary = "导出modbus tag列表")
114     @PreAuthorize("@ss.hasPermission('data:channel-opcua-tag:export')")
115     @ApiAccessLog(operateType = EXPORT)
116     public void exportPointList(@Validated OpcUaTagPageReqVO reqVO, HttpServletResponse response) throws IOException {
117         reqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
118         PageResult<ChannelOPCUATagEntity> page = channelOpcuaTagService.queryPage(reqVO);
119         List<OpcUaTagExportExcelVO> list = ConvertUtils.sourceToTarget(page.getList(), OpcUaTagExportExcelVO.class);
120         ExcelUtils.write(response, "tag列表.xls", "数据", OpcUaTagExportExcelVO.class, list, true);
121     }
122
123     @GetMapping("/get-import-template")
124     @Operation(summary = "获得tag导入模板")
125     public void importTemplate(HttpServletResponse response) throws IOException {
126         // 手动创建导出 demo
127         List<OpcUaTagImportExcelVO> list = Collections.singletonList(
128                 OpcUaTagImportExcelVO.builder().tagName("Tag名称").dataType("String").address("123").samplingRate(1000).enabled(1)
129                         .build()
130         );
131         // 输出
132         ExcelUtils.write(response, "tag导入模板.xls", "tag列表", OpcUaTagImportExcelVO.class, list,true);
133     }
134
135     @PostMapping("/import")
136     @Operation(summary = "导入tag")
137     @Parameters({
138             @Parameter(name = "file", description = "Excel 文件", required = true),
139             @Parameter(name = "updateSupport", description = "是否支持更新,默认为 false", example = "true")
140     })
141     @PreAuthorize("@ss.hasPermission('data:channel-opcua-tag:import')")
142     public CommonResult<TagImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
143                                                      @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport,
144                                                      @RequestParam("device") String device) throws Exception {
145         List<OpcUaTagImportExcelVO> list = ExcelUtils.read(file, OpcUaTagImportExcelVO.class);
146         return success(channelOpcuaTagService.importOpcUaTagList(list, updateSupport,device));
147     }
a6de49 148 }