Compare commits

...

5 Commits

Author SHA1 Message Date
Kane Wang 2847747f53 完成日志功能! 2022-05-10 16:32:59 +08:00
Kane Wang 1231fe3132 保存进度! 2022-05-10 16:03:40 +08:00
Kane Wang fbe9f8e532 完成打包! 2022-05-10 14:24:07 +08:00
Kane Wang 53849e4f74 保存进度! 2022-05-09 17:03:26 +08:00
Kane Wang d1bcb3b956 保存进度! 2022-05-06 17:33:56 +08:00
10 changed files with 249 additions and 130 deletions

View File

@ -133,4 +133,7 @@ $RECYCLE.BIN/
# Windows shortcuts # Windows shortcuts
*.lnk *.lnk
target/* target/*
logs/*
logs

View File

@ -3,6 +3,7 @@
"key": "fe9fa8eeeb6f4301a92541eed565dd15", "key": "fe9fa8eeeb6f4301a92541eed565dd15",
"query_url": "https://devapi.qweather.com/v7/warning/now?", "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", "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": [ "cities": [
{ {
"city_name": "厦门", "city_name": "厦门",

View File

@ -0,0 +1,10 @@
handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler
.level = INFO
java.util.logging.FileHandler.pattern = ./logs/log_%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.append = true;

View File

@ -23,7 +23,7 @@
<version>4.13.2</version> <version>4.13.2</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <!-- <dependency>
<groupId>commons-beanutils</groupId> <groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId> <artifactId>commons-beanutils</artifactId>
<version>1.9.4</version> <version>1.9.4</version>
@ -53,7 +53,7 @@
<artifactId>json-lib</artifactId> <artifactId>json-lib</artifactId>
<version>2.4</version> <version>2.4</version>
<classifier>jdk15</classifier> <classifier>jdk15</classifier>
</dependency> </dependency> -->
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId> <artifactId>jackson-core</artifactId>
@ -79,10 +79,39 @@
</properties> </properties>
<build> <build>
<defaultGoal>compile</defaultGoal> <defaultGoal>compile</defaultGoal>
<plugins> <plugins>
<!-- <plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M6</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId> <artifactId>maven-assembly-plugin</artifactId>
</plugin> --> <configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>AppMain</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assmenbly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins> </plugins>
</build> </build>
<profiles> <profiles>

View File

@ -2,18 +2,19 @@
* @Author: Kane * @Author: Kane
* @Date: 2022-04-22 10:53:49 * @Date: 2022-04-22 10:53:49
* @LastEditors: Kane * @LastEditors: Kane
* @LastEditTime: 2022-05-06 11:08:07 * @LastEditTime: 2022-05-10 16:28:07
* @FilePath: \DisasterWarning\src\main\java\AppMain.java * @FilePath: \DisasterWarning\src\main\java\AppMain.java
* @Description: 和风天气预警推送厦门太保公众号主程序 * @Description: 和风天气预警推送厦门太保公众号主程序
* *
* Copyright (c) ${2022} by Kane, All Rights Reserved. * Copyright (c) ${2022} by Kane, All Rights Reserved.
*/ */
import com.cpic.xim.config.AppConfigManager;
import com.cpic.xim.config.City; import com.cpic.xim.config.City;
import com.cpic.xim.notify.disaster.QWeatherDisasterWarning; import com.cpic.xim.notify.disaster.QWeatherDisasterWarning;
import com.cpic.xim.notify.disaster.WeatherDisasterWarningGrabber; import com.cpic.xim.notify.disaster.WeatherDisasterWarningGrabber;
import com.cpic.xim.wechat.officalAccount.sendMessage; import com.cpic.xim.wechat.officalAccount.sendMessage;
import com.cpic.xim.config.WeatherDisasterNotifyConfig; import com.cpic.xim.config.WeatherDisasterNotifyConfig;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Vector; import java.util.Vector;
@ -22,7 +23,7 @@ import java.util.logging.*;
public class AppMain public class AppMain
{ {
private final static String LOG_FILE_PATH = "./app.log"; // private final static String LOG_FILE_PATH = "./logs/app%u.log";
public static void main( String[] args ) public static void main( String[] args )
{ {
@ -45,11 +46,10 @@ public class AppMain
return; return;
} }
// 读取配置 // 读取配置
try try
{ {
config = WeatherDisasterNotifyConfig.getConfig(); config = AppConfigManager.getConfig();
} }
catch ( IOException error ) catch ( IOException error )
{ {
@ -66,57 +66,73 @@ public class AppMain
String userKey = config.getKey(); 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 try
{ {
json = WeatherDisasterWarningGrabber.getWeatherDisasterWarningJSON( queryURL, logger.log( Level.INFO, "查询结束,休眠{0}分钟。", config.getQueryInterval() );
userKey, city.getCityCode() ); Thread.sleep( config.getQueryInterval() * 1000 * 60 );
warning = WeatherDisasterWarningGrabber.convertWeatherDisasterWarning( json );
logger.log( Level.INFO, "查询{0}天气预警,结果:{1}。", new Object[]
{ city.getCityName(), json} );
// 判断是否有警报
if ( warning.getWarning().isEmpty() == true)
{
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 ) catch ( InterruptedException error )
{ {
System.out.println( "查询" + city.getCityName() + "出现异常!" ); logger.log( Level.SEVERE, "线程休眠异常,错误信息:{0}", new Object[]
System.out.println( error.getMessage() ); { 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()} );
} }
} }
} }
@ -126,17 +142,15 @@ public class AppMain
*/ */
private static void setRootLogger() throws IOException private static void setRootLogger() throws IOException
{ {
Logger rootLogger = Logger.getLogger( "com.cpicxim" ); LogManager logManager = LogManager.getLogManager();
ConsoleHandler consoleHandler = new ConsoleHandler(); // 使用外部的配置文件
FileHandler fileHandler = new FileHandler( LOG_FILE_PATH ); FileInputStream configFile = new FileInputStream( "./logging.properties" );
SimpleFormatter formatter = new SimpleFormatter(); // 使用jar中的配置文件
// InputStream configFile =
// AppMain.class.getClassLoader().getResourceAsStream( "logging.properties" );
consoleHandler.setFormatter( formatter ); logManager.readConfiguration( configFile );
fileHandler.setFormatter( formatter );
rootLogger.addHandler( consoleHandler );
rootLogger.addHandler( fileHandler );
rootLogger.setUseParentHandlers( false ); Logger.getLogger( "com.cpicxim" );
rootLogger.setLevel( Level.ALL );
} }
} }

