From 3cda5b8c0618bcf4d81c0792e25563f77fa71d24 Mon Sep 17 00:00:00 2001 From: jinpengyong <jpy123456> Date: Fri, 10 Sep 2021 15:08:44 +0800 Subject: [PATCH] 国控站aqi数据接入 --- screen-job/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java | 137 ++++++++++++++++++++++++++++++++++----------- 1 files changed, 104 insertions(+), 33 deletions(-) diff --git a/screen-job/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java b/screen-job/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java index dd63413..caa9bb8 100644 --- a/screen-job/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java +++ b/screen-job/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java @@ -2,12 +2,10 @@ import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.moral.api.entity.HistoryHourly; import com.moral.api.entity.Sensor; import com.moral.api.mapper.HistoryHourlyMapper; import com.moral.api.mapper.HistoryMinutelyMapper; import com.moral.api.service.HistoryHourlyService; -import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.moral.api.service.SensorService; import com.moral.constant.Constants; import com.moral.constant.RedisConstants; @@ -26,6 +24,7 @@ import java.util.Map; import java.util.OptionalDouble; import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import java.util.stream.DoubleStream; @@ -38,7 +37,7 @@ * @since 2021-06-28 */ @Service -public class HistoryHourlyServiceImpl extends ServiceImpl<HistoryHourlyMapper, HistoryHourly> implements HistoryHourlyService { +public class HistoryHourlyServiceImpl implements HistoryHourlyService { @Autowired private HistoryHourlyMapper historyHourlyMapper; @@ -53,6 +52,11 @@ private SensorService sensorService; @Override + public void createTable(String timeUnits) { + historyHourlyMapper.createTable(timeUnits); + } + + @Override public void insertHistoryHourly() { //������������������yyyy-MM-dd HH:mm String format = DateUtils.yyyy_MM_dd_HH_EN; @@ -63,21 +67,21 @@ Date now = new Date(); String time = DateUtils.dateToDateString(now, format) + ":00:00"; - QueryWrapper<HistoryHourly> queryWrapper = new QueryWrapper<>(); - queryWrapper.eq("time", time); //������������������������������������������������macs������������������������������ - Integer count = historyHourlyMapper.selectCount(queryWrapper); - if (macs.size() > count) { + Map<String, Object> prop = new HashMap<>(); + prop.put("timeUnits", DateUtils.getDateStringOfMon(0, DateUtils.yyyyMM_EN)); + prop.put("time", time); + Integer count = historyHourlyMapper.selectCountByTime(prop); + + if (macs.size() <= count) { + return; + } else { macs.removeIf(mac -> { - queryWrapper.clear(); - queryWrapper.eq("time", time); - queryWrapper.eq("mac", mac); - Integer num = historyHourlyMapper.selectCount(queryWrapper); + prop.put("mac", mac); + Integer num = historyHourlyMapper.selectCountByTime(prop); return num != 0; }); } - - Map<String, Object> params = new HashMap<>(); //������������ String dateString = DateUtils.getDateStringOfHour(-1, format); @@ -91,9 +95,11 @@ //������ QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>(); - sensorQueryWrapper.select("code").eq("is_delete", Constants.NOT_DELETE); - List<Object> sensorCodes = sensorService.listObjs(sensorQueryWrapper); + sensorQueryWrapper.select("code", "lower", "upper").eq("is_delete", Constants.NOT_DELETE); + List<Sensor> sensors = sensorService.list(sensorQueryWrapper); + //������������������������������������������ + Map<String, Object> params = new HashMap<>(); params.put("timeUnits", timeUnits); params.put("start", start); params.put("end", end); @@ -109,47 +115,112 @@ .collect(Collectors.groupingBy(o -> (String) o.get("mac"))); //��������������������������� - List<HistoryHourly> insertData = new ArrayList<>(); + List<Map<String, Object>> insertData = new ArrayList<>(); data.forEach((key, value) -> { - HistoryHourly historyHourly = new HistoryHourly(); - historyHourly.setMac(key); - historyHourly.setTime(end); + Map<String, Object> historyHourly = new HashMap<>(); + historyHourly.put("mac", key); + historyHourly.put("time", end); + Map<String, Object> jsonMap = new HashMap<>(); + Map<String, Object> map = new HashMap<>(); + map.put("data", value); + map.put("type", "hour"); + + 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()); + } + } + } //��������������������������� - Object windDirAvg = AmendUtils.getWindDirAvg(value); - if (windDirAvg != null) { - jsonMap.put(Constants.SENSOR_CODE_WIND_DIR, windDirAvg); + Map<String, Object> windDirAvg = AmendUtils.getWindDirAvg(map); + if (!ObjectUtils.isEmpty(windDirAvg)) { + jsonMap.putAll(windDirAvg); } //������������������������������������ - sensorCodes.forEach(sensorCode -> { - OptionalDouble optionalDouble = value.parallelStream() + sensors.forEach(sensor -> { + String sensorCode = sensor.getCode(); + Double upper = sensor.getUpper(); + Double lower = sensor.getLower(); + AtomicInteger size = new AtomicInteger(); + DoubleStream optionalDouble = value.parallelStream() .flatMapToDouble(v -> { Map<String, Object> dataValue = JSONObject.parseObject((String) v.get("value"), Map.class); - Object sensorValue = dataValue.get(sensorCode.toString()); + 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; } - return DoubleStream.of(Double.parseDouble(sensorValue.toString())); - }).average(); - if (optionalDouble.isPresent()) { + + //������������������������������������ + double aDouble = Double.parseDouble(sensorValue.toString()); + if (!ObjectUtils.isEmpty(upper)) { + if (aDouble < upper) { + return null; + } + } + if (!ObjectUtils.isEmpty(lower)) { + if (aDouble > lower) { + return null; + } + } + size.getAndIncrement(); + return DoubleStream.of(aDouble); + }); + OptionalDouble average = optionalDouble.average(); + if (average.isPresent()) { //��������������������� - double sciCal = AmendUtils.sciCal(optionalDouble.getAsDouble(), 4); - jsonMap.put(sensorCode.toString(), sciCal); + double sciCal = AmendUtils.sciCal(average.getAsDouble(), 4); + jsonMap.put(sensorCode, sciCal); + //��������� + if (size.get() >= 45) { + jsonMap.put(sensorCode + "-" + Constants.MARKER_BIT_KEY, Constants.MARKER_BIT_TRUE); + } else { + jsonMap.put(sensorCode + "-" + Constants.MARKER_BIT_KEY, Constants.MARKER_BIT_FALSE); + } } }); - historyHourly.setValue(JSONObject.toJSONString(jsonMap)); - historyHourly.setVersion((Integer) value.get(0).get("version")); + historyHourly.put("version", value.get(0).get("version")); + historyHourly.put("value", JSONObject.toJSONString(jsonMap)); insertData.add(historyHourly); }); //��������������� historyHourlyMapper.insertHistoryHourly(insertData); } + + @Override + public List<Map<String, Object>> selectDailyData(Map<String, Object> params) { + return historyHourlyMapper.selectDailyData(params); + } } -- Gitblit v1.8.0