jinpengyong
2022-11-29 25576c047e39101a4784d8acffc92af3f35aa215
screen-manage/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java
@@ -1,5 +1,6 @@
package com.moral.api.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
@@ -21,6 +22,7 @@
import com.moral.constant.RedisConstants;
import com.moral.util.ConvertUtils;
import com.moral.util.DateUtils;
import com.sun.javafx.collections.MappingChange;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@@ -35,11 +37,9 @@
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
@@ -292,14 +292,9 @@
            queryWrapper.eq("monitor_point_id", mpId);
        }
        //设备名模糊查询
        if (name != null) {
            queryWrapper.like("name", name);
        }
        //mac模糊查询
        if (mac != null) {
            queryWrapper.like("mac", mac);
        //设备名称或mac模糊查询
        if (name != null && mac != null) {
            queryWrapper.like("name", name).or().like("mac", mac);
        }
        //排序参数,默认create_time降序
@@ -353,9 +348,11 @@
        //扩展字段
        deviceInfo.put("extend", device.getExtend());
        //乡镇街道信息
        deviceInfo.put("town", device.getTown());
        //行业
        deviceInfo.put("profession", device.getProfession());
        deviceInfo.put("professionName", device.getProfessionName());
        deviceInfo.put("professions", device.getProfessions());
        //工艺
        deviceInfo.put("tech", device.getTech());
@@ -447,8 +444,10 @@
        return deviceInfo;
    }
    @Autowired
    private  HistoryHourlyMapper historyHourlyMapper;
    @Override
    public Map<String, Object> adjustDeviceData(Map<String, Object> deviceData) {
    public Map<String, Object> adjustDeviceData(Map<String, Object> deviceData,String code) {
        String mac = deviceData.remove("mac").toString();
        //从redis获取校准公式
        Map<String, Object> adjustFormula = redisTemplate.opsForHash().entries(RedisConstants.ADJUST + "_" + mac);
@@ -459,6 +458,26 @@
            Map<String, Object> aqiMap = null;
            if (govMpInfo.get("guid") != null) {
                aqiMap = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.AQI_DATA, govMpInfo.get("guid").toString());
            }
            if (code.equals("2")){
                if (ObjectUtils.isEmpty(aqiMap.get("a21005")) || ObjectUtils.isEmpty(aqiMap.get("a21026"))
                 || ObjectUtils.isEmpty(aqiMap.get("a21004")) || ObjectUtils.isEmpty(aqiMap.get("a34002"))
                 || ObjectUtils.isEmpty(aqiMap.get("a34004")) || ObjectUtils.isEmpty(aqiMap.get("a05024"))
                 || ObjectUtils.isEmpty(aqiMap)){
                    HashMap<String, Object> result = new HashMap<>();
                    String dataTime = deviceData.get("DataTime").toString();
                    Date time = DateUtils.getDate(dataTime, DateUtils.yyyyMMddHHmmss_EN);
                    String yearAndMonth = DateUtils.dateToDateString(DateUtils.addHours(time, -1), DateUtils.yyyyMM_EN);
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
                    String format = simpleDateFormat.format(DateUtils.addHours(time, -1));
                    result.put("timeUnits",yearAndMonth);
                    result.put("mac",mac);
                    result.put("time",format);
                    Map<String, Object> map = historyHourlyMapper.selectHistoryHourly(result);
                    if (!ObjectUtils.isEmpty(map)){
                       return JSON.parseObject(map.get("value").toString(),  Map.class);
                    }
                }
            }
            return adjustDataUtils.adjust(deviceData, adjustFormula, ObjectUtils.isEmpty(aqiMap) ? null : aqiMap);
        }
@@ -504,19 +523,21 @@
            String sensorCode = sensor.getCode();
            //转换公式
            String formula = sensor.getFormula();
            //转换单位前因子值
            String sensorValue = (String) deviceData.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;
            //设备所传因子与配置型号因子不一致的处理逻辑
            if (deviceData.get(sensorCode) != null) {
                String sensorValue = String.valueOf(deviceData.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;
                }
            }
        }
        //修改设备状态
@@ -525,13 +546,35 @@
        deviceMapper.update(null, updateWrapper);
    }
    @Override
    public List<Map<String, Object>> selectMonitorPiontAndDeviceByOrgId(int orgId) {
        QueryWrapper<MonitorPoint> monitorPointQueryWrapper = new QueryWrapper<>();
        monitorPointQueryWrapper.eq("is_delete", Constants.NOT_DELETE);
        monitorPointQueryWrapper.eq("organization_id", orgId);
        List<MonitorPoint> monitorPointList = monitorPointMapper.selectList(monitorPointQueryWrapper);
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (MonitorPoint monitorPoint : monitorPointList) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap = JSON.parseObject(JSON.toJSONString(monitorPoint), Map.class);
            int mp_id = monitorPoint.getId();
            QueryWrapper<Device> deviceQueryWrapper = new QueryWrapper<>();
            deviceQueryWrapper.eq("is_delete", Constants.NOT_DELETE);
            deviceQueryWrapper.eq("monitor_point_id", mp_id);
            List<Device> devices = new ArrayList<>();
            devices = deviceMapper.selectList(deviceQueryWrapper);
            resultMap.put("devices", devices);
            resultList.add(resultMap);
        }
        return resultList;
    }
    //根据因子值判断状态
    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 + 1;
                state = i + 2;
                break;
            }
        }