kaiyu
2021-07-15 8a1e35934f5ad3e3febe6168e8eb990a0cd9cadb
Merge branch 'dev' of http://blit.7drlb.com:8888/r/moral into dev
12 files added
4 files modified
612 ■■■■■ changed files
screen-api/src/main/java/com/moral/api/controller/WebController.java 66 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/entity/HistoryDaily.java 45 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/entity/HistoryHourly.java 50 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/mapper/HistoryDailyMapper.java 20 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/mapper/HistoryHourlyMapper.java 16 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/HistoryDailyService.java 20 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/HistoryHourlyService.java 20 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/impl/HistoryDailyServiceImpl.java 44 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java 50 ●●●●● patch | view | raw | blame | history
screen-api/src/main/resources/mapper/HistoryDailyMapper.xml 21 ●●●●● patch | view | raw | blame | history
screen-api/src/main/resources/mapper/HistoryHourlyMapper.xml 13 ●●●●● patch | view | raw | blame | history
screen-common/src/main/java/com/moral/constant/Constants.java 24 ●●●●● patch | view | raw | blame | history
screen-common/src/main/java/com/moral/constant/RedisConstants.java 5 ●●●●● patch | view | raw | blame | history
screen-common/src/main/java/com/moral/util/AQIUtils.java 193 ●●●●● patch | view | raw | blame | history
screen-common/src/main/java/com/moral/util/DateUtils.java 18 ●●●● patch | view | raw | blame | history
screen-job/src/main/java/com/moral/api/service/impl/HistoryFiveMinutelyServiceImpl.java 7 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/controller/WebController.java
New file
@@ -0,0 +1,66 @@
package com.moral.api.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.moral.api.service.HistoryDailyService;
import com.moral.api.service.HistoryHourlyService;
import com.moral.constant.ResponseCodeEnum;
import com.moral.constant.ResultMessage;
import com.moral.util.WebUtils;
@Slf4j
@Api(tags = {"web端接口"})
@RestController
@RequestMapping("/web")
public class WebController {
    @Autowired
    private HistoryHourlyService historyHourlyService;
    @Autowired
    private HistoryDailyService historyDailyService;
    @GetMapping("getHourlyAqi")
    @ApiOperation(value = "获取一小时AQI", notes = "获取一小时AQI")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "mac", value = "设备mac", required = true, paramType = "query", dataType = "String")
    })
    public ResultMessage getHourlyAqi(HttpServletRequest request) {
        Map<String, Object> params = WebUtils.getParametersStartingWith(request, null);
        if (!params.containsKey("mac")) {
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        Map<String, Object> result = historyHourlyService.getHourlyAqiByMac(params.get("mac").toString());
        return ResultMessage.ok(result);
    }
    @GetMapping("getMonthAvg")
    @ApiOperation(value = "获取某设备某因子本月均值", notes = "获取某设备某因子本月均值")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "mac", value = "设备mac", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sensorCode", value = "因子code", required = true, paramType = "query", dataType = "String")
    })
    public ResultMessage getMonthAvg(HttpServletRequest request) {
        Map<String, Object> params = WebUtils.getParametersStartingWith(request, null);
        if (!params.containsKey("mac") || !params.containsKey("sensorCode")) {
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        Map<String, Object> result = historyDailyService.getMonthAvg(params);
        return ResultMessage.ok(result);
    }
}
screen-api/src/main/java/com/moral/api/entity/HistoryDaily.java
New file
@@ -0,0 +1,45 @@
package com.moral.api.entity;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
 * <p>
 * 日数据
 * </p>
 *
 * @author moral
 * @since 2021-07-14
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class HistoryDaily extends Model<HistoryDaily> {
    private static final long serialVersionUID = 1L;
    /**
     * 设备mac
     */
    private String mac;
    /**
     * 时间
     */
    private Date time;
    /**
     * 数据
     */
    private String value;
    @Override
    protected Serializable pkVal() {
        return null;
    }
}
screen-api/src/main/java/com/moral/api/entity/HistoryHourly.java
New file
@@ -0,0 +1,50 @@
package com.moral.api.entity;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
 * <p>
 * 已校准小时表
 * </p>
 *
 * @author moral
 * @since 2021-07-14
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class HistoryHourly extends Model<HistoryHourly> {
    private static final long serialVersionUID = 1L;
    /**
     * 设备mac
     */
    private String mac;
    /**
     * 数据时间
     */
    private Date time;
    /**
     * 数据
     */
    private String value;
    /**
     * 型号
     */
    private Integer version;
    @Override
    protected Serializable pkVal() {
        return null;
    }
}
screen-api/src/main/java/com/moral/api/mapper/HistoryDailyMapper.java
New file
@@ -0,0 +1,20 @@
package com.moral.api.mapper;
import java.util.Map;
import com.moral.api.entity.HistoryDaily;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
 * <p>
 * 日数据 Mapper 接口
 * </p>
 *
 * @author moral
 * @since 2021-07-14
 */
