| | |
| | | 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.*; |
| | |
| | | @Service |
| | | public class HistoryFiveMinutelyServiceImpl implements HistoryFiveMinutelyService { |
| | | |
| | | @Value("${result.device.list}") |
| | | private String deviceList; |
| | | @Autowired |
| | | private HistoryFiveMinutelyMapper historyFiveMinutelyMapper; |
| | | |
| | |
| | | |
| | | @Autowired |
| | | private RedisTemplate redisTemplate; |
| | | |
| | | @Autowired |
| | | private DeviceService deviceService; |
| | | |
| | | @Override |
| | | public void createTable(String timeUnits) { |
| | |
| | | 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"))); |
| | |
| | | //银行家算法修约 |
| | | double sciCal = AmendUtils.sciCal(average.getAsDouble(), 4); |
| | | jsonMap.put(sensorCode, sciCal); |
| | | |
| | | } |
| | | |
| | | }); |
| | |
| | | jsonMap.put("dataTime", DateUtils.dateToDateString(start)); |
| | | redisTemplate.opsForHash().put(RedisConstants.DATA_FIVE_MINUTES, key, jsonMap); |
| | | insertData.add(dataMap); |
| | | judgeDeviceState(key,jsonMap); |
| | | }); |
| | | |
| | | //5分钟表后缀 |
| | |
| | | //存入数据库 |
| | | historyFiveMinutelyMapper.insertHistoryFiveMinutely(insertData, insertTimeUnits); |
| | | } |
| | | |
| | | // 修改设备状态 |
| | | private void judgeDeviceState(String mac,Map<String, Object> dataMap){ |
| | | Device device = (Device) redisTemplate.opsForHash().get(RedisConstants.DEVICE_INFO, mac); |
| | | List<Sensor> sensors = device.getVersion().getSensors(); |
| | | Expression expression; |
| | | int state = 1; |
| | | for (Sensor sensor : sensors) { |
| | | //因子报警等级 |
| | | 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 timeUnits = DateUtils.dateToDateString(start, DateUtils.yyyyMM_EN); |
| | | params.put("timeUnits", timeUnits); |
| | | if(!StringUtils.isEmpty(mac)){ |
| | | /*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)){ |
| | | /*if(!StringUtils.isEmpty(yz)){ |
| | | sensorQueryWrapper.eq("code",yz); |
| | | } |
| | | }*/ |
| | | List<Sensor> sensors = sensorService.list(sensorQueryWrapper); |
| | | |
| | | //获取所有设备的5分钟内的数据 |
| | |
| | | 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"))); |
| | |
| | | jsonMap.put("dataTime", DateUtils.dateToDateString(start)); |
| | | // redisTemplate.opsForHash().put(RedisConstants.DATA_FIVE_MINUTES, key, jsonMap); |
| | | //insertData.add(dataMap); |
| | | judgeDeviceState(key,jsonMap); |
| | | System.out.println(dataMap); |
| | | }); |
| | | |