View File

@ -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;
}
}

View File

@ -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; package com.cpic.xim.config;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Vector; import java.util.Vector;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
public class WeatherDisasterNotifyConfig 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() public WeatherDisasterNotifyConfig()
{}; {};
@ -106,6 +56,18 @@ public class WeatherDisasterNotifyConfig
return cities; return cities;
} }
public int getQueryInterval()
{
return queryInterval;
}
public void setQueryInterval( int queryInterval )
{
this.queryInterval = queryInterval;
}
public void setCities( Vector<City> cities ) public void setCities( Vector<City> cities )
{ {
this.cities = cities; this.cities = cities;
@ -131,12 +93,24 @@ public class WeatherDisasterNotifyConfig
this.wechatOfficalAccountURL = wechatOfficalAccountURL; this.wechatOfficalAccountURL = wechatOfficalAccountURL;
} }
@JsonProperty( "title")
private String title; private String title;
@JsonProperty( "key")
private String key; private String key;
@JsonProperty( "query_url")
private String queryUrl; private String queryUrl;
@JsonProperty( "query_interval")
private int queryInterval;
@JsonProperty( "wechat_officalaccount_url") @JsonProperty( "wechat_officalaccount_url")
private String wechatOfficalAccountURL; private String wechatOfficalAccountURL;
@JsonProperty( "cities")
private Vector<City> cities; private Vector<City> cities;
@JsonProperty( "notify_stuffs")
private Vector<CpicxmStuff> notifyStuffs; private Vector<CpicxmStuff> notifyStuffs;
} }

View File

@ -0,0 +1,10 @@
handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler
.level = INFO
java.util.logging.FileHandler.pattern = ./logs/log_%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 10
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.append = true;

View File

@ -2,7 +2,7 @@
* @Author: Kane * @Author: Kane
* @Date: 2022-04-22 10:53:49 * @Date: 2022-04-22 10:53:49
* @LastEditors: Kane * @LastEditors: Kane
* @LastEditTime: 2022-05-06 11:14:35 * @LastEditTime: 2022-05-10 11:33:41
* @FilePath: \DisasterWarning\src\test\java\com\cpic\xim\wechat\officalAccount\sendMessageTest.java * @FilePath: \DisasterWarning\src\test\java\com\cpic\xim\wechat\officalAccount\sendMessageTest.java
* @Description: * @Description:
* *
@ -12,7 +12,6 @@ package com.cpic.xim.wechat.officalAccount;
import com.cpic.xim.notify.disaster.QWeatherDisasterWarning; import com.cpic.xim.notify.disaster.QWeatherDisasterWarning;
import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import com.cpic.xim.httpUtil.*; import com.cpic.xim.httpUtil.*;
import java.io.IOException; import java.io.IOException;
@ -61,7 +60,7 @@ public class sendMessageTest
} }
@Test // @Test
public void testSendWeatherDisasterWarning() throws IOException public void testSendWeatherDisasterWarning() throws IOException
{ {
String warningJSON = String warningJSON =