From 4b8ec78caa1246d014c6dc8dd6b35be6205ea43b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=B1=9F=E7=8E=AE?= Date: Mon, 27 Mar 2023 18:42:54 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BB=E5=8F=96=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nl/acs/auto/run/AbstractAutoRunnable.java | 105 ++++++++++ .../org/nl/acs/auto/run/ThreadStatusEnum.java | 22 +++ .../nl/acs/auto/run/ThreadUsedStatusEnum.java | 23 +++ .../nl/acs/opc/DeviceOpcProtocolRunable.java | 55 +++++- .../acs/opc/DeviceOpcSynchronizeAutoRun.java | 17 +- .../main/java/org/nl/acs/udw/UdwConfig.java | 10 + .../main/java/org/nl/acs/udw/UnifiedData.java | 25 +++ .../org/nl/acs/udw/UnifiedDataAccessor.java | 17 ++ .../acs/udw/UnifiedDataAccessorFactory.java | 20 ++ .../org/nl/acs/udw/UnifiedDataAppService.java | 47 +++++ .../main/java/org/nl/acs/udw/dto/UdwDto.java | 16 ++ .../nl/acs/udw/rest/UdwManagerController.java | 37 ++++ .../nl/acs/udw/service/UdwManageService.java | 28 +++ .../service/impl/UdwManagerServiceImpl.java | 156 +++++++++++++++ .../service/impl/UnifiedDataAccessorImpl.java | 47 +++++ .../impl/UnifiedDataAppServiceImpl.java | 185 ++++++++++++++++++ .../acs/udw/service/impl/UnifiedDataUnit.java | 21 ++ .../common/exception/BadRequestException.java | 41 ++++ .../src/main/resources/logback-spring.xml | 2 +- 19 files changed, 855 insertions(+), 19 deletions(-) create mode 100644 hd/nladmin-system/src/main/java/org/nl/acs/auto/run/AbstractAutoRunnable.java create mode 100644 hd/nladmin-system/src/main/java/org/nl/acs/auto/run/ThreadStatusEnum.java create mode 100644 hd/nladmin-system/src/main/java/org/nl/acs/auto/run/ThreadUsedStatusEnum.java create mode 100644 hd/nladmin-system/src/main/java/org/nl/acs/udw/UdwConfig.java create mode 100644 hd/nladmin-system/src/main/java/org/nl/acs/udw/UnifiedData.java create mode 100644 hd/nladmin-system/src/main/java/org/nl/acs/udw/UnifiedDataAccessor.java create mode 100644 hd/nladmin-system/src/main/java/org/nl/acs/udw/UnifiedDataAccessorFactory.java create mode 100644 hd/nladmin-system/src/main/java/org/nl/acs/udw/UnifiedDataAppService.java create mode 100644 hd/nladmin-system/src/main/java/org/nl/acs/udw/dto/UdwDto.java create mode 100644 hd/nladmin-system/src/main/java/org/nl/acs/udw/rest/UdwManagerController.java create mode 100644 hd/nladmin-system/src/main/java/org/nl/acs/udw/service/UdwManageService.java create mode 100644 hd/nladmin-system/src/main/java/org/nl/acs/udw/service/impl/UdwManagerServiceImpl.java create mode 100644 hd/nladmin-system/src/main/java/org/nl/acs/udw/service/impl/UnifiedDataAccessorImpl.java create mode 100644 hd/nladmin-system/src/main/java/org/nl/acs/udw/service/impl/UnifiedDataAppServiceImpl.java create mode 100644 hd/nladmin-system/src/main/java/org/nl/acs/udw/service/impl/UnifiedDataUnit.java create mode 100644 hd/nladmin-system/src/main/java/org/nl/modules/common/exception/BadRequestException.java diff --git a/hd/nladmin-system/src/main/java/org/nl/acs/auto/run/AbstractAutoRunnable.java b/hd/nladmin-system/src/main/java/org/nl/acs/auto/run/AbstractAutoRunnable.java new file mode 100644 index 0000000..f1bc363 --- /dev/null +++ b/hd/nladmin-system/src/main/java/org/nl/acs/auto/run/AbstractAutoRunnable.java @@ -0,0 +1,105 @@ +package org.nl.acs.auto.run; + +import lombok.extern.slf4j.Slf4j; + +import java.util.Date; + +@Slf4j +public abstract class AbstractAutoRunnable implements Runnable { + private ThreadStatusEnum status; + private Date startTime; + private Date stopTime; + private String stopMessage; + private ThreadUsedStatusEnum usedStatus; + @Override + public void run() { + this.setStatus(ThreadStatusEnum.run); + this.setStartTime(new Date()); + this.setStopMessage(""); + String true_clear = "执行完毕"; + + try { + this.before(); + //子类该方法是个死循环 + this.autoRun(); + this.setStopMessage(true_clear); + } catch (Throwable arg5) { + log.warn("", arg5); + System.out.println(arg5.getMessage()); + // this.setStopMessage(ExceptionUtlEx.getSimpleTrace(arg5)); + } finally { + this.setStopTime(new Date()); + this.setStatus(ThreadStatusEnum.stop); + this.after(); + } + + } + + public void before() throws Exception { + } + + public void after() { + } + + public void stop() { + this.after(); + } + + public Boolean getForbidStop() { + return Boolean.valueOf(false); + } + + public Boolean getForbidDisable() { + return Boolean.valueOf(false); + } + + public abstract String getCode(); + + public abstract String getName(); + + public String getDescription() { + return this.getName(); + } + + public abstract void autoRun() throws Exception; + + public ThreadStatusEnum getStatus() { + return this.status; + } + + public void setStatus(ThreadStatusEnum status) { + this.status = status; + } + + public Date getStartTime() { + return this.startTime; + } + + public void setStartTime(Date startTime) { + this.startTime = startTime; + } + + public Date getStopTime() { + return this.stopTime; + } + + public void setStopTime(Date stopTime) { + this.stopTime = stopTime; + } + + public String getStopMessage() { + return this.stopMessage; + } + + public void setStopMessage(String stopMessage) { + this.stopMessage = stopMessage; + } + + public ThreadUsedStatusEnum getUsedStatus() { + return this.usedStatus; + } + + public void setUsedStatus(ThreadUsedStatusEnum usedStatus) { + this.usedStatus = usedStatus; + } +} diff --git a/hd/nladmin-system/src/main/java/org/nl/acs/auto/run/ThreadStatusEnum.java b/hd/nladmin-system/src/main/java/org/nl/acs/auto/run/ThreadStatusEnum.java new file mode 100644 index 0000000..12652a6 --- /dev/null +++ b/hd/nladmin-system/src/main/java/org/nl/acs/auto/run/ThreadStatusEnum.java @@ -0,0 +1,22 @@ +package org.nl.acs.auto.run; + +public enum ThreadStatusEnum { + create("创建", 1), run("运行", 2), stop("停止", 3); + + private String description; + private int order; + + private ThreadStatusEnum(String name, int order) { + this.description = name; + this.order = order; + } + + public String description() { + return this.description; + } + + public int getOrder() { + return this.order; + } + +} diff --git a/hd/nladmin-system/src/main/java/org/nl/acs/auto/run/ThreadUsedStatusEnum.java b/hd/nladmin-system/src/main/java/org/nl/acs/auto/run/ThreadUsedStatusEnum.java new file mode 100644 index 0000000..deda8a1 --- /dev/null +++ b/hd/nladmin-system/src/main/java/org/nl/acs/auto/run/ThreadUsedStatusEnum.java @@ -0,0 +1,23 @@ +package org.nl.acs.auto.run; + +public enum ThreadUsedStatusEnum { + + used("使用", 1), unUsed("未使用", 2); + + private String description; + private int order; + + private ThreadUsedStatusEnum(String name, int order) { + this.description = name; + this.order = order; + } + + public String description() { + return this.description; + } + + public int getOrder() { + return this.order; + } + +} diff --git a/hd/nladmin-system/src/main/java/org/nl/acs/opc/DeviceOpcProtocolRunable.java b/hd/nladmin-system/src/main/java/org/nl/acs/opc/DeviceOpcProtocolRunable.java index dfb3cd3..975510f 100644 --- a/hd/nladmin-system/src/main/java/org/nl/acs/opc/DeviceOpcProtocolRunable.java +++ b/hd/nladmin-system/src/main/java/org/nl/acs/opc/DeviceOpcProtocolRunable.java @@ -4,8 +4,8 @@ import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import lombok.extern.slf4j.Slf4j; -import org.nl.modules.udw.UnifiedDataAccessor; -import org.nl.modules.udw.UnifiedDataAccessorFactory; +import org.nl.acs.udw.UnifiedDataAccessor; +import org.nl.acs.udw.UnifiedDataAccessorFactory; import org.openscada.opc.lib.da.Group; import org.openscada.opc.lib.da.Item; import org.openscada.opc.lib.da.ItemState; @@ -19,10 +19,12 @@ public class DeviceOpcProtocolRunable implements Runnable { OpcServerManageDto OpcServer; int error_num; String message; + private Server server; public DeviceOpcProtocolRunable() { this.error_num = 0; this.message = null; + this.server = null; } public List getProtocols() { @@ -60,8 +62,12 @@ public class DeviceOpcProtocolRunable implements Runnable { public void run() { while (true) { try { - Server server = OpcServerUtl.getServerWithOutException(this.OpcServer.getOpc_host(), this.OpcServer.getCls_id(), this.OpcServer.getUser(), this.OpcServer.getPassword(), this.OpcServer.getDomain()); - Group group = server.addGroup(); + this.server = OpcServerUtl.getServerWithOutException(this.OpcServer.getOpc_host(), this.OpcServer.getCls_id(), this.OpcServer.getUser(), this.OpcServer.getPassword(), this.OpcServer.getDomain()); +// Group group = server.addGroup(this.OpcServer.getOpc_host()); + Group group = server.addGroup(this.OpcServer.getOpc_code()); + if(ObjectUtil.isEmpty(group)){ + log.info("group is null "); + } List itemsString = new ArrayList(); Iterator it = this.protocols.iterator(); @@ -110,14 +116,22 @@ public class DeviceOpcProtocolRunable implements Runnable { label97: while (true) { long begin = System.currentTimeMillis(); - Map itemStatus = group.read(true, (Item[]) itemsMap.values().toArray(new Item[0])); + Map itemStatus = null; + try { + itemStatus = group.read(true, (Item[]) itemsMap.values().toArray(new Item[0])); + } catch (Exception e){ + System.out.println("数据同步异常:"+ this.getOpcServer().getOpc_code()); + log.trace("数据同步异常:{}", this.getOpcServer().getOpc_code()); + //e.printStackTrace(); + } long end = System.currentTimeMillis(); log.trace("{} 开始记时{}", tag, DateUtil.now()); long duration = end - begin; log.trace("{} 读取耗时:{}", tag, duration); + System.out.println("线程:"+tag + " 读取耗时:"+ duration); if (duration > 1000L) { if (!time_out) { - log.warn("{} 读取超时 : {}", tag, duration); + log.warn(" {} 读取超时 : {}", tag, duration); } time_out = true; @@ -125,6 +139,12 @@ public class DeviceOpcProtocolRunable implements Runnable { time_out = false; } + if(ObjectUtil.isEmpty(itemStatus)) { + System.out.println( tag + " :itemStatus is null"); + log.warn(" {} 读取异常 : {} itemStatus is null", tag); + } + + Set items = itemStatus.keySet(); Iterator var18 = items.iterator(); @@ -152,12 +172,13 @@ public class DeviceOpcProtocolRunable implements Runnable { value = OpcUtl.getValue(item, itemState); his = accessor_value.getValue(item.getId()); if (!ObjectUtil.equal(itemState.getQuality(), QualityTypeValue.OPC_QUALITY_GOOD) && his != null) { - log.warn("opc 值不健康 item: {}, 状态: {}", item.getId(), itemState.getQuality()); + log.warn("opc 值不健康 item: {}, 状态: {},当前读取值:{}, 系统内存值{} ", item.getId(), itemState.getQuality(), value, his); } } while (ObjectUtil.equal(value, his));//如果两次的值相等,不走下面的代码 OpcItemDto itemDto = this.getItem(item.getId()); - if (itemDto.getNeed_log() != null && itemDto.getNeed_log()) { + //默认记录日志 + if (true) { StringBuilder sb = new StringBuilder(); //设备的ITEM项 List relate_items = itemDto.getRelate_items(); @@ -169,16 +190,28 @@ public class DeviceOpcProtocolRunable implements Runnable { sb.append("key:" + relate + "value:" + obj + ";"); } - log.info("信号{}变更从{}->{};信号快照:{}", new Object[]{item.getId(), his, value, sb}); + log.warn("信号{}变更从{}->{};信号快照:{}", new Object[]{item.getId(), his, value, sb}); } +// accessor_value.setValueWithPersistence(item.getId(),accessor_value.getValue(item.getId())); +// accessor_value.getHistoryUnifiedData(item.getId()); //设置值 accessor_value.setValue(item.getId(), value); } } } catch (Exception var30) { - String error_message = "设备信息同步异常"; + if (this.server != null) { + try { + this.server.disconnect(); + } catch (Exception var25) { + log.warn("{} : server disconnect", var25); + } + } + + this.server = null; + + String error_message = "设备信息同步异常,"+var30; if (!StrUtil.equals(this.message, error_message)) { log.warn("", var30); } @@ -187,6 +220,8 @@ public class DeviceOpcProtocolRunable implements Runnable { Thread.sleep((long) (OpcConfig.synchronized_exception_wait_second * 1000)); } catch (InterruptedException e) { e.printStackTrace(); + log.warn("OPC 读取线程读取异常{} :", e); +// Thread.currentThread().interrupt();不会真正停止线程 } ++this.error_num; if (this.error_num > 3 && !StrUtil.equals(this.message, error_message)) { diff --git a/hd/nladmin-system/src/main/java/org/nl/acs/opc/DeviceOpcSynchronizeAutoRun.java b/hd/nladmin-system/src/main/java/org/nl/acs/opc/DeviceOpcSynchronizeAutoRun.java index c703a1c..16c6c65 100644 --- a/hd/nladmin-system/src/main/java/org/nl/acs/opc/DeviceOpcSynchronizeAutoRun.java +++ b/hd/nladmin-system/src/main/java/org/nl/acs/opc/DeviceOpcSynchronizeAutoRun.java @@ -1,7 +1,7 @@ package org.nl.acs.opc; import cn.hutool.core.util.ObjectUtil; -import org.nl.start.auto.run.AbstractAutoRunnable; +import org.nl.acs.auto.run.AbstractAutoRunnable; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -17,6 +17,8 @@ import java.util.concurrent.Executors; */ @Component public class DeviceOpcSynchronizeAutoRun extends AbstractAutoRunnable { + + public static boolean isRun = false; ExecutorService executorService = Executors.newCachedThreadPool(); @Autowired private DeviceAppService deviceAppService; @@ -36,24 +38,22 @@ public class DeviceOpcSynchronizeAutoRun extends AbstractAutoRunnable { @Override public void autoRun() throws Exception { { + isRun = true; Map servers = this.opcServerManageService.queryAllServerMap(); Map>> pros; - do{ - Thread.sleep(1000L); - pros = this.deviceAppService.findAllFormatProtocolFromDriver(); - }while (ObjectUtil.isEmpty(pros)); + do{ + Thread.sleep(1000L); + pros = this.deviceAppService.findAllFormatProtocolFromDriver(); + }while (ObjectUtil.isEmpty(pros)); Set keys = pros.keySet(); Iterator var4 = keys.iterator(); - System.out.println("test:" + var4.hasNext()); //代码执行一次 while (var4.hasNext()) { String key = (String) var4.next(); List> list = (List) pros.get(key); OpcServerManageDto opcServer = (OpcServerManageDto) servers.get(key); Iterator var8 = list.iterator(); - System.out.println("test2:" + var8.hasNext()); - while (var8.hasNext()) { List groupProtols = (List) var8.next(); DeviceOpcProtocolRunable runable = new DeviceOpcProtocolRunable(); @@ -75,6 +75,7 @@ public class DeviceOpcSynchronizeAutoRun extends AbstractAutoRunnable { @Override public void after() { + isRun = false; this.executorService.shutdownNow(); this.executorService = Executors.newCachedThreadPool(); } diff --git a/hd/nladmin-system/src/main/java/org/nl/acs/udw/UdwConfig.java b/hd/nladmin-system/src/main/java/org/nl/acs/udw/UdwConfig.java new file mode 100644 index 0000000..8b03a9a --- /dev/null +++ b/hd/nladmin-system/src/main/java/org/nl/acs/udw/UdwConfig.java @@ -0,0 +1,10 @@ +package org.nl.acs.udw; + +public class UdwConfig { + + //历史记录最大数量 + public static int max_history_length = 10; + + public UdwConfig() { + } +} diff --git a/hd/nladmin-system/src/main/java/org/nl/acs/udw/UnifiedData.java b/hd/nladmin-system/src/main/java/org/nl/acs/udw/UnifiedData.java new file mode 100644 index 0000000..0ebbb7c --- /dev/null +++ b/hd/nladmin-system/src/main/java/org/nl/acs/udw/UnifiedData.java @@ -0,0 +1,25 @@ +package org.nl.acs.udw; + +import lombok.Data; + +import java.util.Date; + +@Data +public class UnifiedData { + private Object value; + private Date last_modify_date; + + public UnifiedData() { + this.last_modify_date = new Date(); + } + + public UnifiedData(Object value) { + this.value = value; + this.last_modify_date = new Date(); + } + + public void changeValue(Object value) { + this.value = value; + this.last_modify_date = new Date(); + } +} diff --git a/hd/nladmin-system/src/main/java/org/nl/acs/udw/UnifiedDataAccessor.java b/hd/nladmin-system/src/main/java/org/nl/acs/udw/UnifiedDataAccessor.java new file mode 100644 index 0000000..559373f --- /dev/null +++ b/hd/nladmin-system/src/main/java/org/nl/acs/udw/UnifiedDataAccessor.java @@ -0,0 +1,17 @@ +package org.nl.acs.udw; + +import java.util.List; + +public interface UnifiedDataAccessor { + List getAllKey(); + + Object getValue(String key); + + void setValue(String key, Object value); + + UnifiedData getUnifiedData(String key); + + List getHistoryUnifiedData(String key); + + void setValueWithPersistence(String key, Object value); +} diff --git a/hd/nladmin-system/src/main/java/org/nl/acs/udw/UnifiedDataAccessorFactory.java b/hd/nladmin-system/src/main/java/org/nl/acs/udw/UnifiedDataAccessorFactory.java new file mode 100644 index 0000000..1626862 --- /dev/null +++ b/hd/nladmin-system/src/main/java/org/nl/acs/udw/UnifiedDataAccessorFactory.java @@ -0,0 +1,20 @@ +package org.nl.acs.udw; + +import org.nl.acs.udw.service.impl.UnifiedDataAccessorImpl; +import org.nl.acs.udw.service.impl.UnifiedDataAppServiceImpl; + +public class UnifiedDataAccessorFactory { + public UnifiedDataAccessorFactory() { + } + + public static UnifiedDataAppService getUnifiedDataAppService() { + return UnifiedDataAppServiceImpl.getInstance(); + } + + public static UnifiedDataAccessor getAccessor(String unified_key) { + UnifiedDataAccessorImpl accessor = new UnifiedDataAccessorImpl(); + accessor.setUnifiedKey(unified_key); + accessor.setUnifiedDataService(getUnifiedDataAppService()); + return accessor; + } +} diff --git a/hd/nladmin-system/src/main/java/org/nl/acs/udw/UnifiedDataAppService.java b/hd/nladmin-system/src/main/java/org/nl/acs/udw/UnifiedDataAppService.java new file mode 100644 index 0000000..7c3e429 --- /dev/null +++ b/hd/nladmin-system/src/main/java/org/nl/acs/udw/UnifiedDataAppService.java @@ -0,0 +1,47 @@ +package org.nl.acs.udw; + +import cn.hutool.core.util.ObjectUtil; +import org.nl.acs.udw.service.impl.UnifiedDataUnit; + +import java.util.List; + +public interface UnifiedDataAppService { + /** + * 获取所有的key + * + * @return + */ + List getAllUnifiedKey(); + + /** + * 根据key获取数据单元 + * + * @param key + * @return + */ + UnifiedDataUnit getUnifiedDataUnit(String key); + + UnifiedData getUnifiedData(String var1, String var2); + + Object getValue(String var1, String var2); + + void setValue(String var1, String var2, Object var3); + + void setValueNoLog(String var1, String var2, Object var3); + + List getHistoryUnifiedData(String var1, String var2); + + List getAllDataKey(String var1); + + void removeValue(String var1, String var2); + + void setValueWithPersistenceNoLog(String var1, String var2, Object var3); + + void setValueWithPersistence(String var1, String var2, Object var3); + + void removeValueWithPersistence(String var1, String var2); + + static boolean isEquals(Object a, Object b) { + return ObjectUtil.equal(a, b); + } +} diff --git a/hd/nladmin-system/src/main/java/org/nl/acs/udw/dto/UdwDto.java b/hd/nladmin-system/src/main/java/org/nl/acs/udw/dto/UdwDto.java new file mode 100644 index 0000000..0a7e198 --- /dev/null +++ b/hd/nladmin-system/src/main/java/org/nl/acs/udw/dto/UdwDto.java @@ -0,0 +1,16 @@ +package org.nl.acs.udw.dto; + +import lombok.Data; + +import java.util.Date; + +/** + * 统一数据源管理 + */ +@Data +public class UdwDto { + private String unified_key; + private String key; + private Object value; + private Date last_modify_date; +} diff --git a/hd/nladmin-system/src/main/java/org/nl/acs/udw/rest/UdwManagerController.java b/hd/nladmin-system/src/main/java/org/nl/acs/udw/rest/UdwManagerController.java new file mode 100644 index 0000000..ac6e2d0 --- /dev/null +++ b/hd/nladmin-system/src/main/java/org/nl/acs/udw/rest/UdwManagerController.java @@ -0,0 +1,37 @@ + +package org.nl.acs.udw.rest; + + +import io.swagger.annotations.Api; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.nl.acs.udw.service.UdwManageService; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +@Api(tags = "内存点位管理") +@RequestMapping("/api/udw") +@Slf4j +public class UdwManagerController { + + private final UdwManageService udwManageService; + +// @GetMapping +// @Log("查询内存点位") +// @ApiOperation("查询内存点位") +// @SaIgnore +// public ResponseEntity query(@RequestParam JSONObject whereJson) { +// return new ResponseEntity<>(udwManageService.queryByConditions(whereJson), HttpStatus.OK); +// } + +// @GetMapping +// @Log("查询内存点位") +// @ApiOperation("查询内存点位") +// @SaIgnore +// //@PreAuthorize("@el.check('device:list')") +// public ResponseEntity query(@RequestParam Map whereJson, Pageable page) { +// return new ResponseEntity<>(udwManageService.queryAll(whereJson, page), HttpStatus.OK); +// } +} diff --git a/hd/nladmin-system/src/main/java/org/nl/acs/udw/service/UdwManageService.java b/hd/nladmin-system/src/main/java/org/nl/acs/udw/service/UdwManageService.java new file mode 100644 index 0000000..6d3ef62 --- /dev/null +++ b/hd/nladmin-system/src/main/java/org/nl/acs/udw/service/UdwManageService.java @@ -0,0 +1,28 @@ +package org.nl.acs.udw.service; + +import com.alibaba.fastjson.JSONObject; +import org.nl.acs.udw.dto.UdwDto; +import org.springframework.data.domain.Pageable; + +import java.util.List; +import java.util.Map; + +public interface UdwManageService { + /** + * 根据条件查询 + * + * @param where + * @return + */ + List queryByConditions(JSONObject where); + + /** + * 查询数据分页 + * + * @param whereJson 条件 + * @param page 分页参数 + * @return Map + */ + Map queryAll(Map whereJson, Pageable page); + +} diff --git a/hd/nladmin-system/src/main/java/org/nl/acs/udw/service/impl/UdwManagerServiceImpl.java b/hd/nladmin-system/src/main/java/org/nl/acs/udw/service/impl/UdwManagerServiceImpl.java new file mode 100644 index 0000000..6c9b7af --- /dev/null +++ b/hd/nladmin-system/src/main/java/org/nl/acs/udw/service/impl/UdwManagerServiceImpl.java @@ -0,0 +1,156 @@ +package org.nl.acs.udw.service.impl; + +import cn.hutool.core.util.StrUtil; +import com.alibaba.fastjson.JSONObject; +import org.nl.acs.udw.UnifiedData; +import org.nl.acs.udw.UnifiedDataAccessorFactory; +import org.nl.acs.udw.dto.UdwDto; +import org.nl.acs.udw.service.UdwManageService; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +@Service +public class UdwManagerServiceImpl implements UdwManageService { + + public UdwManagerServiceImpl() { + } + + @Override + public List queryByConditions(JSONObject where) { + String unified_key = null; + String keys = null; + + UnifiedDataUnit unifiedDataUnit = UnifiedDataAccessorFactory.getUnifiedDataAppService().getUnifiedDataUnit(unified_key); + if (unifiedDataUnit == null) { + return null; + } else { + String key; + UdwDto udwDto; + Map storage; + ArrayList udwDtos; + Iterator var14; + if (keys != null) { + storage = unifiedDataUnit.getStorage(); + udwDtos = new ArrayList(); + var14 = storage.keySet().iterator(); + + while(var14.hasNext()) { + key = (String)var14.next(); + if (key.indexOf(keys) != -1) { + udwDto = new UdwDto(); + udwDto.setUnified_key(unified_key); + udwDto.setKey(key); + udwDto.setValue(((UnifiedData)storage.get(key)).getValue()); + udwDtos.add(udwDto); + } + } + + return udwDtos; + } else { + storage = unifiedDataUnit.getStorage(); + udwDtos = new ArrayList(); + var14 = storage.keySet().iterator(); + + while(var14.hasNext()) { + key = (String)var14.next(); + udwDto = new UdwDto(); + udwDto.setUnified_key(unified_key); + udwDto.setKey(key); + udwDto.setValue(((UnifiedData)storage.get(key)).getValue()); + udwDtos.add(udwDto); + } + + return udwDtos; + } + } + } + + @Override + public Map queryAll(Map whereJson, Pageable page) { + + String unified_key = (String) whereJson.get("unified_key"); + String keys = (String) whereJson.get("code"); + if(StrUtil.isEmpty(unified_key)) + { + unified_key = "opc_value"; + } +// String unified_key = (String) whereJson.get("unified_key"); +// String code = (String) whereJson.get("code"); + +// unified_key = whereJson.get("key").toString(); +// keys = whereJson.get("value").toString(); + + + //[[{"column":"unified_key","value":"cached","compareType":"equals","columnType":"object"}]] + UnifiedDataUnit unifiedDataUnit = UnifiedDataAccessorFactory.getUnifiedDataAppService().getUnifiedDataUnit(unified_key); + if (unifiedDataUnit == null) { + return null; + } else { + String key; + UdwDto udwDto; + Map storage; + ArrayList udwDtos; + Iterator var14; + if (keys != null) { + storage = unifiedDataUnit.getStorage(); + udwDtos = new ArrayList(); + var14 = storage.keySet().iterator(); + + while(var14.hasNext()) { + key = (String)var14.next(); + if (key.indexOf(keys) != -1) { + udwDto = new UdwDto(); + udwDto.setUnified_key(unified_key); + udwDto.setKey(key); + udwDto.setValue(((UnifiedData)storage.get(key)).getValue()); + udwDtos.add(udwDto); + } + } + + Integer currentPageNumber = page.getPageNumber() + 1; + Integer pageMaxSize = page.getPageSize(); + + List orderbyDtoList = (List) udwDtos.stream().skip((currentPageNumber - 1) * pageMaxSize) + .limit(pageMaxSize) + .collect(Collectors.toList()); + + JSONObject jo = new JSONObject(); + jo.put("content", orderbyDtoList); + jo.put("totalElements", udwDtos.size()); + + return jo; + } else { + storage = unifiedDataUnit.getStorage(); + udwDtos = new ArrayList(); + var14 = storage.keySet().iterator(); + + while(var14.hasNext()) { + key = (String)var14.next(); + udwDto = new UdwDto(); + udwDto.setUnified_key(unified_key); + udwDto.setKey(key); + udwDto.setValue(((UnifiedData)storage.get(key)).getValue()); + udwDtos.add(udwDto); + } + Integer currentPageNumber = page.getPageNumber() + 1; + Integer pageMaxSize = page.getPageSize(); + + List orderbyDtoList = (List) udwDtos.stream().skip((currentPageNumber - 1) * pageMaxSize) + .limit(pageMaxSize) + .collect(Collectors.toList()); + + JSONObject jo = new JSONObject(); + jo.put("content", orderbyDtoList); + jo.put("totalElements", udwDtos.size()); + + return jo; + } + } + } +} diff --git a/hd/nladmin-system/src/main/java/org/nl/acs/udw/service/impl/UnifiedDataAccessorImpl.java b/hd/nladmin-system/src/main/java/org/nl/acs/udw/service/impl/UnifiedDataAccessorImpl.java new file mode 100644 index 0000000..995ab9a --- /dev/null +++ b/hd/nladmin-system/src/main/java/org/nl/acs/udw/service/impl/UnifiedDataAccessorImpl.java @@ -0,0 +1,47 @@ +package org.nl.acs.udw.service.impl; + +import org.nl.acs.udw.UnifiedData; +import org.nl.acs.udw.UnifiedDataAccessor; +import org.nl.acs.udw.UnifiedDataAppService; + +import java.util.List; + +public class UnifiedDataAccessorImpl implements UnifiedDataAccessor { + private String unified_key; + private UnifiedDataAppService unifiedDataAppService; + + public UnifiedDataAccessorImpl() { + } + + public void setUnifiedKey(String unified_key) { + this.unified_key = unified_key; + } + + public void setUnifiedDataService(UnifiedDataAppService unifiedDataService) { + this.unifiedDataAppService = unifiedDataService; + } + + public List getAllKey() { + return this.unifiedDataAppService.getAllDataKey(this.unified_key); + } + + public Object getValue(String key) { + return this.unifiedDataAppService.getValue(this.unified_key, key); + } + + public void setValue(String key, Object value) { + this.unifiedDataAppService.setValue(this.unified_key, key, value); + } + + public void setValueWithPersistence(String key, Object value) { + this.unifiedDataAppService.setValueWithPersistence(this.unified_key, key, value); + } + + public UnifiedData getUnifiedData(String key) { + return this.unifiedDataAppService.getUnifiedData(this.unified_key, key); + } + + public List getHistoryUnifiedData(String key) { + return this.unifiedDataAppService.getHistoryUnifiedData(this.unified_key, key); + } +} diff --git a/hd/nladmin-system/src/main/java/org/nl/acs/udw/service/impl/UnifiedDataAppServiceImpl.java b/hd/nladmin-system/src/main/java/org/nl/acs/udw/service/impl/UnifiedDataAppServiceImpl.java new file mode 100644 index 0000000..7c6bbe0 --- /dev/null +++ b/hd/nladmin-system/src/main/java/org/nl/acs/udw/service/impl/UnifiedDataAppServiceImpl.java @@ -0,0 +1,185 @@ +package org.nl.acs.udw.service.impl; + +import lombok.extern.slf4j.Slf4j; +import org.nl.acs.udw.UdwConfig; +import org.nl.acs.udw.UnifiedData; +import org.nl.acs.udw.UnifiedDataAppService; +import org.nl.modules.common.exception.BadRequestException; + +import java.util.*; + +@Slf4j +public class UnifiedDataAppServiceImpl implements UnifiedDataAppService { + public static UnifiedDataAppService unifiedDataAppService; + private Map factory = Collections.synchronizedMap(new HashMap()); + + private UnifiedDataAppServiceImpl() { + } + + public static UnifiedDataAppService getInstance() { + if (unifiedDataAppService == null) { + Class var0 = UnifiedDataAppServiceImpl.class; + synchronized (UnifiedDataAppServiceImpl.class) { + if (unifiedDataAppService == null) { + unifiedDataAppService = new UnifiedDataAppServiceImpl(); + } + } + } + return unifiedDataAppService; + } + + public List getAllUnifiedKey() { + return new ArrayList(this.factory.keySet()); + } + + public UnifiedDataUnit getUnifiedDataUnit(String unified_key) { + UnifiedDataUnit dataUnit = (UnifiedDataUnit) this.factory.get(unified_key); + return dataUnit == null ? null : dataUnit; + } + + public List getAllDataKey(String unified_key) { + UnifiedDataUnit dataUnit = (UnifiedDataUnit) this.factory.get(unified_key); + if (dataUnit == null) { + return new ArrayList(); + } else { + Map storage = dataUnit.getStorage(); + return new ArrayList(storage.keySet()); + } + } + + public UnifiedData getUnifiedData(String unified_key, String key) { + UnifiedDataUnit dataUnit = this.getUnifiedDataUnit(unified_key); + if (dataUnit == null) { + return null; + } else { + Map storage = dataUnit.getStorage(); + return (UnifiedData) storage.get(key); + } + } + + public Object getValue(String unified_key, String key) { + UnifiedData unifiedData = this.getUnifiedData(unified_key, key); + return unifiedData == null ? null : unifiedData.getValue(); + } + + public void removeValueWithPersistence(String unified_key, String key) { + UnifiedDataUnit dataUnit = this.getUnifiedDataUnit(unified_key); + if (dataUnit != null) { + Map storage = dataUnit.getStorage(); + if (storage.containsKey(key)) { + storage.remove(key); + } + + Map> history = dataUnit.getHistory(); + if (history.containsKey(key)) { + history.remove(key); + } + + /*PersistenceService persistenceService = PersistenceServiceFactory.getPersistenceService(); + persistenceService.deleteData(unified_key, key);*/ + } + } + + public void removeValue(String unified_key, String key) { + UnifiedDataUnit dataUnit = this.getUnifiedDataUnit(unified_key); + if (dataUnit != null) { + Map storage = dataUnit.getStorage(); + if (storage.containsKey(key)) { + storage.remove(key); + } + + Map> history = dataUnit.getHistory(); + if (history.containsKey(key)) { + history.remove(key); + } + + if (history.size() == 0) { + this.factory.remove(unified_key); + } + + } + } + + public void setValueNoLog(String unified_key, String key, Object value) { + this.setValue(unified_key, key, value, false, false); + } + + public void setValue(String unified_key, String key, Object value) { + this.setValue(unified_key, key, value, false, true); + } + + public void setValueWithPersistenceNoLog(String unified_key, String key, Object value) { + this.setValue(unified_key, key, value, true, false); + } + + public void setValueWithPersistence(String unified_key, String key, Object value) { + this.setValue(unified_key, key, value, true, true); + } + + public synchronized void setValue(String unified_key, String key, Object value, boolean save, boolean is_log) { + if (unified_key == null) { + throw new BadRequestException(""); + //throw new BusinessException(SystemMessage.cant_be_empty, new Object[]{"unified_key"}); + } else if (key == null) { + throw new BadRequestException(""); + //throw new BusinessException(SystemMessage.cant_be_empty, new Object[]{"key"}); + } else { + if (!this.factory.containsKey(unified_key)) { + this.factory.put(unified_key, new UnifiedDataUnit(unified_key)); + } + + UnifiedDataUnit dataUnit = (UnifiedDataUnit) this.factory.get(unified_key); + Map storage = dataUnit.getStorage(); + if (!storage.containsKey(key)) { + storage.put(key, new UnifiedData()); + } + + UnifiedData unifiedData = (UnifiedData) storage.get(key); + if (!UnifiedDataAppService.isEquals(unifiedData.getValue(), value)) { + Map> history = dataUnit.getHistory(); + List historyunifiedData = (List) history.get(key); + if (historyunifiedData == null) { + history.put(key, new ArrayList()); + } + + UnifiedData historydata = new UnifiedData(); + historydata.setLast_modify_date(unifiedData.getLast_modify_date()); + historydata.setValue(unifiedData.getValue()); + + while (((List) history.get(key)).size() > UdwConfig.max_history_length) { + ((List) history.get(key)).remove(UdwConfig.max_history_length); + } + + ((List) history.get(key)).add(0, historydata); + Object oldvalue = unifiedData.getValue(); + unifiedData.changeValue(value); + if (save) { + /*PersistenceService persistenceService = PersistenceServiceFactory.getPersistenceService(); + persistenceService.saveData(unified_key, key, StringUtl.getString(value)); + if (is_log) { + this.businessLogger.setResource(unified_key, unified_key); + this.businessLogger.setMaterial(key, key); + this.businessLogger.setContainer(StringUtl.getString(value)); + this.businessLogger.log("统一数据源中: unit: {}, key: {}, 值: {} 更改为 {}。", new Object[]{unified_key, key, oldvalue, value}); + }*/ + } + + if (is_log && key != null && !key.endsWith("heartbeat") && !key.endsWith("distancex") && !key.endsWith("distancey") && !key.endsWith("Xwz") && !key.endsWith("Ywz") && !key.endsWith("Zwz")) { + log.trace("统一数据源中: unit: {}, key: {}, 值: {} 更改为 {}。", new Object[]{unified_key, key, oldvalue, value}); + } + } + + } + } + + public List getHistoryUnifiedData(String unified_key, String key) { + UnifiedDataUnit dataUnit = this.getUnifiedDataUnit(unified_key); + if (dataUnit == null) { + return new ArrayList(); + } else { + Map> history = dataUnit.getHistory(); + List result = (List) history.get(key); + return (List) (result == null ? new ArrayList() : result); + } + } +} diff --git a/hd/nladmin-system/src/main/java/org/nl/acs/udw/service/impl/UnifiedDataUnit.java b/hd/nladmin-system/src/main/java/org/nl/acs/udw/service/impl/UnifiedDataUnit.java new file mode 100644 index 0000000..a5866c4 --- /dev/null +++ b/hd/nladmin-system/src/main/java/org/nl/acs/udw/service/impl/UnifiedDataUnit.java @@ -0,0 +1,21 @@ +package org.nl.acs.udw.service.impl; + +import lombok.Data; +import org.nl.acs.udw.UnifiedData; + +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Data +public class UnifiedDataUnit { + private String unifiedKey; + private Map storage = Collections.synchronizedMap(new HashMap()); + private Map> history = Collections.synchronizedMap(new HashMap()); + + public UnifiedDataUnit(String unifiedKey) { + this.unifiedKey = unifiedKey; + } + +} diff --git a/hd/nladmin-system/src/main/java/org/nl/modules/common/exception/BadRequestException.java b/hd/nladmin-system/src/main/java/org/nl/modules/common/exception/BadRequestException.java new file mode 100644 index 0000000..e683723 --- /dev/null +++ b/hd/nladmin-system/src/main/java/org/nl/modules/common/exception/BadRequestException.java @@ -0,0 +1,41 @@ +/* + * Copyright 2019-2020 Zheng Jie + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.nl.modules.common.exception; + +import lombok.Getter; +import org.springframework.http.HttpStatus; + +import static org.springframework.http.HttpStatus.BAD_REQUEST; + +/** + * @author Zheng Jie + * @date 2018-11-23 + * 统一异常处理 + */ +@Getter +public class BadRequestException extends RuntimeException{ + + private Integer status = BAD_REQUEST.value(); + + public BadRequestException(String msg){ + super(msg); + } + + public BadRequestException(HttpStatus status, String msg){ + super(msg); + this.status = status.value(); + } +} diff --git a/hd/nladmin-system/src/main/resources/logback-spring.xml b/hd/nladmin-system/src/main/resources/logback-spring.xml index 9bd5afe..f52b6e5 100644 --- a/hd/nladmin-system/src/main/resources/logback-spring.xml +++ b/hd/nladmin-system/src/main/resources/logback-spring.xml @@ -66,7 +66,7 @@ https://juejin.cn/post/6844903775631572999 - +