From c21c161bc5ecddbe2a1d5174c2d178d768939e17 Mon Sep 17 00:00:00 2001 From: jinpengyong <jpy123456> Date: Fri, 26 Jan 2024 09:56:22 +0800 Subject: [PATCH] Merge branch 'dev' of http://blit.7drlb.com:8888/r/moral into wb --- screen-job/src/main/java/com/moral/api/service/impl/HistoryFiveMinutelyServiceImpl.java | 381 ++++++++++++++++++++++++++++++++++------------------- 1 files changed, 242 insertions(+), 139 deletions(-) diff --git a/screen-job/src/main/java/com/moral/api/service/impl/HistoryFiveMinutelyServiceImpl.java b/screen-job/src/main/java/com/moral/api/service/impl/HistoryFiveMinutelyServiceImpl.java index 03e64dc..47beefb 100644 --- a/screen-job/src/main/java/com/moral/api/service/impl/HistoryFiveMinutelyServiceImpl.java +++ b/screen-job/src/main/java/com/moral/api/service/impl/HistoryFiveMinutelyServiceImpl.java @@ -1,9 +1,16 @@ package com.moral.api.service.impl; +import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; +import com.googlecode.aviator.AviatorEvaluator; +import com.googlecode.aviator.Expression; +import com.moral.api.entity.Device; +import com.moral.api.service.DeviceService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; import java.util.*; @@ -26,6 +33,8 @@ @Service public class HistoryFiveMinutelyServiceImpl implements HistoryFiveMinutelyService { + @Value("${result.device.list}") + private String deviceList; @Autowired private HistoryFiveMinutelyMapper historyFiveMinutelyMapper; @@ -37,6 +46,9 @@ @Autowired private RedisTemplate redisTemplate; + + @Autowired + private DeviceService deviceService; @Override public void createTable(String timeUnits) { @@ -73,7 +85,232 @@ if (fiveMinutelyData.size() == 0) { return; } + listDistDeviceState(params); + //���mac������ + Map<String, List<Map<String, Object>>> data = fiveMinutelyData.parallelStream() + .collect(Collectors.groupingBy(o -> (String) o.get("mac"))); + //��������������������������� + List<Map<String, Object>> insertData = new ArrayList<>(); + + data.forEach((key, value) -> { + Map<String, Object> dataMap = new HashMap<>(); + dataMap.put("mac", key); + dataMap.put("time", start); + Map<String, Object> jsonMap = new HashMap<>(); + + + Map<String, Object> map = new HashMap<>(); + map.put("data", value); + map.put("type", "fiveMinutes"); + for (Sensor sensor : sensors) { + String sensorCode = sensor.getCode(); + + //��������������� + if (sensorCode.equals(Constants.SENSOR_CODE_WIND_DIR)) { + if (sensor.getUpper() != null) { + map.put("windDirUpper", sensor.getUpper()); + } + if (sensor.getLower() != null) { + map.put("windDirLower", sensor.getLower()); + } + } + + //��������������� + if (sensorCode.equals(Constants.SENSOR_CODE_WIND_SPEED)) { + if (sensor.getUpper() != null) { + map.put("windSpeedUpper", sensor.getUpper()); + } + if (sensor.getLower() != null) { + map.put("windSpeedLower", sensor.getLower()); + } + } + } + + //��������������������������� + Map<String, Object> windDirAvg = AmendUtils.getWindDirAvg(map); + if (!ObjectUtils.isEmpty(windDirAvg)) { + jsonMap.putAll(windDirAvg); + } + + //������������������������������������ + sensors.forEach(sensor -> { + String sensorCode = sensor.getCode(); + Double upper = sensor.getUpper(); + Double lower = sensor.getLower(); + DoubleStream optionalDouble = value.parallelStream() + .flatMapToDouble(v -> { + Map<String, Object> dataValue = JSONObject.parseObject((String) v.get("value"), Map.class); + Object sensorValue = dataValue.get(sensorCode); + //������������������������ + Object flag = dataValue.get(sensorCode + "-" + Constants.MARKER_BIT_KEY); + if (!Constants.MARKER_BIT_TRUE.equals(flag)) { + return null; + } + + if (ObjectUtils.isEmpty(sensorValue)) { + return null; + } + //������������������ + if (Constants.SENSOR_CODE_WIND_DIR.equals(sensorCode)) { + return null; + } + //������������������������������������ + double aDouble = Double.parseDouble(sensorValue.toString()); + if (!ObjectUtils.isEmpty(upper)) { + if (aDouble > upper) { + return null; + } + } + if (!ObjectUtils.isEmpty(lower)) { + if (aDouble < lower) { + return null; + } + } + return DoubleStream.of(aDouble); + }); + + OptionalDouble average = optionalDouble.average(); + if (average.isPresent()) { + //��������������������� + double sciCal = AmendUtils.sciCal(average.getAsDouble(), 4); + jsonMap.put(sensorCode, sciCal); + + } + + }); + dataMap.put("value", JSONObject.toJSONString(jsonMap)); + //������redis + jsonMap.put("dataTime", DateUtils.dateToDateString(start)); + redisTemplate.opsForHash().put(RedisConstants.DATA_FIVE_MINUTES, key, jsonMap); + insertData.add(dataMap); + judgeDeviceState(key,jsonMap); + }); + + //5��������������� + String insertTimeUnits = DateUtils.dateToDateString(start, DateUtils.yyyyMM_EN); + //��������������� + historyFiveMinutelyMapper.insertHistoryFiveMinutely(insertData, insertTimeUnits); + } + + // ������������������ + private void judgeDeviceState(String mac,Map<String, Object> dataMap){ + List<String> sensorList = Arrays.asList("a34004","a34002","a21026","a21004","a21005","a05024","a99054"); + Device device = (Device) redisTemplate.opsForHash().get(RedisConstants.DEVICE_INFO, mac); + if(Objects.isNull(device)){ + return; + } + List<Sensor> sensors = device.getVersion().getSensors(); + Expression expression; + int state = 1; + for (Sensor sensor : sensors) { + if(!sensorList.contains(sensor.getCode())){ + continue; + } + //������������������ + String alarmLevel = sensor.getAlarmLevel(); + if (StringUtils.isEmpty(alarmLevel)) { + continue; + } + List<Object> list = JSONObject.parseObject(alarmLevel, List.class); + String sensorCode = sensor.getCode(); + //������������ + String formula = sensor.getFormula(); + + //��������������������������������������������������������������� + if (dataMap.get(sensorCode) != null) { + String sensorValue = String.valueOf(dataMap.get(sensorCode)); + double value = Double.parseDouble(sensorValue); + if (formula != null) { + //������������������ + sensorValue = formula.replace("{0}", sensorValue); + expression = AviatorEvaluator.compile(sensorValue); + value = Double.parseDouble(expression.execute().toString()); + } + int sensorState = judgeState(list, value); + if (sensorState > state) { + state = sensorState; + } + } + } + //������������������ + Device devices = new Device(); + devices.setId(device.getId()); + devices.setState(String.valueOf(state)); + deviceService.updateById(devices); + } + + + private int judgeState(List<Object> levels, Double data) { + int state = 1; + for (int i = levels.size() - 1; i >= 0; i--) { + Double level = Double.parseDouble(levels.get(i).toString()); + if (data >= level) { + state = i + 2; + break; + } + } + return state; + } + + private void listDistDeviceState(Map<String, Object> params){ + QueryWrapper<Device> queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("is_delete",Constants.NOT_DELETE); + //������������������������ + List<Device> devices = deviceService.list(queryWrapper); + List<String> macOldList = devices.stream().map(Device::getMac).collect(Collectors.toList()); + List<String> macList = historyMinutelyMapper.getHistoryMinutelyMacData(params); + if(!CollectionUtils.isEmpty(macOldList)){ + List<String> listDictDevice = Arrays.asList(deviceList.split(",")); + List<String> list2 = macOldList.stream().filter(e -> !listDictDevice.contains(e)).collect(Collectors.toList()); + List<String> list3 = list2.stream().filter(e -> !macList.contains(e)).collect(Collectors.toList()); + List<Integer> idList = devices.stream().filter(e->list3.contains(e.getMac())).map(Device::getId).collect(Collectors.toList()); + for(Integer i : idList){ + Device device = new Device(); + device.setId(i); + device.setState(Constants.DEVICE_STATE_OFFLINE); + deviceService.updateById(device); + } + } + } + + + + + @Override + public void insertHistoryFiveMinutely(String yz, String mac) { + String format = DateUtils.yyyy_MM_dd_HH_mm_EN; + Date now = new Date(); + now = DateUtils.getFiveMinuteDate(now); + + //������������������������������ + Map<String, Object> params = new HashMap<>(); + //������������ + Date start = DateUtils.dataToTimeStampTime(DateUtils.getDateOfMin(now, -5), format); + //������������ + Date end = DateUtils.dataToTimeStampTime(now, format); + params.put("start", start); + params.put("end", end); + //������������������������������ + String timeUnits = DateUtils.dateToDateString(start, DateUtils.yyyyMM_EN); + params.put("timeUnits", timeUnits); + /*if(!StringUtils.isEmpty(mac)){ + params.put("macs", Arrays.asList(mac)); + }*/ + //������ + QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>(); + sensorQueryWrapper.select("code", "lower", "upper").eq("is_delete", Constants.NOT_DELETE); + /*if(!StringUtils.isEmpty(yz)){ + sensorQueryWrapper.eq("code",yz); + }*/ + List<Sensor> sensors = sensorService.list(sensorQueryWrapper); + + //���������������������5������������������ + List<Map<String, Object>> fiveMinutelyData = historyMinutelyMapper.getHistoryMinutelyData(params); + if (fiveMinutelyData.size() == 0) { + return; + } + listDistDeviceState(params); //���mac������ Map<String, List<Map<String, Object>>> data = fiveMinutelyData.parallelStream() .collect(Collectors.groupingBy(o -> (String) o.get("mac"))); @@ -171,148 +408,14 @@ jsonMap.put("dataTime", DateUtils.dateToDateString(start)); redisTemplate.opsForHash().put(RedisConstants.DATA_FIVE_MINUTES, key, jsonMap); insertData.add(dataMap); - }); + System.out.println(">>>"+key+"->>>"+jsonMap.toString()); + judgeDeviceState(key,jsonMap); + }); //5��������������� - String insertTimeUnits = DateUtils.dateToDateString(start, DateUtils.yyyyMM_EN); + // String insertTimeUnits = DateUtils.dateToDateString(start, DateUtils.yyyyMM_EN); //��������������� - historyFiveMinutelyMapper.insertHistoryFiveMinutely(insertData, insertTimeUnits); - } - - @Override - public void insertHistoryFiveMinutely(String yz, String mac) { - String format = DateUtils.yyyy_MM_dd_HH_mm_EN; - Date now = new Date(); - now = DateUtils.getFiveMinuteDate(now); - - //������������������������������ - Map<String, Object> params = new HashMap<>(); - //������������ - Date start = DateUtils.dataToTimeStampTime(DateUtils.getDateOfMin(now, -5), format); - //������������ - Date end = DateUtils.dataToTimeStampTime(now, format); - params.put("start", start); - params.put("end", end); - //������������������������������ - String timeUnits = DateUtils.dateToDateString(start, DateUtils.yyyyMM_EN); - params.put("timeUnits", timeUnits); - if(!StringUtils.isEmpty(mac)){ - params.put("macs", Arrays.asList(mac)); - } - //������ - QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>(); - sensorQueryWrapper.select("code", "lower", "upper").eq("is_delete", Constants.NOT_DELETE); - if(!StringUtils.isEmpty(yz)){ - sensorQueryWrapper.eq("code",yz); - } - List<Sensor> sensors = sensorService.list(sensorQueryWrapper); - - //���������������������5������������������ - List<Map<String, Object>> fiveMinutelyData = historyMinutelyMapper.getHistoryMinutelyData(params); - if (fiveMinutelyData.size() == 0) { - return; - } - - //���mac������ - Map<String, List<Map<String, Object>>> data = fiveMinutelyData.parallelStream() - .collect(Collectors.groupingBy(o -> (String) o.get("mac"))); - - //��������������������������� - List<Map<String, Object>> insertData = new ArrayList<>(); - - data.forEach((key, value) -> { - Map<String, Object> dataMap = new HashMap<>(); - dataMap.put("mac", key); - dataMap.put("time", start); - Map<String, Object> jsonMap = new HashMap<>(); - - - Map<String, Object> map = new HashMap<>(); - map.put("data", value); - map.put("type", "fiveMinutes"); - for (Sensor sensor : sensors) { - String sensorCode = sensor.getCode(); - - //��������������� - if (sensorCode.equals(Constants.SENSOR_CODE_WIND_DIR)) { - if (sensor.getUpper() != null) { - map.put("windDirUpper", sensor.getUpper()); - } - if (sensor.getLower() != null) { - map.put("windDirLower", sensor.getLower()); - } - } - - //��������������� - if (sensorCode.equals(Constants.SENSOR_CODE_WIND_SPEED)) { - if (sensor.getUpper() != null) { - map.put("windSpeedUpper", sensor.getUpper()); - } - if (sensor.getLower() != null) { - map.put("windSpeedLower", sensor.getLower()); - } - } - } - - //��������������������������� - Map<String, Object> windDirAvg = AmendUtils.getWindDirAvg(map); - if (!ObjectUtils.isEmpty(windDirAvg)) { - jsonMap.putAll(windDirAvg); - } - - //������������������������������������ - sensors.forEach(sensor -> { - String sensorCode = sensor.getCode(); - Double upper = sensor.getUpper(); - Double lower = sensor.getLower(); - DoubleStream optionalDouble = value.parallelStream() - .flatMapToDouble(v -> { - Map<String, Object> dataValue = JSONObject.parseObject((String) v.get("value"), Map.class); - Object sensorValue = dataValue.get(sensorCode); - //������������������������ - Object flag = dataValue.get(sensorCode + "-" + Constants.MARKER_BIT_KEY); - if (!Constants.MARKER_BIT_TRUE.equals(flag)) { - return null; - } - - if (ObjectUtils.isEmpty(sensorValue)) { - return null; - } - //������������������ - if (Constants.SENSOR_CODE_WIND_DIR.equals(sensorCode)) { - return null; - } - //������������������������������������ - double aDouble = Double.parseDouble(sensorValue.toString()); - if (!ObjectUtils.isEmpty(upper)) { - if (aDouble > upper) { - return null; - } - } - if (!ObjectUtils.isEmpty(lower)) { - if (aDouble < lower) { - return null; - } - } - return DoubleStream.of(aDouble); - }); - - OptionalDouble average = optionalDouble.average(); - if (average.isPresent()) { - //��������������������� - double sciCal = AmendUtils.sciCal(average.getAsDouble(), 4); - jsonMap.put(sensorCode, sciCal); - } - - }); - dataMap.put("value", JSONObject.toJSONString(jsonMap)); - //������redis - jsonMap.put("dataTime", DateUtils.dateToDateString(start)); - // redisTemplate.opsForHash().put(RedisConstants.DATA_FIVE_MINUTES, key, jsonMap); - //insertData.add(dataMap); - System.out.println(dataMap); - }); - + // historyFiveMinutelyMapper.insertHistoryFiveMinutely(insertData, insertTimeUnits); } } -- Gitblit v1.8.0