提交 | 用户 | 时间
|
487b2f
|
1 |
package cn.stylefeng.guns.opcua.controller; |
YY |
2 |
|
|
3 |
import java.util.List; |
|
4 |
import java.util.stream.Collectors; |
|
5 |
import java.util.stream.Stream; |
|
6 |
|
|
7 |
import javax.servlet.http.HttpServletRequest; |
|
8 |
|
|
9 |
import org.springframework.beans.factory.annotation.Autowired; |
|
10 |
import org.springframework.stereotype.Controller; |
|
11 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
12 |
import org.springframework.web.bind.annotation.ResponseBody; |
|
13 |
import cn.stylefeng.guns.opcua.client.ClientHandler; |
|
14 |
import cn.stylefeng.guns.opcua.entity.NodeEntity; |
|
15 |
|
|
16 |
//import com.google.common.collect.Lists; |
|
17 |
|
|
18 |
/** |
|
19 |
* @ClassName: OpcUaController |
|
20 |
* @Description: OpcUa控制器 |
|
21 |
* @author yyt |
|
22 |
* @date 2023年10月13日 |
|
23 |
*/ |
|
24 |
@Controller |
|
25 |
public class CommonController { |
|
26 |
|
|
27 |
@Autowired |
|
28 |
private ClientHandler clientHandler; |
|
29 |
|
|
30 |
/** |
|
31 |
* @MethodName: connect |
|
32 |
* @Description: opcua连接并订阅变量 |
|
33 |
* @param request |
|
34 |
* @param response |
|
35 |
* @return |
|
36 |
* @CreateTime 2023年10月13日 |
|
37 |
*/ |
|
38 |
@RequestMapping("/connect") |
|
39 |
@ResponseBody |
|
40 |
public String connect() { |
|
41 |
|
|
42 |
try { |
|
43 |
return clientHandler.connect(); |
|
44 |
} catch (Exception e) { |
|
45 |
e.printStackTrace(); |
|
46 |
return "fail"; |
|
47 |
} |
|
48 |
} |
|
49 |
|
|
50 |
/** |
|
51 |
* @MethodName: disconnect |
|
52 |
* @Description: disconnect |
|
53 |
* @return |
|
54 |
* @CreateTime 2023年10月13日 |
|
55 |
*/ |
|
56 |
@RequestMapping("/disconnect") |
|
57 |
@ResponseBody |
|
58 |
public String disconnect() { |
|
59 |
|
|
60 |
try { |
|
61 |
return clientHandler.disconnect(); |
|
62 |
} catch (Exception e) { |
|
63 |
e.printStackTrace(); |
|
64 |
return "fail"; |
|
65 |
} |
|
66 |
} |
|
67 |
|
|
68 |
/** |
|
69 |
* @MethodName: subscribe |
|
70 |
* @Description: subscribe |
|
71 |
* @return |
|
72 |
* @CreateTime 2023年10月13日 |
|
73 |
*/ |
|
74 |
@RequestMapping("/subscribe") |
|
75 |
@ResponseBody |
|
76 |
public String subscribe(HttpServletRequest request) { |
|
77 |
|
|
78 |
try { |
|
79 |
List<NodeEntity> nodes = Stream.of(request.getParameter("id").split(",")) |
|
80 |
.map(id -> NodeEntity.builder().index(2).identifier(id).build()).collect(Collectors.toList()); |
|
81 |
|
|
82 |
return clientHandler.subscribe(nodes); |
|
83 |
} catch (Exception e) { |
|
84 |
e.printStackTrace(); |
|
85 |
return "fail"; |
|
86 |
} |
|
87 |
} |
|
88 |
|
|
89 |
/** |
|
90 |
* @MethodName: write |
|
91 |
* @Description: 节点写入 |
|
92 |
* @param request |
|
93 |
* @return |
|
94 |
* @CreateTime 2023年10月13日 |
|
95 |
*/ |
|
96 |
@RequestMapping("/write") |
|
97 |
@ResponseBody |
|
98 |
public String write(HttpServletRequest request) { |
|
99 |
|
|
100 |
NodeEntity node = NodeEntity.builder().index(2).identifier(request.getParameter("id")) |
|
101 |
.value(request.getParameter("value")).type(request.getParameter("type")).build(); |
|
102 |
|
|
103 |
try { |
|
104 |
return clientHandler.write(node); |
|
105 |
} catch (Exception e) { |
|
106 |
e.printStackTrace(); |
|
107 |
return "fail"; |
|
108 |
} |
|
109 |
} |
|
110 |
|
|
111 |
/** |
|
112 |
* @MethodName: read |
|
113 |
* @Description: read |
|
114 |
* @param request |
|
115 |
* @return |
|
116 |
* @CreateTime 2023年10月13日 |
|
117 |
*/ |
|
118 |
@RequestMapping("/read") |
|
119 |
@ResponseBody |
|
120 |
public String read(HttpServletRequest request) { |
|
121 |
|
|
122 |
NodeEntity node = NodeEntity.builder().index(2).identifier(request.getParameter("id")).build(); |
|
123 |
|
|
124 |
try { |
|
125 |
return clientHandler.read(node); |
|
126 |
} catch (Exception e) { |
|
127 |
e.printStackTrace(); |
|
128 |
return "fail"; |
|
129 |
} |
|
130 |
} |
|
131 |
} |