From 3b1844c994e58b995fd5639da2374134b471ddb9 Mon Sep 17 00:00:00 2001 From: Kane Wang Date: Tue, 7 Jun 2022 14:03:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=92=8C=E9=A3=8E=E5=A4=A9=E6=B0=94json?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E5=8F=98=E6=9B=B4=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/java/DisasterWarning/pom.xml | 2 + .../src/main/java/AppMain.java | 139 ++++------- .../disaster/QWeatherDisasterWarningItem.java | 230 ++++++++++++------ 数据/和风天气/实例-东山.json | 2 +- 4 files changed, 207 insertions(+), 166 deletions(-) diff --git a/code/java/DisasterWarning/pom.xml b/code/java/DisasterWarning/pom.xml index f84e896..f97c464 100644 --- a/code/java/DisasterWarning/pom.xml +++ b/code/java/DisasterWarning/pom.xml @@ -9,6 +9,8 @@ com.oracle ojdbc8 19.3.0.0.0 + org.apache.httpcomponents diff --git a/code/java/DisasterWarning/src/main/java/AppMain.java b/code/java/DisasterWarning/src/main/java/AppMain.java index 4a76847..3739496 100644 --- a/code/java/DisasterWarning/src/main/java/AppMain.java +++ b/code/java/DisasterWarning/src/main/java/AppMain.java @@ -1,8 +1,9 @@ + /* * @Author: Kane * @Date: 2022-04-22 10:53:49 * @LastEditors: Kane - * @LastEditTime: 2022-05-12 09:52:45 + * @LastEditTime: 2022-06-07 13:16:35 * @FilePath: \DisasterWarning\src\main\java\AppMain.java * @Description: 和风天气预警推送厦门太保公众号主程序! * @@ -21,43 +22,34 @@ import java.sql.SQLException; import java.util.Vector; import java.util.logging.*; - -public class AppMain -{ +public class AppMain { // 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; WeatherDisasterNotifyConfig config = null; QWeatherDisasterWarning warning = null; Logger logger = null; // 配置logger - try - { + try { setRootLogger(); - logger = Logger.getLogger( "com.cpicxim" ); - } - catch ( IOException error ) - { - System.out.println( "配置logger失败,原因:" + error.getMessage() ); + logger = Logger.getLogger("com.cpicxim"); + } catch (IOException error) { + System.out.println("配置logger失败,原因:" + error.getMessage()); return; } // 读取配置 - try - { + try { config = AppConfigManager.getConfig(); - } - catch ( IOException error ) - { - System.out.println( "读取配置文件失败!" ); - System.out.println( error.getMessage() ); + } catch (IOException error) { + System.out.println("读取配置文件失败!"); + System.out.println(error.getMessage()); - logger.log( Level.SEVERE, "读取配置文件失败:{0}", error.getMessage() ); + logger.log(Level.SEVERE, "读取配置文件失败:{0}", error.getMessage()); return; } @@ -67,90 +59,68 @@ public class AppMain String userKey = config.getKey(); // 遍历所有城市,查询是否有警报,有则推送。 - while (true) - { - for ( City city : cities ) - { - try - { - json = WeatherDisasterWarningGrabber.getWeatherDisasterWarningJSON( queryURL, - userKey, city.getCityCode() ); - warning = WeatherDisasterWarningGrabber.convertWeatherDisasterWarning( json ); + 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} ); + logger.log(Level.INFO, "查询{0}天气预警,结果:{1}。", new Object[] { city.getCityName(), json }); Vector warningItems = warning.getWarning(); // 判断是否有警报,没有警报就结束当前城市的处理 - if ( warningItems.isEmpty() == true) - { - logger.log( Level.INFO, "查询{0}天气预警,无警报!。", new Object[] - { city.getCityName()} ); + if (warningItems.isEmpty() == true) { + logger.log(Level.INFO, "查询{0}天气预警,无警报!。", new Object[] { city.getCityName() }); continue; } // 有警报,就遍历警报数组 - for ( QWeatherDisasterWarningItem item : warningItems ) - { + for (QWeatherDisasterWarningItem item : warningItems) { // 先检查是否已经发送过 - if ( sendMessage.checkWarningHasSended( item.getId() ) == false) - { - logger.log( Level.INFO, "查询{0}天气预警,ID:{1},已有发送记录,跳过。", new Object[] - { city.getCityName(), item.getId()} ); + if (sendMessage.checkWarningHasSended(item.getId()) == false) { + logger.log(Level.INFO, "查询{0}天气预警,ID:{1},已有发送记录,跳过。", + new Object[] { city.getCityName(), item.getId() }); continue; } // 没有发送过 - logger.log( Level.INFO, "查询{0}天气预警,发送日志。", new Object[] - { city.getCityName()} ); + logger.log(Level.INFO, "查询{0}天气预警,发送日志。", new Object[] { city.getCityName() }); - sendMessage.sendWeatherDisasterWarning( config.getWechatOfficalAccountURL(), - item ); + sendMessage.sendWeatherDisasterWarning(config.getWechatOfficalAccountURL(), + item); - logger.log( Level.INFO, "{0}天气预警,日志发送成功。", new Object[] - { city.getCityName()} ); + logger.log(Level.INFO, "{0}天气预警,日志发送成功。", new Object[] { city.getCityName() }); // 将发送的警报,保存起来 - sendMessage.saveWeatherDisasterWarning( city.getCityName(), item ); + sendMessage.saveWeatherDisasterWarning(city.getCityName(), item); } - } - catch ( IOException error ) - { - System.out.println( "查询" + city.getCityName() + "出现异常!" ); - System.out.println( error.getMessage() ); + } 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()} ); + 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()} ); + } catch (Exception error) { + logger.log(Level.SEVERE, "查询 {0} 出现未知错误:{1}。", + new Object[] { 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()} ); + 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() }); } } } @@ -158,17 +128,16 @@ public class AppMain /** * 设置JUL的logger。 */ - private static void setRootLogger() throws IOException - { + private static void setRootLogger() throws IOException { LogManager logManager = LogManager.getLogManager(); // 使用外部的配置文件。 - FileInputStream configFile = new FileInputStream( "./logging.properties" ); + FileInputStream configFile = new FileInputStream("./logging.properties"); // 使用jar中的配置文件。 // InputStream configFile = // AppMain.class.getClassLoader().getResourceAsStream( "logging.properties" ); - logManager.readConfiguration( configFile ); + logManager.readConfiguration(configFile); - Logger.getLogger( "com.cpicxim" ); + Logger.getLogger("com.cpicxim"); } } diff --git a/code/java/DisasterWarning/src/main/java/com/cpic/xim/notify/disaster/QWeatherDisasterWarningItem.java b/code/java/DisasterWarning/src/main/java/com/cpic/xim/notify/disaster/QWeatherDisasterWarningItem.java index 53397ea..6f87493 100644 --- a/code/java/DisasterWarning/src/main/java/com/cpic/xim/notify/disaster/QWeatherDisasterWarningItem.java +++ b/code/java/DisasterWarning/src/main/java/com/cpic/xim/notify/disaster/QWeatherDisasterWarningItem.java @@ -7,21 +7,21 @@ import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonProperty; //import com.fasterxml.jackson.annotation.JsonIgnore; -public class QWeatherDisasterWarningItem -{ - public QWeatherDisasterWarningItem() - {} +public class QWeatherDisasterWarningItem { + public QWeatherDisasterWarningItem() { + } - public QWeatherDisasterWarningItem( String id, String sender, Date pubTime, String title, - String status, String level, String type, String typeName, String text, String related, - String urgency, String certainty) - { + public QWeatherDisasterWarningItem(String id, String sender, Date pubTime, String title, String status, + String level, String severity, String severityColor, String type, String typeName, String text, + String related, String urgency, String certainty) { this.id = id; this.sender = sender; this.pubTime = pubTime; this.title = title; this.status = status; this.level = level; + this.severity = severity; + this.severityColor = severityColor; this.type = type; this.typeName = typeName; this.text = text; @@ -31,163 +31,233 @@ public class QWeatherDisasterWarningItem } @Override - public boolean equals( Object o ) - { - if ( this == o) - { - return true; - } - if ( o == null || getClass() != o.getClass()) - { - return false; - } - QWeatherDisasterWarningItem that = (QWeatherDisasterWarningItem) o; - return id.equals( that.id ) && Objects.equals( sender, that.sender ) - && Objects.equals( pubTime, that.pubTime ) && Objects.equals( title, that.title ) - && Objects.equals( status, that.status ) && Objects.equals( level, that.level ) - && Objects.equals( type, that.type ) && Objects.equals( typeName, that.typeName ) - && Objects.equals( text, that.text ) && Objects.equals( related, that.related ) - && Objects.equals( urgency, that.urgency ) - && Objects.equals( certainty, that.certainty ); + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((certainty == null) ? 0 : certainty.hashCode()); + result = prime * result + ((id == null) ? 0 : id.hashCode()); + result = prime * result + ((level == null) ? 0 : level.hashCode()); + result = prime * result + ((pubTime == null) ? 0 : pubTime.hashCode()); + result = prime * result + ((related == null) ? 0 : related.hashCode()); + result = prime * result + ((sender == null) ? 0 : sender.hashCode()); + result = prime * result + ((severity == null) ? 0 : severity.hashCode()); + result = prime * result + ((severityColor == null) ? 0 : severityColor.hashCode()); + result = prime * result + ((status == null) ? 0 : status.hashCode()); + result = prime * result + ((text == null) ? 0 : text.hashCode()); + result = prime * result + ((title == null) ? 0 : title.hashCode()); + result = prime * result + ((type == null) ? 0 : type.hashCode()); + result = prime * result + ((typeName == null) ? 0 : typeName.hashCode()); + result = prime * result + ((urgency == null) ? 0 : urgency.hashCode()); + return result; } @Override - public int hashCode() - { - return Objects.hash( id, sender, pubTime, title, status, level, type, typeName, text, - related, urgency, certainty ); + public boolean equals(Object obj) { + if (this == obj) + return true; + 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; } - public void setId( String id ) - { + public void setId(String id) { this.id = id; } - public String getSender() - { + public String getSender() { return sender; } - public void setSender( String sender ) - { + public void setSender(String sender) { this.sender = sender; } - public Date getPubTime() - { + public Date getPubTime() { return pubTime; } - public void setPubTime( Date pubTime ) - { + public void setPubTime(Date pubTime) { this.pubTime = pubTime; } - public String getTitle() - { + public String getTitle() { return title; } - public void setTitle( String title ) - { + public void setTitle(String title) { this.title = title; } - public String getStatus() - { + public String getStatus() { return status; } - public void setStatus( String status ) - { + public void setStatus(String status) { this.status = status; } - public String getLevel() - { + public String getLevel() { return level; } - public void setLevel( String level ) - { + public void setLevel(String 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; } - public void setType( String type ) - { + public void setType(String type) { this.type = type; } - public String getTypeName() - { + public String getTypeName() { return typeName; } - public void setTypeName( String typeName ) - { + public void setTypeName(String typeName) { this.typeName = typeName; } - public String getText() - { + public String getText() { return text; } - public void setText( String text ) - { + public void setText(String text) { this.text = text; } - public String getRelated() - { + public String getRelated() { return related; } - public void setRelated( String related ) - { + public void setRelated(String related) { this.related = related; } - public String getUrgency() - { + public String getUrgency() { return urgency; } - public void setUrgency( String urgency ) - { + public void setUrgency(String urgency) { this.urgency = urgency; } - public String getCertainty() - { + public String getCertainty() { return certainty; } - public void setCertainty( String certainty ) - { + public void setCertainty(String certainty) { this.certainty = certainty; } private String id; private String sender; - @JsonProperty( "pubTime") - @JsonFormat( shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd\'T\'HH:mmXXX") + @JsonProperty("pubTime") + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd\'T\'HH:mmXXX") private Date pubTime; private String title; private String status; private String level; + private String severity; + private String severityColor; + private String type; private String typeName; private String text; diff --git a/数据/和风天气/实例-东山.json b/数据/和风天气/实例-东山.json index 5cf31f7..166b5cc 100644 --- a/数据/和风天气/实例-东山.json +++ b/数据/和风天气/实例-东山.json @@ -19,5 +19,5 @@ } ], "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" }