潘志宝
2024-10-13 86145e48fa25a6ff050b8f6020a0dd75b9c0d3d0
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.iailab.module.data.channel.tag.controller.admin;
 
import com.iailab.module.data.channel.kio.entity.ChannelKioDeviceEntity;
import com.iailab.module.data.channel.kio.entity.ChannelKioTagEntity;
import com.iailab.module.data.common.enums.DataSourceType;
import com.iailab.framework.common.pojo.CommonResult;
import com.iailab.module.data.channel.kio.service.ChannelKioDeviceService;
import com.iailab.module.data.channel.kio.service.ChannelKioTagService;
import com.iailab.module.data.channel.modbus.entity.ChannelModBusDeviceEntity;
import com.iailab.module.data.channel.modbus.entity.ChannelModBusTagEntity;
import com.iailab.module.data.channel.modbus.service.ChannelModbusDeviceService;
import com.iailab.module.data.channel.modbus.service.ChannelModbusTagService;
import com.iailab.module.data.channel.opcua.entity.ChannelOPCUADeviceEntity;
import com.iailab.module.data.channel.opcua.service.ChannelOPCUADeviceService;
import com.iailab.module.data.channel.opcua.service.ChannelOPCUATagService;
import com.iailab.module.data.channel.tag.dto.TagOptionDTO;
import com.iailab.module.data.channel.http.entity.HttpApiEntity;
import com.iailab.module.data.channel.http.entity.HttpTagEntity;
import com.iailab.module.data.channel.http.service.HttpApiService;
import com.iailab.module.data.channel.opcua.entity.ChannelOPCUATagEntity;
import com.iailab.module.data.channel.http.service.HttpTagService;
import javax.annotation.Resource;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
 
/**
 * @author PanZhibao
 * @Description
 * @createTime 2024年05月16日
 */
@RestController
@RequestMapping("/data/channel/tag")
public class TagController {
 
    @Resource
    private ChannelOPCUADeviceService channelOPCUADeviceService;
 
    @Resource
    private ChannelOPCUATagService channelOPCUATagService;
 
    @Resource
    private ChannelModbusDeviceService channelModbusDeviceService;
 
    @Resource
    private ChannelModbusTagService channelModbusTagService;
 
    @Resource
    private ChannelKioDeviceService channelKioDeviceService;
 
    @Resource
    private ChannelKioTagService channelKioTagService;
 
    @Resource
    private HttpApiService httpApiService;
 
    @Resource
    private HttpTagService tagService;
 
 
    @GetMapping("tree")
    public CommonResult<List<TagOptionDTO>> tagTree() {
        List<TagOptionDTO> result = new ArrayList<>();
 
        TagOptionDTO opcuaData = new TagOptionDTO();
        opcuaData.setValue(DataSourceType.OPCUA.getCode());
        opcuaData.setLabel(DataSourceType.OPCUA.getDesc());
        List<ChannelOPCUADeviceEntity> opcuaDeviceList = channelOPCUADeviceService.list(new HashMap<>());
        List<TagOptionDTO> opcuaDeviceOp = new ArrayList<>();
        if (!CollectionUtils.isEmpty(opcuaDeviceList)) {
            opcuaDeviceList.forEach(item -> {
                TagOptionDTO op1 = new TagOptionDTO();
                op1.setValue(item.getId());
                op1.setLabel(item.getServerName());
                List<ChannelOPCUATagEntity> tags = channelOPCUATagService.getByDevice(item.getServerName());
                List<TagOptionDTO> op2 = new ArrayList<>();
                tags.forEach(item1 -> {
                    TagOptionDTO op3 = new TagOptionDTO();
                    op3.setValue(item1.getTagName());
                    op3.setLabel(item1.getTagName());
                    op2.add(op3);
                });
                op1.setChildren(op2);
                opcuaDeviceOp.add(op1);
            });
        }
        opcuaData.setChildren(opcuaDeviceOp);
        result.add(opcuaData);
 
        TagOptionDTO modbusData = new TagOptionDTO();
        modbusData.setValue(DataSourceType.ModBus.getCode());
        modbusData.setLabel(DataSourceType.ModBus.getDesc());
        List<ChannelModBusDeviceEntity> modbusList = channelModbusDeviceService.list(new HashMap<>());
        List<TagOptionDTO> modbusDeviceOp = new ArrayList<>();
        if (!CollectionUtils.isEmpty(modbusList)) {
            modbusList.forEach(item -> {
                TagOptionDTO op1 = new TagOptionDTO();
                op1.setValue(item.getId());
                op1.setLabel(item.getName());
                List<ChannelModBusTagEntity> tags = channelModbusTagService.getByDevice(item.getName());
                List<TagOptionDTO> op2 = new ArrayList<>();
                tags.forEach(item1 -> {
                    TagOptionDTO op3 = new TagOptionDTO();
                    op3.setValue(item1.getTagName());
                    op3.setLabel(item1.getTagName());
                    op2.add(op3);
                });
                op1.setChildren(op2);
                modbusDeviceOp.add(op1);
            });
        }
        modbusData.setChildren(modbusDeviceOp);
        result.add(modbusData);
 
 
        TagOptionDTO kioData = new TagOptionDTO();
        kioData.setValue(DataSourceType.KIO.getCode());
        kioData.setLabel(DataSourceType.KIO.getDesc());
        List<ChannelKioDeviceEntity> kioList = channelKioDeviceService.list(new HashMap<>());
        List<TagOptionDTO> kioDeviceOp = new ArrayList<>();
        if (!CollectionUtils.isEmpty(kioList)) {
            kioList.forEach(item -> {
                TagOptionDTO op1 = new TagOptionDTO();
                op1.setValue(item.getId());
                op1.setLabel(item.getInstanceName());
                List<ChannelKioTagEntity> tags = channelKioTagService.getByDevice(item.getInstanceName());
                List<TagOptionDTO> op2 = new ArrayList<>();
                tags.forEach(item1 -> {
                    TagOptionDTO op3 = new TagOptionDTO();
                    op3.setValue(item1.getTagName());
                    op3.setLabel(item1.getTagName());
                    op2.add(op3);
                });
                op1.setChildren(op2);
                kioDeviceOp.add(op1);
            });
        }
        kioData.setChildren(kioDeviceOp);
        result.add(kioData);
 
        TagOptionDTO httpApiData = new TagOptionDTO();
        httpApiData.setValue(DataSourceType.HTTP.getCode());
        httpApiData.setLabel(DataSourceType.HTTP.getDesc());
        List<HttpApiEntity> httpApiList = httpApiService.list();
        List<TagOptionDTO> httpApiOp = new ArrayList<>();
        if (!CollectionUtils.isEmpty(httpApiList)) {
            httpApiList.forEach(item -> {
                TagOptionDTO op1 = new TagOptionDTO();
                op1.setValue(item.getId());
                op1.setLabel(item.getName());
                List<HttpTagEntity> tags = tagService.getApiId(item.getId());
                List<TagOptionDTO> op2 = new ArrayList<>();
                tags.forEach(item1 -> {
                    TagOptionDTO op3 = new TagOptionDTO();
                    op3.setValue(item1.getTagName());
                    op3.setLabel(item1.getTagName());
                    op2.add(op3);
                });
                op1.setChildren(op2);
                httpApiOp.add(op1);
            });
        }
        httpApiData.setChildren(httpApiOp);
        result.add(httpApiData);
 
        return new CommonResult().setData(result);
    }
}