From 1165bc2afb29084578ec34be15478415012d98e3 Mon Sep 17 00:00:00 2001 From: "hupenghui@qniao.cn" <1049970895> Date: Tue, 9 Aug 2022 23:56:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 + .../gizwits/GizWitsIotMonitoringDataJob.java | 88 ++++++++++++++----- .../iot/gizwits/utils/EsRestClientUtil.java | 1 - src/test/java/Demo1.java | 11 ++- 4 files changed, 79 insertions(+), 24 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..79fb450 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target/* +/.idea/* +/*.iml diff --git a/src/main/java/com/qniao/iot/gizwits/GizWitsIotMonitoringDataJob.java b/src/main/java/com/qniao/iot/gizwits/GizWitsIotMonitoringDataJob.java index b1a02cf..8801708 100644 --- a/src/main/java/com/qniao/iot/gizwits/GizWitsIotMonitoringDataJob.java +++ b/src/main/java/com/qniao/iot/gizwits/GizWitsIotMonitoringDataJob.java @@ -1,5 +1,6 @@ package com.qniao.iot.gizwits; +import cn.hutool.core.collection.ListUtil; import cn.hutool.core.date.LocalDateTimeUtil; import cn.hutool.core.util.ArrayUtil; import cn.hutool.json.JSONUtil; @@ -163,8 +164,8 @@ public class GizWitsIotMonitoringDataJob { List receivedEventList = new ArrayList<>(); EsRestClientUtil.queryDeviceListPageResult(searchSourceBuilder, receivedEventList::add, MachineIotDataReceivedEvent.class, getIndicesList()); - List> tuple3List = statistics(receivedEventList); - + //List> tuple3List = statistics(receivedEventList); + return null; } /** @@ -172,35 +173,80 @@ public class GizWitsIotMonitoringDataJob { * @param receivedEventList * @return 时长,数量 */ - private List> statistics(List receivedEventList) { - - Map map = new HashMap<>(); - MachineIotDataReceivedEvent firstEvent; - Integer nextPwrStat; - ArrayList nextWorkingStatList; - boolean isHasWaitingWork = false; + private List> statistics(List receivedEventList) { + List> mapList = new ArrayList<>(); + MachineIotDataReceivedEvent startEvent = null; + List nextPwrStatList = ListUtil.toList(0, 1); + ArrayList nextWorkingStatList = ListUtil.toList(0, 1, 2); + Map workingJobMap = new HashMap<>(2); + Map map = new HashMap<>(2); + MachineIotDataReceivedEvent waitJobEvent = null; + // 一个工作周期期间是否待机过 + boolean isHadWaitJob = false; for (int i = 0; i < receivedEventList.size(); i++) { MachineIotDataReceivedEvent receivedEvent = receivedEventList.get(i); - firstEvent = receivedEvent; Integer machinePwrStat = receivedEvent.getMachinePwrStat(); Integer machineWorkingStat = receivedEvent.getMachineWorkingStat(); Long reportTime = receivedEvent.getReportTime(); + map.clear(); + if(nextPwrStatList.contains(machinePwrStat) && nextWorkingStatList.contains(machineWorkingStat)) { - if (i == 0) { - Instant instant = Instant.ofEpochMilli(reportTime * 1000); - ZoneId zone = ZoneId.systemDefault(); - LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone); - LocalDateTime startTime = LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.MIN); - long l = Duration.between(startTime, localDateTime).get(SECONDS); - map.put("currJobDuration", l); - map.put("currJobCount", receivedEvent.getCurrJobCount()); - nextPwrStat = 1; + LocalDateTime startTime; + if(startEvent == null) { + LocalDateTime localDateTime = LocalDateTime + .ofInstant(Instant.ofEpochMilli(reportTime * 1000), ZoneId.systemDefault()); + startTime = LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.MIN); + }else { + startTime = LocalDateTime + .ofInstant(Instant.ofEpochMilli(startEvent.getReportTime() * 1000), + ZoneId.systemDefault()); + } + if(machinePwrStat.equals(0)) { + LocalDateTime localDateTime = LocalDateTime + .ofInstant(Instant.ofEpochMilli(reportTime * 1000), + ZoneId.systemDefault()); + long l = Duration.between(startTime, localDateTime).get(SECONDS); + if(isHadWaitJob) { + map.put("currJobDuration", workingJobMap.get("currJobDuration")); + }else { + map.put("currJobDuration", l); + } + map.put("currJobCount", receivedEvent.getCurrJobCount()); + mapList.add(map); + nextPwrStatList = ListUtil.toList(1); + workingJobMap.clear(); + mapList.add(map); + }else { + if(machineWorkingStat.equals(1)) { + // 工作中,只计算工作时长 + if(isHadWaitJob) { + // 如果前面的消息是待机消息 + startTime = LocalDateTime + .ofInstant(Instant.ofEpochMilli(waitJobEvent.getReportTime() * 1000), + ZoneId.systemDefault()); + // 状态重置 + waitJobEvent = null; + isHadWaitJob = false; + } + LocalDateTime localDateTime = LocalDateTime + .ofInstant(Instant.ofEpochMilli(reportTime * 1000), + ZoneId.systemDefault()); + long l = Duration.between(startTime, localDateTime).get(SECONDS); + workingJobMap.put("currJobDuration", l + workingJobMap.get("currJobDuration")); + } + if(machineWorkingStat.equals(2)) { + // 待机中 + isHadWaitJob = true; + waitJobEvent = receivedEvent; + } + } + }else { + startEvent = receivedEvent; } - } - + return mapList; } private String[] getIndicesList() throws IOException { diff --git a/src/main/java/com/qniao/iot/gizwits/utils/EsRestClientUtil.java b/src/main/java/com/qniao/iot/gizwits/utils/EsRestClientUtil.java index 4c3f963..4bae186 100644 --- a/src/main/java/com/qniao/iot/gizwits/utils/EsRestClientUtil.java +++ b/src/main/java/com/qniao/iot/gizwits/utils/EsRestClientUtil.java @@ -86,6 +86,5 @@ public class EsRestClientUtil { } catch (IOException e) { e.printStackTrace(); } - return tupleList; } } \ No newline at end of file diff --git a/src/test/java/Demo1.java b/src/test/java/Demo1.java index 8d76d0e..a43822f 100644 --- a/src/test/java/Demo1.java +++ b/src/test/java/Demo1.java @@ -1,11 +1,18 @@ import java.sql.Date; import java.time.LocalDate; +import java.util.HashMap; +import java.util.Map; public class Demo1 { public static void main(String[] args) { - LocalDate localDate = new Date(1659088698L * 1000).toLocalDate(); - System.out.println(localDate); + Map workingJobMap = new HashMap<>(); + workingJobMap.put("231231", 2325443523L); + workingJobMap.put("23rwer31", 2325443523L); + workingJobMap.put("2231f3f231", 2325443523L); + workingJobMap.clear(); + workingJobMap.put("231232222231", 2325443523L); + System.out.println(workingJobMap.get("231232222231")); } }