diff --git a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/device_driver/basedriver/agv/xg_agv/XgagvDeviceDriver.java b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/device_driver/basedriver/agv/xg_agv/XgagvDeviceDriver.java index aec69c3..725301d 100644 --- a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/device_driver/basedriver/agv/xg_agv/XgagvDeviceDriver.java +++ b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/device_driver/basedriver/agv/xg_agv/XgagvDeviceDriver.java @@ -177,7 +177,7 @@ public class XgagvDeviceDriver extends AbstractOpcDeviceDriver implements Device // .build(); // luceneExecuteLogService.deviceExecuteLog(logDto); luceneExecuteLogService.deviceExecuteLog( - new LuceneLogDto(this.getDevice().getOpc_server_code(),this.getDevice().getOpc_plc_code(),this.device_code,ItemProtocol.to_home_relocation,this.last_home_relocation,this.home_relocation)); + new LuceneLogDto(this.getDevice().getOpc_server_code(),this.getDevice().getOpc_plc_code(),this.device_code,ItemProtocol.to_home_relocation,String.valueOf(this.last_home_relocation),String.valueOf(this.home_relocation))); } if (stop != last_stop) { diff --git a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/device_driver/tianneng/baoshijian/BaoshijianSiteDeviceDriver.java b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/device_driver/tianneng/baoshijian/BaoshijianSiteDeviceDriver.java index f0adfd9..a6988e4 100644 --- a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/device_driver/tianneng/baoshijian/BaoshijianSiteDeviceDriver.java +++ b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/device_driver/tianneng/baoshijian/BaoshijianSiteDeviceDriver.java @@ -166,7 +166,6 @@ public class BaoshijianSiteDeviceDriver extends AbstractOpcDeviceDriver implemen String message = null; try { device_code = this.getDeviceCode(); - log.info("123:"+device_code); x0 = this.itemProtocol.getX0(); x1 = this.itemProtocol.getX1(); x2 = this.itemProtocol.getX2(); diff --git a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/DeviceAppService.java b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/DeviceAppService.java index b507a05..170860b 100644 --- a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/DeviceAppService.java +++ b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/DeviceAppService.java @@ -39,7 +39,7 @@ public interface DeviceAppService { * * @return */ - Map>> findAllFormatProtocolFromDriver(); + Map>> findAllFormatProtocolFromDriver(); Map>> findAllFormatProtocolFromDriverByPlc(); diff --git a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/DeviceAppServiceImpl.java b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/DeviceAppServiceImpl.java index 53ab65d..375a009 100644 --- a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/DeviceAppServiceImpl.java +++ b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/DeviceAppServiceImpl.java @@ -128,7 +128,7 @@ public class DeviceAppServiceImpl implements DeviceAppService, ApplicationAutoIn while (var21.hasNext()) { String string = (String) var21.next(); List device = (List) deviceMap.get(string); - if (temp.size() + device.size() > WcsConfig.opc_group_sync_max_item) { + if (temp.size() + device.size() > org.nl.acs.opc.WcsConfig.opc_group_sync_max_item) { if (temp.size() > 0) { serverResult.add(temp); temp = new ArrayList(); @@ -234,14 +234,13 @@ public class DeviceAppServiceImpl implements DeviceAppService, ApplicationAutoIn @Override public List findDeviceDriver(Class clazz) { - List list = new ArrayList<>(); - List devices = new ArrayList<>(this.findAllDevice()); // Create a copy of the collection - Iterator var3 = devices.iterator(); + List list = new ArrayList(); + Iterator var3 = this.findAllDevice().iterator(); while (var3.hasNext()) { - Device device = var3.next(); + Device device = (Device) var3.next(); DeviceDriver deviceDriver = device.getDeviceDriver(); - // 查找某驱动的所有子类 + //查找某驱动的所有子类 if (deviceDriver != null && clazz.isAssignableFrom(deviceDriver.getClass())) { list.add((T) device.getDeviceDriver()); } @@ -250,7 +249,6 @@ public class DeviceAppServiceImpl implements DeviceAppService, ApplicationAutoIn return list; } - @Override public Map findDeviceOptions(DeviceType deviceType) { List list = this.findDevice(deviceType); diff --git a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/DeviceManageService.java b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/DeviceManageService.java index a592f2a..275b53c 100644 --- a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/DeviceManageService.java +++ b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/DeviceManageService.java @@ -3,7 +3,7 @@ package org.nl.acs.opc; import java.util.List; public interface DeviceManageService { - List queryAllWithExtra(); + List queryAllWithExtra(); List queryAllWithExtra(String device_code); } diff --git a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/DeviceOpcProtocolRunable.java b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/DeviceOpcProtocolRunable.java index 62b48e3..2028cc8 100644 --- a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/DeviceOpcProtocolRunable.java +++ b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/DeviceOpcProtocolRunable.java @@ -1,24 +1,21 @@ package org.nl.acs.opc; -import cn.hutool.core.date.DateUtil; -import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; +import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; import org.nl.acs.opc.service.dto.OpcServerManageDto; import org.nl.acs.udw.UnifiedDataAccessor; import org.nl.acs.udw.UnifiedDataAccessorFactory; import org.nl.acs.udw.UnifiedDataAppService; +import org.nl.common.enums.LogTypeEnum; import org.nl.config.SpringContextHolder; +import org.nl.system.service.lucene.LuceneExecuteLogService; +import org.nl.system.service.lucene.dto.LuceneLogDto; import org.openscada.opc.lib.da.*; -import org.springframework.stereotype.Service; import java.util.*; -/** - * @author 20220102CG\noblelift - */ @Slf4j -@Service public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerConnectionStateListener { List protocols; OpcServerManageDto OpcServer; @@ -31,8 +28,7 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC private int all_null; private Map itemSearchCache; -// @Autowired -// OpcServerService opcServerService; + LuceneExecuteLogService lucene = SpringContextHolder.getBean(LuceneExecuteLogService.class); public DeviceOpcProtocolRunable() { @@ -59,12 +55,10 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC this.OpcServer = opcServer; } - private OpcItemDto getItem(String item) { OpcItemDto x = (OpcItemDto) this.itemSearchCache.get(item); if (x == null) { Iterator var3 = this.protocols.iterator(); - while (var3.hasNext()) { OpcItemDto dto = (OpcItemDto) var3.next(); if (StrUtil.equals(item, dto.getItem_code())) { @@ -74,11 +68,9 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC } } } - return x; } - @Override public void run() { if (OpcConfig.opc_item_read_using_callback) { @@ -88,8 +80,8 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC } } - private void runOld() { + OpcServerService opcServerService = SpringContextHolder.getBean(OpcServerService .class); while (true) { start: try { @@ -102,11 +94,11 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC server.disconnect(); log.trace("清理server..."); } - OpcServerService opcServerService = SpringContextHolder.getBean(OpcServerService.class); - group = opcServerService.getServer(this.getOpcServer().getOpc_code()); -// this.server = OpcServerUtl.getServerWithOutException(this.OpcServer.getOpc_host(), this.OpcServer.getCls_id(), this.OpcServer.getUser(), this.OpcServer.getPassword(), this.OpcServer.getDomain()); -// this.server.addStateListener(this); -// group = this.server.addGroup(); + +// group =opcServerService.getServer(this.getOpcServer().getOpc_code()); + this.server = OpcServerUtl.getServerWithOutException(this.OpcServer.getOpc_host(), this.OpcServer.getCls_id(), this.OpcServer.getUser(), this.OpcServer.getPassword(), this.OpcServer.getDomain()); + this.server.addStateListener(this); + group = this.server.addGroup(); List itemsString = new ArrayList(); Iterator var3 = this.protocols.iterator(); @@ -127,7 +119,7 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC try { Item item = group.addItem(string); itemsMap.put(string, item); - log.trace("添加成功 {}", string); +// log.trace("添加成功 {}", string); } catch (Exception var26) { err_message.append(string + ":" + var26.getMessage()); if (!is_error) { @@ -139,13 +131,14 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC String tag; if (is_error) { tag = err_message.toString(); - log.warn("{}:{}", OpcConfig.resource_code, tag); +// log.warn("{}:{}", OpcConfig.resource_code, tag); } if (!OpcStartTag.is_run) { OpcStartTag.is_run = true; } + //线程名 tag = ""; if (log.isWarnEnabled()) { tag = Thread.currentThread().getName(); @@ -153,26 +146,25 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC tag = tag + this.getOpcGroupID(); } } - UnifiedDataAccessor accessor_value = UnifiedDataAccessorFactory.getAccessor(OpcConfig.udw_opc_value_key); boolean time_out = false; + label97: while (DeviceOpcSynchronizeAutoRun.isRun) { long begin = System.currentTimeMillis(); if (log.isTraceEnabled()) { - log.trace("{} 开始记时{}", tag, DateUtil.now()); +// log.trace("{} 开始记时{}", tag, DateUtil.now()); } Map itemStatus = group.read(true, (Item[]) itemsMap.values().toArray(new Item[0])); long end = System.currentTimeMillis(); long duration = end - begin; if (log.isTraceEnabled()) { - log.trace("{} 读取耗时:{}", tag, duration); +// log.trace("{} 读取耗时:{}", tag, duration); } - if (duration > 1000L) { if (!time_out) { - log.warn("{} 读取超时 : {}", tag, duration); +// log.warn("{} 读取超时 : {}", tag, duration); } time_out = true; @@ -195,8 +187,7 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC String itemId = item.getId(); Object his = accessor_value.getValue(itemId); if (!ObjectUtl.isEquals(itemState.getQuality(), QualityTypeValue.OPC_QUALITY_GOOD) && his != null) { - log.warn("opc 值不健康 item: {}, 状态: {}", itemId, itemState.getQuality()); - valueAllNotNull = true; +// log.warn("opc 值不健康 item: {}, 状态: {}", itemId, itemState.getQuality()); } if (!UnifiedDataAppService.isEquals(value, his)) { @@ -204,9 +195,8 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC if (true) { this.logItemChanged(itemId, accessor_value, value, itemDto); } - if (!ObjectUtil.isEmpty(value)) { - accessor_value.setValue(itemId, value); - } + + accessor_value.setValue(itemId, value); } } @@ -229,30 +219,29 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC if (log.isWarnEnabled()) { log.warn("{} 所有内容都为空, all_null:{} ,暂定{}s", tag, all_null, 5000 + random); } - - ThreadUtl.sleep((long) (5000 + random)); + ThreadUtl.sleep((long) ((new Random()).nextInt(3) + 1) * 1000); + break start; } else if (this.all_null < 6) { if (log.isWarnEnabled()) { log.warn(tag + "重新创建server"); log.warn("{} 所有内容都为空, all_null:{} ,暂定{}s", tag, all_null, 30000 + random); } -// ThreadUtl.sleep((long) (30000 + random)); ThreadUtl.sleep((long) ((new Random()).nextInt(3) + 1) * 1000); break start; } else if (this.all_null < 12) { if (log.isWarnEnabled()) { - log.warn("{} 所有内容都为空, all_null:{} ,暂定{}ms", tag, all_null, '\uea60' + random); +// log.warn("{} 所有内容都为空, all_null:{} ,暂定{}ms", tag, all_null, '\uea60' + random); } + ThreadUtl.sleep((long) ((new Random()).nextInt(3) + 1) * 1000); + break start; - ThreadUtl.sleep((long) ('\uea60' + random)); } else { if (log.isWarnEnabled()) { - log.warn("{} 所有内容都为空, all_null:{} ,暂定{}ms", tag, all_null, 120000 + random); +// log.warn("{} 所有内容都为空, all_null:{} ,暂定{}ms", tag, all_null, 120000 + random); } - - ThreadUtl.sleep((long) (120000 + random)); +// ThreadUtl.sleep((long) ((new Random()).nextInt(3) + 1) * 1000); +// break start; } - ++this.all_null; } else { this.all_null = 0; @@ -268,9 +257,9 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC try { this.server.disconnect(); } catch (Exception var25) { + } } - this.server = null; if (!DeviceOpcSynchronizeAutoRun.isRun) { log.warn("opc线程停止2。。。"); @@ -357,14 +346,12 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC } } - - @Override public void connectionStateChanged(boolean connected) { if (!connected) { this.server = null; } - log.warn("opc server {} {}", this.getOpcGroupID(), connected ? "connected" : "disconnected"); +// log.warn("opc server {} {}", this.getOpcGroupID(), connected ? "connected" : "disconnected"); } private String getOpcGroupID() { @@ -379,9 +366,6 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC long seconds = mss % 60000L / 1000L; return days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds "; } - - - @Override public void changed(Item item, ItemState itemState) { String itemId = item.getId(); @@ -397,7 +381,7 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC // } else if (log.isInfoEnabled()) { // log.info("Item {} new value: {}, Timestamp: {}, Quality: {}", new Object[]{itemId, itemState.getValue(), itemState.getTimestamp().getTime(), itemState.getQuality()}); // } - log.trace("Item {} new value: {}, Timestamp: {}", new Object[]{itemId, itemState.getValue(), itemState.getTimestamp().getTime()}); +// log.trace("Item {} new value: {}, Timestamp: {}", new Object[]{itemId, itemState.getValue(), itemState.getTimestamp().getTime()}); OpcItemDto itemDto = this.getItem(itemId); // if (Boolean.TRUE.equals(itemDto.getNeed_log())) { @@ -413,6 +397,8 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC private void logItemChanged(String itemId, UnifiedDataAccessor accessor_value, Object value, OpcItemDto itemDto) { Object his = accessor_value.getValue(itemId); + itemDto.setHis_item_value(his); + itemDto.setItem_value(value); List relate_items = itemDto.getRelate_items(); if (relate_items != null && !relate_items.isEmpty()) { StringBuilder sb = new StringBuilder(); @@ -423,21 +409,23 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC Object obj = accessor_value.getValue(relate); sb.append("key:" + relate + "value:" + obj + ";"); } - log.warn("设备:{}信号{}变更从{}->{};信号快照:{}", new Object[]{itemDto.getDevice_code(), itemId, his, value, sb}); -// this.businessLogger.setResource(itemDto.getDevice_code(), itemDto.getDevice_name()).log("信号{}变更从{}->{};信号快照:{}", new Object[]{itemId, his, value, sb}); - } else { - if(his instanceof int[]){ - if(!Arrays.equals((long[]) his, (long[]) value)){ - log.warn("设备:{}信号{}变更从{}->{};信号快照:{}", new Object[]{itemDto.getDevice_code(), itemId, his, value}); - } - } else if(his instanceof String){ - if(!StrUtil.equals((CharSequence) his, (CharSequence) value)){ - log.warn("设备:{}信号{}变更从{}->{};信号快照:{}", new Object[]{itemDto.getDevice_code(), itemId, his, value}); - } - } else { - log.warn("设备:{}信号{}变更从{}->{};信号快照:{}", new Object[]{itemDto.getDevice_code(), itemId, his, value}); + if (!itemDto.getItem_code().endsWith("heartbeat") && !itemDto.getItem_code().endsWith("time")) { + // 存在上次点位值为null情况 则不记录日志 + LuceneLogDto luceneLogDto = new LuceneLogDto(itemDto.getOpc_server_code(), itemDto.getOpc_plc_code(), itemDto.getDevice_code(), itemDto.getItem_code().substring(itemDto.getItem_code().lastIndexOf(".") + 1), + String.valueOf(itemDto.getHis_item_value()), String.valueOf(itemDto.getItem_value())); + luceneLogDto.setLogType(LogTypeEnum.DEVICE_LOG.getDesc()); + log.info("{}", JSON.toJSONString(luceneLogDto)); + } + } else { +// log.info("设备:{}信号{}变更从{}->{};信号快照:{}", new Object[]{itemDto.getDevice_code(), itemId, his, value}); + // 存在上次点位值为null情况 则不记录日志 + if (!itemDto.getItem_code().endsWith("heartbeat") && !itemDto.getItem_code().endsWith("time")) { + LuceneLogDto luceneLogDto = new LuceneLogDto(itemDto.getOpc_server_code(), itemDto.getOpc_plc_code(), itemDto.getDevice_code(), itemDto.getItem_code().substring(itemDto.getItem_code().lastIndexOf(".") + 1), + String.valueOf(itemDto.getHis_item_value()), String.valueOf(itemDto.getItem_value())); + luceneLogDto.setLogType(LogTypeEnum.DEVICE_LOG.getDesc()); + log.info("{}", JSON.toJSONString(luceneLogDto)); + } -// this.businessLogger.setResource(itemDto.getDevice_code(), itemDto.getDevice_name()).log("信号{}变更从{}->{}", new Object[]{itemId, his, value}); } } diff --git a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/DeviceOpcSynchronizeAutoRun.java b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/DeviceOpcSynchronizeAutoRun.java index 26a91f0..5d0cad8 100644 --- a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/DeviceOpcSynchronizeAutoRun.java +++ b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/DeviceOpcSynchronizeAutoRun.java @@ -1,24 +1,17 @@ package org.nl.acs.opc; +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; import lombok.extern.slf4j.Slf4j; import org.apache.lucene.util.NamedThreadFactory; import org.nl.acs.auto.run.AbstractAutoRunnable; import org.nl.acs.opc.service.dto.OpcServerManageDto; -import org.nl.acs.udw.UnifiedDataAccessor; -import org.nl.acs.udw.UnifiedDataAccessorFactory; -import org.nl.acs.udw.UnifiedDataAppService; -import org.nl.config.SpringContextHolder; -import org.nl.system.service.param.ISysParamService; -import org.openscada.opc.lib.da.Group; -import org.openscada.opc.lib.da.Item; -import org.openscada.opc.lib.da.ItemState; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.*; import java.util.Map.Entry; import java.util.concurrent.*; -import java.util.regex.Pattern; /** * OPC设备同步启动 @@ -26,288 +19,66 @@ import java.util.regex.Pattern; @Component @Slf4j public class DeviceOpcSynchronizeAutoRun extends AbstractAutoRunnable { - static boolean isRun = true; + + public static boolean isRun = false; + ExecutorService executorService = Executors.newCachedThreadPool(); @Autowired private DeviceAppService deviceAppService; @Autowired private OpcServerManageService opcServerManageService; -// @Autowired -// LuceneExecuteLogService lucene; - - static ExecutorService executorService; - public static Map opcServersConfig; - public static Map itemCodeOpcItemDtoMapping = new ConcurrentHashMap(); - - static boolean canRefreshOpcEntity = true; - private long lastRefreshOpcEntityTime; - static UnifiedDataAccessor udw; - private static Map canReadOpcValues; - private static volatile Map opcCodeOpcEntityMapping; - - public DeviceOpcSynchronizeAutoRun() { - this.lastRefreshOpcEntityTime = 0L; - } + @Override public String getCode() { return DeviceOpcSynchronizeAutoRun.class.getSimpleName(); } + @Override public String getName() { return "opc设备同步器"; } - static Group getGroup(String opcCode) throws Exception { - OpcServerService opcServerService = SpringContextHolder.getBean(OpcServerService.class); - return opcServerService.getServer(opcCode); - } - - static void submitTimeLimitTask(Runnable runnable, String opcCode) { - CompletableFuture future = CompletableFuture.runAsync(runnable, executorService); - -// try { -// future.get(10L, TimeUnit.SECONDS); -// } catch (InterruptedException var9) { -// Thread.currentThread().interrupt(); -// } catch (ExecutionException var10) { -// var10.printStackTrace(); -// } catch (TimeoutException var11) { -// itemCodeOpcItemDtoMapping.keySet().forEach((key) -> { -// udw.setValue(key, (Object) null); -// }); -// canReadOpcValues = new ConcurrentHashMap<>(); -// System.out.println("opc设备同步器 任务执行超时,取消任务..."); -// future.cancel(true); -// } finally { -// canRefreshOpcEntity = true; -// if (opcCode != null) { -// canReadOpcValues.put(opcCode, true); -// } -// -// } - } - - private ExecutorService createThreadPool() { - ThreadPoolExecutor executor = new ThreadPoolExecutor(32, 32, 10L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), new NamedThreadFactory("opc-sync")); - executor.allowCoreThreadTimeOut(true); - return executor; - } - - public void autoRun() { - OpcStartTag.is_run = true; - opcServersConfig = this.opcServerManageService.queryAllServerMap(); - executorService = this.createThreadPool(); - opcCodeOpcEntityMapping = new ConcurrentHashMap(); - itemCodeOpcItemDtoMapping.keySet().forEach((key) -> { - udw.setValue(key, (Object) null); - }); - canRefreshOpcEntity = true; - canReadOpcValues.clear(); - - while (true) { - this.refreshOpcEntity(); - Iterator var1 = opcServersConfig.keySet().iterator(); - - while (var1.hasNext()) { - String opcCode = (String) var1.next(); - submitTimeLimitTask(() -> { - boolean in = false; - try { - if (canReadOpcValues.computeIfAbsent(opcCode, (key) -> true)) { - in = true; - canReadOpcValues.put(opcCode, false); - this.readOpcValues(opcCode); - } - } catch (Exception var3) { - var3.printStackTrace(); - } finally { - canRefreshOpcEntity = true; - if (opcCode != null && in) { - canReadOpcValues.put(opcCode, true); - } - } - }, opcCode); - } - - ThreadUtl.sleep((long) OpcConfig.synchronized_millisecond); - } - } - - private void readOpcValues(String opcCode) throws Exception { - synchronized (opcCode.intern()) { - OpcEntity opcEntity = (OpcEntity) opcCodeOpcEntityMapping.get(opcCode); - if (opcEntity != null) { - if (opcEntity.getItems().size() != 0) { - long begin = System.currentTimeMillis(); - if (log.isTraceEnabled()) { - log.trace("opc {} 开始计时{}", opcCode, begin); - } - - new HashMap(); - - Map itemStatus; - try { - itemStatus = opcEntity.readAll(); - } catch (Exception var15) { - itemStatus = opcEntity.readDividually(); - } - - long end = System.currentTimeMillis(); - long duration = end - begin; - if (log.isTraceEnabled()) { - log.trace("opc {} 读取耗时:{}", opcCode, duration); - } - - if (duration > 1000L) { - log.warn("opc {} 读取超时 : {}", opcCode, duration); - } - -// boolean allNull = itemStatus.entrySet().stream().map((map) -> { -// return OpcUtl.getValue((Item)map.getKey(), (ItemState)map.getValue()); -// }).allMatch(Objects::isNull); -// if (allNull) { -// opcEntity.getItems().clear(); -// } - - UnifiedDataAccessor udw = opcEntity.getUdw(); - - - Set items = itemStatus.keySet(); - Iterator var18 = items.iterator(); - - while (var18.hasNext()) { - Item item = (Item) var18.next(); - ItemState itemState = (ItemState) itemStatus.get(item); - Object nowValue = OpcUtl.getValue(item, itemState); - String itemId = item.getId(); - Object historyValue = udw.getValue(itemId); - if (!ObjectUtl.isEquals(itemState.getQuality(), QualityTypeValue.OPC_QUALITY_GOOD) && historyValue != null) { - log.warn("opc 值不健康 item: {}, 状态: {}", itemId, itemState.getQuality()); - } - if (!UnifiedDataAppService.isEquals(nowValue, historyValue)) { - OpcItemDto itemDto = (OpcItemDto) itemCodeOpcItemDtoMapping.get(itemId); - if (true) { - this.logItemChanged(itemId, udw, nowValue, itemDto); - } - udw.setValue(itemId, nowValue); - } - - } + @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)); + Set keys = pros.keySet(); + Iterator var4 = keys.iterator(); + //代码执行一次 + while (var4.hasNext()) { + String key = (String) var4.next(); + List> list = (List) pros.get(key); + OpcServerManageDto opcServer = (OpcServerManageDto) servers.get(key); + Iterator var8 = list.iterator(); + while (var8.hasNext()) { + List groupProtols = (List) var8.next(); + DeviceOpcProtocolRunable runable = new DeviceOpcProtocolRunable(); + runable.setProtocols(groupProtols); + runable.setOpcServer(opcServer); + this.executorService.submit(runable); } } - } - } - private void refreshOpcEntity() { - if (canRefreshOpcEntity) { - canRefreshOpcEntity = false; - long now = System.currentTimeMillis(); - if (now - this.lastRefreshOpcEntityTime >= 20000L) { - this.lastRefreshOpcEntityTime = now; - submitTimeLimitTask(() -> { - try { - Map>> protocol = this.deviceAppService.findAllFormatProtocolFromDriver(); - Iterator var2 = protocol.entrySet().iterator(); + // 同步无光电设备信号 + //Map>> pros1 = this.deviceAppService.findAllFormatProtocolFromDriver(); + //List opcDrivers = this.deviceAppService.findDeviceDriver(DeviceDriver.class); - while (var2.hasNext()) { - Entry>> stringListEntry = (Entry) var2.next(); - String opcCode = (String) stringListEntry.getKey(); - List> opcItemDtos = (List) stringListEntry.getValue(); - ((OpcEntity) opcCodeOpcEntityMapping.computeIfAbsent(opcCode, OpcEntity::new)).reload(opcItemDtos); - } - } catch (Exception var6) { - var6.printStackTrace(); - } finally { - canRefreshOpcEntity = true; - } - - }, (String) null); + while (true) { + Thread.sleep(3000L); } } } - private void logMessage(String errorMessage) { - try { -// issueLogger.setResource(OpcConfig.resource_code, OpcConfig.resource_name).setError(StringUtl.getString(100), "设备同步通信异常").log(errorMessage, new Object[0]); -// businessLogger.setResource(OpcConfig.resource_code, OpcConfig.resource_name).setError(StringUtl.getString(100), "设备同步通信异常").log(errorMessage, new Object[0]); - } catch (Exception var5) { - var5.printStackTrace(); - } - - } - + @Override public void after() { - OpcStartTag.is_run = false; - opcCodeOpcEntityMapping.values().forEach((opcEntity) -> { - opcEntity.cleanUdwCache(); - OpcServerService opcServerService = SpringContextHolder.getBean(OpcServerService.class); - opcServerService.cleanGroups(opcEntity.getOpcCode()); - }); - opcCodeOpcEntityMapping = new ConcurrentHashMap(); - itemCodeOpcItemDtoMapping = new ConcurrentHashMap(); - executorService.shutdownNow(); - } - - private void logItemChanged(String itemId, UnifiedDataAccessor accessor_value, Object value, OpcItemDto itemDto) { - ISysParamService paramService = SpringContextHolder.getBean(ISysParamService.class); - Object his = accessor_value.getValue(itemId); - List relate_items = itemDto.getRelate_items(); - if (relate_items != null && !relate_items.isEmpty()) { - StringBuilder sb = new StringBuilder(); - Iterator var8 = relate_items.iterator(); - - while (var8.hasNext()) { - String relate = (String) var8.next(); - Object obj = accessor_value.getValue(relate); - sb.append("key:" + relate + "value: " + obj + ";"); - } - if (!itemDto.getItem_code().endsWith("heartbeat") && !itemDto.getItem_code().endsWith("time") && !itemDto.getItem_code().endsWith("consumption")) { - // 存在上次点位值为null情况 则不记录日志 - if(!(his instanceof Float) && !(value instanceof Float)){ - log.info(itemDto.getOpc_server_code(), itemDto.getOpc_plc_code(),4, itemDto.getDevice_code(), itemDto.getItem_code().substring(itemDto.getItem_code().lastIndexOf(".") + 1), String.valueOf(his), String.valueOf(value)); -// LuceneLogDto luceneLogDto = new LuceneLogDto(itemDto.getOpc_server_code(), itemDto.getOpc_plc_code(),4, itemDto.getDevice_code(), itemDto.getItem_code().substring(itemDto.getItem_code().lastIndexOf(".") + 1), -// String.valueOf(his), String.valueOf(value)); -// luceneLogDto.setLogType(LogTypeEnum.DEVICE_LOG.getDesc()); -// String logLevel = paramService.findByCode(AcsConfig.LOGLEVEL).getValue(); -// if(StrUtil.isNotEmpty(logLevel) && isNumeric(logLevel) && (luceneLogDto.getLog_level() >= Integer.parseInt(logLevel))){ -// log.info("{}", JSON.toJSONString(luceneLogDto)); -// } - } - } - } else { - -// if (!itemDto.getItem_code().endsWith("heartbeat") && !itemDto.getItem_code().endsWith("time") && !itemDto.getItem_code().endsWith("consumption")) { -// if(!(his instanceof Float) && !(value instanceof Float)){ -// LuceneLogDto luceneLogDto = new LuceneLogDto(itemDto.getOpc_server_code(), itemDto.getOpc_plc_code(), itemDto.getDevice_code(), itemDto.getItem_code().substring(itemDto.getItem_code().lastIndexOf(".") + 1), -// String.valueOf(his), String.valueOf(value)); -// luceneLogDto.setLogType(LogTypeEnum.DEVICE_LOG.getDesc()); -// log.info("{}", JSON.toJSONString(luceneLogDto)); -// } -// } - - if (!itemDto.getItem_code().endsWith("heartbeat") && !itemDto.getItem_code().endsWith("time") && !itemDto.getItem_code().endsWith("consumption")) { - log.info(itemDto.getOpc_server_code(), itemDto.getOpc_plc_code(),4, itemDto.getDevice_code(), itemDto.getItem_code().substring(itemDto.getItem_code().lastIndexOf(".") + 1), String.valueOf(his), String.valueOf(value)); -// if(!(his instanceof Float) && !(value instanceof Float)){ -// LuceneLogDto luceneLogDto = new LuceneLogDto(itemDto.getOpc_server_code(), itemDto.getOpc_plc_code(),4, itemDto.getDevice_code(), itemDto.getItem_code().substring(itemDto.getItem_code().lastIndexOf(".") + 1), -// String.valueOf(his), String.valueOf(value)); -// luceneLogDto.setLogType(LogTypeEnum.DEVICE_LOG.getDesc()); -// String logLevel = paramService.findByCode(AcsConfig.LOGLEVEL).getValue(); -// if(StrUtil.isNotEmpty(logLevel) && isNumeric(logLevel) && (luceneLogDto.getLog_level() >= Integer.parseInt(logLevel))){ -// log.info("{}", JSON.toJSONString(luceneLogDto)); -// } -// } - } - - } - } - - static { - udw = UnifiedDataAccessorFactory.getAccessor(OpcConfig.udw_opc_value_key); - canReadOpcValues = new ConcurrentHashMap(); - opcCodeOpcEntityMapping = new ConcurrentHashMap(); - } - - public static boolean isNumeric(String str) { - return Pattern.compile("^[0-9]+$").matcher(str).matches(); + isRun = false; + this.executorService.shutdownNow(); + this.executorService = Executors.newCachedThreadPool(); } } diff --git a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/OpcEntity.java b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/OpcEntity.java index 8708c3f..8bf9cc4 100644 --- a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/OpcEntity.java +++ b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/OpcEntity.java @@ -5,172 +5,170 @@ 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; -import org.springframework.beans.factory.annotation.Autowired; import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; - public class OpcEntity { - @Autowired - OpcServerService opcServerService; - private final UnifiedDataAccessor udw; - private Map> items; - private Map> someFailDevices; - private String opcCode; - public OpcEntity(String opcCode) { - this.udw = UnifiedDataAccessorFactory.getAccessor(OpcConfig.udw_opc_value_key); - this.items = new ConcurrentHashMap(); - this.someFailDevices = new ConcurrentHashMap(); - this.opcCode = opcCode; - } - - public void reload(List> opcItemDtos) { - Map> itemCodes = new ConcurrentHashMap(); - (opcItemDtos.stream().flatMap(Collection::stream).collect(Collectors.groupingBy(OpcItemDto::getDevice_code))).forEach((deviceCodes, opcItemDtoList) -> { - itemCodes.put(deviceCodes, opcItemDtoList.stream().map(OpcItemDto::getItem_code).collect(Collectors.toList())); - }); - DeviceOpcSynchronizeAutoRun.itemCodeOpcItemDtoMapping.putAll((Map)opcItemDtos.stream().flatMap(Collection::stream).collect(Collectors.toMap(OpcItemDto::getItem_code, (obj) -> { - return obj; - },(k, v) -> k))); - if (this.items.size() == 0) { - itemCodes.values().stream().flatMap(Collection::stream).forEach((key) -> { - this.udw.setValue(key, (Object)null); - }); - this.addItemsIntoGroup(itemCodes); - } else { - if (this.someFailDevices.size() > 0) { - this.reAddDevices(); - } - - } - } - - private void reAddDevices() { - Map> addItems = new ConcurrentHashMap(); - StringBuilder err_message = new StringBuilder(); - this.someFailDevices.forEach((deviceCode, itemCodesList) -> { - itemCodesList.forEach((itemCode) -> { - try { - Group group = DeviceOpcSynchronizeAutoRun.getGroup(this.opcCode); - ((List)addItems.computeIfAbsent(deviceCode, (key) -> { - return new ArrayList(); - })).add(group.addItem(itemCode)); - } catch (Exception var6) { - err_message.append(itemCode).append(" 添加失败; "); - } - - }); - List deviceItems = (List)addItems.get(deviceCode); - if (deviceItems != null && deviceItems.size() == itemCodesList.size()) { - this.someFailDevices.remove(deviceCode); - } else if (itemCodesList.size() == 0) { - addItems.remove(deviceCode); - } else { - assert deviceItems != null; - - ((List)this.someFailDevices.get(deviceCode)).removeAll(deviceItems); - } - - synchronized(this.opcCode.intern()) { - this.items.putAll(addItems); - } - - if (err_message.length() > 0) { - String errMsg = err_message.toString(); - //this.log.warn("{}:{}", com.wxzd.wcs.opc.OpcConfig.resource_code, errMsg); - } - - }); - } - - private void addItemsIntoGroup(Map> itemCodes) { - try { - Group group = DeviceOpcSynchronizeAutoRun.getGroup(this.opcCode); - StringBuilder err_message = new StringBuilder(); - Map> items = new ConcurrentHashMap(); - itemCodes.forEach((deviceCode, itemCodesList) -> { - itemCodesList.forEach((itemCode) -> { - try { - ((List)items.computeIfAbsent(deviceCode, (key) -> { - return new ArrayList(); - })).add(group.addItem(itemCode)); - } catch (Exception var7) { - ((List)this.someFailDevices.computeIfAbsent(deviceCode, (key) -> { - return new ArrayList(); - })).add(itemCode); - this.udw.setValue(itemCode, (Object)null); - err_message.append(itemCode).append(" 添加失败; "); - } - - }); - List deviceItems = (List)items.get(deviceCode); - if (deviceItems != null && deviceItems.size() != itemCodesList.size()) { - items.remove(deviceCode); - this.someFailDevices.put(deviceCode, itemCodesList); - } - - }); - synchronized(this.opcCode.intern()) { - this.items = items; - } - - if (err_message.length() > 0) { - String errMsg = err_message.toString(); -// this.log.warn("{}:{}", OpcConfig.resource_code, errMsg); - } - } catch (Exception var8) { - var8.printStackTrace(); - } - - } - - public void cleanUdwCache() { - this.items.values().stream().flatMap(Collection::stream).map(Item::getId).forEach((key) -> { - this.udw.setValue(key, (Object)null); - }); - } - - public Map readAll() throws Exception { - return opcServerService.getServer(this.opcCode).read(true, (Item[])this.items.values().stream().flatMap(Collection::stream).toArray((x$0) -> { - return new Item[x$0]; - })); - } - - public Map readDividually() { - Map result = new HashMap(); - CompletableFuture[] futures = (CompletableFuture[])this.items.entrySet().stream().map((entry) -> { - return CompletableFuture.runAsync(() -> { - try { - Group group = DeviceOpcSynchronizeAutoRun.getGroup(this.opcCode); - result.putAll(group.read(true, (Item[])((List)entry.getValue()).toArray(new Item[0]))); - } catch (Exception var5) { - String deviceCode = (String)entry.getKey(); - // to do -// this.someFailDevices.put(deviceCode, ((List)entry.getValue()).stream().map(Item::getId).collect(Collectors.toList())); - this.items.remove(deviceCode); - } - - }, DeviceOpcSynchronizeAutoRun.executorService); - }).toArray((x$0) -> { - return new CompletableFuture[x$0]; - }); - CompletableFuture.allOf(futures).join(); - return result; - } - - - public UnifiedDataAccessor getUdw() { - return this.udw; - } - - public Map> getItems() { - return this.items; - } - - public String getOpcCode() { - return this.opcCode; - } +// OpcServerService opcServerService = SpringContextHolder.getBean(OpcServerService.class); +// private final UnifiedDataAccessor udw; +// private Map> items; +// private Map> someFailDevices; +// private String opcCode; +// +// public OpcEntity(String opcCode) { +// this.udw = UnifiedDataAccessorFactory.getAccessor(OpcConfig.udw_opc_value_key); +// this.items = new ConcurrentHashMap(); +// this.someFailDevices = new ConcurrentHashMap(); +// this.opcCode = opcCode; +// } +// +// public void reload(List> opcItemDtos) { +// Map> itemCodes = new ConcurrentHashMap(); +// (opcItemDtos.stream().flatMap(Collection::stream).collect(Collectors.groupingBy(OpcItemDto::getDevice_code))).forEach((deviceCodes, opcItemDtoList) -> { +// itemCodes.put(deviceCodes, opcItemDtoList.stream().map(OpcItemDto::getItem_code).collect(Collectors.toList())); +// }); +// DeviceOpcSynchronizeAutoRun.itemCodeOpcItemDtoMapping.putAll((Map)opcItemDtos.stream().flatMap(Collection::stream).collect(Collectors.toMap(OpcItemDto::getItem_code, (obj) -> { +// return obj; +// },(k, v) -> k))); +// if (this.items.size() == 0) { +// itemCodes.values().stream().flatMap(Collection::stream).forEach((key) -> { +// this.udw.setValue(key, (Object)null); +// }); +// this.addItemsIntoGroup(itemCodes); +// } else { +// if (this.someFailDevices.size() > 0) { +// this.reAddDevices(); +// } +// +// } +// } +// +// private void reAddDevices() { +// Map> addItems = new ConcurrentHashMap(); +// StringBuilder err_message = new StringBuilder(); +// this.someFailDevices.forEach((deviceCode, itemCodesList) -> { +// itemCodesList.forEach((itemCode) -> { +// try { +// Group group = DeviceOpcSynchronizeAutoRun.getGroup(this.opcCode); +// ((List)addItems.computeIfAbsent(deviceCode, (key) -> { +// return new ArrayList(); +// })).add(group.addItem(itemCode)); +// } catch (Exception var6) { +// err_message.append(itemCode).append(" 添加失败; "); +// } +// +// }); +// List deviceItems = (List)addItems.get(deviceCode); +// if (deviceItems != null && deviceItems.size() == itemCodesList.size()) { +// this.someFailDevices.remove(deviceCode); +// } else if (itemCodesList.size() == 0) { +// addItems.remove(deviceCode); +// } else { +// assert deviceItems != null; +// +// ((List)this.someFailDevices.get(deviceCode)).removeAll(deviceItems); +// } +// +// synchronized(this.opcCode.intern()) { +// this.items.putAll(addItems); +// } +// +// if (err_message.length() > 0) { +// String errMsg = err_message.toString(); +// //this.log.warn("{}:{}", com.wxzd.wcs.opc.OpcConfig.resource_code, errMsg); +// } +// +// }); +// } +// +// private void addItemsIntoGroup(Map> itemCodes) { +// try { +// Group group = DeviceOpcSynchronizeAutoRun.getGroup(this.opcCode); +// StringBuilder err_message = new StringBuilder(); +// Map> items = new ConcurrentHashMap(); +// itemCodes.forEach((deviceCode, itemCodesList) -> { +// itemCodesList.forEach((itemCode) -> { +// try { +// ((List)items.computeIfAbsent(deviceCode, (key) -> { +// return new ArrayList(); +// })).add(group.addItem(itemCode)); +// } catch (Exception var7) { +// ((List)this.someFailDevices.computeIfAbsent(deviceCode, (key) -> { +// return new ArrayList(); +// })).add(itemCode); +// this.udw.setValue(itemCode, (Object)null); +// err_message.append(itemCode).append(" 添加失败; "); +// } +// +// }); +// List deviceItems = (List)items.get(deviceCode); +// if (deviceItems != null && deviceItems.size() != itemCodesList.size()) { +// items.remove(deviceCode); +// this.someFailDevices.put(deviceCode, itemCodesList); +// } +// +// }); +// synchronized(this.opcCode.intern()) { +// this.items = items; +// } +// +// if (err_message.length() > 0) { +// String errMsg = err_message.toString(); +//// this.log.warn("{}:{}", OpcConfig.resource_code, errMsg); +// } +// } catch (Exception var8) { +// var8.printStackTrace(); +// } +// +// } +// +// public void cleanUdwCache() { +// this.items.values().stream().flatMap(Collection::stream).map(Item::getId).forEach((key) -> { +// this.udw.setValue(key, (Object)null); +// }); +// } +// +// public Map readAll() throws Exception { +// return opcServerService.getServer(this.opcCode).read(true, (Item[])this.items.values().stream().flatMap(Collection::stream).toArray((x$0) -> { +// return new Item[x$0]; +// })); +// } +// +// public Map readDividually() { +// Map result = new HashMap(); +// CompletableFuture[] futures = (CompletableFuture[])this.items.entrySet().stream().map((entry) -> { +// return CompletableFuture.runAsync(() -> { +// try { +// Group group = DeviceOpcSynchronizeAutoRun.getGroup(this.opcCode); +// result.putAll(group.read(true, (Item[])((List)entry.getValue()).toArray(new Item[0]))); +// } catch (Exception var5) { +// String deviceCode = (String)entry.getKey(); +// // to do +//// this.someFailDevices.put(deviceCode, ((List)entry.getValue()).stream().map(Item::getId).collect(Collectors.toList())); +// this.items.remove(deviceCode); +// } +// +// }, DeviceOpcSynchronizeAutoRun.executorService); +// }).toArray((x$0) -> { +// return new CompletableFuture[x$0]; +// }); +// CompletableFuture.allOf(futures).join(); +// return result; +// } +// +// +// public UnifiedDataAccessor getUdw() { +// return this.udw; +// } +// +// public Map> getItems() { +// return this.items; +// } +// +// public String getOpcCode() { +// return this.opcCode; +// } } diff --git a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/OpcItemDto.java b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/OpcItemDto.java index 677d00f..bd71a09 100644 --- a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/OpcItemDto.java +++ b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/OpcItemDto.java @@ -1,8 +1,11 @@ package org.nl.acs.opc; +import lombok.Data; + import java.util.ArrayList; import java.util.List; +@Data public class OpcItemDto { private String device_code; private String device_name; @@ -11,6 +14,7 @@ public class OpcItemDto { private String opc_plc_code; private String item_code; private Object item_value; + private Object his_item_value; private Boolean need_log = Boolean.valueOf(false); private List relate_items = new ArrayList(); diff --git a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/config/lucene/AsyncLuceneAppender.java b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/config/lucene/AsyncLuceneAppender.java new file mode 100644 index 0000000..e964510 --- /dev/null +++ b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/config/lucene/AsyncLuceneAppender.java @@ -0,0 +1,43 @@ +package org.nl.config.lucene; +/** + * @author ldjun + * @version 1.0 + * @date 2023年08月24日 13:00 + * @desc desc + */ + +import ch.qos.logback.classic.spi.ILoggingEvent; +import cn.hutool.core.util.IdUtil; +import com.yomahub.tlog.core.context.AspectLogContext; +import com.yomahub.tlog.core.enhance.logback.async.AspectLogbackAsyncAppender; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.MDC; + +import java.util.Map; + +public class AsyncLuceneAppender extends AspectLogbackAsyncAppender { + + + @Override + protected void append(ILoggingEvent event) { + String traceId = AspectLogContext.getLogValue(); + if (StringUtils.isEmpty(traceId)){ + traceId = IdUtil.nanoId()+"@"; + AspectLogContext.putLogValue(traceId); + }else { + if (!traceId.contains("@")){ + AspectLogContext.putLogValue(traceId+"@"); + } + } + if (StringUtils.isNotEmpty(traceId)){ + MDC.put("traceId",traceId); + Map mdcPropertyMap = event.getMDCPropertyMap(); + if (mdcPropertyMap.getClass().getName().contains("SynchronizedMap")){ + mdcPropertyMap.put("traceId",traceId); + } + MDC.clear(); + } + super.append(event); + } + +} diff --git a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/config/lucene/Indexer.java b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/config/lucene/Indexer.java index 350942c..18eff3f 100644 --- a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/config/lucene/Indexer.java +++ b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/config/lucene/Indexer.java @@ -1,184 +1,178 @@ -package org.nl.config.lucene;//package org.nl.config.lucene; -// -//import com.alibaba.fastjson.JSONObject; -//import org.apache.commons.io.FileUtils; -//import org.apache.lucene.analysis.Analyzer; -//import org.apache.lucene.document.Document; -//import org.apache.lucene.document.Field; -//import org.apache.lucene.document.TextField; -//import org.apache.lucene.index.IndexWriter; -//import org.apache.lucene.index.IndexWriterConfig; -//import org.apache.lucene.store.Directory; -//import org.apache.lucene.store.FSDirectory; -//import org.springframework.beans.factory.annotation.Value; -//import org.wltea.analyzer.lucene.IKAnalyzer; -// -//import java.io.BufferedReader; -//import java.io.File; -//import java.io.FileReader; -//import java.io.IOException; -//import java.nio.file.Paths; -//import java.util.Set; -// -///** -// * lucene索引器 -// */ -//public class Indexer { -// /** -// * 写索引实例 -// */ -// private IndexWriter writer; -// -// public IndexWriter getWriter() { -// return writer; -// } -// -// /** -// * 构造方法,实例化IndexWriter -// * -// * @param indexDir -// * @throws Exception -// */ -// public Indexer(String indexDir) throws Exception { -// Directory dir = FSDirectory.open(Paths.get(indexDir)); -// //标准分词器,会自动去掉空格啊,is a the等单词 -//// Analyzer analyzer = new StandardAnalyzer(); -// Analyzer analyzer = new IKAnalyzer(); -// //将标准分词器配到写索引的配置中 -// IndexWriterConfig config = new IndexWriterConfig(analyzer); -// //实例化写索引对象 -// writer = new IndexWriter(dir, config); -// } -// -// /** -// * 索引指定目录下的所有文件 -// * -// * @param dataDir -// * @return -// * @throws Exception -// */ -// public int indexAll(String dataDir) throws Exception { -// // 获取该路径下的所有文件 -// File[] files = new File(dataDir).listFiles(); -// if (null != files) { -// for (File file : files) { -// //调用下面的indexFile方法,对每个文件进行索引 -// indexFile(file); -// } -// } -// //返回索引的文件数 -//// return writer.numDocs(); -// return writer.numRamDocs(); -// } -// -// /** -// * 索引指定的文件 -// * -// * @param file -// * @throws Exception -// */ -// private void indexFile(File file) throws Exception { -// System.out.println("索引文件的路径:" + file.getCanonicalPath()); -// //调用下面的getDocument方法,获取该文件的document -// Document doc = getDocument(file); -// //添加索引文档 -// //Document doc = json2Doc(jsonDoc); -//// Document doc = new Document(); -//// doc.add(new TextField("content", jsonDoc, Field.Store.YES)); -// Field fieldContent = new TextField("fieldContent", FileUtils.readFileToString(null, "UTF-8"), Field.Store.YES); -// -// //将doc添加到索引中 -// writer.addDocument(doc); -// } -// -// /** -// * 获取文档,文档里再设置每个字段,就类似于数据库中的一行记录 -// * -// * @param file -// * @return -// * @throws Exception -// */ -// private Document getDocument(File file) throws Exception { +package org.nl.config.lucene; + +import com.alibaba.fastjson.JSONObject; +import org.apache.commons.io.FileUtils; +import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.document.TextField; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.FSDirectory; +import org.springframework.beans.factory.annotation.Value; +import org.wltea.analyzer.lucene.IKAnalyzer; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.Set; + +/** + * lucene索引器 + */ +public class Indexer { + /** + * 写索引实例 + */ + private IndexWriter writer; + + public IndexWriter getWriter() { + return writer; + } + + /** + * 构造方法,实例化IndexWriter + * @param indexDir + * @throws Exception + */ + public Indexer(String indexDir) throws Exception { + Directory dir = FSDirectory.open(Paths.get(indexDir)); + //标准分词器,会自动去掉空格啊,is a the等单词 +// Analyzer analyzer = new StandardAnalyzer(); + Analyzer analyzer = new IKAnalyzer(); + //将标准分词器配到写索引的配置中 + IndexWriterConfig config = new IndexWriterConfig(analyzer); + //实例化写索引对象 + writer = new IndexWriter(dir, config); + } + + /** + * 索引指定目录下的所有文件 + * @param dataDir + * @return + * @throws Exception + */ + public int indexAll(String dataDir) throws Exception { + // 获取该路径下的所有文件 + File[] files = new File(dataDir).listFiles(); + if (null != files) { + for (File file : files) { + //调用下面的indexFile方法,对每个文件进行索引 + indexFile(file); + } + } + //返回索引的文件数 +// return writer.numDocs(); + return writer.numRamDocs(); + } + + /** + * 索引指定的文件 + * @param file + * @throws Exception + */ + private void indexFile(File file) throws Exception { + System.out.println("索引文件的路径:" + file.getCanonicalPath()); + //调用下面的getDocument方法,获取该文件的document + Document doc = getDocument(file); + //添加索引文档 + //Document doc = json2Doc(jsonDoc); // Document doc = new Document(); -// //开始添加字段 -// //添加内容 -// doc.add(new TextField("contents", new FileReader(file))); -// //添加文件名,并把这个字段存到索引文件里 -// doc.add(new TextField("fileName", file.getName(), Field.Store.YES)); -// //添加文件路径 -// doc.add(new TextField("fullPath", file.getCanonicalPath(), Field.Store.YES)); -// return doc; -// } -// -// public Document json2Doc(String strDoc) { -// Document doc = new Document(); -// JSONObject jsonDoc = JSONObject.parseObject(strDoc); -// Set keys = jsonDoc.keySet(); -// for (String key : keys) { -// doc.add(new TextField(key, jsonDoc.getString(key), Field.Store.YES)); -// } -// return doc; -// } -// -// public void addLogIndex(String msg) throws IOException { -// //步骤一:创建Directory对象,用于指定索引库的位置 RAMDirectory内存 -// Directory directory = FSDirectory.open(new File("D:\\lucene\\index").toPath()); -// //步骤二:创建一个IndexWriter对象,用于写索引 -//// Analyzer analyzer = new StandardAnalyzer(); -// IndexWriter indexWriter = new IndexWriter(directory, new IndexWriterConfig(new IKAnalyzer(false))); -//// indexWriter.deleteAll();//清理所有索引库 -//// IndexWriter indexWriter=new IndexWriter(directory,new IndexWriterConfig(new StandardAnalyzer())); -// //记录索引开始时间 -// long startTime = System.currentTimeMillis(); -// //步骤三:读取磁盘中文件,对应每一个文件创建一个文档对象 -// Document document = new Document(); -//// document.add(new TextField("fieldContent", device_id, Field.Store.YES)); -// document.add(new TextField("fieldContent", msg, Field.Store.YES)); -// indexWriter.addDocument(document); -// //记录索引结束时间 -// long endTime = System.currentTimeMillis(); -// System.out.println("建立索引" + "共耗时" + (endTime - startTime) + "毫秒"); -// indexWriter.commit(); -// //步骤八:关闭资源 -// indexWriter.close(); -// System.out.println("建立索引成功-----关闭资源"); -// } -// -// //系统的日志文件路径 -// @Value("${logging.file.path}") -// private String logUrl; -// -// public static void main(String[] args) throws IOException { -// //步骤一:创建Directory对象,用于指定索引库的位置 RAMDirectory内存 -// Directory directory = FSDirectory.open(new File("D:\\lucene\\index").toPath()); -// //步骤二:创建一个IndexWriter对象,用于写索引 -//// Analyzer analyzer = new StandardAnalyzer(); -// IndexWriter indexWriter = new IndexWriter(directory, new IndexWriterConfig(new IKAnalyzer(false))); -// +// doc.add(new TextField("content", jsonDoc, Field.Store.YES)); + Field fieldContent=new TextField("fieldContent", FileUtils.readFileToString(null,"UTF-8"), Field.Store.YES); + + //将doc添加到索引中 + writer.addDocument(doc); + } + + /** + * 获取文档,文档里再设置每个字段,就类似于数据库中的一行记录 + * @param file + * @return + * @throws Exception + */ + private Document getDocument(File file) throws Exception { + Document doc = new Document(); + //开始添加字段 + //添加内容 + doc.add(new TextField("contents", new FileReader(file))); + //添加文件名,并把这个字段存到索引文件里 + doc.add(new TextField("fileName", file.getName(), Field.Store.YES)); + //添加文件路径 + doc.add(new TextField("fullPath", file.getCanonicalPath(), Field.Store.YES)); + return doc; + } + public Document json2Doc(String strDoc) { + Document doc = new Document(); + JSONObject jsonDoc = JSONObject.parseObject(strDoc); + Set keys = jsonDoc.keySet(); + for (String key : keys) { + doc.add(new TextField(key, jsonDoc.getString(key), Field.Store.YES)); + } + return doc; + } + + public void addLogIndex(String msg) throws IOException { + //步骤一:创建Directory对象,用于指定索引库的位置 RAMDirectory内存 + Directory directory = FSDirectory.open(new File("D:\\lucene\\index").toPath()); + //步骤二:创建一个IndexWriter对象,用于写索引 +// Analyzer analyzer = new StandardAnalyzer(); + IndexWriter indexWriter=new IndexWriter(directory,new IndexWriterConfig(new IKAnalyzer(false))); // indexWriter.deleteAll();//清理所有索引库 -//// indexWriter=new IndexWriter(directory,new IndexWriterConfig(new StandardAnalyzer())); -// //记录索引开始时间 -// long startTime = System.currentTimeMillis(); -// //步骤三:读取磁盘中文件,对应每一个文件创建一个文档对象 -// File file = new File("D:\\testlog"); -// //步骤四:获取文件列表 -// File[] files = file.listFiles(); -// for (File item : files) { -// BufferedReader bufferedReader = new BufferedReader(new FileReader(item)); -// String strLine = null; -// while (null != (strLine = bufferedReader.readLine())) { -// Document document = new Document(); -//// document.add(new Field()); -// document.add(new TextField("fieldContent", strLine, Field.Store.YES)); -// indexWriter.addDocument(document); -// } -// } -// //记录索引结束时间 -// long endTime = System.currentTimeMillis(); -// System.out.println("建立索引" + "共耗时" + (endTime - startTime) + "毫秒"); -// indexWriter.commit(); -// //步骤八:关闭资源 -// indexWriter.close(); -// System.out.println("建立索引成功-----关闭资源"); -// } -//} +// IndexWriter indexWriter=new IndexWriter(directory,new IndexWriterConfig(new StandardAnalyzer())); + //记录索引开始时间 + long startTime = System.currentTimeMillis(); + //步骤三:读取磁盘中文件,对应每一个文件创建一个文档对象 + Document document = new Document(); +// document.add(new TextField("fieldContent", device_id, Field.Store.YES)); + document.add(new TextField("fieldContent", msg, Field.Store.YES)); + indexWriter.addDocument(document); + //记录索引结束时间 + long endTime = System.currentTimeMillis(); + System.out.println("建立索引"+ "共耗时" + (endTime-startTime) + "毫秒"); + indexWriter.commit(); + //步骤八:关闭资源 + indexWriter.close(); + System.out.println("建立索引成功-----关闭资源"); + } + //系统的日志文件路径 + @Value("${logging.file.path}") + private String logUrl; + + public static void main(String[] args)throws IOException { + //步骤一:创建Directory对象,用于指定索引库的位置 RAMDirectory内存 + Directory directory = FSDirectory.open(new File("D:\\lucene\\index").toPath()); + //步骤二:创建一个IndexWriter对象,用于写索引 +// Analyzer analyzer = new StandardAnalyzer(); + IndexWriter indexWriter=new IndexWriter(directory,new IndexWriterConfig(new IKAnalyzer(false))); + + indexWriter.deleteAll();//清理所有索引库 +// IndexWriter indexWriter=new IndexWriter(directory,new IndexWriterConfig(new StandardAnalyzer())); + //记录索引开始时间 + long startTime = System.currentTimeMillis(); + //步骤三:读取磁盘中文件,对应每一个文件创建一个文档对象 + File file=new File("D:\\testlog"); + //步骤四:获取文件列表 + File[] files = file.listFiles(); + for (File item:files) { + BufferedReader bufferedReader = new BufferedReader(new FileReader(item)); + String strLine = null; + while(null != (strLine = bufferedReader.readLine())){ + Document document = new Document(); +// document.add(new Field()); + document.add(new TextField("fieldContent", strLine, Field.Store.YES)); + indexWriter.addDocument(document); + } + } + //记录索引结束时间 + long endTime = System.currentTimeMillis(); + System.out.println("建立索引"+ "共耗时" + (endTime-startTime) + "毫秒"); + indexWriter.commit(); + //步骤八:关闭资源 + indexWriter.close(); + System.out.println("建立索引成功-----关闭资源"); + } +} diff --git a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/config/lucene/LogMessageConstant.java b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/config/lucene/LogMessageConstant.java new file mode 100644 index 0000000..6cac540 --- /dev/null +++ b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/config/lucene/LogMessageConstant.java @@ -0,0 +1,44 @@ +package org.nl.config.lucene; + +/** + * @Author: lyd + * @Description: 定义lucene相关常量 + * @Date: 2023/8/25 + */ +public class LogMessageConstant { + /** 级别 */ + public final static String FIELD_LEVEL = "level"; + /** 时间 */ + public final static String FIELD_TIMESTAMP = "timestamp"; + /** 类的限定名 */ + public final static String FIELD_CLASS_NAME = "logger"; + /** 线程名 */ + public final static String FIELD_THREAD = "thread"; + /** 日志内容 */ + public final static String FIELD_MESSAGE = "message"; + public final static String FIELD_TRACEID = "tlogTraceId"; + // 定义颜色值 + /** 文本颜色:黑色 */ + public final static String COLOR_BLACK = "\u001B[30m"; + /** 文本颜色:红色 */ + public final static String COLOR_RED = "\u001B[31m"; + /** 文本颜色:绿色 */ + public final static String COLOR_GREEN = "\u001B[32m"; + /** 文本颜色:黄色 */ + public final static String COLOR_YELLOW = "\u001B[33m"; + /** 文本颜色:蓝色 */ + public final static String COLOR_BLUE = "\u001B[34m"; + /** 文本颜色:品红色 */ + public final static String COLOR_MAGENTA = "\u001B[35m"; + /** 文本颜色:青色 */ + public final static String COLOR_CYAN = "\u001B[36m"; + /** 文本颜色:白色 */ + public final static String COLOR_WHITE = "\u001B[37m"; + /** 文本颜色重置 */ + public final static String COLOR_RESET = "\u001B[0m"; + /** 背景颜色:黄色 */ + public final static String BACKGROUND_YELLOW = "\u001B[43m"; + + /** 索引路径 */ + public final static String INDEX_DIR = "D:\\lucene\\index"; +} diff --git a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/config/lucene/LuceneAppender.java b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/config/lucene/LuceneAppender.java new file mode 100644 index 0000000..f3a9c26 --- /dev/null +++ b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/config/lucene/LuceneAppender.java @@ -0,0 +1,117 @@ +package org.nl.config.lucene; +/** + * @author ldjun + * @version 1.0 + * @date 2023年08月24日 13:00 + * @desc desc + */ + +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.AppenderBase; +import cn.hutool.core.date.DateTime; +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONObject; +import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.document.NumericDocValuesField; +import org.apache.lucene.document.StringField; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.FSDirectory; +import org.nl.system.service.lucene.dto.LuceneLogDto; +import org.wltea.analyzer.lucene.IKAnalyzer; + +import java.io.IOException; +import java.nio.file.Paths; +import java.util.Map; + +public class LuceneAppender extends AppenderBase { + + private Directory index; + private IndexWriter indexWriter; + + + @Override + public void start() { + super.start(); + try { + index = FSDirectory.open(Paths.get(LogMessageConstant.INDEX_DIR)); + } catch (IOException e) { + e.printStackTrace(); + } + + // 初始化 Lucene 索引 + Analyzer analyzer = new IKAnalyzer(); + IndexWriterConfig config = new IndexWriterConfig(analyzer); + try { + indexWriter = new IndexWriter(index, config); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + @Override + protected void append(ILoggingEvent event) { + String message = event.getFormattedMessage(); + try { +// String[] split = message.split("@"); + LuceneLogDto luceneLogDto = JSONObject.parseObject(message, LuceneLogDto.class); + Document document = new Document(); + try { + //向document对象中添加域。 + Map mdcPropertyMap = event.getMDCPropertyMap(); + String traceId = mdcPropertyMap.get("traceId"); + System.out.println("---追踪号---"+traceId); + if (ObjectUtil.isNotEmpty(traceId)) { + document.add(new StringField("trace_id", traceId, Field.Store.YES)); + } + if (ObjectUtil.isNotEmpty(luceneLogDto.getDevice_code())) { + document.add(new StringField("device_code", luceneLogDto.getDevice_code(), Field.Store.YES)); + } + if (ObjectUtil.isNotEmpty(luceneLogDto.getContent())) { + document.add(new StringField("fieldContent", luceneLogDto.getContent(), Field.Store.YES)); + } + if (ObjectUtil.isNotEmpty(luceneLogDto.getMethod())) { + document.add(new StringField("method", luceneLogDto.getMethod(), Field.Store.YES)); + } + if (ObjectUtil.isNotEmpty(luceneLogDto.getStatus_code())) { + document.add(new StringField("status_code", luceneLogDto.getStatus_code(), Field.Store.YES)); + } + if (ObjectUtil.isNotEmpty(luceneLogDto.getRequestparam())) { + document.add(new StringField("requestparam", luceneLogDto.getRequestparam(), Field.Store.YES)); + } + if (ObjectUtil.isNotEmpty(luceneLogDto.getResponseparam())) { + document.add(new StringField("responseparam", luceneLogDto.getResponseparam(), Field.Store.YES)); + } + document.add(new StringField("logType", luceneLogDto.getLogType(), Field.Store.YES)); + document.add(new StringField("logTime", DateUtil.format(new DateTime(), "yyyy-MM-dd HH:mm:ss.SSS"), Field.Store.YES)); + document.add(new NumericDocValuesField("time",System.currentTimeMillis()));//排序 + + try { + indexWriter.addDocument(document); + indexWriter.commit(); + } catch (IOException e) { + e.printStackTrace(); + } + } catch (Exception e) { + return; + } + } catch (Exception e){ + return; + } + } + + @Override + public void stop() { + super.stop(); + try { + indexWriter.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/config/lucene/Searcher.java b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/config/lucene/Searcher.java index 6694f30..62abc03 100644 --- a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/config/lucene/Searcher.java +++ b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/config/lucene/Searcher.java @@ -2,7 +2,6 @@ package org.nl.config.lucene; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; -import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.apache.lucene.document.Document; @@ -15,10 +14,9 @@ import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.BytesRef; import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.List; -import java.util.Map; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.*; /** * lucene查询器 @@ -30,40 +28,27 @@ public class Searcher { //获取要查询的路径,也就是索引所在的位置 Directory dir = FSDirectory.open(Paths.get(indexDir)); IndexReader reader = DirectoryReader.open(dir); - //构建IndexSearcher IndexSearcher searcher = new IndexSearcher(reader); - //标准分词器,会自动去掉空格啊,is a the等单词 -// Analyzer analyzer = new StandardAnalyzer(); -// Analyzer analyzer = new IKAnalyzer(false); - //查询解析器 -// QueryParser queryParser = new QueryParser("fieldContent", analyzer); - - //记录索引开始时间 - long startTime = System.currentTimeMillis(); // 实际上Lucene本身不支持分页。因此我们需要自己进行逻辑分页。我们要准备分页参数: int pageSize = Integer.parseInt(whereJson.get("size").toString());// 每页条数 int pageNum = Integer.parseInt(whereJson.get("page").toString());// 当前页码 - int start = pageNum * pageSize;// 当前页的起始条数 - int end = start + pageSize;// 当前页的结束条数(不能包含) - // 创建排序对象,需要排序字段SortField,参数:字段的名称、字段的类型、是否反转如果是false,升序。true降序 - Sort sort = new Sort(new SortField("logTime", SortField.Type.DOC,true)); - TopDocs docs = null; BooleanQuery.Builder booleanQueryBuilder = new BooleanQuery.Builder(); //时间范围查询 String startDate = (String) whereJson.get("begin_time"); String endDate = (String) whereJson.get("end_time"); - Calendar calendar=Calendar.getInstance(); - calendar.set(1970, 0, 1); + if (startDate == null){ + Calendar calendar=Calendar.getInstance(); + calendar.set(1970, 0, 1); startDate = DateUtil.format(calendar.getTime(),"yyyy-MM-dd HH:mm:ss.SSS"); }else{ - startDate = LuceneServiceAutoRun.getDate(startDate); + startDate = getDate(startDate); } if (endDate == null){ endDate = DateUtil.format(new DateTime(),"yyyy-MM-dd HH:mm:ss.SSS"); } else { - endDate = LuceneServiceAutoRun.getDate(endDate); + endDate = getDate(endDate); } TermRangeQuery termRangeQuery = new TermRangeQuery("logTime", new BytesRef(startDate), new BytesRef(endDate), true, true); booleanQueryBuilder.add(termRangeQuery,BooleanClause.Occur.MUST); @@ -91,21 +76,19 @@ public class Searcher { WildcardQuery query = new WildcardQuery(new Term("fieldContent", "*"+(String) whereJson.get("blurry")+"*")); booleanQueryBuilder.add(query, BooleanClause.Occur.MUST); } - docs = searcher.search(booleanQueryBuilder.build(), end,sort); - //记录索引时间 - long endTime = System.currentTimeMillis(); - log.info("匹配{}共耗时{}毫秒",booleanQueryBuilder.build(),(endTime-startTime)); - log.info("查询到{}条日志文件", docs.totalHits.value); - List list = new ArrayList<>(); - ScoreDoc[] scoreDocs = docs.scoreDocs; - if (end > docs.totalHits.value) end = (int) docs.totalHits.value; - JSONArray array = new JSONArray(); - for (int i = start; i < end; i++) { - ScoreDoc scoreDoc = scoreDocs[i]; + TopFieldCollector collector = TopFieldCollector.create(new Sort(new SortField("time", SortField.Type.LONG,true)), 20000, 0); + searcher.search(booleanQueryBuilder.build(), collector); + TopDocs topDocs = collector.topDocs(pageNum*pageSize, pageSize); + int totalSize = collector.getTotalHits(); + ScoreDoc[] scoreDocs = topDocs.scoreDocs; + + List list = new ArrayList<>(); + for (ScoreDoc scoreDoc : scoreDocs) { Document doc = reader.document(scoreDoc.doc); JSONObject object = new JSONObject(); object.put("content",doc.get("fieldContent")); +// object.put("trace_id",doc.get("trace_id")); object.put("device_code",doc.get("device_code")); object.put("logTime",doc.get("logTime")); object.put("method",doc.get("method")); @@ -113,28 +96,21 @@ public class Searcher { object.put("requestparam",doc.get("requestparam")); object.put("responseparam",doc.get("responseparam")); if(doc.get("fieldContent") != null) { - array.add(object); + list.add(object); } } - for(Object logDto:array){ - log.info(logDto.toString()); - } reader.close(); + dir.close(); JSONObject jo = new JSONObject(); - jo.put("content", array); - jo.put("totalElements", docs.totalHits.value); + jo.put("content", list); + jo.put("totalElements", totalSize); return jo; } - public static void main(String[] args) { - String indexDir = "D:\\lucene\\index"; - //查询这个字符串 - String q = "07.832"; - Map whereJson = null; - try { - search(indexDir, q,whereJson); - } catch (Exception e) { - e.printStackTrace(); - } + public static String getDate(String timeString) throws ParseException { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");//时间格式 + Date date = sdf.parse(timeString); + timeString = DateUtil.format(date, "yyyy-MM-dd HH:mm:ss.SSS");//格式化后的时间 + return timeString; } } diff --git a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/lucene/LuceneExecuteLogService.java b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/lucene/LuceneExecuteLogService.java index cd2f571..148ea02 100644 --- a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/lucene/LuceneExecuteLogService.java +++ b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/lucene/LuceneExecuteLogService.java @@ -1,8 +1,7 @@ package org.nl.system.service.lucene; -import org.nl.system.service.lucene.dto.LuceneLogDto; -import java.io.IOException; +import org.nl.system.service.lucene.dto.LuceneLogDto; public interface LuceneExecuteLogService { /** @@ -26,7 +25,7 @@ public interface LuceneExecuteLogService { * * @param luceneLogDto 日志结果对象 */ - void interfaceExecuteLog(LuceneLogDto luceneLogDto) throws IOException; + void interfaceExecuteLog(LuceneLogDto luceneLogDto); /** * 设备执行日志,会保留历史记录 diff --git a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/lucene/LuceneService.java b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/lucene/LuceneService.java index 96560e8..ce75a3e 100644 --- a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/lucene/LuceneService.java +++ b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/lucene/LuceneService.java @@ -7,6 +7,11 @@ import java.util.Map; public interface LuceneService { + /** + * 获取labels和values树 + * @return + */ +// JSONArray getLabelsValues(); /** * 获取数据分页 diff --git a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/lucene/dto/LuceneLogDto.java b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/lucene/dto/LuceneLogDto.java index 7cda043..edacda4 100644 --- a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/lucene/dto/LuceneLogDto.java +++ b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/lucene/dto/LuceneLogDto.java @@ -69,18 +69,38 @@ public class LuceneLogDto { private String update_time; - - public LuceneLogDto (final String opc_server_code,final String opc_plc_code, - final String device_code,final String to_home,final int last_home, - final int home) { - super (); + public LuceneLogDto(final String opc_server_code, final String opc_plc_code, + final String device_code, final String to_home, final String last_home, + final String home) { + super(); this.device_code = device_code; - this.content = "信号" + this.content = "信号 [" + opc_server_code + "." + opc_plc_code + "." + device_code + "." - + to_home + "变更从" - + last_home + "->" + + to_home + "] 发生变更 " + + last_home + " -> " + home; } + + public LuceneLogDto(final String device_code, final String remark) { + super(); + this.device_code = device_code; + this.content = "设备 [" + + device_code + + "] - " + + remark; + } + + public LuceneLogDto(final LuceneLogDto dto) { + super(); + this.device_code = device_code; + this.method = method; + + this.content = "设备 [" + + device_code + + "] - " + + remark; + } + } diff --git a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/lucene/impl/LuceneExecuteLogServiceImpl.java b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/lucene/impl/LuceneExecuteLogServiceImpl.java index faf8385..f631600 100644 --- a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/lucene/impl/LuceneExecuteLogServiceImpl.java +++ b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/lucene/impl/LuceneExecuteLogServiceImpl.java @@ -1,27 +1,15 @@ package org.nl.system.service.lucene.impl; -import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; -import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSON; import lombok.RequiredArgsConstructor; -import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; -import org.apache.lucene.document.Document; -import org.apache.lucene.document.Field; -import org.apache.lucene.document.StringField; -import org.apache.lucene.index.IndexWriter; import org.nl.common.enums.LogTypeEnum; -import org.nl.config.lucene.DynamicLogger; -import org.nl.config.lucene.LuceneServiceAutoRun; import org.nl.system.service.lucene.LuceneExecuteLogService; import org.nl.system.service.lucene.dto.LuceneLogDto; -import org.slf4j.Logger; import org.slf4j.MDC; -import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; -import java.io.IOException; - /** * @author jlm * @description 服务实现 @@ -32,69 +20,21 @@ import java.io.IOException; @Slf4j public class LuceneExecuteLogServiceImpl implements LuceneExecuteLogService { - //日志目录 - @Value("${logging.file.path}") - private String logPath; - @Override public void deviceItemValue(String device_code, String key, String value) { String now = DateUtil.now(); } - @SneakyThrows @Override public void deviceExecuteLog(LuceneLogDto luceneLogDto) { luceneLogDto.setLogType(LogTypeEnum.DEVICE_LOG.getDesc()); - addIndex(luceneLogDto); + log.info("{}", JSON.toJSONString(luceneLogDto)); } @Override - public void interfaceExecuteLog(LuceneLogDto luceneLogDto) throws IOException { + public void interfaceExecuteLog(LuceneLogDto luceneLogDto) { luceneLogDto.setLogType(LogTypeEnum.INTERFACE_LOG.getDesc()); - addIndex(luceneLogDto); - } - - private void addIndex(LuceneLogDto luceneLogDto) throws IOException { - IndexWriter indexWriter = LuceneServiceAutoRun.getIndexWriter(); - //创建一个Document对象 - Document document = new Document(); - try { - //记录索引开始时间 - long startTime = System.currentTimeMillis(); - //向document对象中添加域。 - if (ObjectUtil.isNotEmpty(luceneLogDto.getDevice_code())) { - document.add(new StringField("device_code", luceneLogDto.getDevice_code(), Field.Store.YES)); -// document.add(new TextField("device_code", luceneLogDto.getDevice_code(), Field.Store.YES)); - } - if (ObjectUtil.isNotEmpty(luceneLogDto.getContent())) { - document.add(new StringField("fieldContent", luceneLogDto.getContent(), Field.Store.YES)); - } - if (ObjectUtil.isNotEmpty(luceneLogDto.getMethod())) { - document.add(new StringField("method", luceneLogDto.getMethod(), Field.Store.YES)); - } - if (ObjectUtil.isNotEmpty(luceneLogDto.getStatus_code())) { - document.add(new StringField("status_code", luceneLogDto.getStatus_code(), Field.Store.YES)); - } - if (ObjectUtil.isNotEmpty(luceneLogDto.getRequestparam())) { - document.add(new StringField("requestparam", luceneLogDto.getRequestparam(), Field.Store.YES)); - } - if (ObjectUtil.isNotEmpty(luceneLogDto.getResponseparam())) { - document.add(new StringField("responseparam", luceneLogDto.getResponseparam(), Field.Store.YES)); - } - document.add(new StringField("logType", luceneLogDto.getLogType(), Field.Store.YES)); - document.add(new StringField("logTime", DateUtil.format(new DateTime(), "yyyy-MM-dd HH:mm:ss.SSS"), Field.Store.YES)); - indexWriter.addDocument(document); - //记录索引结束时间 - long endTime = System.currentTimeMillis(); - indexWriter.commit(); - //实现日志文件按业务独立生成日志文件到指定路径 - DynamicLogger loggerBuilder =new DynamicLogger(logPath+"\\"+luceneLogDto.getLogType()+"\\"); - Logger logger = loggerBuilder.getLogger(luceneLogDto.getDevice_code()); -// logger.info("设备{}建立索引共耗时{}毫秒",luceneLogDto.getDevice_code(),endTime-startTime); - logger.info("{}",luceneLogDto.toString()); - } catch (Exception e) { - log.error(e.getMessage(), e); - } + log.info("{}", JSON.toJSONString(luceneLogDto)); } @Override diff --git a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/lucene/impl/LuceneServiceImpl.java b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/lucene/impl/LuceneServiceImpl.java index f7cc829..5ca7cc5 100644 --- a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/lucene/impl/LuceneServiceImpl.java +++ b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/lucene/impl/LuceneServiceImpl.java @@ -20,10 +20,51 @@ import java.util.Map; @Slf4j public class LuceneServiceImpl implements LuceneService { +// @Value("${loki.url}") +// private String lokiUrl; + +// @Value("${loki.systemName}") +// private String systemName; + //日志索引目录 @Value("${lucene.index.path}") private String luceneUrl; + /** + * 获取labels和values树 + * + * @return + */ +// @Override +// public JSONArray getLabelsValues() { +// JSONArray result = new JSONArray(); +// // 获取所有标签 +// String labelString = HttpUtil.get(lokiUrl + "/labels", CharsetUtil.CHARSET_UTF_8); +// JSONObject parse = (JSONObject) JSONObject.parse(labelString); +// JSONArray labels = parse.getJSONArray("data"); +// for (int i=0; i getAll(Map whereJson, Pageable page) { JSONObject jo = new JSONObject(); diff --git a/acs/nladmin-system/nlsso-server/src/main/resources/log/AcsToWms.xml b/acs/nladmin-system/nlsso-server/src/main/resources/log/AcsToWms.xml new file mode 100644 index 0000000..e3182fb --- /dev/null +++ b/acs/nladmin-system/nlsso-server/src/main/resources/log/AcsToWms.xml @@ -0,0 +1,32 @@ + + + + + + + + + ${LOG_HOME}/ACS请求WMS/%d{yyyy-MM-dd}.%i.log + + 15 + + 100MB + + 2GB + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + ${log.charset} + + + + + + + 512 + + + + + diff --git a/acs/nladmin-system/nlsso-server/src/main/resources/log/AgvNdcOneDeviceDriver.xml b/acs/nladmin-system/nlsso-server/src/main/resources/log/AgvNdcOneDeviceDriver.xml index ec8c7bf..5ea9f1d 100644 --- a/acs/nladmin-system/nlsso-server/src/main/resources/log/AgvNdcOneDeviceDriver.xml +++ b/acs/nladmin-system/nlsso-server/src/main/resources/log/AgvNdcOneDeviceDriver.xml @@ -10,7 +10,7 @@ 15 - 50MB + 100MB 2GB @@ -21,13 +21,12 @@ - - - - - - + + + + 512 + + + diff --git a/acs/nladmin-system/nlsso-server/src/main/resources/log/AutoCreateInst.xml b/acs/nladmin-system/nlsso-server/src/main/resources/log/AutoCreateInst.xml index c3bbeed..46daf98 100644 --- a/acs/nladmin-system/nlsso-server/src/main/resources/log/AutoCreateInst.xml +++ b/acs/nladmin-system/nlsso-server/src/main/resources/log/AutoCreateInst.xml @@ -10,7 +10,7 @@ 15 - 50MB + 100MB 2GB @@ -21,13 +21,12 @@ - - - - - - + + + + 512 + + + diff --git a/acs/nladmin-system/nlsso-server/src/main/resources/log/LnshRGVDeviceDriver.xml b/acs/nladmin-system/nlsso-server/src/main/resources/log/LnshRGVDeviceDriver.xml index 4436995..436236e 100644 --- a/acs/nladmin-system/nlsso-server/src/main/resources/log/LnshRGVDeviceDriver.xml +++ b/acs/nladmin-system/nlsso-server/src/main/resources/log/LnshRGVDeviceDriver.xml @@ -10,7 +10,7 @@ 15 - 50MB + 100MB 2GB @@ -21,13 +21,12 @@ - - - - - - + + + + 512 + + + diff --git a/acs/nladmin-system/nlsso-server/src/main/resources/log/Lucene.xml b/acs/nladmin-system/nlsso-server/src/main/resources/log/Lucene.xml index 857dfa3..9fecc86 100644 --- a/acs/nladmin-system/nlsso-server/src/main/resources/log/Lucene.xml +++ b/acs/nladmin-system/nlsso-server/src/main/resources/log/Lucene.xml @@ -23,7 +23,7 @@ - + diff --git a/acs/nladmin-system/nlsso-server/src/main/resources/log/NDCAgvServiceImpl.xml b/acs/nladmin-system/nlsso-server/src/main/resources/log/NDCAgvServiceImpl.xml index 4d994b7..402e64a 100644 --- a/acs/nladmin-system/nlsso-server/src/main/resources/log/NDCAgvServiceImpl.xml +++ b/acs/nladmin-system/nlsso-server/src/main/resources/log/NDCAgvServiceImpl.xml @@ -10,7 +10,7 @@ 15 - 50MB + 100MB 2GB @@ -22,12 +22,12 @@ - - - - - + + + + 512 + + + diff --git a/acs/nladmin-system/nlsso-server/src/main/resources/log/NDCSocketConnectionAutoRun.xml b/acs/nladmin-system/nlsso-server/src/main/resources/log/NDCSocketConnectionAutoRun.xml new file mode 100644 index 0000000..4b8a81a --- /dev/null +++ b/acs/nladmin-system/nlsso-server/src/main/resources/log/NDCSocketConnectionAutoRun.xml @@ -0,0 +1,32 @@ + + + + + + + + + ${LOG_HOME}/NDC交互日志/%d{yyyy-MM-dd}.%i.log + + 15 + + 100MB + + 2GB + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + ${log.charset} + + + + + + + 512 + + + + + diff --git a/acs/nladmin-system/nlsso-server/src/main/resources/log/WmsToAcs.xml b/acs/nladmin-system/nlsso-server/src/main/resources/log/WmsToAcs.xml index 9ea3c61..393d6dc 100644 --- a/acs/nladmin-system/nlsso-server/src/main/resources/log/WmsToAcs.xml +++ b/acs/nladmin-system/nlsso-server/src/main/resources/log/WmsToAcs.xml @@ -10,7 +10,7 @@ 15 - 50MB + 100MB 2GB @@ -21,13 +21,12 @@ - - - - - - + + + + 512 + + + diff --git a/acs/nladmin-system/nlsso-server/src/main/resources/logback-spring.xml b/acs/nladmin-system/nlsso-server/src/main/resources/logback-spring.xml index 507ec06..5f7a50d 100644 --- a/acs/nladmin-system/nlsso-server/src/main/resources/logback-spring.xml +++ b/acs/nladmin-system/nlsso-server/src/main/resources/logback-spring.xml @@ -14,10 +14,13 @@ https://juejin.cn/post/6844903775631572999 + + + + + - @@ -27,15 +30,12 @@ https://juejin.cn/post/6844903775631572999 - true - - + ${log.pattern} + ${log.charset} - - @@ -44,138 +44,108 @@ https://juejin.cn/post/6844903775631572999 15 - 50MB + 100MB 20GB - - - + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n - - - + 0 - 500 + 200 - + + + + + 512 - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - - - - - - - - - + + - + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - + + - - + + - - + + - - + + - - + + + + + + + + + + + + + + diff --git a/lms/nladmin-system/nlsso-server/pom.xml b/lms/nladmin-system/nlsso-server/pom.xml index 15d4894..bf00f6a 100644 --- a/lms/nladmin-system/nlsso-server/pom.xml +++ b/lms/nladmin-system/nlsso-server/pom.xml @@ -458,6 +458,12 @@ lucene-queryparser ${lucene.version} + + com.yomahub + tlog-core + 1.5.0 + compile + diff --git a/lms/nladmin-system/nlsso-server/src/main/java/org/nl/wms/cockpit/controller/CockpitController.java b/lms/nladmin-system/nlsso-server/src/main/java/org/nl/wms/cockpit/controller/CockpitController.java index f388299..b00807a 100644 --- a/lms/nladmin-system/nlsso-server/src/main/java/org/nl/wms/cockpit/controller/CockpitController.java +++ b/lms/nladmin-system/nlsso-server/src/main/java/org/nl/wms/cockpit/controller/CockpitController.java @@ -70,7 +70,7 @@ public class CockpitController { } @PostMapping("/getTbxInfo") - @Log("获取固化室信息") + @Log("获取涂板线信息") @ApiOperation("获取涂板线信息") public ResponseEntity getTbxInfo() { return new ResponseEntity<>(cockpitService.getTbxInfo(), HttpStatus.OK); diff --git a/lms/nladmin-system/nlsso-server/src/main/java/org/nl/wms/pda/service/dao/mapper/PdaMapper.xml b/lms/nladmin-system/nlsso-server/src/main/java/org/nl/wms/pda/service/dao/mapper/PdaMapper.xml index 88aac69..d682c9c 100644 --- a/lms/nladmin-system/nlsso-server/src/main/java/org/nl/wms/pda/service/dao/mapper/PdaMapper.xml +++ b/lms/nladmin-system/nlsso-server/src/main/java/org/nl/wms/pda/service/dao/mapper/PdaMapper.xml @@ -15,7 +15,8 @@ pw.operator, pw.create_name, pw.plan_qty, - pw.real_qty + pw.real_qty, + pw.point_name FROM `pdm_bd_workorder` pw LEFT JOIN md_base_material mm ON mm.material_id = pw.material_id @@ -90,17 +91,18 @@ diff --git a/lms/nladmin-system/nlsso-server/src/main/java/org/nl/wms/pda/service/dao/vo/GhsPointVo.java b/lms/nladmin-system/nlsso-server/src/main/java/org/nl/wms/pda/service/dao/vo/GhsPointVo.java index 2ee4e0b..970b330 100644 --- a/lms/nladmin-system/nlsso-server/src/main/java/org/nl/wms/pda/service/dao/vo/GhsPointVo.java +++ b/lms/nladmin-system/nlsso-server/src/main/java/org/nl/wms/pda/service/dao/vo/GhsPointVo.java @@ -5,7 +5,7 @@ import lombok.Data; @Data public class GhsPointVo { private String point_code; - private String vehicle_name; + private String material_name; private String group_id; private String material_qty; } diff --git a/lms/nladmin-system/nlsso-server/src/main/java/org/nl/wms/pda/service/dao/vo/ManualWorkOrderVo.java b/lms/nladmin-system/nlsso-server/src/main/java/org/nl/wms/pda/service/dao/vo/ManualWorkOrderVo.java index a0d2245..f5b5aad 100644 --- a/lms/nladmin-system/nlsso-server/src/main/java/org/nl/wms/pda/service/dao/vo/ManualWorkOrderVo.java +++ b/lms/nladmin-system/nlsso-server/src/main/java/org/nl/wms/pda/service/dao/vo/ManualWorkOrderVo.java @@ -17,4 +17,5 @@ public class ManualWorkOrderVo { private String create_name; private String plan_qty; private String real_qty; + private String point_name; } diff --git a/lms/nladmin-system/nlsso-server/src/main/resources/config/application-dev.yml b/lms/nladmin-system/nlsso-server/src/main/resources/config/application-dev.yml index 37eb038..9948a0a 100644 --- a/lms/nladmin-system/nlsso-server/src/main/resources/config/application-dev.yml +++ b/lms/nladmin-system/nlsso-server/src/main/resources/config/application-dev.yml @@ -6,7 +6,7 @@ spring: druid: db-type: com.alibaba.druid.pool.DruidDataSource driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy - url: jdbc:log4jdbc:mysql://${DB_HOST:127.0.0.1}:${DB_PORT:3306}/${DB_NAME:tn_lms}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&useOldAliasMetadataBehavior=true + url: jdbc:log4jdbc:mysql://${DB_HOST:127.0.0.1}:${DB_PORT:3306}/${DB_NAME:lms}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&useOldAliasMetadataBehavior=true # url: jdbc:log4jdbc:mysql://${DB_HOST:47.111.78.178}:${DB_PORT:3306}/${DB_NAME:stand_lms}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&useOldAliasMetadataBehavior=true username: ${DB_USER:root} password: ${DB_PWD:123456} @@ -55,6 +55,7 @@ spring: redis: #数据库索引 database: ${REDIS_DB:2} +# host: ${REDIS_HOST:10.44.101.112} host: ${REDIS_HOST:127.0.0.1} port: ${REDIS_PORT:6379} password: ${REDIS_PWD:}