和风天气json结构变更。

This commit is contained in:
Kane Wang 2022-06-07 14:03:35 +08:00
parent db8f9e9c05
commit 3b1844c994
4 changed files with 207 additions and 166 deletions

View File

@ -9,6 +9,8 @@
<groupId>com.oracle</groupId> <groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId> <artifactId>ojdbc8</artifactId>
<version>19.3.0.0.0</version> <version>19.3.0.0.0</version>
<!-- <scope>system</scope>
<systemPath>${project.basedir}/lib/ojdbc8.jar</systemPath> -->
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId> <groupId>org.apache.httpcomponents</groupId>

View File

@ -1,8 +1,9 @@
/* /*
* @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-12 09:52:45 * @LastEditTime: 2022-06-07 13:16:35
* @FilePath: \DisasterWarning\src\main\java\AppMain.java * @FilePath: \DisasterWarning\src\main\java\AppMain.java
* @Description: 和风天气预警推送厦门太保公众号主程序 * @Description: 和风天气预警推送厦门太保公众号主程序
* *
@ -21,43 +22,34 @@ import java.sql.SQLException;
import java.util.Vector; import java.util.Vector;
import java.util.logging.*; import java.util.logging.*;
public class AppMain {
public class AppMain
{
// private final static String LOG_FILE_PATH = "./logs/app%u.log"; // private final static String LOG_FILE_PATH = "./logs/app%u.log";
public static void main( String[] args ) public static void main(String[] args) {
{
String json; String json;
WeatherDisasterNotifyConfig config = null; WeatherDisasterNotifyConfig config = null;
QWeatherDisasterWarning warning = null; QWeatherDisasterWarning warning = null;
Logger logger = null; Logger logger = null;
// 配置logger // 配置logger
try try {
{
setRootLogger(); setRootLogger();
logger = Logger.getLogger( "com.cpicxim" ); logger = Logger.getLogger("com.cpicxim");
} } catch (IOException error) {
catch ( IOException error ) System.out.println("配置logger失败原因" + error.getMessage());
{
System.out.println( "配置logger失败原因" + error.getMessage() );
return; return;
} }
// 读取配置 // 读取配置
try try {
{
config = AppConfigManager.getConfig(); config = AppConfigManager.getConfig();
} } catch (IOException error) {
catch ( IOException error ) System.out.println("读取配置文件失败!");
{ System.out.println(error.getMessage());
System.out.println( "读取配置文件失败!" );
System.out.println( error.getMessage() );
logger.log( Level.SEVERE, "读取配置文件失败:{0}", error.getMessage() ); logger.log(Level.SEVERE, "读取配置文件失败:{0}", error.getMessage());
return; return;
} }
@ -67,90 +59,68 @@ public class AppMain
String userKey = config.getKey(); String userKey = config.getKey();
// 遍历所有城市查询是否有警报有则推送 // 遍历所有城市查询是否有警报有则推送
while (true) while (true) {
{ for (City city : cities) {
for ( City city : cities ) try {
{ json = WeatherDisasterWarningGrabber.getWeatherDisasterWarningJSON(queryURL,
try userKey, city.getCityCode());
{ warning = WeatherDisasterWarningGrabber.convertWeatherDisasterWarning(json);
json = WeatherDisasterWarningGrabber.getWeatherDisasterWarningJSON( queryURL,
userKey, city.getCityCode() );
warning = WeatherDisasterWarningGrabber.convertWeatherDisasterWarning( json );
logger.log( Level.INFO, "查询{0}天气预警,结果:{1}。", new Object[] logger.log(Level.INFO, "查询{0}天气预警,结果:{1}。", new Object[] { city.getCityName(), json });
{ city.getCityName(), json} );
Vector<QWeatherDisasterWarningItem> warningItems = warning.getWarning(); Vector<QWeatherDisasterWarningItem> warningItems = warning.getWarning();
// 判断是否有警报没有警报就结束当前城市的处理 // 判断是否有警报没有警报就结束当前城市的处理
if ( warningItems.isEmpty() == true) if (warningItems.isEmpty() == true) {
{ logger.log(Level.INFO, "查询{0}天气预警,无警报!。", new Object[] { city.getCityName() });
logger.log( Level.INFO, "查询{0}天气预警,无警报!。", new Object[]
{ city.getCityName()} );
continue; continue;
} }
// 有警报就遍历警报数组 // 有警报就遍历警报数组
for ( QWeatherDisasterWarningItem item : warningItems ) for (QWeatherDisasterWarningItem item : warningItems) {
{
// 先检查是否已经发送过 // 先检查是否已经发送过
if ( sendMessage.checkWarningHasSended( item.getId() ) == false) if (sendMessage.checkWarningHasSended(item.getId()) == false) {
{ logger.log(Level.INFO, "查询{0}天气预警ID:{1},已有发送记录,跳过。",
logger.log( Level.INFO, "查询{0}天气预警ID:{1},已有发送记录,跳过。", new Object[] new Object[] { city.getCityName(), item.getId() });
{ city.getCityName(), item.getId()} );
continue; continue;
} }
// 没有发送过 // 没有发送过
logger.log( Level.INFO, "查询{0}天气预警,发送日志。", new Object[] logger.log(Level.INFO, "查询{0}天气预警,发送日志。", new Object[] { city.getCityName() });
{ city.getCityName()} );
sendMessage.sendWeatherDisasterWarning( config.getWechatOfficalAccountURL(), sendMessage.sendWeatherDisasterWarning(config.getWechatOfficalAccountURL(),
item ); item);
logger.log( Level.INFO, "{0}天气预警,日志发送成功。", new Object[] logger.log(Level.INFO, "{0}天气预警,日志发送成功。", new Object[] { city.getCityName() });
{ city.getCityName()} );
// 将发送的警报保存起来 // 将发送的警报保存起来
sendMessage.saveWeatherDisasterWarning( city.getCityName(), item ); sendMessage.saveWeatherDisasterWarning(city.getCityName(), item);
} }
} } catch (IOException error) {
catch ( IOException error ) System.out.println("查询" + city.getCityName() + "出现异常!");
{ System.out.println(error.getMessage());
System.out.println( "查询" + city.getCityName() + "出现异常!" );
System.out.println( error.getMessage() );
logger.log( Level.SEVERE, "查询 {0} 出现异常:{1}。", new Object[] logger.log(Level.SEVERE, "查询 {0} 出现异常:{1}。",
{ city.getCityName(), error.getMessage()} ); new Object[] { city.getCityName(), error.getMessage() });
} } catch (SQLException error) {
catch ( SQLException error ) logger.log(Level.SEVERE, "查询 {0} 写入数据库失败:{1}。",
{ new Object[] { city.getCityName(), error.getMessage() });
logger.log( Level.SEVERE, "查询 {0} 写入数据库失败:{1}。", new Object[] } catch (ClassNotFoundException error) {
{ city.getCityName(), error.getMessage()} ); logger.log(Level.SEVERE, "查询 {0} 加载oracle驱动失败:{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) {
catch ( Exception error ) logger.log(Level.SEVERE, "查询 {0} 出现未知错误:{1}。",
{ new Object[] { city.getCityName(), error.getMessage() });
logger.log( Level.SEVERE, "查询 {0} 出现未知错误:{1}。", new Object[]
{ city.getCityName(), error.getMessage()} );
} }
} }
try try {
{ logger.log(Level.INFO, "查询结束,休眠{0}分钟。", config.getQueryInterval());
logger.log( Level.INFO, "查询结束,休眠{0}分钟。", config.getQueryInterval() ); Thread.sleep(config.getQueryInterval() * 1000 * 60);
Thread.sleep( config.getQueryInterval() * 1000 * 60 ); } catch (InterruptedException error) {
} logger.log(Level.SEVERE, "线程休眠异常,错误信息:{0}", new Object[] { error.getMessage() });
catch ( InterruptedException error )
{
logger.log( Level.SEVERE, "线程休眠异常,错误信息:{0}", new Object[]
{ error.getMessage()} );
} }
} }
} }
@ -158,17 +128,16 @@ public class AppMain
/** /**
* 设置JUL的logger * 设置JUL的logger
*/ */
private static void setRootLogger() throws IOException private static void setRootLogger() throws IOException {
{
LogManager logManager = LogManager.getLogManager(); LogManager logManager = LogManager.getLogManager();
// 使用外部的配置文件 // 使用外部的配置文件
FileInputStream configFile = new FileInputStream( "./logging.properties" ); FileInputStream configFile = new FileInputStream("./logging.properties");
// 使用jar中的配置文件 // 使用jar中的配置文件
// InputStream configFile = // InputStream configFile =
// AppMain.class.getClassLoader().getResourceAsStream( "logging.properties" ); // AppMain.class.getClassLoader().getResourceAsStream( "logging.properties" );
logManager.readConfiguration( configFile ); logManager.readConfiguration(configFile);
Logger.getLogger( "com.cpicxim" ); Logger.getLogger("com.cpicxim");
} }
} }