public interface HistoryDailyMapper extends BaseMapper<HistoryDaily> {
    Map<String, Object> getAvgByMac(Map<String, Object> params);
}
screen-api/src/main/java/com/moral/api/mapper/HistoryHourlyMapper.java
New file
@@ -0,0 +1,16 @@
package com.moral.api.mapper;
import com.moral.api.entity.HistoryHourly;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
 * <p>
 * 已校准小时表 Mapper 接口
 * </p>
 *
 * @author moral
 * @since 2021-07-14
 */
public interface HistoryHourlyMapper extends BaseMapper<HistoryHourly> {
}
screen-api/src/main/java/com/moral/api/service/HistoryDailyService.java
New file
@@ -0,0 +1,20 @@
package com.moral.api.service;
import java.util.Map;
import com.moral.api.entity.HistoryDaily;
import com.baomidou.mybatisplus.extension.service.IService;
/**
 * <p>
 * 日数据 服务类
 * </p>
 *
 * @author moral
 * @since 2021-07-14
 */
public interface HistoryDailyService extends IService<HistoryDaily> {
    Map<String, Object> getMonthAvg(Map<String, Object> params);
}
screen-api/src/main/java/com/moral/api/service/HistoryHourlyService.java
New file
@@ -0,0 +1,20 @@
package com.moral.api.service;
import java.util.Map;
import com.moral.api.entity.HistoryHourly;
import com.baomidou.mybatisplus.extension.service.IService;
/**
 * <p>
 * 已校准小时表 服务类
 * </p>
 *
 * @author moral
 * @since 2021-07-14
 */
public interface HistoryHourlyService extends IService<HistoryHourly> {
    Map<String,Object> getHourlyAqiByMac(String mac);
}
screen-api/src/main/java/com/moral/api/service/impl/HistoryDailyServiceImpl.java
New file
@@ -0,0 +1,44 @@
package com.moral.api.service.impl;
import com.moral.api.entity.HistoryDaily;
import com.moral.api.mapper.HistoryDailyMapper;
import com.moral.api.service.HistoryDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.constant.Constants;
import com.moral.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.util.HashMap;
import java.util.Map;
/**
 * <p>
 * 日数据 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-07-14
 */
@Service
public class HistoryDailyServiceImpl extends ServiceImpl<HistoryDailyMapper, HistoryDaily> implements HistoryDailyService {
    @Autowired
    private HistoryDailyMapper historyDailyMapper;
    @Override
    public Map<String, Object> getMonthAvg(Map<String, Object> params) {
        params.put("start", DateUtils.getFirstDayOfCurrMonth());
        params.put("end", DateUtils.getLastDayOfCurrMonth());
        Map<String, Object> data = historyDailyMapper.getAvgByMac(params);
        Map<String, Object> result = new HashMap<>();
        if (ObjectUtils.isEmpty(data)) {
            result.put("avg", Constants.NULL_VALUE);
            return result;
        }
        result.put("avg", data.get(params.get("sensorCode")));
        return result;
    }
}
screen-api/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java
New file
@@ -0,0 +1,50 @@
package com.moral.api.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.HistoryHourly;
import com.moral.api.mapper.HistoryHourlyMapper;
import com.moral.api.service.HistoryHourlyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.constant.Constants;
import com.moral.util.AQIUtils;
import com.moral.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
 * <p>
 * 已校准小时表 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-07-14
 */
