lizijie
2019-07-10 e97c393e23f4c015a44b5fef5fd26ae288bc6c0f
天气预报
1 files added
8 files modified
207 ■■■■■ changed files
pom.xml 27 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/controller/ScreenController.java 61 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/mapper/MonitorPointMapper.java 1 ●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/MonitorPointService.java 2 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/WeatherService.java 3 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/impl/MonitorPointServiceImpl.java 6 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/impl/WeatherServiceImpl.java 58 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/util/TempAllocationUtils.java 44 ●●●●● patch | view | raw | blame | history
src/main/resources/mapper/MonitorPointMapper.xml 5 ●●●●● patch | view | raw | blame | history
pom.xml
@@ -203,6 +203,33 @@
            <artifactId>aviator</artifactId>
            <version>4.2.0</version>
        </dependency>
        <!-- 获取天气信息相关依赖 -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.2.1</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-util</artifactId>
            <version>9.3.7.v20160115</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.5</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
src/main/java/com/moral/controller/ScreenController.java
@@ -7,6 +7,9 @@
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
@@ -23,6 +26,7 @@
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.http.HttpResponse;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
@@ -90,6 +94,7 @@
import com.moral.service.SensorService;
import com.moral.service.SensorUnitService;
import com.moral.service.WeatherService;
import com.moral.util.TempAllocationUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
@@ -975,6 +980,7 @@
    public ResultBean<List<Map<String, Object>>> AIForecast (HttpServletRequest request) throws Exception {
        Map<String, Object> parameters = getParametersStartingWith(request, null);
        //System.out.println(parameters);
        String monitorPoint = (String) parameters.get("monitorPoint");
        String intoTime = parameters.get("time").toString();
        String[] timeArray = intoTime.split("-");
        //System.out.println(timeArray);
@@ -997,6 +1003,15 @@
        // monitorPointService.isCompensateCalculation(parameters);
        List<Map<String, Object>> list = historyMinutelyService.getMonitorPointOrDeviceAvgData(parameters);
        
        Map<String, Object> mpMap = new HashMap<>();
        mpMap = monitorPointService.selectAllById(monitorPoint);
        //生命一个map,用来存放查询天气信息是需要的参数
        Map<String, Object> weatherParameters = new HashMap<>();
        weatherParameters.put("organizationId", mpMap.get("organization_id"));
        weatherParameters.put("time", intoTime);
        //获取天气信息
        Map<String, Object> weatherMap = weatherService.get15DayWeatherDataByRegion(weatherParameters);
        Map<String, Object> tempMap = TempAllocationUtils.tempAllocation(weatherMap);
        //获取单位信息
          Map<String, Map<String, Object>> sensorUnitMap = sensorUnitService.getSensorsByMonitPointId(monitor_point_id);
@@ -1004,6 +1019,7 @@
            String time = map.get("time").toString();
            time = time.substring(time.length() - 2);
            map.put("time", Integer.valueOf(time));
            map.put("temp", tempMap.get(map.get("time").toString()));
            List<Number> values = new ArrayList<Number>();
            List<String> units = new ArrayList<String>();
            for (String string : sensorKeys) {
@@ -1039,5 +1055,50 @@
        }
        return new ResultBean<List<Map<String, Object>>>(list);
    }
    @GetMapping("getWeather")
    @ApiOperation(value = "获取天气信息", notes = "获取天气信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "organizationId", defaultValue = "5", value = "登录账号的组织id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "time", value = "时间,天(格式:2018-03-06)", required = true, paramType = "query", dataType = "String"),
    })
    public ResultBean<Map<String, Object>> getWeather(HttpServletRequest request) throws Exception {
        Map<String, Object> parameters = getParametersStartingWith(request, null);
        Map<String, Object> map = weatherService.get15DayWeatherDataByRegion(parameters);
        /*Map<String, Object> tempMap = new HashMap<>();
        Double tempDay = Double.parseDouble(map.get("tempDay").toString());
        Double tempNight = Double.parseDouble(map.get("tempNight").toString());
        Double time5 = tempNight;
        Double time13 = tempDay;
        Double difference = tempDay-tempNight;
        Double dif_8 = difference/8;
        Double dif_16 = difference/16;
        tempMap.put("5", time5);
        tempMap.put("13", time13);
        String time = null;
        Double t = time5;
        for(int i=4;i>=0;i--) {
            time = ""+i;
            t = t+dif_16;
            String tempLast = ""+Math.round(t);
            tempMap.put(time, tempLast);
        }
        Double y = time5;
        for(int i=6;i<13;i++) {
            time = ""+i;
            y = y+dif_8;
            String tempLast = ""+Math.round(y);
            tempMap.put(time, tempLast);
        }
        Double z = time13;
        for(int i=14;i<=23;i++) {
            time = ""+i;
            z = z-dif_16;
            String tempLast = ""+Math.round(z);
            tempMap.put(time, tempLast);
        }*/
        Map<String, Object> tempMap = TempAllocationUtils.tempAllocation(map);
        return new ResultBean<Map<String, Object>>(tempMap);
    }
}
src/main/java/com/moral/mapper/MonitorPointMapper.java
@@ -12,4 +12,5 @@
    List<MonitorPoint> selectByMap(Map<String, Object> params);
    List<Integer> selectOrganizationIds(int id);
    List<Integer> selectVersionsById(int id);
    Map<String, Object> selectAllById(int id);
}
src/main/java/com/moral/service/MonitorPointService.java
@@ -39,4 +39,6 @@
    Collection<Object> getDevicesStateByRegion(Map<String, Object> parameters);
    void isCompensateCalculation(Map<String, Object> parameters);
    Map<String, Object> selectAllById(String id);
}
src/main/java/com/moral/service/WeatherService.java
@@ -1,9 +1,12 @@
package com.moral.service;
import java.util.List;
import java.util.Map;
public interface WeatherService {
    Map<String, Object> getWeatherDataByRegion(Map<String, Object> parameters);
    Map<String, Object> get15DayWeatherDataByRegion(Map<String, Object> parameters);
}
src/main/java/com/moral/service/impl/MonitorPointServiceImpl.java
@@ -358,4 +358,10 @@
        }
        
    }
    @Override
    public Map<String, Object> selectAllById(String id) {
        int id2 = Integer.parseInt(id);
        Map<String, Object> map = monitorPointMapper.selectAllById(id2);
        return map;
    }
}
src/main/java/com/moral/service/impl/WeatherServiceImpl.java
@@ -1,5 +1,8 @@
package com.moral.service.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -68,5 +71,60 @@
        result.putAll((Map<? extends String, ? extends Object>) ((Map)map.get("data")).get("condition"));
        return result;
    }
    @Override
    public Map<String, Object> get15DayWeatherDataByRegion(Map<String, Object> parameters) {
        Object organizationId = parameters.get("organizationId");
        ValidateUtil.notNull(organizationId, "param.is.null");
        String areaName = "昆山市";
        String parentName = "";
        Organization organization = organizationService.getOrganizationById(Integer.valueOf(parameters.get("organizationId").toString()));
        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=aqi", Map.class,cityID);
        List<Map<String, Object>> result = new ArrayList<>();
        map =  restTemplate.getForObject("http://sapi.7drlb.com/api/mj?cityID={1}&apiKey=forecast15days", Map.class,cityID);
        result.addAll((Collection<? extends Map<String, Object>>) ((Map)map.get("data")).get("forecast"));
        for (Map<String, Object> map2 : result) {
            Map<String, Object> returnMap = new HashMap<>();
            String predictDate = map2.get("predictDate").toString();
            String time = parameters.get("time").toString();
            if (predictDate.equals(time)) {
                returnMap = map2;
                return returnMap;
            }
        }
        return null;
    }
}
src/main/java/com/moral/util/TempAllocationUtils.java
New file
@@ -0,0 +1,44 @@
package com.moral.util;
import java.util.HashMap;
import java.util.Map;
public class TempAllocationUtils {
    public static Map<String, Object> tempAllocation(Map<String, Object> map){
        Map<String, Object> tempMap = new HashMap<>();
        Double tempDay = Double.parseDouble(map.get("tempDay").toString());
        Double tempNight = Double.parseDouble(map.get("tempNight").toString());
        Double time5 = tempNight;
        Double time13 = tempDay;
        Double difference = tempDay-tempNight;
        Double dif_8 = difference/8;
        Double dif_16 = difference/16;
        tempMap.put("5", time5);
        tempMap.put("13", time13);
        String time = null;
        Double t = time5;
        for(int i=4;i>=0;i--) {
            time = ""+i;
            t = t+dif_16;
            String tempLast = ""+Math.round(t);
            tempMap.put(time, tempLast);
        }
        Double y = time5;
        for(int i=6;i<13;i++) {
            time = ""+i;
            y = y+dif_8;
            String tempLast = ""+Math.round(y);
            tempMap.put(time, tempLast);
        }
        Double z = time13;
        for(int i=14;i<=23;i++) {
            time = ""+i;
            z = z-dif_16;
            String tempLast = ""+Math.round(z);
            tempMap.put(time, tempLast);
        }
        return tempMap;
    }
}
src/main/resources/mapper/MonitorPointMapper.xml
@@ -143,4 +143,9 @@
            right join monitor_point mpt on mpt.id = dev.monitor_point_id
            where mpt.id = #{id}  ORDER BY version DESC
    </select>
    <select id="selectAllById" resultType="java.util.Map">
            SELECT *
            FROM monitor_point
            WHERE id = #{id}
    </select>
</mapper>