Es 工具封装

yun-zuoyi
wei.peng 5 years ago
parent 5dd3f6a38a
commit 6ffb1685d7

@ -1,176 +1,176 @@
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.UaSubscription;
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;
/**
* @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, (uaMonitoredItem, integer) -> {
uaMonitoredItem.setValueConsumer((node, value) -> {
LOGGER.info("OPC订阅回调 {} - {} , callback {}", node.getReadValueId().getNodeId(), 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 = "查询Opc订阅", notes = "查询Opc订阅")
@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);
}
}
}
//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.UaSubscription;
//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;
//
///**
// * @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, (uaMonitoredItem, integer) -> {
// uaMonitoredItem.setValueConsumer((node, value) -> {
// LOGGER.info("OPC订阅回调 {} - {} , callback {}", node.getReadValueId().getNodeId(), 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 = "查询Opc订阅", notes = "查询Opc订阅")
// @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);
// }
// }
//
//}

@ -29,6 +29,7 @@ import org.bson.Document;
import org.bson.conversions.Bson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.stereotype.Service;
@ -45,6 +46,7 @@ import java.util.Map;
* @Modify:
**/
@Service
@Primary
public class SysLogMongoService implements ISysLogService {
public static final Logger LOGGER = LoggerFactory.getLogger(SysLogMongoService.class);

@ -1,66 +1,64 @@
package test.cn.estsh.i3plus.core.apiservice.serviceimpl.busi;
import cn.estsh.i3plus.core.api.iservice.busi.ISysLogExceptionService;
import cn.estsh.i3plus.core.api.iservice.busi.ISysLogSystemService;
import cn.estsh.i3plus.core.apiservice.serviceimpl.busi.TestBase;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.platform.platbean.SysLogException;
import cn.estsh.i3plus.pojo.platform.platbean.SysLogSystem;
import com.alibaba.fastjson.JSON;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import javax.transaction.Transactional;
import java.util.List;
/**
* @Description :
* @Reference :
* @Author : alwaysfrin
* @CreateDate : 2019-04-09 14:38
* @Modify:
**/
public class TestMongoDb extends TestBase {
@Autowired
private ISysLogExceptionService sysLogExceptionService;
@Autowired
private ISysLogSystemService syslogSystemService;
/**
*
*/
@Test
@Transactional
public void testListSysConfig() {
Pager pager = new Pager();
pager.setStartRow(0);
pager.setPageSize(10);
SysLogException sysLogException = new SysLogException();
ListPager logExceptionList = sysLogExceptionService.querySysLogExceptionByPager(sysLogException, pager);
System.out.println(logExceptionList);
}
@Test
@Transactional
public void testListSysLogSystem() {
Pager pager = new Pager();
pager.setStartRow(0);
pager.setPageSize(10);
SysLogSystem logSystem = new SysLogSystem();
logSystem.setLogLevel(1);
logSystem.setLogModuleId(1);
System.out.println("2===============");
ListPager logSystemList = syslogSystemService.querySysLogSystemByPager(logSystem,pager);
System.out.println(logSystemList.getObjectList().size());
System.out.println("===============pager:" + pager);
}
}
//package test.cn.estsh.i3plus.core.apiservice.serviceimpl.busi;
//
//import cn.estsh.i3plus.core.api.iservice.busi.ISysLogExceptionService;
//import cn.estsh.i3plus.core.api.iservice.busi.ISysLogSystemService;
//import cn.estsh.i3plus.core.apiservice.serviceimpl.busi.TestBase;
//import cn.estsh.i3plus.pojo.base.bean.ListPager;
//import cn.estsh.i3plus.pojo.base.common.Pager;
//import cn.estsh.i3plus.pojo.platform.platbean.SysLogException;
//import cn.estsh.i3plus.pojo.platform.platbean.SysLogSystem;
//import org.junit.Test;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import javax.transaction.Transactional;
//
///**
// * @Description :
// * @Reference :
// * @Author : alwaysfrin
// * @CreateDate : 2019-04-09 14:38
// * @Modify:
// **/
//public class TestMongoDb extends TestBase {
//
//
// @Autowired
// private ISysLogExceptionService sysLogExceptionService;
//
// @Autowired
// private ISysLogSystemService syslogSystemService;
//
// /**
// * 测试 查询所有系统参数
// */
// @Test
// @Transactional
// public void testListSysConfig() {
// Pager pager = new Pager();
// pager.setStartRow(0);
// pager.setPageSize(10);
//
// SysLogException sysLogException = new SysLogException();
//
// ListPager logExceptionList = sysLogExceptionService.querySysLogExceptionByPager(sysLogException, pager);
// System.out.println(logExceptionList);
// }
//
//
// @Test
// @Transactional
// public void testListSysLogSystem() {
// Pager pager = new Pager();
// pager.setStartRow(0);
// pager.setPageSize(10);
//
// SysLogSystem logSystem = new SysLogSystem();
// logSystem.setLogLevel(1);
// logSystem.setLogModuleId(1);
//
// System.out.println("2===============");
// ListPager logSystemList = syslogSystemService.querySysLogSystemByPager(logSystem,pager);
// System.out.println(logSystemList.getObjectList().size());
// System.out.println("===============pager:" + pager);
// }
//}

Loading…
Cancel
Save