工业互联网平台2.0版本后端代码
dongyukun
2024-11-05 e8ad669f7c97d45cd23630dc101180a130d6c17e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.iailab.module.data.channel.opcda.controller.admin;
 
import com.iailab.framework.common.pojo.CommonResult;
import com.iailab.framework.common.pojo.PageResult;
import com.iailab.framework.common.util.object.BeanUtils;
import com.iailab.module.data.channel.opcda.collector.OpcDACollector;
import com.iailab.module.data.channel.opcda.entity.ChannelOPCDADeviceEntity;
import com.iailab.module.data.channel.opcda.entity.ChannelOPCDATagEntity;
import com.iailab.module.data.channel.opcda.service.ChannelOPCDADeviceService;
import com.iailab.module.data.channel.opcda.service.ChannelOPCDATagService;
import com.iailab.module.data.channel.opcda.vo.OpcDaTagPageReqVO;
import com.iailab.module.data.channel.opcda.vo.OpcDaTagRespVO;
import com.iailab.module.data.common.enums.DataSourceType;
import com.iailab.module.data.common.exception.RRException;
import com.iailab.module.data.common.utils.TagUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
 
import static com.iailab.framework.common.pojo.CommonResult.success;
 
/**
 * 操作OPCDA tag配置
 * @author lirm
 * @Description
 * @createTime 2024年08月26日
 */
@RestController
@RequestMapping("/data/channel/opcda/tag")
public class ChannelOPCDATagController {
    @Autowired
    private ChannelOPCDATagService channelOPCDATagService;
 
    @Autowired
    private OpcDACollector opcDACollector;
 
    @Autowired
    private ChannelOPCDADeviceService channelOPCDADeviceService;
 
    @PreAuthorize("@ss.hasPermission('data:channel-opcua:query')")
    @GetMapping("page")
    public CommonResult<PageResult<OpcDaTagRespVO>> list(@Valid OpcDaTagPageReqVO reqVO) {
 
        PageResult<ChannelOPCDATagEntity> page = channelOPCDATagService.queryPage(reqVO);
        PageResult<OpcDaTagRespVO> pageResultVO = new PageResult<>();
        pageResultVO.setTotal(page.getTotal());
 
        List<OpcDaTagRespVO> vos = page.getList().stream().map(entity -> {
 
            OpcDaTagRespVO vo = BeanUtils.toBean(entity,OpcDaTagRespVO.class);
            List<String[]> tags = new ArrayList<>();
            String[] array  = {reqVO.getServerId(),entity.getTagName()};
            tags.add(array);
            ChannelOPCDADeviceEntity OPCDADevice = channelOPCDADeviceService.info(reqVO.getServerId());
            vo.setDataValue(Double.parseDouble(opcDACollector.getTagValues(tags).get(TagUtils.genTagId(DataSourceType.OPCDA.getCode(), OPCDADevice.getServerName(),entity.getTagName())).toString()));
            return vo;
        }).collect(Collectors.toList());
 
        pageResultVO.setList(vos);
 
        return success(pageResultVO);
    }
 
    @PreAuthorize("@ss.hasPermission('data:channel-opcua:query')")
    @GetMapping("/info/{id}")
    public CommonResult<ChannelOPCDATagEntity> info(@PathVariable("id") String id) {
        ChannelOPCDATagEntity info = channelOPCDATagService.info(id);
        return success(info);
    }
 
    @PreAuthorize("@ss.hasPermission('data:channel-opcua:create')")
    @PostMapping("/create")
    public CommonResult<Boolean> create(@RequestBody ChannelOPCDATagEntity channelOPCDATagEntity) {
        String id = UUID.randomUUID().toString();
        channelOPCDATagEntity.setId(id);
        channelOPCDATagEntity.setCreateTime(new Date());
        channelOPCDATagService.add(channelOPCDATagEntity);
        return success(true);
    }
 
    @PreAuthorize("@ss.hasPermission('data:channel-opcua:update')")
    @PutMapping("/update")
    public CommonResult<Boolean> update(@RequestBody ChannelOPCDATagEntity channelOPCDATagEntity) {
        channelOPCDATagEntity.setUpdateTime(new Date());
        channelOPCDATagService.update(channelOPCDATagEntity);
        return success(true);
    }
 
    @PreAuthorize("@ss.hasPermission('data:channel-opcua:delete')")
    @DeleteMapping("/delete")
    public CommonResult<Boolean> delete(@RequestParam("id") String id) {
        channelOPCDATagService.delete(id);
        return success(true);
    }
 
    @PostMapping("/import/{serverId}")
    public CommonResult<String> importTag(@PathVariable("serverId") String serverId, @RequestParam("file") MultipartFile file) {
        try {
            if (file.isEmpty()) {
                throw new RRException("上传文件不能为空");
            }
            channelOPCDATagService.importTag(serverId, file);
        } catch (Exception ex) {
            ex.getMessage();
        }
        return success("上传成功");
    }
}