pom.xml
@@ -150,6 +150,24 @@ <version>2.1.2</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.18</version> </dependency> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0.2</version> </dependency> </dependencies> <build> src/main/java/com/moral/entity/AreaNames.java
New file @@ -0,0 +1,12 @@ package com.moral.entity; import lombok.Data; @Data public class AreaNames { private String provinceName; private String cityName; private String areaName; private String townName; private String villageName; } src/main/java/com/moral/entity/Organization.java
New file @@ -0,0 +1,100 @@ package com.moral.entity; import java.util.Date; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Transient; import lombok.Data; @Data public class Organization { /** * This field was generated by MyBatis Generator. This field corresponds to the database column organization.id * @mbggenerated Thu Dec 07 15:18:16 CST 2017 */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; /** * This field was generated by MyBatis Generator. This field corresponds to the database column organization.name * @mbggenerated Thu Dec 07 15:18:16 CST 2017 */ private String name; /** * This field was generated by MyBatis Generator. This field corresponds to the database column organization.rank * @mbggenerated Thu Dec 07 15:18:16 CST 2017 */ private Integer rank; /** * This field was generated by MyBatis Generator. This field corresponds to the database column organization.province_code * @mbggenerated Thu Dec 07 15:18:16 CST 2017 */ private Integer provinceCode; /** * This field was generated by MyBatis Generator. This field corresponds to the database column organization.city_code * @mbggenerated Thu Dec 07 15:18:16 CST 2017 */ private Integer cityCode; /** * This field was generated by MyBatis Generator. This field corresponds to the database column organization.area_code * @mbggenerated Thu Dec 07 15:18:16 CST 2017 */ private Integer areaCode; /** * This field was generated by MyBatis Generator. This field corresponds to the database column organization.address * @mbggenerated Thu Dec 07 15:18:16 CST 2017 */ private String address; /** * This field was generated by MyBatis Generator. This field corresponds to the database column organization.telephone * @mbggenerated Thu Dec 07 15:18:16 CST 2017 */ private String telephone; /** * This field was generated by MyBatis Generator. This field corresponds to the database column organization.email * @mbggenerated Thu Dec 07 15:18:16 CST 2017 */ private String email; /** * This field was generated by MyBatis Generator. This field corresponds to the database column organization.is_delete * @mbggenerated Thu Dec 07 15:18:16 CST 2017 */ private String isDelete; /** * This field was generated by MyBatis Generator. This field corresponds to the database column organization.create_time * @mbggenerated Thu Dec 07 15:18:16 CST 2017 */ private Date createTime; /** * This field was generated by MyBatis Generator. This field corresponds to the database column organization.expire_time * @mbggenerated Thu Dec 07 15:18:16 CST 2017 */ private Date expireTime; /** * This field was generated by MyBatis Generator. This field corresponds to the database column organization.description * @mbggenerated Thu Dec 07 15:18:16 CST 2017 */ private String description; private Long townCode; private Long villageCode; @Transient private Integer parentId; @Transient private AreaNames areaNames; } src/main/java/com/moral/mapper/HistoryHourlyMapper.java
@@ -11,4 +11,8 @@ List<Map<String, Object>> getHourlySensorData(Map<String, Object> parameters); List<Map<String, Object>> getHourlySensorDataToCalculateO3(Map<String, Object> parameters); int insertHistoryO3Hourly(@Param("list") List<Map<String, Object>> list); } src/main/java/com/moral/mapper/OrganizationMapper.java
New file @@ -0,0 +1,13 @@ package com.moral.mapper; import java.util.Map; import com.moral.entity.Organization; public interface OrganizationMapper{ Organization getOrganizationByMac(Map<String, Object> parameters); Organization getOrganizationInfoByMac(String mac); } src/main/java/com/moral/service/HistoryHourlyService.java
@@ -7,6 +7,10 @@ int insertHistoryHourly(List<Map<String, Object>> list); List<Map<String, Object>> getHourlySensorData(Map<String,Object> parameters); List<Map<String, Object>> getHourlySensorData(Map<String, Object> parameters); List<Map<String, Object>> getHourlySensorDataToCalculateO3(Map<String, Object> parameters); int insertHistoryO3Hourly(List<Map<String, Object>> list); } src/main/java/com/moral/service/OrganizationService.java
New file @@ -0,0 +1,12 @@ package com.moral.service; import java.util.Map; import com.moral.entity.Organization; public interface OrganizationService { Organization getOrganizationByMac(Map<String, Object> parameters); Organization getOrganizationInfoByMac(String mac); } src/main/java/com/moral/service/impl/HistoryHourlyServiceImpl.java
@@ -26,4 +26,14 @@ return historyHourlyMapper.getHourlySensorData(parameters); } @Override public List<Map<String, Object>> getHourlySensorDataToCalculateO3(Map<String, Object> parameters) { return historyHourlyMapper.getHourlySensorDataToCalculateO3(parameters); } @Override public int insertHistoryO3Hourly(List<Map<String, Object>> list) { return historyHourlyMapper.insertHistoryO3Hourly(list); } } src/main/java/com/moral/service/impl/OrganizationServiceImpl.java
New file @@ -0,0 +1,29 @@ package com.moral.service.impl; import java.util.Map; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.moral.entity.Organization; import com.moral.mapper.OrganizationMapper; import com.moral.service.OrganizationService; @Service public class OrganizationServiceImpl implements OrganizationService { @Resource private OrganizationMapper organizationMapper; @Override public Organization getOrganizationByMac(Map<String, Object> parameters) { return organizationMapper.getOrganizationByMac(parameters); } @Override public Organization getOrganizationInfoByMac(String mac) { return organizationMapper.getOrganizationInfoByMac(mac); } } src/main/java/com/moral/task/HistoryTableInsertTask.java
@@ -4,6 +4,7 @@ import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; @@ -20,12 +21,15 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.moral.entity.Organization; import com.moral.service.DeviceService; import com.moral.service.HistoryDailyService; import com.moral.service.HistoryHourlyService; import com.moral.service.HistoryMinutelyService; import com.moral.service.HistoryService; import com.moral.service.OrganizationService; import com.moral.service.SensorService; import com.moral.util.WeatherUtil; import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.handler.annotation.XxlJob; import com.xxl.job.core.log.XxlJobLogger; @@ -51,6 +55,13 @@ @Resource private HistoryDailyService historyDailyService; @Resource private OrganizationService organizationService; @Resource private WeatherUtil weatherUtil; @XxlJob("historyMinutely") public ReturnT insertHistoryMinutelyTable(String params) { @@ -341,4 +352,107 @@ return returnT; } @XxlJob("historyO3") public ReturnT insertHistoryO3Table(String params) { Map macMap = JSON.parseObject(params); List<String> macList = (List<String>) macMap.get("mac"); String temperatureRatio = macMap.get("temperatureRatio").toString(); String illuminationRatio = macMap.get("illuminationRatio").toString(); String pressureRatio1 = macMap.get("pressureRatio1").toString(); String pressureRatio2 = macMap.get("pressureRatio2").toString(); String humidityRatio = macMap.get("humidityRatio").toString(); String windSpeedRatio = macMap.get("windSpeedRatio").toString(); LocalDateTime time = LocalDateTime.now(); LocalDateTime value = time.truncatedTo(ChronoUnit.HOURS); LocalDateTime illuminationTime = value.minusHours(1); LocalDateTime temperatureTime = value.minusHours(2); LocalDateTime predictionTime = value.plusHours(2); Integer predictionHour = predictionTime.getHour(); Map<String, Object> devices = new HashMap<>(); devices.put("sensorKeys", Arrays.asList("e7", "e12")); devices.put("macList", macList); devices.put("time", illuminationTime); List<Map<String, Object>> hourlyIlluminationData = historyHourlyService.getHourlySensorDataToCalculateO3(devices); XxlJobLogger.log("insertHistoryO3Table-IlluminationDataCount:" + hourlyIlluminationData.size()); devices.put("time", temperatureTime); List<Map<String, Object>> hourlytemperatureData = historyHourlyService.getHourlySensorDataToCalculateO3(devices); XxlJobLogger.log("insertHistoryO3Table-temperatureDataCount:" + hourlytemperatureData.size()); List<Map<String, Object>> hourlyDataList = new ArrayList<>(); BigDecimal temperature; BigDecimal illumination; BigDecimal pressure = new BigDecimal("0"); BigDecimal humidity = new BigDecimal("0"); BigDecimal windSpeed = new BigDecimal("0"); try { for (Map<String, Object> IlluminationData : hourlyIlluminationData) { if (!ObjectUtils.isEmpty(IlluminationData)) { Map<String, Object> hourlyDataMap = new LinkedHashMap<>(); hourlyDataMap.put("mac", IlluminationData.get("mac")); hourlyDataMap.put("time", predictionTime); Organization organizationInfoByMac = organizationService.getOrganizationInfoByMac(IlluminationData.get("mac").toString()); Map<String, Object> parameters = new HashMap<>(); parameters.put("mac", IlluminationData.get("mac")); parameters.put("villageCode", organizationInfoByMac.getVillageCode()); parameters.put("townCode", organizationInfoByMac.getTownCode()); parameters.put("areaCode", organizationInfoByMac.getAreaCode()); parameters.put("cityCode", organizationInfoByMac.getCityCode()); parameters.put("provinceCode", organizationInfoByMac.getProvinceCode()); List<Map<String, Object>> weatherByMac = weatherUtil.getWeatherByMac(parameters); for (Map<String, Object> stringObjectMap : weatherByMac) { Integer mjHour = Integer.valueOf(stringObjectMap.get("hour").toString()); if (predictionHour == mjHour) { pressure = new BigDecimal(stringObjectMap.get("pressure").toString()); humidity = new BigDecimal(stringObjectMap.get("humidity").toString()); windSpeed = new BigDecimal(stringObjectMap.get("windSpeed").toString()); } } JSONObject jo = new JSONObject(true); JSONArray jsonArray = new JSONArray(); for (Map<String, Object> temperatureData : hourlytemperatureData) { if (!ObjectUtils.isEmpty(temperatureData)) { if (IlluminationData.get("mac").equals(temperatureData.get("mac"))) { if (temperatureData.get("e7") != null) { temperature = new BigDecimal(temperatureData.get("e7").toString()); } else { temperature = new BigDecimal("0"); } if (temperatureData.get("e12") != null) { illumination = new BigDecimal(IlluminationData.get("e12").toString()); } else { illumination = new BigDecimal("0"); } BigDecimal bignum1 = temperature.multiply(new BigDecimal(temperatureRatio)); BigDecimal bignum2 = illumination.divide(new BigDecimal(illuminationRatio)); BigDecimal bignum3 = new BigDecimal(pressureRatio1).divide(pressure.subtract(new BigDecimal(pressureRatio2)), 4, BigDecimal.ROUND_HALF_UP); BigDecimal bignum4 = humidity.multiply(new BigDecimal(humidityRatio)); BigDecimal bignum5 = windSpeed.multiply(new BigDecimal(windSpeedRatio)); BigDecimal result = bignum1.add(bignum2).add(bignum3).subtract(bignum4).subtract(bignum5).setScale(4, BigDecimal.ROUND_HALF_UP); jo.put("e15", result); jo.put("e6", humidity); jo.put("e7", temperature); jo.put("e12", illumination); jo.put("e18", windSpeed); jo.put("e28", pressure); } } } jsonArray.add(jo); hourlyDataMap.put("json", jsonArray.get(0).toString()); hourlyDataList.add(hourlyDataMap); } } if (!CollectionUtils.isEmpty(hourlyDataList)) { historyHourlyService.insertHistoryO3Hourly(hourlyDataList); ReturnT returnT = new ReturnT(200, "插入臭氧成功"); return returnT; } } catch (Exception e) { XxlJobLogger.log("insertHistoryO3TableException:" + e.getMessage()); logger.error(e.getMessage()); e.printStackTrace(); } ReturnT returnT = new ReturnT(500, "插入臭氧失败"); return returnT; } } src/main/java/com/moral/util/Dom4jUtils.java
New file @@ -0,0 +1,37 @@ package com.moral.util; import java.io.File; import java.net.URL; import java.util.ArrayList; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.springframework.stereotype.Component; @Component @SuppressWarnings("unchecked") public class Dom4jUtils { private static ArrayList<Element> elements; public static List<Element> readDocument() { try { URL url = com.moral.util.Dom4jUtils.class.getClassLoader().getResource("system" + File.separator + "city.xml"); Document document = new SAXReader().read(url); Element rootElement = document.getRootElement(); elements = (ArrayList<Element>) rootElement.elements(); // System.out.println(elements.size()); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } return elements; } public static List<Element> getDocuments() { return elements; } } src/main/java/com/moral/util/WeatherUtil.java
New file @@ -0,0 +1,71 @@ package com.moral.util; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.dom4j.Element; import org.springframework.stereotype.Component; import org.springframework.util.ObjectUtils; import org.springframework.web.client.RestTemplate; import com.moral.entity.AreaNames; import com.moral.entity.Organization; import com.moral.service.OrganizationService; @Component public class WeatherUtil { @Resource private OrganizationService organizationService; public List<Map<String, Object>> getWeatherByMac(Map<String, Object> parameters) { String areaName = "昆山市"; String parentName = ""; Organization organization = organizationService.getOrganizationByMac(parameters); if (organization.getAreaNames() != null) { AreaNames areaNames = organization.getAreaNames(); if (ObjectUtils.isEmpty(areaNames.getAreaName())) { if (ObjectUtils.isEmpty(areaNames.getCityName())) { areaName = areaNames.getProvinceName(); } else { areaName = areaNames.getCityName(); } } else { areaName = areaNames.getAreaName(); parentName = areaNames.getCityName(); if ("市辖区".equals(areaName)) { areaName = parentName; } } } List<Element> elements = Dom4jUtils.readDocument(); String cityID = "1102"; for (Element element : elements) { String name = element.element("name").getText(); if (areaName.equals(name)) { cityID = element.element("Fid").getText(); break; } if (name.endsWith(areaName)) { if (name.startsWith(parentName)) { cityID = element.element("Fid").getText(); break; } } } RestTemplate restTemplate = new RestTemplate(); Map<String, Object> map = restTemplate.getForObject("http://sapi.7drlb.com/api/mj?cityID={1}&apiKey=forecast24hours", Map.class, cityID); List<Map<String, Object>> mapList = new ArrayList<>(); if (!map.isEmpty()) { if ((Collection<? extends Map<String, Object>>) ((Map) map.get("data")).get("hourly") != null) { mapList.addAll((Collection<? extends Map<String, Object>>) ((Map) map.get("data")).get("hourly")); } } return mapList; } } src/main/resources/mapper/HistoryHourlyMapper.xml
@@ -14,7 +14,7 @@ <select id="getHourlySensorData" resultType="java.util.LinkedHashMap"> SELECT h.mac, <foreach collection="sensorKeys" separator="," item="sensorKey"> <foreach collection="sensorKeys" separator="," item="sensorKey"> AVG(json->'$.${sensorKey}[0]') AS '${sensorKey}', MIN(json->'$.${sensorKey}[1]') AS 'MIN${sensorKey}', MAX(json->'$.${sensorKey}[2]') AS 'MAX${sensorKey}' @@ -24,7 +24,7 @@ WHERE h.time >= #{start} AND h.time <![CDATA[<]]> #{end} AND h.mac in AND h.mac in <foreach collection="macs" open="(" separator="," close=")" item="mac"> #{mac} @@ -33,4 +33,34 @@ h.mac </select> <select id="getHourlySensorDataToCalculateO3" resultType="java.util.LinkedHashMap"> SELECT h.mac, <foreach collection="sensorKeys" separator="," item="sensorKey"> AVG(json->'$.${sensorKey}[0]') AS '${sensorKey}', MIN(json->'$.${sensorKey}[1]') AS 'MIN${sensorKey}', MAX(json->'$.${sensorKey}[2]') AS 'MAX${sensorKey}' </foreach> FROM history_hourly h WHERE h.time = #{time} AND h.mac in <foreach collection="macList" open="(" separator="," close=")" item="mac"> #{mac} </foreach> group by h.mac </select> <insert id="insertHistoryO3Hourly"> insert into history_hourly_o3 values <foreach collection="list" item="map" separator=","> (#{map.mac},#{map.time},#{map.json}) </foreach> </insert> </mapper> src/main/resources/mapper/OrganizationMapper.xml
New file @@ -0,0 +1,86 @@ <?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.mapper.OrganizationMapper"> <resultMap id="BaseResultMap" type="com.moral.entity.Organization"> <id column="id" jdbcType="INTEGER" property="id"/> <result column="name" jdbcType="VARCHAR" property="name"/> <result column="rank" jdbcType="INTEGER" property="rank"/> <result column="province_code" jdbcType="INTEGER" property="provinceCode"/> <result column="city_code" jdbcType="INTEGER" property="cityCode"/> <result column="area_code" jdbcType="INTEGER" property="areaCode"/> <result column="town_code" jdbcType="BIGINT" property="townCode"/> <result column="village_code" jdbcType="BIGINT" property="villageCode"/> <result column="address" jdbcType="VARCHAR" property="address"/> <result column="telephone" jdbcType="VARCHAR" property="telephone"/> <result column="email" jdbcType="VARCHAR" property="email"/> <result column="is_delete" jdbcType="CHAR" property="isDelete"/> <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/> <result column="expire_time" jdbcType="TIMESTAMP" property="expireTime"/> <result column="description" jdbcType="VARCHAR" property="description"/> <!-- 与省市县一对一的关系 --> <association property="areaNames" javaType="com.moral.entity.AreaNames"> <result column="province_name" property="provinceName" jdbcType="VARCHAR"/> <result column="city_name" property="cityName" jdbcType="VARCHAR"/> <result column="area_name" property="areaName" jdbcType="VARCHAR"/> <result column="town_name" property="townName" jdbcType="VARCHAR"/> <result column="village_name" property="villageName" jdbcType="VARCHAR"/> </association> </resultMap> <select id="getOrganizationByMac" resultMap="BaseResultMap"> SELECT * FROM organization o <if test="provinceCode != null" > ,province p </if> <if test="cityCode != null" > ,city c </if> <if test="areaCode != null" > ,area a </if> <if test="townCode != null" > ,town t </if> <if test="villageCode != null" > ,village v </if> <where> <if test="provinceCode != null" > AND o.province_code = p.province_code </if> <if test="cityCode != null" > AND o.city_code = c.city_code </if> <if test="areaCode != null" > AND o.area_code = a.area_code </if> <if test="townCode != null" > AND o.town_code = t.town_code </if> <if test="villageCode != null" > AND o.village_code = v.village_code </if> AND o.id = (select org.id from organization org, monitor_point m, device d where m.id = d.monitor_point_id and m.organization_id = org.id AND d.mac = #{mac}) </where> </select> <select id="getOrganizationInfoByMac" resultMap="BaseResultMap"> select org.* from organization org, monitor_point m, device d where m.id = d.monitor_point_id and m.organization_id = org.id and d.mac = #{mac} </select> </mapper> src/main/resources/system/city.xml
New file Diff too large