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

@ -134,3 +134,6 @@ $RECYCLE.BIN/
*.lnk
target/*
logs/*
logs

View File

@ -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": "厦门",

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>
<scope>test</scope>
</dependency>
<dependency>
<!-- <dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
@ -53,7 +53,7 @@
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
</dependency> -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
@ -79,10 +79,39 @@
</properties>
<build>
<defaultGoal>compile</defaultGoal>
<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>
</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>
</build>
<profiles>

View File

@ -2,18 +2,19 @@
* @Author: Kane
* @Date: 2022-04-22 10:53:49
* @LastEditors: Kane
* @LastEditTime: 2022-05-06 11:08:07
* @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;
import com.cpic.xim.wechat.officalAccount.sendMessage;
import com.cpic.xim.config.WeatherDisasterNotifyConfig;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Vector;
@ -22,7 +23,7 @@ import java.util.logging.*;
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 )
{
@ -45,11 +46,10 @@ public class AppMain
return;
}
// 读取配置
try
{
config = WeatherDisasterNotifyConfig.getConfig();
config = AppConfigManager.getConfig();
}
catch ( IOException error )
{
@ -66,6 +66,8 @@ public class AppMain
String userKey = config.getKey();
// 遍历所有城市查询是否有警报有则推送
while (true)
{
for ( City city : cities )
{
try
@ -80,6 +82,8 @@ public class AppMain
// 判断是否有警报
if ( warning.getWarning().isEmpty() == true)
{
logger.log( Level.INFO, "查询{0}天气预警,无警报!。", new Object[]
{ city.getCityName()} );
continue;
}
@ -119,6 +123,18 @@ public class AppMain
{ city.getCityName(), error.getMessage()} );
}
}
try
{
logger.log( Level.INFO, "查询结束,休眠{0}分钟。", config.getQueryInterval() );
Thread.sleep( config.getQueryInterval() * 1000 * 60 );
}
catch ( InterruptedException error )
{
logger.log( Level.SEVERE, "线程休眠异常,错误信息:{0}", new Object[]
{ error.getMessage()} );
}
}
}
/**
@ -126,17 +142,15 @@ 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();
LogManager logManager = LogManager.getLogManager();
// 使用外部的配置文件
FileInputStream configFile = new FileInputStream( "./logging.properties" );
// 使用jar中的配置文件
// InputStream configFile =
// AppMain.class.getClassLoader().getResourceAsStream( "logging.properties" );
consoleHandler.setFormatter( formatter );
fileHandler.setFormatter( formatter );
rootLogger.addHandler( consoleHandler );
rootLogger.addHandler( fileHandler );
logManager.readConfiguration( configFile );
rootLogger.setUseParentHandlers( false );
rootLogger.setLevel( Level.ALL );
Logger.getLogger( "com.cpicxim" );
}
}

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;
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<City> 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<City> cities;
@JsonProperty( "notify_stuffs")
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
* @Date: 2022-04-22 10:53:49
* @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
* @Description:
*
@ -12,7 +12,6 @@ package com.cpic.xim.wechat.officalAccount;
import com.cpic.xim.notify.disaster.QWeatherDisasterWarning;
import org.junit.Test;
import static org.junit.Assert.*;
import com.cpic.xim.httpUtil.*;
import java.io.IOException;
@ -61,7 +60,7 @@ public class sendMessageTest
}
@Test
// @Test
public void testSendWeatherDisasterWarning() throws IOException
{
String warningJSON =