@Service
public class HistoryHourlyServiceImpl extends ServiceImpl<HistoryHourlyMapper, HistoryHourly> implements HistoryHourlyService {
    @Autowired
    private HistoryHourlyMapper historyHourlyMapper;
    @Override
    public Map<String, Object> getHourlyAqiByMac(String mac) {
        QueryWrapper<HistoryHourly> queryWrapper = new QueryWrapper<>();
        String time = DateUtils.dateToDateString(new Date(), DateUtils.yyyy_MM_dd_HH_EN) + ":00:00";
        queryWrapper.eq("mac", mac).eq("time", time);
        //获取小时数据
        HistoryHourly historyHourly = historyHourlyMapper.selectOne(queryWrapper);
        Map<String, Object> result = new HashMap<>();
        if (historyHourly == null) {
            result.put("AQI", Constants.NULL_VALUE);
            return result;
        }
        Map<String, Object> data = JSONObject.parseObject(historyHourly.getValue(), Map.class);
        result.put("AQI", AQIUtils.hourlyAqi(data));
        return result;
    }
}
screen-api/src/main/resources/mapper/HistoryDailyMapper.xml
New file
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.moral.api.mapper.HistoryDailyMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.moral.api.entity.HistoryDaily">
        <result column="mac" property="mac"/>
        <result column="time" property="time"/>
        <result column="value" property="value"/>
    </resultMap>
    <select id="getAvgByMac" resultType="java.util.Map">
        SELECT
        AVG(`value` ->'$.${sensorCode}') AS '${sensorCode}'
        FROM history_daily
        WHERE mac = #{mac}
        AND time <![CDATA[>=]]> #{start}
        AND time <![CDATA[<=]]> #{end}
    </select>
</mapper>
screen-api/src/main/resources/mapper/HistoryHourlyMapper.xml
New file
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.moral.api.mapper.HistoryHourlyMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.moral.api.entity.HistoryHourly">
        <result column="mac" property="mac"/>
        <result column="time" property="time"/>
        <result column="value" property="value"/>
        <result column="version" property="version"/>
    </resultMap>
