From 2847747f534bc964ec36c099d83dcdeb5b87417e Mon Sep 17 00:00:00 2001 From: Kane Wang Date: Tue, 10 May 2022 16:32:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=97=A5=E5=BF=97=E5=8A=9F?= =?UTF-8?q?=E8=83=BD!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/java/DisasterWarning/config.json | 1 + .../src/main/java/AppMain.java | 131 +++++++++--------- .../com/cpic/xim/config/AppConfigManager.java | 79 +++++++++++ .../config/WeatherDisasterNotifyConfig.java | 96 +++++-------- 4 files changed, 181 insertions(+), 126 deletions(-) create mode 100644 code/java/DisasterWarning/src/main/java/com/cpic/xim/config/AppConfigManager.java diff --git a/code/java/DisasterWarning/config.json b/code/java/DisasterWarning/config.json index 014224c..3795a43 100644 --- a/code/java/DisasterWarning/config.json +++ b/code/java/DisasterWarning/config.json @@ -3,6 +3,7 @@ "key": "fe9fa8eeeb6f4301a92541eed565dd15", "query_url": "https://devapi.qweather.com/v7/warning/now?", "wechat_officalaccount_url": "https://cxxmwx.cpic.com.cn/app/index.php?i=2&c=entry&do=send_group_tpl_api&m=ok_tplmessage", + "query_interval": 10, "cities": [ { "city_name": "厦门", diff --git a/code/java/DisasterWarning/src/main/java/AppMain.java b/code/java/DisasterWarning/src/main/java/AppMain.java index 1c071ff..9373c01 100644 --- a/code/java/DisasterWarning/src/main/java/AppMain.java +++ b/code/java/DisasterWarning/src/main/java/AppMain.java @@ -2,12 +2,13 @@ * @Author: Kane * @Date: 2022-04-22 10:53:49 * @LastEditors: Kane - * @LastEditTime: 2022-05-10 16:01:13 + * @LastEditTime: 2022-05-10 16:28:07 * @FilePath: \DisasterWarning\src\main\java\AppMain.java * @Description: 和风天气预警推送厦门太保公众号主程序! * * Copyright (c) ${2022} by Kane, All Rights Reserved. */ +import com.cpic.xim.config.AppConfigManager; import com.cpic.xim.config.City; import com.cpic.xim.notify.disaster.QWeatherDisasterWarning; import com.cpic.xim.notify.disaster.WeatherDisasterWarningGrabber; @@ -48,7 +49,7 @@ public class AppMain // 读取配置 try { - config = WeatherDisasterNotifyConfig.getConfig(); + config = AppConfigManager.getConfig(); } catch ( IOException error ) { @@ -65,59 +66,73 @@ public class AppMain String userKey = config.getKey(); // 遍历所有城市,查询是否有警报,有则推送。 - for ( City city : cities ) + while (true) { + for ( City city : cities ) + { + try + { + json = WeatherDisasterWarningGrabber.getWeatherDisasterWarningJSON( queryURL, + userKey, city.getCityCode() ); + warning = WeatherDisasterWarningGrabber.convertWeatherDisasterWarning( json ); + + logger.log( Level.INFO, "查询{0}天气预警,结果:{1}。", new Object[] + { city.getCityName(), json} ); + + // 判断是否有警报 + if ( warning.getWarning().isEmpty() == true) + { + logger.log( Level.INFO, "查询{0}天气预警,无警报!。", new Object[] + { city.getCityName()} ); + continue; + } + + logger.log( Level.INFO, "查询{0}天气预警,发送日志。", new Object[] + { city.getCityName()} ); + + sendMessage.sendWeatherDisasterWarning( config.getWechatOfficalAccountURL(), + warning ); + + logger.log( Level.INFO, "{0}天气预警,日志发送成功。", new Object[] + { city.getCityName()} ); + + sendMessage.saveWeatherDisasterWarning( city.getCityName(), warning ); + } + catch ( IOException error ) + { + System.out.println( "查询" + city.getCityName() + "出现异常!" ); + System.out.println( error.getMessage() ); + + logger.log( Level.SEVERE, "查询 {0} 出现异常:{1}。", new Object[] + { city.getCityName(), error.getMessage()} ); + } + catch ( SQLException error ) + { + logger.log( Level.SEVERE, "查询 {0} 写入数据库失败:{1}。", new Object[] + { city.getCityName(), error.getMessage()} ); + } + catch ( ClassNotFoundException error ) + { + logger.log( Level.SEVERE, "查询 {0} 加载oracle驱动失败:{1}。", new Object[] + { city.getCityName(), error.getMessage()} ); + + } + catch ( Exception error ) + { + logger.log( Level.SEVERE, "查询 {0} 出现未知错误:{1}。", new Object[] + { city.getCityName(), error.getMessage()} ); + } + } + try { - json = WeatherDisasterWarningGrabber.getWeatherDisasterWarningJSON( queryURL, - userKey, city.getCityCode() ); - warning = WeatherDisasterWarningGrabber.convertWeatherDisasterWarning( json ); - - logger.log( Level.INFO, "查询{0}天气预警,结果:{1}。", new Object[] - { city.getCityName(), json} ); - - // 判断是否有警报 - if ( warning.getWarning().isEmpty() == true) - { - logger.log( Level.INFO, "查询{0}天气预警,无警报!。", new Object[] - { city.getCityName()} ); - continue; - } - - logger.log( Level.INFO, "查询{0}天气预警,发送日志。", new Object[] - { city.getCityName()} ); - - sendMessage.sendWeatherDisasterWarning( config.getWechatOfficalAccountURL(), - warning ); - - logger.log( Level.INFO, "{0}天气预警,日志发送成功。", new Object[] - { city.getCityName()} ); - - sendMessage.saveWeatherDisasterWarning( city.getCityName(), warning ); + logger.log( Level.INFO, "查询结束,休眠{0}分钟。", config.getQueryInterval() ); + Thread.sleep( config.getQueryInterval() * 1000 * 60 ); } - catch ( IOException error ) + catch ( InterruptedException error ) { - System.out.println( "查询" + city.getCityName() + "出现异常!" ); - System.out.println( error.getMessage() ); - - logger.log( Level.SEVERE, "查询 {0} 出现异常:{1}。", new Object[] - { city.getCityName(), error.getMessage()} ); - } - catch ( SQLException error ) - { - logger.log( Level.SEVERE, "查询 {0} 写入数据库失败:{1}。", new Object[] - { city.getCityName(), error.getMessage()} ); - } - catch ( ClassNotFoundException error ) - { - logger.log( Level.SEVERE, "查询 {0} 加载oracle驱动失败:{1}。", new Object[] - { city.getCityName(), error.getMessage()} ); - - } - catch ( Exception error ) - { - logger.log( Level.SEVERE, "查询 {0} 出现未知错误:{1}。", new Object[] - { city.getCityName(), error.getMessage()} ); + logger.log( Level.SEVERE, "线程休眠异常,错误信息:{0}", new Object[] + { error.getMessage()} ); } } } @@ -127,20 +142,6 @@ public class AppMain */ private static void setRootLogger() throws IOException { - // Logger rootLogger = Logger.getLogger( "com.cpicxim" ); - // ConsoleHandler consoleHandler = new ConsoleHandler(); - // FileHandler fileHandler = new FileHandler( LOG_FILE_PATH ); - // SimpleFormatter formatter = new SimpleFormatter(); - - // consoleHandler.setFormatter( formatter ); - // fileHandler.setFormatter( formatter ); - - // rootLogger.addHandler( consoleHandler ); - // rootLogger.addHandler( fileHandler ); - - // rootLogger.setUseParentHandlers( false ); - // rootLogger.setLevel( Level.ALL ); - LogManager logManager = LogManager.getLogManager(); // 使用外部的配置文件。 FileInputStream configFile = new FileInputStream( "./logging.properties" ); @@ -150,6 +151,6 @@ public class AppMain logManager.readConfiguration( configFile ); - Logger rootLogger = Logger.getLogger( "com.cpicxim" ); + Logger.getLogger( "com.cpicxim" ); } } diff --git a/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/AppConfigManager.java b/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/AppConfigManager.java new file mode 100644 index 0000000..69fcfa0 --- /dev/null +++ b/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/AppConfigManager.java @@ -0,0 +1,79 @@ +/* + * @Author: Kane + * @Date: 2022-05-10 16:06:14 + * @LastEditors: Kane + * @LastEditTime: 2022-05-10 16:09:07 + * @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\config\AppConfigManager.java + * @Description: + * + * Copyright (c) ${2022} by Kane, All Rights Reserved. + */ + +package com.cpic.xim.config; + +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.FileInputStream; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.PropertyNamingStrategy; + +public class AppConfigManager +{ + private static final int BUFFER_SIZE = 1024; + private static final String CONFIG_FILE_CHARSET = "UTF-8"; + private static final String CONFIG_FILE_PATH = "./config.json"; + private static WeatherDisasterNotifyConfig appConfig = null; + + public static WeatherDisasterNotifyConfig getConfig() throws IOException + + { + if ( appConfig != null) + { + return appConfig; + } + + ObjectMapper mapper = new ObjectMapper(); + FileInputStream configFile = null; + InputStreamReader in = null; + StringBuffer json = null; + char[] buffer = new char[BUFFER_SIZE]; + + // 设置json属性 + mapper.setPropertyNamingStrategy( PropertyNamingStrategy.SNAKE_CASE ); + + try + { + configFile = new FileInputStream( CONFIG_FILE_PATH ); + in = new InputStreamReader( configFile, CONFIG_FILE_CHARSET ); + json = new StringBuffer(); + + int length = in.read( buffer ); + + while (length != -1) + { + json.append( buffer ); + + length = in.read( buffer ); + } + + appConfig = mapper.readValue( json.toString(), WeatherDisasterNotifyConfig.class ); + } + finally + { + if ( configFile != null) + { + try + { + configFile.close(); + } + catch ( IOException e ) + { + e.printStackTrace(); + } + } + } + + return appConfig; + } +} diff --git a/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/WeatherDisasterNotifyConfig.java b/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/WeatherDisasterNotifyConfig.java index cafd3fe..dd1892d 100644 --- a/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/WeatherDisasterNotifyConfig.java +++ b/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/WeatherDisasterNotifyConfig.java @@ -1,72 +1,22 @@ +/* + * @Author: Kane + * @Date: 2022-04-24 10:21:46 + * @LastEditors: Kane + * @LastEditTime: 2022-05-10 16:16:28 + * @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\config\WeatherDisasterNotifyConfig.java + * @Description: + * + * Copyright (c) ${2022} by Kane, All Rights Reserved. + */ package com.cpic.xim.config; -import java.io.InputStreamReader; -import java.io.FileInputStream; -import java.io.IOException; import java.util.Vector; import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.PropertyNamingStrategy; + public class WeatherDisasterNotifyConfig { - private static final int BUFFER_SIZE = 1024; - private static final String CONFIG_FILE_CHARSET = "UTF-8"; - private static final String CONFIG_FILE_PATH = "./config.json"; - private static WeatherDisasterNotifyConfig appConfig = null; - - public static WeatherDisasterNotifyConfig getConfig() throws IOException - - { - if ( appConfig != null) - { - return appConfig; - } - - ObjectMapper mapper = new ObjectMapper(); - FileInputStream configFile = null; - InputStreamReader in = null; - StringBuffer json = null; - char[] buffer = new char[BUFFER_SIZE]; - - // 设置json属性 - mapper.setPropertyNamingStrategy( PropertyNamingStrategy.SNAKE_CASE ); - - try - { - configFile = new FileInputStream( CONFIG_FILE_PATH ); - in = new InputStreamReader( configFile, CONFIG_FILE_CHARSET ); - json = new StringBuffer(); - - int length = in.read( buffer ); - - while (length != -1) - { - json.append( buffer ); - - length = in.read( buffer ); - } - - appConfig = mapper.readValue( json.toString(), WeatherDisasterNotifyConfig.class ); - } - finally - { - if ( configFile != null) - { - try - { - configFile.close(); - } - catch ( IOException e ) - { - e.printStackTrace(); - } - } - } - - return appConfig; - } public WeatherDisasterNotifyConfig() {}; @@ -106,6 +56,18 @@ public class WeatherDisasterNotifyConfig return cities; } + + public int getQueryInterval() + { + return queryInterval; + } + + public void setQueryInterval( int queryInterval ) + { + this.queryInterval = queryInterval; + } + + public void setCities( Vector cities ) { this.cities = cities; @@ -131,12 +93,24 @@ public class WeatherDisasterNotifyConfig this.wechatOfficalAccountURL = wechatOfficalAccountURL; } + @JsonProperty( "title") private String title; + + @JsonProperty( "key") private String key; + + @JsonProperty( "query_url") private String queryUrl; + @JsonProperty( "query_interval") + private int queryInterval; + @JsonProperty( "wechat_officalaccount_url") private String wechatOfficalAccountURL; + + @JsonProperty( "cities") private Vector cities; + + @JsonProperty( "notify_stuffs") private Vector notifyStuffs; }