diff --git a/code/java/DisasterWarning/.gitignore b/code/java/DisasterWarning/.gitignore new file mode 100644 index 0000000..569855c --- /dev/null +++ b/code/java/DisasterWarning/.gitignore @@ -0,0 +1,136 @@ +### Java template +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +### VisualStudioCode template +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ + +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### Windows template +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +target/* \ No newline at end of file diff --git a/code/java/DisasterWarning/.vscode/launch.json b/code/java/DisasterWarning/.vscode/launch.json new file mode 100644 index 0000000..a17e9db --- /dev/null +++ b/code/java/DisasterWarning/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // 使用 IntelliSense 了解相关属性。 + // 悬停以查看现有属性的描述。 + // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "java", + "name": "Launch AppMain", + "request": "launch", + "mainClass": "AppMain", + "projectName": "disaster_warning" + } + ] +} \ No newline at end of file diff --git a/code/java/DisasterWarning/.vscode/settings.json b/code/java/DisasterWarning/.vscode/settings.json new file mode 100644 index 0000000..7831eae --- /dev/null +++ b/code/java/DisasterWarning/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "java.configuration.updateBuildConfiguration": "automatic", + "java.format.settings.url": "D:\\工作文档\\配置备份\\vscode\\eclipse-java-google-style.xml" +} \ No newline at end of file diff --git a/code/java/DisasterWarning/config.json b/code/java/DisasterWarning/config.json new file mode 100644 index 0000000..014224c --- /dev/null +++ b/code/java/DisasterWarning/config.json @@ -0,0 +1,18 @@ +{ + "title": "天气灾害预警配置文件", + "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", + "cities": [ + { + "city_name": "厦门", + "city_code": "101230201" + } + ], + "notify_stuffs": [ + { + "stuff_name": "王炜", + "mobile_phone": "15959215339" + } + ] +} diff --git a/code/java/DisasterWarning/db.json b/code/java/DisasterWarning/db.json new file mode 100644 index 0000000..7d27945 --- /dev/null +++ b/code/java/DisasterWarning/db.json @@ -0,0 +1,12 @@ +{ + "tns_name": "xmcx1", + "ip_addr": "10.39.0.86", + "user_name": "", + "password": "", + "tables": [ + { + "table_name": "", + "table_description": "" + } + ] +} \ No newline at end of file diff --git a/code/java/DisasterWarning/disaster_warning.iml b/code/java/DisasterWarning/disaster_warning.iml new file mode 100644 index 0000000..93e5b3b --- /dev/null +++ b/code/java/DisasterWarning/disaster_warning.iml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/code/java/DisasterWarning/disaster_warning.ipr b/code/java/DisasterWarning/disaster_warning.ipr new file mode 100644 index 0000000..ac62426 --- /dev/null +++ b/code/java/DisasterWarning/disaster_warning.ipr @@ -0,0 +1,379 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/code/java/DisasterWarning/pom.xml b/code/java/DisasterWarning/pom.xml new file mode 100644 index 0000000..f2ef2b1 --- /dev/null +++ b/code/java/DisasterWarning/pom.xml @@ -0,0 +1,92 @@ + + + 4.0.0 + com.cpic.xim + disaster_warning + 1.0-SNAPSHOT + + + org.apache.httpcomponents + httpclient + 4.5.13 + + + junit + junit + 4.13.2 + test + + + commons-beanutils + commons-beanutils + 1.9.4 + + + commons-collections + commons-collections + 20040616 + + + commons-lang + commons-lang + 2.6 + + + commons-logging + commons-logging + 1.1.1 + + + net.sf.ezmorph + ezmorph + 1.0.6 + + + net.sf.json-lib + json-lib + 2.4 + jdk15 + + + com.fasterxml.jackson.core + jackson-core + 2.9.8 + + + com.fasterxml.jackson.core + jackson-databind + 2.9.8 + + + com.fasterxml.jackson.core + jackson-annotations + 2.9.8 + + + + 1.8 + 1.8 + UTF-8 + UTF-8 + UTF-8 + + + compile + + + + dev + + + true + + + + test + + + false + + + + \ No newline at end of file diff --git a/code/java/DisasterWarning/src/main/java/AppMain.java b/code/java/DisasterWarning/src/main/java/AppMain.java new file mode 100644 index 0000000..f3ea44a --- /dev/null +++ b/code/java/DisasterWarning/src/main/java/AppMain.java @@ -0,0 +1,62 @@ + + +import com.cpic.xim.config.City; +import com.cpic.xim.notify.disaster.QWeatherDisasterWarning; +import com.cpic.xim.notify.disaster.WeatherDisasterWarningGrabber; +import com.cpic.xim.config.WeatherDisasterNotifyConfig; + +import java.io.IOException; +import java.util.Vector; + +public class AppMain +{ + private static final String CONFIG_FILE_PATH = "./config.json"; + + public static void main( String[] args ) + { + String json; + WeatherDisasterNotifyConfig config = null; + QWeatherDisasterWarning warning = null; + + // 读取配置 + try + { + config = WeatherDisasterNotifyConfig.load( CONFIG_FILE_PATH ); + } + catch (IOException error) + { + System.out.println( "读取配置文件失败!" ); + System.out.println( error.getMessage() ); + + return; + } + + Vector cities = config.getCities(); + String queryURL = config.getQueryUrl(); + String userKey = config.getKey(); + + // 遍历所有城市,查询是否有警报,有则推送。 + for ( City city : cities) + { + try + { + json = WeatherDisasterWarningGrabber.getWeatherDisasterWarningJSON( queryURL, + userKey, city.getCityCode() ); + warning = WeatherDisasterWarningGrabber.convertWeatherDisasterWarning( json ); + + // 判断是否有警报 + if ( warning.getWarning().isEmpty() == true) + { + + } + } + catch (IOException error) + { + System.out.println( "查询" + city.getCityName() + "出现异常!" ); + System.out.println( error.getMessage() ); + + continue; + } + } + } +} diff --git a/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/City.java b/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/City.java new file mode 100644 index 0000000..9efeaeb --- /dev/null +++ b/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/City.java @@ -0,0 +1,52 @@ +package com.cpic.xim.config; + +import java.util.Objects; + +public class City +{ + public City() {} + + public String getCityName() + { + return cityName; + } + + public void setCityName( String cityName ) + { + this.cityName = cityName; + } + + public String getCityCode() + { + return cityCode; + } + + public void setCityCode( String cityCode ) + { + this.cityCode = cityCode; + } + + @Override + public boolean equals( Object o ) + { + if ( this == o ) + { + return true; + } + if ( o == null || getClass() != o.getClass() ) + { + return false; + } + City city = (City) o; + return cityName.equals( city.cityName ) && cityCode.equals( city.cityCode ); + } + + @Override + public int hashCode() + { + return Objects.hash( cityName, cityCode ); + } + + private String cityName; + private String cityCode; +} diff --git a/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/CpicxmStuff.java b/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/CpicxmStuff.java new file mode 100644 index 0000000..643dbfb --- /dev/null +++ b/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/CpicxmStuff.java @@ -0,0 +1,52 @@ +package com.cpic.xim.config; + +import java.util.Objects; + +public class CpicxmStuff +{ + public CpicxmStuff() {}; + + public String getStuffName() + { + return stuffName; + } + + public void setStuffName( String stuffName ) + { + this.stuffName = stuffName; + } + + public String getMobilePhone() + { + return mobilePhone; + } + + public void setMobilePhone( String mobilePhone ) + { + this.mobilePhone = mobilePhone; + } + + @Override + public boolean equals( Object o ) + { + if ( this == o ) + { + return true; + } + if ( !( o instanceof CpicxmStuff ) ) + { + return false; + } + CpicxmStuff that = (CpicxmStuff) o; + return stuffName.equals( that.stuffName ) && mobilePhone.equals( that.mobilePhone ); + } + + @Override + public int hashCode() + { + return Objects.hash( stuffName, mobilePhone ); + } + + private String stuffName; + private String mobilePhone; +} 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 new file mode 100644 index 0000000..f3bdeaf --- /dev/null +++ b/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/WeatherDisasterNotifyConfig.java @@ -0,0 +1,134 @@ +package com.cpic.xim.config; + +import java.io.FileReader; +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; + + public static WeatherDisasterNotifyConfig load( String filePath ) throws IOException + + { + WeatherDisasterNotifyConfig config = null; + ObjectMapper mapper = new ObjectMapper(); + FileReader configFile = null; + StringBuffer json = null; + char[] buffer = new char[BUFFER_SIZE]; + + // 设置json属性 + mapper.setPropertyNamingStrategy( PropertyNamingStrategy.SNAKE_CASE ); + + try + { + configFile = new FileReader( filePath ); + json = new StringBuffer(); + + int length = configFile.read( buffer ); + + while (length != -1) + { + json.append( buffer ); + + length = configFile.read( buffer ); + } + + config = mapper.readValue( json.toString(), WeatherDisasterNotifyConfig.class ); + } + finally + { + if ( configFile != null) + { + try + { + configFile.close(); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + } + + return config; + } + + public WeatherDisasterNotifyConfig() + {}; + + public String getTitle() + { + return title; + } + + public void setTitle( String title ) + { + this.title = title; + } + + public String getKey() + { + return key; + } + + public void setKey( String key ) + { + this.key = key; + } + + public String getQueryUrl() + { + return queryUrl; + } + + public void setQueryUrl( String queryUrl ) + { + this.queryUrl = queryUrl; + } + + public Vector getCities() + { + return cities; + } + + public void setCities( Vector cities ) + { + this.cities = cities; + } + + public Vector getNotifyStuffs() + { + return notifyStuffs; + } + + public void setNotifyStuffs( Vector notifyStuffs ) + { + this.notifyStuffs = notifyStuffs; + } + + public String getWechatOfficalAccountURL() + { + return wechatOfficalAccountURL; + } + + public void setWechatOfficalAccountURL( String wechatOfficalAccountURL ) + { + this.wechatOfficalAccountURL = wechatOfficalAccountURL; + } + + private String title; + private String key; + private String queryUrl; + + @JsonProperty( "wechat_officalaccount_url") + private String wechatOfficalAccountURL; + private Vector cities; + private Vector notifyStuffs; + + +} diff --git a/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/db/DBTable.java b/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/db/DBTable.java new file mode 100644 index 0000000..9843d27 --- /dev/null +++ b/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/db/DBTable.java @@ -0,0 +1,52 @@ +package com.cpic.xim.config.db; + +import java.util.Objects; + +public class DBTable +{ + public DBTable() {} + + public String getTableName() + { + return tableName; + } + + public void setTableName( String tableName ) + { + this.tableName = tableName; + } + + public String getTableDescription() + { + return tableDescription; + } + + public void setTableDescription( String tableDescription ) + { + this.tableDescription = tableDescription; + } + + @Override + public boolean equals( Object o ) + { + if ( this == o ) + { + return true; + } + if ( !( o instanceof DBTable ) ) + { + return false; + } + DBTable dbTable = (DBTable) o; + return tableName.equals( dbTable.tableName ) && tableDescription.equals( dbTable.tableDescription ); + } + + @Override + public int hashCode() + { + return Objects.hash( tableName, tableDescription ); + } + + private String tableName; + private String tableDescription; +} diff --git a/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/db/OracleConfig.java b/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/db/OracleConfig.java new file mode 100644 index 0000000..868ae8c --- /dev/null +++ b/code/java/DisasterWarning/src/main/java/com/cpic/xim/config/db/OracleConfig.java @@ -0,0 +1,13 @@ +package com.cpic.xim.config.db; + +import java.util.Vector; + +public class OracleConfig +{ + private String tnsName; + private String ipAddr; + private String jdbcURL; + private String userName; + private String password; + private Vector tables; +} diff --git a/code/java/DisasterWarning/src/main/java/com/cpic/xim/httpUtil/HttpUtils.java b/code/java/DisasterWarning/src/main/java/com/cpic/xim/httpUtil/HttpUtils.java new file mode 100644 index 0000000..fa611a0 --- /dev/null +++ b/code/java/DisasterWarning/src/main/java/com/cpic/xim/httpUtil/HttpUtils.java @@ -0,0 +1,26 @@ +/* + * @Author: Kane + * @Date: 2022-04-22 09:54:05 + * @LastEditors: Kane + * @LastEditTime: 2022-04-22 10:32:28 + * @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\httpUtil\HttpUtils.java + * @Description: + * + * Copyright (c) ${2022} by Kane, All Rights Reserved. + */ + +package com.cpic.xim.httpUtil; + +import java.util.Map; + +/** + * + */ +public class HttpUtils +{ + public void postHttpRequest( String url, Map headers, + Map params ) + { + + } +} diff --git a/code/java/DisasterWarning/src/main/java/com/cpic/xim/notify/disaster/QWeatherDisasterWarning.java b/code/java/DisasterWarning/src/main/java/com/cpic/xim/notify/disaster/QWeatherDisasterWarning.java new file mode 100644 index 0000000..e527a88 --- /dev/null +++ b/code/java/DisasterWarning/src/main/java/com/cpic/xim/notify/disaster/QWeatherDisasterWarning.java @@ -0,0 +1,77 @@ +package com.cpic.xim.notify.disaster; + +import java.util.Vector; + +public class QWeatherDisasterWarning +{ + public QWeatherDisasterWarning( String code, + String updateTime, + String fxLink, + Vector warning, + QWeatherDisasterWarningRefer refer ) + { + this.code = code; + this.updateTime = updateTime; + this.fxLink = fxLink; + this.warning = warning; + this.refer = refer; + } + + public QWeatherDisasterWarning() {} + + public String getCode() + { + return code; + } + + public void setCode( String code ) + { + this.code = code; + } + + public String getUpdateTime() + { + return updateTime; + } + + public void setUpdateTime( String updateTime ) + { + this.updateTime = updateTime; + } + + public String getFxLink() + { + return fxLink; + } + + public void setFxLink( String fxLink ) + { + this.fxLink = fxLink; + } + + public Vector getWarning() + { + return warning; + } + + public void setWarning( Vector warning ) + { + this.warning = warning; + } + + public QWeatherDisasterWarningRefer getRefer() + { + return refer; + } + + public void setRefer( QWeatherDisasterWarningRefer refer ) + { + this.refer = refer; + } + + private String code; + private String updateTime; + private String fxLink; + private Vector warning; + private QWeatherDisasterWarningRefer refer; +} 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 new file mode 100644 index 0000000..ad4ba37 --- /dev/null +++ b/code/java/DisasterWarning/src/main/java/com/cpic/xim/notify/disaster/QWeatherDisasterWarningItem.java @@ -0,0 +1,212 @@ +package com.cpic.xim.notify.disaster; + +import java.util.Objects; + +import com.fasterxml.jackson.annotation.JsonFormat; +//import com.fasterxml.jackson.annotation.JsonIgnore; + +public class QWeatherDisasterWarningItem +{ + public QWeatherDisasterWarningItem() {} + public QWeatherDisasterWarningItem( String id, + String sender, + String pubTime, + String title, + String status, + String level, + 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.type = type; + this.typeName = typeName; + this.text = text; + this.related = related; + this.urgency = urgency; + this.certainty = certainty; + } + + @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 ); + } + + @Override + public int hashCode() + { + return Objects.hash( id, + sender, + pubTime, + title, + status, + level, + type, + typeName, + text, + related, + urgency, + certainty ); + } + + public String getId() + { + return id; + } + + public void setId( String id ) + { + this.id = id; + } + + public String getSender() + { + return sender; + } + + public void setSender( String sender ) + { + this.sender = sender; + } + + public String getPubTime() + { + return pubTime; + } + + public void setPubTime( String pubTime ) + { + this.pubTime = pubTime; + } + + public String getTitle() + { + return title; + } + + public void setTitle( String title ) + { + this.title = title; + } + + public String getStatus() + { + return status; + } + + public void setStatus( String status ) + { + this.status = status; + } + + public String getLevel() + { + return level; + } + + public void setLevel( String level ) + { + this.level = level; + } + + public String getType() + { + return type; + } + + public void setType( String type ) + { + this.type = type; + } + + public String getTypeName() + { + return typeName; + } + + public void setTypeName( String typeName ) + { + this.typeName = typeName; + } + + public String getText() + { + return text; + } + + public void setText( String text ) + { + this.text = text; + } + + public String getRelated() + { + return related; + } + + public void setRelated( String related ) + { + this.related = related; + } + + public String getUrgency() + { + return urgency; + } + + public void setUrgency( String urgency ) + { + this.urgency = urgency; + } + + public String getCertainty() + { + return certainty; + } + + public void setCertainty( String certainty ) + { + this.certainty = certainty; + } + + private String id; + private String sender; + @JsonFormat(pattern="yyyy-MM-dd'T'HH:mm") + private String pubTime; + private String title; + private String status; + private String level; + private String type; + private String typeName; + private String text; + private String related; + private String urgency; + private String certainty; +} diff --git a/code/java/DisasterWarning/src/main/java/com/cpic/xim/notify/disaster/QWeatherDisasterWarningRefer.java b/code/java/DisasterWarning/src/main/java/com/cpic/xim/notify/disaster/QWeatherDisasterWarningRefer.java new file mode 100644 index 0000000..39d2581 --- /dev/null +++ b/code/java/DisasterWarning/src/main/java/com/cpic/xim/notify/disaster/QWeatherDisasterWarningRefer.java @@ -0,0 +1,41 @@ +/* + * @Author: Kane + * @Date: 2022-03-17 21:08:09 + * @LastEditors: Kane + * @LastEditTime: 2022-03-18 00:30:53 + * @FilePath: \天气灾害预警\src\main\java\com\cpic\xim\disaster_warning\QWeatherDisasterWarningRefer.java + * @Description: + * + * Copyright (c) ${2022} by Kane, All Rights Reserved. + */ +package com.cpic.xim.notify.disaster; + +//import java.util.Objects; + +public class QWeatherDisasterWarningRefer +{ + public QWeatherDisasterWarningRefer() {} + + public String[] getSources() + { + return sources; + } + + public void setSources( String[] sources ) + { + this.sources = sources; + } + + public String[] getLicense() + { + return license; + } + + public void setLicense( String[] license ) + { + this.license = license; + } + + private String[] sources; + private String[] license; +} diff --git a/code/java/DisasterWarning/src/main/java/com/cpic/xim/notify/disaster/WeatherDisasterWarningGrabber.java b/code/java/DisasterWarning/src/main/java/com/cpic/xim/notify/disaster/WeatherDisasterWarningGrabber.java new file mode 100644 index 0000000..ea144cf --- /dev/null +++ b/code/java/DisasterWarning/src/main/java/com/cpic/xim/notify/disaster/WeatherDisasterWarningGrabber.java @@ -0,0 +1,131 @@ +/** + * @author 王炜 + * @version 1.0 + */ +package com.cpic.xim.notify.disaster; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.BufferedReader; + +import java.net.MalformedURLException; +import java.net.URL; +import java.net.HttpURLConnection; +import java.util.zip.GZIPInputStream; + +/** + * + */ +public class WeatherDisasterWarningGrabber +{ + /*** + * 从和风天气获取天气警报json字符串 + * @param cityCode 城市或区域代码 + * @return 返回警报的json字符串 + */ + public static String getWeatherDisasterWarningJSON( String queryURL, + String userKey, + String cityCode ) + { + //拼接url字符串 + String json = ""; + String requestURL = queryURL + "key=" + userKey + "&location=" + cityCode; + + //链接用 + HttpURLConnection connection = null; + URL url = null; + InputStream inputStream = null; + BufferedReader bufferedReader = null; + StringBuilder buffer = new StringBuilder(); + + try + { + url = new URL( requestURL ); + connection = (HttpURLConnection) url.openConnection(); + + connection.setRequestMethod( "GET" ); + connection.setConnectTimeout( 15000 ); + connection.setReadTimeout( 60000 ); + connection.connect(); + + //如果responseCode为200,说明访问成功! + if ( connection.getResponseCode() == 200 ) + { + //注意,和风使用了gzip压缩响应体 + inputStream = new GZIPInputStream( connection.getInputStream() ); + bufferedReader = new BufferedReader( new InputStreamReader( inputStream, "UTF-8" ) ); + + //读出数据 + String temp = bufferedReader.readLine(); + + while ( temp != null ) + { + buffer.append( temp ); + + temp = bufferedReader.readLine(); + } + + json = buffer.toString(); + } + } + catch ( MalformedURLException error ) + { + error.printStackTrace(); + } + catch ( IOException error ) + { + System.out.println( "读取失败!" ); + } + finally + { + if ( bufferedReader != null ) + { + try + { + bufferedReader.close(); + } + catch ( IOException error ) + { + error.printStackTrace(); + } + } + + if ( inputStream != null ) + { + try + { + inputStream.close(); + } + catch ( IOException error ) + { + error.printStackTrace(); + } + } + + if ( connection != null ) + { + connection.disconnect(); + } + } + + return json; + } + + /** + * 将天气警告的json字符串转换成java对象。 + * @param json json字符串 + * @return 返回 QWeatherDisasterWarning 对象。 + * @throws IOException + */ + public static QWeatherDisasterWarning convertWeatherDisasterWarning( String json ) + throws IOException + { + ObjectMapper mapper = new ObjectMapper(); + QWeatherDisasterWarning warning = mapper.readValue( json, QWeatherDisasterWarning.class ); + + return warning; + } +} diff --git a/code/java/DisasterWarning/src/main/java/com/cpic/xim/wechat/officalAccount/sendMessage.java b/code/java/DisasterWarning/src/main/java/com/cpic/xim/wechat/officalAccount/sendMessage.java new file mode 100644 index 0000000..66329cd --- /dev/null +++ b/code/java/DisasterWarning/src/main/java/com/cpic/xim/wechat/officalAccount/sendMessage.java @@ -0,0 +1,311 @@ + +package com.cpic.xim.wechat.officalAccount; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.io.*; + + +import java.net.URL; +import java.net.HttpURLConnection; +import java.nio.charset.StandardCharsets; +import java.util.Map; + +/** + * 推送微信公众号消息。 + * + * @author Kane + */ +public class sendMessage +{ + + /** + * 向公众号推送通知消息。接口文档参考 彭奕洁 编写《消息发送接口调用文档》 + * + * @param wechatOfficalAccountURL 公众号接口网址。 + * @param title 通知的标题。 + * @param notifyType 通知类型。 + * @param notifyMessage 通知文本内容。 + */ + public static void postNotifyMessageJSON( String wechatOfficalAccountURL, String title, + String notifyType, String notifyMessage ) + + { + // 设置推送内容 + WechatOfficalAccountMessageParameter param = new WechatOfficalAccountMessageParameter(); + + param.setFirst( title ); + param.setKeyword1( notifyType ); + param.setKeyword1Color( "#ff0000" ); + param.setKeyword2( notifyMessage ); + + // 转换成json + ObjectMapper mapper; + String json = null; + + try + { + mapper = new ObjectMapper(); + mapper.setSerializationInclusion( JsonInclude.Include.NON_NULL ); + + json = mapper.writeValueAsString( param ); + + if ( json.isEmpty()) + { + + } + + System.out.println( json ); + } + catch (JsonProcessingException error) + { + error.printStackTrace(); + + return; + } + + // 推送数据 + URL url; + HttpURLConnection connection = null; + + StringBuilder result = new StringBuilder(); + OutputStreamWriter out = null; + BufferedReader in = null; + + try + { + url = new URL( wechatOfficalAccountURL ); + connection = (HttpURLConnection) url.openConnection(); + + connection.setRequestMethod( "POST" ); + connection.setRequestProperty( "accept", "*/*" ); + connection.setRequestProperty( "Connection", "Keep-Alive" ); + connection.setRequestProperty( "Content-Type", "application/json" ); + connection.setDoInput( true ); + connection.setDoOutput( true ); + connection.connect(); + + out = new OutputStreamWriter( connection.getOutputStream(), StandardCharsets.UTF_8 ); + out.write( json ); + out.flush(); + + in = new BufferedReader( new InputStreamReader( connection.getInputStream() ) ); + + String line = in.readLine(); + + while (line != null) + { + result.append( line ); + + line = in.readLine(); + } + } + catch (Exception error) + { + error.printStackTrace(); + } + finally + { + try + { + if ( in != null) + { + in.close(); + } + + if ( out != null) + { + out.close(); + } + + if ( connection != null) + { + connection.disconnect(); + } + } + catch (Exception error) + { + error.printStackTrace(); + } + } + + } + + /** + * @ + */ + public static void postNotifyMessage( String URL, Map header, String param ) + { + + } +} + + +class WechatOfficalAccountMessageParameter +{ + + WechatOfficalAccountMessageParameter() + { + tplID = 57; + groupID = 1; + } + + @JsonProperty( "tplid") + private int tplID; + + @JsonProperty( "groupid") + private int groupID; + + @JsonProperty( "first") + private String first; + + @JsonProperty( "keyword1") + private String keyword1; + + @JsonProperty( "keyword1color") + private String keyword1Color; + + @JsonProperty( "keyword2") + private String keyword2; + + @JsonProperty( "keyword2color") + private String keyword2Color; + + @JsonProperty( "remark") + private String remark; + + @JsonProperty( "remarkcolor") + private String remarkColor; + + @JsonProperty( "url") + private String url; + + @JsonProperty( "appid") + private String appid; + + @JsonProperty( "pagepath") + private String pagePath; + + public int getTplID() + { + return tplID; + } + + public void setTplID( int tplID ) + { + this.tplID = tplID; + } + + public int getGroupID() + { + return groupID; + } + + public void setGroupID( int groupID ) + { + this.groupID = groupID; + } + + public String getFirst() + { + return first; + } + + public void setFirst( String first ) + { + this.first = first; + } + + public String getKeyword1() + { + return keyword1; + } + + public void setKeyword1( String keyword1 ) + { + this.keyword1 = keyword1; + } + + public String getKeyword1Color() + { + return keyword1Color; + } + + public void setKeyword1Color( String keyword1Color ) + { + this.keyword1Color = keyword1Color; + } + + public String getKeyword2() + { + return keyword2; + } + + public void setKeyword2( String keyword2 ) + { + this.keyword2 = keyword2; + } + + public String getKeyword2Color() + { + return keyword2Color; + } + + public void setKeyword2Color( String keyword2Color ) + { + this.keyword2Color = keyword2Color; + } + + public String getRemark() + { + return remark; + } + + public void setRemark( String remark ) + { + this.remark = remark; + } + + public String getRemarkColor() + { + return remarkColor; + } + + public void setRemarkColor( String remarkColor ) + { + this.remarkColor = remarkColor; + } + + public String getUrl() + { + return url; + } + + public void setUrl( String url ) + { + this.url = url; + } + + public String getAppid() + { + return appid; + } + + public void setAppid( String appid ) + { + this.appid = appid; + } + + public String getPagePath() + { + return pagePath; + } + + public void setPagePath( String pagePath ) + { + this.pagePath = pagePath; + } +} diff --git a/code/java/DisasterWarning/src/test/java/com/cpic/xim/wechat/officalAccount/sendMessageTest.java b/code/java/DisasterWarning/src/test/java/com/cpic/xim/wechat/officalAccount/sendMessageTest.java new file mode 100644 index 0000000..64d5b9a --- /dev/null +++ b/code/java/DisasterWarning/src/test/java/com/cpic/xim/wechat/officalAccount/sendMessageTest.java @@ -0,0 +1,35 @@ + +package com.cpic.xim.wechat.officalAccount; + +import org.junit.Test; +import static org.junit.Assert.*; + +public class sendMessageTest +{ + private static String url = + "https://cxxmwx.cpic.com.cn/app/index.php?i=2&c=entry&do=send_group_tpl_api&m=ok_tplmessage"; + + @Test + public void postNotifyMessageJson() + { + try + { + sendMessage.postNotifyMessageJSON( url, "警报", "警报标题", "警报内容!" ); + } + catch (Exception error) + { + fail( "测试失败!" ); + } + } + + @Test + public void postNotifyMessage() + { + try + { + } + catch (Exception error) + { + } + } +}