</mapper>
screen-common/src/main/java/com/moral/constant/Constants.java
@@ -153,4 +153,28 @@
     * 一氧化碳code
     * */
    public static final String SENSOR_CODE_CO= "a21005";
    /*
     * PM2.5 code
     * */
    public static final String SENSOR_CODE_PM25= "a34004";
    /*
     * PM10 code
     * */
    public static final String SENSOR_CODE_PM10= "a34002";
    /*
     * SO2 code
     * */
    public static final String SENSOR_CODE_SO2= "a21026";
    /*
     * NO2 code
     * */
    public static final String SENSOR_CODE_NO2= "a21004";
    /** The Constant NULL_VALUE. */
    public static final String NULL_VALUE = "N/V";
}
screen-common/src/main/java/com/moral/constant/RedisConstants.java
@@ -64,5 +64,10 @@
    * */
    public static final String UNIT_CONVERSION = "unit_conversion";
    /*
     * redis中5分钟数据key
     * */
    public static final String DATA_FIVE_MINUTES = "data_five_minutes";
}
screen-common/src/main/java/com/moral/util/AQIUtils.java
New file
@@ -0,0 +1,193 @@
package com.moral.util;
import org.springframework.util.ObjectUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.moral.constant.Constants;
public class AQIUtils {
    public static int hourlyAqi(Map<String, Object> map) {
        List<Integer> AQIList = new ArrayList<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            if (entry.getValue().toString().equals("")) {
                continue;
            }
            int PM2_5AQI;
            int PM10AQI;
            int SO2AQI;
            int NO2AQI;
            int COAQI;
            int O3AQI;
            switch (key) {
                case Constants.SENSOR_CODE_PM25:
                    PM2_5AQI = PM2_5AQI(Double.valueOf(entry.getValue().toString()));
                    AQIList.add(PM2_5AQI);
                    break;
                case Constants.SENSOR_CODE_PM10:
                    PM10AQI = PM10AQI(Double.valueOf(entry.getValue().toString()));
                    AQIList.add(PM10AQI);
                    break;
                case Constants.SENSOR_CODE_SO2:
                    SO2AQI = SO2AQI(Double.valueOf(entry.getValue().toString()));
                    AQIList.add(SO2AQI);
                    break;
                case Constants.SENSOR_CODE_NO2:
                    NO2AQI = NO2AQI(Double.valueOf(entry.getValue().toString()));
                    AQIList.add(NO2AQI);
                    break;
                case Constants.SENSOR_CODE_CO:
                    COAQI = COAQI(Double.valueOf(entry.getValue().toString()));
                    AQIList.add(COAQI);
                    break;
                case Constants.SENSOR_CODE_O3:
                    O3AQI = O3AQI(Double.valueOf(entry.getValue().toString()));
                    AQIList.add(O3AQI);
                    break;
                default:
                    break;
            }
        }
        int AQIMAX = 0;
        if (!ObjectUtils.isEmpty(AQIList)) {
            AQIMAX = Collections.max(AQIList);
        }
        return AQIMAX;
    }
    //PM2.5 IAQI
    public static int PM2_5AQI(Double value) {
        double result;
        if (value <= 0) {
            result = 0;
        } else if (value <= 35) {
            result = 50d / 35d * (value - 0) + 0;
        } else if (value <= 75) {
            result = 50d / 40d * (value - 35) + 50;
        } else if (value <= 115) {
            result = 50d / 40d * (value - 75) + 100;
        } else if (value <= 150) {
            result = 50d / 35d * (value - 115) + 150;
        } else if (value <= 250) {
            result = 100d / 100d * (value - 150) + 200;
        } else if (value <= 350) {
            result = 100d / 100d * (value - 250) + 300;
        } else {
            result = 100d / 150d * (value - 350) + 400;
        }
        return (int) Math.ceil(result);
    }
    //PM10 IAQI
    public static int PM10AQI(Double value) {
        double result;
        if (value <= 0) {
            result = 0;
        } else if (value <= 50) {
            result = 50d / 50d * (value - 0) + 0;
        } else if (value <= 150) {
            result = 50d / 100d * (value - 50) + 50;
        } else if (value <= 250) {
            result = 50d / 100d * (value - 150) + 100;
        } else if (value <= 350) {
            result = 50d / 100d * (value - 250) + 150;
        } else if (value <= 420) {
            result = 100d / 70d * (value - 350) + 200;
        } else if (value <= 500) {
            result = 100d / 80d * (value - 420) + 300;
        } else {
            result = 100d / 100d * (value - 500) + 400;
        }
        return (int) Math.ceil(result);
    }
    //SO2 IAQI
    public static int SO2AQI(Double value) {
        double result;
        if (value <= 0) {
            result = 0;
        } else if (value <= 150) {
            result = 50d / 150d * (value - 0) + 0;
        } else if (value <= 500) {
            result = 50d / 350d * (value - 150) + 50;
        } else if (value <= 650) {
            result = 50d / 150d * (value - 500) + 100;
        } else {
            result = 50d / 150d * (value - 650) + 150;
        }
        return (int) Math.ceil(result);
    }
    //NO2 IAQI
    public static int NO2AQI(Double value) {
        double result;
        if (value <= 0) {
            result = 0;
        } else if (value <= 100) {
            result = 50d / 100d * (value - 0) + 0;
        } else if (value <= 200) {
            result = 50d / 100d * (value - 100) + 50;
        } else if (value <= 700) {
            result = 50d / 500d * (value - 200) + 100;
        } else if (value <= 1200) {
            result = 50d / 500d * (value - 700) + 150;
        } else if (value <= 2340) {
            result = 100d / 1140d * (value - 1200) + 200;
        } else if (value <= 3090) {
            result = 100d / 750d * (value - 2340) + 300;
        } else {
            result = 100d / 750d * (value - 3090) + 400;
        }
        return (int) Math.ceil(result);
    }
    //CO IAQI
    public static int COAQI(Double value) {
        double result;
        if (value <= 0) {
            result = 0;
        } else if (value <= 5) {
            result = 50d / 5d * (value - 0) + 0;
        } else if (value <= 10) {
            result = 50d / 5d * (value - 5) + 50;
        } else if (value <= 35) {
            result = 50d / 25d * (value - 10) + 100;
        } else if (value <= 60) {
            result = 50d / 25d * (value - 35) + 150;
        } else if (value <= 90) {
            result = 100d / 30d * (value - 60) + 200;
        } else if (value <= 120) {
            result = 100d / 30d * (value - 90) + 300;
        } else {
            result = 100d / 30d * (value - 120) + 400;
        }
        return (int) Math.ceil(result);
    }
    //O3 IAQI
    public static int O3AQI(Double value) {
        double result;
        if (value <= 0) {
            result = 0;
        } else if (value <= 100) {
            result = 50d / 100d * (value - 0) + 0;
        } else if (value <= 160) {
            result = 50d / 60d * (value - 100) + 50;
        } else if (value <= 215) {
            result = 50d / 55d * (value - 160) + 100;
        } else if (value <= 265) {
            result = 50d / 150d * (value - 215) + 150;
        } else if (value <= 800) {
            result = 100d / 535d * (value - 265) + 200;
        } else if (value <= 2100) {
            result = 100d / 12d * (value - 800) + 300;
        } else {
            result = 100d / 12d * (value - 48) + 400;
        }
        return (int) Math.ceil(result);
    }
}
screen-common/src/main/java/com/moral/util/DateUtils.java
@@ -761,6 +761,17 @@
    }
    /**
     * 获取当前月第一天
     *
     * @return
     */
    public static Date getFirstDayOfCurrMonth() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        return getDate(dateToDateString(cal.getTime(), yyyy_MM_dd_EN));
    }
    /**
     * 获取当前月的最后一天
     *
     * @return
@@ -769,8 +780,7 @@
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH, 0);
        return cal.getTime();
        return getDate(dateToDateString(cal.getTime(), yyyy_MM_dd_EN));
    }
    /**
@@ -1326,4 +1336,8 @@
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return getDate(dateToDateString(calendar.getTime(), yyyy_MM_dd_EN));
    }
    public static void main(String[] args) {
        System.out.println(getFirstDayOfCurrMonth());
    }
}
screen-job/src/main/java/com/moral/api/service/impl/HistoryFiveMinutelyServiceImpl.java
@@ -1,6 +1,7 @@
package com.moral.api.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
@@ -22,6 +23,7 @@
import com.moral.api.service.HistoryMinutelyService;
import com.moral.api.service.SensorService;
import com.moral.constant.Constants;
import com.moral.constant.RedisConstants;
import com.moral.util.AmendUtils;
import com.moral.util.DateUtils;
@@ -41,6 +43,9 @@
    public void createTable(String timeUnits) {
        historyFiveMinutelyMapper.createTable(timeUnits);
    }
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    @Transactional
@@ -111,6 +116,8 @@
                }
            });
            dataMap.put("value", JSONObject.toJSONString(jsonMap));
            //存入redis
            redisTemplate.opsForHash().put(RedisConstants.DATA_FIVE_MINUTES, key, jsonMap);
            insertData.add(dataMap);
        });