JsonService.java 6.61 KB
package com.viontech.service.impl;


import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;


import com.viontech.model.*;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.viontech.utils.HttpUtils;
import org.springframework.stereotype.Service;

/**
 * @author zhaibolin
 */
//@Configuration
@Service
public class JsonService {

    @Autowired
    private Json jsonclass;

    public SimpleWeather getWeatherInfo(int cityId) throws IOException {
        String json = getWeatherJson(cityId);
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
            WeatherMessage weatherMessage = mapper.readValue(json, WeatherMessage.class);
            String jsons = getAqiJson(cityId);
            ObjectMapper mappers = new ObjectMapper();
            mappers.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            mappers.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            WeatherMessage weatherMessage2 = mappers.readValue(jsons, WeatherMessage.class);

            SimpleWeather simpleWeather = weathers2SimpleWeather(weatherMessage, weatherMessage2);
            return simpleWeather;
        } catch (IOException e) {
            System.out.println("json解析出错!");
            System.out.println(json);
        }
        return null;
    }

    private String getWeatherJson(int cityId) throws IOException {
        String host = jsonclass.getHost1();
        String path = jsonclass.getPath1();
        String method = "POST";
        String appcode = jsonclass.getAppcode();
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appcode);
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("cityId", String.valueOf(cityId));

        HttpResponse response = null;
        try {
            response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return EntityUtils.toString(response.getEntity());

    }

    private String getAqiJson(int cityId) throws IOException {

        String host = jsonclass.getHost2();
        String path = jsonclass.getPath2();
        String method = "POST";
        String appcode = jsonclass.getAppcode();
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appcode);
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("cityId", String.valueOf(cityId));

        HttpResponse response = null;
        try {
            response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//	    return response.toString();
        return EntityUtils.toString(response.getEntity());

    }

    private SimpleWeather weathers2SimpleWeather(WeatherMessage weatherMessage, WeatherMessage weatherMessage2) {
        Data data = weatherMessage.getData();
        if (data == null) {
            return null;
        }
        data.getCity();
        data.getForecast();

        City city = data.getCity();
        if (city == null) {
            return null;
        }
        Forecast forecast = data.getForecast().get(0);
        if (forecast == null) {
            return null;
        }

        SimpleWeather simpleWeather = new SimpleWeather();
        simpleWeather.setType(getWeatherCode(forecast.getConditionDay()));
        simpleWeather.setData_date(forecast.getPredictDate());
        simpleWeather.setHtemp(forecast.getTempDay());
        simpleWeather.setLtemp(forecast.getTempNight());
        if (weatherMessage2.getData().getAqi() == null) {
            simpleWeather.setAqi("0");
        } else {
            simpleWeather.setAqi(weatherMessage2.getData().getAqi().getValue());
        }
        simpleWeather.setFengli(forecast.getWindLevelDay());
        simpleWeather.setFengxiang(forecast.getWindDirDay());

        return simpleWeather;
    }

    private int getWeatherCode(String weather) {
        switch (weather) {
            case "晴":
            case "大部晴朗":
                return 1;
            case "多云":
            case "少云":
                return 2;
            case "阴":
                return 3;
            case "阵雨":
            case "局部阵雨":
            case "小阵雨":
            case "强阵雨":
                return 4;
            case "雷阵雨":
            case "雷电":
            case "雷暴":
                return 5;
            case "雷阵雨伴有冰雹":
            case "冰雹":
            case "冰粒":
            case "冰针":
                return 6;
            case "雨夹雪":
                return 7;
            case "小雨":
            case "小到中雨":
                return 8;
            case "中雨":
            case "雨":
                return 9;
            case "大雨":
            case "中到大雨":
                return 10;
            case "暴雨":
            case "大暴雨":
            case "特大暴雨":
            case "大到暴雨":
                return 11;
            case "阵雪":
            case "小阵雪":
                return 12;
            case "小雪":
                return 13;
            case "中雪":
            case "小到中雪":
            case "雪":
                return 14;
            case "大雪":
                return 15;
            case "暴雪":
                return 16;
            case "雾":
            case "冻雾":
                return 17;
            case "冻雨":
                return 18;
            case "霾":
            case "沙尘暴":
            case "强沙尘暴":
                return 19;
            case "浮尘":
            case "尘卷风":
            case "扬沙":
                return 20;
            default:
                return -1;
        }
    }

}