View File

@ -7,21 +7,21 @@ import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
//import com.fasterxml.jackson.annotation.JsonIgnore; //import com.fasterxml.jackson.annotation.JsonIgnore;
public class QWeatherDisasterWarningItem public class QWeatherDisasterWarningItem {
{ public QWeatherDisasterWarningItem() {
public QWeatherDisasterWarningItem() }
{}
public QWeatherDisasterWarningItem( String id, String sender, Date pubTime, String title, public QWeatherDisasterWarningItem(String id, String sender, Date pubTime, String title, String status,
String status, String level, String type, String typeName, String text, String related, String level, String severity, String severityColor, String type, String typeName, String text,
String urgency, String certainty) String related, String urgency, String certainty) {
{
this.id = id; this.id = id;
this.sender = sender; this.sender = sender;
this.pubTime = pubTime; this.pubTime = pubTime;
this.title = title; this.title = title;
this.status = status; this.status = status;
this.level = level; this.level = level;
this.severity = severity;
this.severityColor = severityColor;
this.type = type; this.type = type;
this.typeName = typeName; this.typeName = typeName;
this.text = text; this.text = text;
@ -31,163 +31,233 @@ public class QWeatherDisasterWarningItem
} }
@Override @Override
public boolean equals( Object o ) public int hashCode() {
{ final int prime = 31;
if ( this == o) int result = 1;
{ result = prime * result + ((certainty == null) ? 0 : certainty.hashCode());
return true; result = prime * result + ((id == null) ? 0 : id.hashCode());
} result = prime * result + ((level == null) ? 0 : level.hashCode());
if ( o == null || getClass() != o.getClass()) result = prime * result + ((pubTime == null) ? 0 : pubTime.hashCode());
{ result = prime * result + ((related == null) ? 0 : related.hashCode());
return false; result = prime * result + ((sender == null) ? 0 : sender.hashCode());
} result = prime * result + ((severity == null) ? 0 : severity.hashCode());
QWeatherDisasterWarningItem that = (QWeatherDisasterWarningItem) o; result = prime * result + ((severityColor == null) ? 0 : severityColor.hashCode());
return id.equals( that.id ) && Objects.equals( sender, that.sender ) result = prime * result + ((status == null) ? 0 : status.hashCode());
&& Objects.equals( pubTime, that.pubTime ) && Objects.equals( title, that.title ) result = prime * result + ((text == null) ? 0 : text.hashCode());
&& Objects.equals( status, that.status ) && Objects.equals( level, that.level ) result = prime * result + ((title == null) ? 0 : title.hashCode());
&& Objects.equals( type, that.type ) && Objects.equals( typeName, that.typeName ) result = prime * result + ((type == null) ? 0 : type.hashCode());
&& Objects.equals( text, that.text ) && Objects.equals( related, that.related ) result = prime * result + ((typeName == null) ? 0 : typeName.hashCode());
&& Objects.equals( urgency, that.urgency ) result = prime * result + ((urgency == null) ? 0 : urgency.hashCode());
&& Objects.equals( certainty, that.certainty ); return result;
} }
@Override @Override
public int hashCode() public boolean equals(Object obj) {
{ if (this == obj)
return Objects.hash( id, sender, pubTime, title, status, level, type, typeName, text, return true;
related, urgency, certainty ); if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
QWeatherDisasterWarningItem other = (QWeatherDisasterWarningItem) obj;
if (certainty == null) {
if (other.certainty != null)
return false;
} else if (!certainty.equals(other.certainty))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (level == null) {
if (other.level != null)
return false;
} else if (!level.equals(other.level))
return false;
if (pubTime == null) {
if (other.pubTime != null)
return false;
} else if (!pubTime.equals(other.pubTime))
return false;
if (related == null) {
if (other.related != null)
return false;
} else if (!related.equals(other.related))
return false;
if (sender == null) {
if (other.sender != null)
return false;
} else if (!sender.equals(other.sender))
return false;
if (severity == null) {
if (other.severity != null)
return false;
} else if (!severity.equals(other.severity))
return false;
if (severityColor == null) {
if (other.severityColor != null)
return false;
} else if (!severityColor.equals(other.severityColor))
return false;
if (status == null) {
if (other.status != null)
return false;
} else if (!status.equals(other.status))
return false;
if (text == null) {
if (other.text != null)
return false;
} else if (!text.equals(other.text))
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
if (type == null) {
if (other.type != null)
return false;
} else if (!type.equals(other.type))
return false;
if (typeName == null) {
if (other.typeName != null)
return false;
} else if (!typeName.equals(other.typeName))
return false;
if (urgency == null) {
if (other.urgency != null)
return false;
} else if (!urgency.equals(other.urgency))
return false;
return true;
} }
public String getId() public String getId() {
{
return id; return id;
} }
public void setId( String id ) public void setId(String id) {
{
this.id = id; this.id = id;
} }
public String getSender() public String getSender() {
{
return sender; return sender;
} }
public void setSender( String sender ) public void setSender(String sender) {
{
this.sender = sender; this.sender = sender;
} }
public Date getPubTime() public Date getPubTime() {
{
return pubTime; return pubTime;
} }
public void setPubTime( Date pubTime ) public void setPubTime(Date pubTime) {
{
this.pubTime = pubTime; this.pubTime = pubTime;
} }
public String getTitle() public String getTitle() {
{
return title; return title;
} }
public void setTitle( String title ) public void setTitle(String title) {
{
this.title = title; this.title = title;
} }
public String getStatus() public String getStatus() {
{
return status; return status;
} }
public void setStatus( String status ) public void setStatus(String status) {
{
this.status = status; this.status = status;
} }
public String getLevel() public String getLevel() {
{
return level; return level;
} }
public void setLevel( String level ) public void setLevel(String level) {
{
this.level = level; this.level = level;
} }
public String getType() public String getSeverity() {
{ return severity;
}
public void setSeverity(String severity) {
this.severity = severity;
}
public String getSeverityColor() {
return severityColor;
}
public void setSeverityColor(String severityColor) {
this.severityColor = severityColor;
}
public String getType() {
return type; return type;
} }
public void setType( String type ) public void setType(String type) {
{
this.type = type; this.type = type;
} }
public String getTypeName() public String getTypeName() {
{
return typeName; return typeName;
} }
public void setTypeName( String typeName ) public void setTypeName(String typeName) {
{
this.typeName = typeName; this.typeName = typeName;
} }
public String getText() public String getText() {
{
return text; return text;
} }
public void setText( String text ) public void setText(String text) {
{
this.text = text; this.text = text;
} }
public String getRelated() public String getRelated() {
{
return related; return related;
} }
public void setRelated( String related ) public void setRelated(String related) {
{
this.related = related; this.related = related;
} }
public String getUrgency() public String getUrgency() {
{
return urgency; return urgency;
} }
public void setUrgency( String urgency ) public void setUrgency(String urgency) {
{
this.urgency = urgency; this.urgency = urgency;
} }
public String getCertainty() public String getCertainty() {
{
return certainty; return certainty;
} }
public void setCertainty( String certainty ) public void setCertainty(String certainty) {
{
this.certainty = certainty; this.certainty = certainty;
} }
private String id; private String id;
private String sender; private String sender;
@JsonProperty( "pubTime") @JsonProperty("pubTime")
@JsonFormat( shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd\'T\'HH:mmXXX") @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd\'T\'HH:mmXXX")
private Date pubTime; private Date pubTime;
private String title; private String title;
private String status; private String status;
private String level; private String level;
private String severity;
private String severityColor;
private String type; private String type;
private String typeName; private String typeName;
private String text; private String text;

View File

@ -19,5 +19,5 @@
} }
], ],
"refer": { "sources": ["12379"], "license": ["no commercial use"] }, "refer": { "sources": ["12379"], "license": ["no commercial use"] },
"testURL": "https://devapi.qweather.com/v7/warning/now?key=fe9fa8eeeb6f4301a92541eed565dd15&location=101230608" "testURL": "https://devapi.qweather.com/v7/warning/now?key=fe9fa8eeeb6f4301a92541eed565dd15&location=101230201"
} }