/*
 * @Author: Kane
 * @Date: 2022-03-16 18:10:58
 * @LastEditors: Kane
 * @LastEditTime: 2022-03-18 00:37:56
 * @FilePath: \天气灾害预警\src\main\java\AppMain.java
 * @Description:
 *
 * Copyright (c) ${2022} by Kane, All Rights Reserved.
 */

import com.cpic.xim.config.City;
import com.cpic.xim.notify.disaster.QWeatherDisasterWarning;
import com.cpic.xim.notify.disaster.WeatherDisasterWarningGrabber;
import com.cpic.xim.config.WeatherDisasterNotifyConfig;

import java.io.IOException;
import java.util.Vector;

public class AppMain
{
    private static final String CONFIG_FILE_PATH = "./config.json";

    public static void main( String[] args )
    {
        String                      cityCode = "101230201";
        String                      json;
        QWeatherDisasterWarning     warning  = null;
        WeatherDisasterNotifyConfig config   = null;

        //读取配置
        try
        {
            config = WeatherDisasterNotifyConfig.load( CONFIG_FILE_PATH );
        }
        catch ( IOException error )
        {
            System.out.println( "读取配置文件失败！" );
            System.out.println( error.getMessage() );

            return;
        }

        Vector<City> cities   = config.getCities();
        String       queryURL = config.getQueryUrl();
        String       userKey  = config.getKey();

        //遍历所有城市，查询是否有警报，有则推送。
        for ( City city : cities )
        {
            try
            {
                json    = WeatherDisasterWarningGrabber.getWeatherDisasterWarningJSON( queryURL,
                                                                                       userKey,
                                                                                       city.getCityCode() );
                warning = WeatherDisasterWarningGrabber.convertWeatherDisasterWarning( json );

            }
            catch ( IOException error )
            {
                System.out.println( "查询" + city.getCityName() + "出现异常！");
                System.out.println( error.getMessage() );

                continue;
            }
        }
    }
}