|
|
|
@ -0,0 +1,180 @@
|
|
|
|
|
package cn.estsh.i3plus.core.apiservice.controller.base;
|
|
|
|
|
|
|
|
|
|
import cn.estsh.i3plus.platform.common.tool.HttpClientTool;
|
|
|
|
|
import cn.estsh.i3plus.platform.plugin.opc.pojo.OpcUAParam;
|
|
|
|
|
import cn.estsh.i3plus.platform.plugin.opc.service.OpcUAService;
|
|
|
|
|
import cn.estsh.i3plus.pojo.base.annotation.AnnoIgnoreLog;
|
|
|
|
|
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
|
|
|
|
|
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
|
|
|
|
|
import cn.estsh.impp.framework.base.controller.CoreBaseController;
|
|
|
|
|
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
|
|
|
|
|
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
|
|
|
|
|
import cn.estsh.impp.framework.boot.util.ResultBean;
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
|
import org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem;
|
|
|
|
|
import org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription;
|
|
|
|
|
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
|
|
|
|
|
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Hashtable;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.function.BiConsumer;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Description :
|
|
|
|
|
* @Reference :
|
|
|
|
|
* @Author : yunhao
|
|
|
|
|
* @CreateDate : 2020-01-08 11:53
|
|
|
|
|
* @Modify:
|
|
|
|
|
**/
|
|
|
|
|
@RestController
|
|
|
|
|
@Api(tags = "压测接口")
|
|
|
|
|
@RequestMapping("/impp/pressure-test")
|
|
|
|
|
public class PressureTestController extends CoreBaseController {
|
|
|
|
|
public static final Logger LOGGER = LoggerFactory.getLogger(PressureTestController.class);
|
|
|
|
|
|
|
|
|
|
private static Map<String,OpcUAService> uaServiceMap = new Hashtable<>();
|
|
|
|
|
|
|
|
|
|
public static Map<UInteger,OpcUAParam> upcSub = new Hashtable<>();
|
|
|
|
|
|
|
|
|
|
private OpcUAService getOpcUaService(OpcUAParam opcUAParam){
|
|
|
|
|
if(!uaServiceMap.containsKey(opcUAParam.getServerUrl())){
|
|
|
|
|
OpcUAService opcService = new OpcUAService();
|
|
|
|
|
opcService.connUaService(opcUAParam);
|
|
|
|
|
uaServiceMap.put(opcUAParam.getServerUrl(),opcService);
|
|
|
|
|
}
|
|
|
|
|
return uaServiceMap.get(opcUAParam.getServerUrl());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/opc-write")
|
|
|
|
|
@ApiOperation(value = "写Opc", notes = "写Opc")
|
|
|
|
|
@AnnoIgnoreLog
|
|
|
|
|
public ResultBean writeOpc(OpcUAParam opcUA){
|
|
|
|
|
try {
|
|
|
|
|
OpcUAService opcService = new OpcUAService();
|
|
|
|
|
boolean editResult = opcService.editOpcParamValue(opcUA);
|
|
|
|
|
return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultObject(editResult);
|
|
|
|
|
}catch(ImppBusiException busExcep){
|
|
|
|
|
return ResultBean.fail(busExcep);
|
|
|
|
|
}catch(Exception e){
|
|
|
|
|
return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/opc-read")
|
|
|
|
|
@ApiOperation(value = "读Opc", notes = "读Opc")
|
|
|
|
|
@AnnoIgnoreLog
|
|
|
|
|
public ResultBean readOpc(OpcUAParam opcUA){
|
|
|
|
|
try {
|
|
|
|
|
Object readResult = getOpcUaService(opcUA).getOpcParamValue(opcUA);
|
|
|
|
|
if(readResult == null){
|
|
|
|
|
return ResultBean.fail();
|
|
|
|
|
}
|
|
|
|
|
return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultObject(readResult);
|
|
|
|
|
}catch(ImppBusiException busExcep){
|
|
|
|
|
return ResultBean.fail(busExcep);
|
|
|
|
|
}catch(Exception e){
|
|
|
|
|
return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/opc-sub")
|
|
|
|
|
@ApiOperation(value = "订阅Opc", notes = "订阅Opc")
|
|
|
|
|
@AnnoIgnoreLog
|
|
|
|
|
public ResultBean subOpc(OpcUAParam opcUA,String callbackMethod,String callbackUrl){
|
|
|
|
|
try {
|
|
|
|
|
UaSubscription subscription = getOpcUaService(opcUA).createSubscription(opcUA);
|
|
|
|
|
getOpcUaService(opcUA).createMonitor(opcUA, subscription, new BiConsumer<UaMonitoredItem, DataValue>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void accept(UaMonitoredItem node, DataValue value) {
|
|
|
|
|
|
|
|
|
|
LOGGER.info("OPC订阅回调: {} - {} , callback {}", node.getReadValueId().getNodeId().toString(),value.getValue(), callbackMethod+callbackUrl);
|
|
|
|
|
HashMap<String,String> param = new HashMap<>();
|
|
|
|
|
param.put("key",node.getReadValueId().getNodeId().toString());
|
|
|
|
|
param.put("value",value.getValue().toString());
|
|
|
|
|
|
|
|
|
|
HttpClientTool.doHttpUrl(CommonEnumUtil.HTTP_METHOD_TYPE.valueOf(callbackMethod), callbackUrl, param);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
upcSub.put(subscription.getSubscriptionId(),opcUA);
|
|
|
|
|
return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultObject(subscription.getSubscriptionId().toString());
|
|
|
|
|
}catch(ImppBusiException busExcep){
|
|
|
|
|
return ResultBean.fail(busExcep);
|
|
|
|
|
}catch(Exception e){
|
|
|
|
|
return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/remove-opc-sub")
|
|
|
|
|
@ApiOperation(value = "移除Opc订阅", notes = "移除Opc订阅")
|
|
|
|
|
@AnnoIgnoreLog
|
|
|
|
|
public ResultBean removeSubOpc(OpcUAParam opcUA,String subscriptionId){
|
|
|
|
|
try {
|
|
|
|
|
getOpcUaService(opcUA).deleteSubscription(opcUA,UInteger.valueOf(subscriptionId));
|
|
|
|
|
upcSub.remove(UInteger.valueOf(subscriptionId));
|
|
|
|
|
return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
|
|
|
|
|
}catch(ImppBusiException busExcep){
|
|
|
|
|
return ResultBean.fail(busExcep);
|
|
|
|
|
}catch(Exception e){
|
|
|
|
|
return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/remove-all-opc-sub")
|
|
|
|
|
@ApiOperation(value = "移除Opc订阅", notes = "移除Opc订阅")
|
|
|
|
|
@AnnoIgnoreLog
|
|
|
|
|
public ResultBean removeAllSubOpc(OpcUAParam opcUA){
|
|
|
|
|
try {
|
|
|
|
|
OpcUAService opcService = getOpcUaService(opcUA);
|
|
|
|
|
for (UInteger uInteger : upcSub.keySet()) {
|
|
|
|
|
opcService.deleteSubscription(opcUA,uInteger);
|
|
|
|
|
}
|
|
|
|
|
upcSub.clear();
|
|
|
|
|
return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
|
|
|
|
|
}catch(ImppBusiException busExcep){
|
|
|
|
|
return ResultBean.fail(busExcep);
|
|
|
|
|
}catch(Exception e){
|
|
|
|
|
return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/show-opc-sub-list")
|
|
|
|
|
@ApiOperation(value = "查询Opc订阅", notes = "查询Opc订阅")
|
|
|
|
|
@AnnoIgnoreLog
|
|
|
|
|
public ResultBean showSubOpcList(){
|
|
|
|
|
try {
|
|
|
|
|
HashMap resultMap = new HashMap<>();
|
|
|
|
|
for (UInteger uInteger : upcSub.keySet()) {
|
|
|
|
|
resultMap.put(uInteger.toString(), JSON.toJSONString(upcSub.get(uInteger)));
|
|
|
|
|
}
|
|
|
|
|
return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultMap(resultMap);
|
|
|
|
|
}catch(ImppBusiException busExcep){
|
|
|
|
|
return ResultBean.fail(busExcep);
|
|
|
|
|
}catch(Exception e){
|
|
|
|
|
return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/test-callback")
|
|
|
|
|
@ApiOperation(value = "回调测试", notes = "回调测试")
|
|
|
|
|
@AnnoIgnoreLog
|
|
|
|
|
public ResultBean showSubOpcList(String key,String value){
|
|
|
|
|
try {
|
|
|
|
|
LOGGER.info("回调:{} : {}", key,value);
|
|
|
|
|
return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultObject(key+":"+value);
|
|
|
|
|
}catch(ImppBusiException busExcep){
|
|
|
|
|
return ResultBean.fail(busExcep);
|
|
|
|
|
}catch(Exception e){
|
|
|
|
|
return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|