Created
November 26, 2016 22:53
-
-
Save simmsb/6326c57689b1a7b3695e6e99248682a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.raspberrypi.WeatherStation; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.ProtocolException; | |
import java.net.URL; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Timestamp; | |
import java.time.LocalDateTime; | |
import java.time.ZoneOffset; | |
import java.time.ZonedDateTime; | |
import java.time.format.DateTimeFormatter; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javax.net.ssl.HttpsURLConnection; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import org.json.JSONTokener; | |
public class CloudUploader | |
extends Thread | |
{ | |
private static final Logger LOG = Logger.getLogger("WeatherStation.CloudUploader"); | |
private boolean any_chance; | |
public CloudUploader() | |
{ | |
try | |
{ | |
Class.forName("com.mysql.jdbc.Driver"); | |
this.any_chance = true; | |
LOG.log(Level.INFO, "CloudUploader thread loaded"); | |
} | |
catch (ClassNotFoundException e) | |
{ | |
LOG.log(Level.SEVERE, "Unable to get MySQL driver: {0}", e.getMessage()); | |
this.any_chance = false; | |
} | |
} | |
public void run() | |
{ | |
LOG.log(Level.INFO, "CloudUploader thread running"); | |
for (;;) | |
{ | |
runUpload(); | |
try | |
{ | |
Thread.sleep(3600000L); | |
} | |
catch (InterruptedException e) | |
{ | |
LOG.log(Level.SEVERE, "Thread interrupted: {0}", e.getMessage()); | |
} | |
} | |
} | |
private void runUpload() | |
{ | |
if (!this.any_chance) { | |
return; | |
} | |
Database.DatabaseConfig config = WeatherStation.getDatabase().getConfig(); | |
if ((!config.getMysqlGood()) || (!config.getCloudGood())) | |
{ | |
LOG.log(Level.WARNING, "Configuration inadequate for cloud upload"); | |
return; | |
} | |
long start_time = System.currentTimeMillis(); | |
int uploaded = 0; | |
try | |
{ | |
Connection con = DriverManager.getConnection("jdbc:mysql://" + config.getMysqlHost() + ":3306/" + config.getMysqlDb(), config | |
.getMysqlUser(), config.getMysqlPass());Throwable localThrowable9 = null; | |
try | |
{ | |
try | |
{ | |
PreparedStatement command = con.prepareStatement("SELECT * FROM WEATHER_MEASUREMENT WHERE REMOTE_ID IS NULL LIMIT 50", 1003, 1008);Throwable localThrowable10 = null; | |
try | |
{ | |
ResultSet r = command.executeQuery();Throwable localThrowable11 = null; | |
try | |
{ | |
while (r.next()) | |
{ | |
int remote = sendToCloud(r, config.getCloudUrl(), config.getCloudUser(), config.getCloudPass()); | |
if (remote > 0) | |
{ | |
r.updateInt("REMOTE_ID", remote); | |
r.updateRow(); | |
uploaded++; | |
} | |
} | |
} | |
catch (Throwable localThrowable1) | |
{ | |
localThrowable11 = localThrowable1;throw localThrowable1; | |
} | |
finally {} | |
} | |
catch (Throwable localThrowable4) | |
{ | |
localThrowable10 = localThrowable4;throw localThrowable4; | |
} | |
finally {} | |
} | |
catch (SQLException e) | |
{ | |
LOG.log(Level.SEVERE, "Database error read failed: {0}", e.getMessage()); | |
} | |
} | |
catch (Throwable localThrowable7) | |
{ | |
localThrowable9 = localThrowable7;throw localThrowable7; | |
} | |
finally | |
{ | |
if (con != null) { | |
if (localThrowable9 != null) { | |
try | |
{ | |
con.close(); | |
} | |
catch (Throwable localThrowable8) | |
{ | |
localThrowable9.addSuppressed(localThrowable8); | |
} | |
} else { | |
con.close(); | |
} | |
} | |
} | |
} | |
catch (SQLException e) | |
{ | |
LOG.log(Level.SEVERE, "Unable to connect to database: {0}", e.getMessage()); | |
} | |
sendToCloud(null, config.getCloudUrl(), null, null); | |
LOG.log(Level.INFO, "Uploaded {0} reading{1} in {2} seconds", new Object[] { | |
Integer.valueOf(uploaded), uploaded == 1 ? "" : "s", | |
String.format("%.2f", new Object[] {Double.valueOf((System.currentTimeMillis() - start_time) / 1000.0D) }) }); | |
} | |
private static int sendToCloud(ResultSet r, String url, String user, String pass) | |
{ | |
try | |
{ | |
con = (HttpsURLConnection)new URL(url).openConnection(); | |
} | |
catch (IOException e) | |
{ | |
HttpsURLConnection con; | |
LOG.log(Level.SEVERE, "Failed to connection to cloud: {0}", e.getMessage()); | |
return 64536; | |
} | |
HttpsURLConnection con; | |
if (r == null) | |
{ | |
con.disconnect(); | |
return 0; | |
} | |
try | |
{ | |
con.setRequestMethod("POST"); | |
} | |
catch (ProtocolException e) | |
{ | |
LOG.log(Level.SEVERE, "Failed to set POST method: {0}", e.getMessage()); | |
return 64536; | |
} | |
con.setDoOutput(false); | |
con.setDoInput(true); | |
con.setRequestProperty("Content-type", "tex/plain"); | |
con.setRequestProperty("Accept", "text/plain"); | |
Object created; | |
try | |
{ | |
con.setRequestProperty("LOCAL_ID", String.valueOf(r.getInt("ID"))); | |
con.setRequestProperty("AMB_TEMP", String.valueOf(r.getFloat("AMBIENT_TEMPERATURE"))); | |
con.setRequestProperty("GND_TEMP", String.valueOf(r.getFloat("GROUND_TEMPERATURE"))); | |
String[] keys = { "AIR_QUALITY", "AIR_PRESSURE", "HUMIDITY", "WIND_DIRECTION", "WIND_SPEED", "WIND_GUST_SPEED", "RAINFALL" }; | |
for (String key : keys) { | |
con.setRequestProperty(key, String.valueOf(r.getFloat(key))); | |
} | |
created = r.getTimestamp("CREATED"); | |
ZonedDateTime zoned = ZonedDateTime.of(LocalDateTime.ofEpochSecond(((Timestamp)created).getTime() / 1000L, 0, ZoneOffset.UTC), ZoneOffset.UTC); | |
String when = zoned.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME); | |
con.setRequestProperty("READING_TIMESTAMP", when); | |
} | |
catch (SQLException e) | |
{ | |
LOG.log(Level.SEVERE, "Failed to obtain POST value: {0}", e.getMessage()); | |
return 64536; | |
} | |
con.setRequestProperty("WEATHER_STN_NAME", user); | |
con.setRequestProperty("WEATHER_STN_PASS", pass); | |
try | |
{ | |
int response_code = con.getResponseCode(); | |
if (response_code != 201) | |
{ | |
LOG.log(Level.WARNING, "Unexpected cloud response: {0}", Integer.valueOf(response_code)); | |
return 64536; | |
} | |
} | |
catch (IOException e) | |
{ | |
LOG.log(Level.SEVERE, "Failed to get response: {0}", e.getMessage()); | |
return 64536; | |
} | |
try | |
{ | |
InputStream ins = con.getInputStream();created = null; | |
try | |
{ | |
JSONTokener tokener = new JSONTokener(ins); | |
JSONObject response = new JSONObject(tokener); | |
try | |
{ | |
int remote_id = response.getInt("ORCL_RECORD_ID"); | |
long skipped = 0L; | |
while (ins.read() >= 0) { | |
skipped += 1L; | |
} | |
if (skipped > 0L) { | |
LOG.log(Level.WARNING, "Spurious bytes on end of response: {0}", Long.valueOf(skipped)); | |
} | |
return remote_id; | |
} | |
catch (JSONException e) | |
{ | |
LOG.log(Level.WARNING, "Failed to parse cloud response", e.getMessage()); | |
if (ins == null) { | |
break label646; | |
} | |
} | |
if (created != null) { | |
try | |
{ | |
ins.close(); | |
} | |
catch (Throwable localThrowable4) | |
{ | |
((Throwable)created).addSuppressed(localThrowable4); | |
} | |
} else { | |
ins.close(); | |
} | |
} | |
catch (Throwable localThrowable5) | |
{ | |
created = localThrowable5;throw localThrowable5; | |
} | |
finally | |
{ | |
if (ins != null) { | |
if (created != null) { | |
try | |
{ | |
ins.close(); | |
} | |
catch (Throwable localThrowable3) | |
{ | |
((Throwable)created).addSuppressed(localThrowable3); | |
} | |
} else { | |
ins.close(); | |
} | |
} | |
} | |
} | |
catch (IOException e) | |
{ | |
label646: | |
LOG.log(Level.SEVERE, "Failed to read response: {0}", e.getMessage()); | |
} | |
return 64536; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment