diff --git a/data/README.md b/data/README.md new file mode 100644 index 0000000..e56fff8 --- /dev/null +++ b/data/README.md @@ -0,0 +1,2 @@ +## data store directory +here you will find the saved json files \ No newline at end of file diff --git a/src/main/java/sh/adb/sensorCommunityAPI/DataStoreBot.java b/src/main/java/sh/adb/sensorCommunityAPI/DataStoreBot.java new file mode 100644 index 0000000..e15644d --- /dev/null +++ b/src/main/java/sh/adb/sensorCommunityAPI/DataStoreBot.java @@ -0,0 +1,60 @@ +package sh.adb.sensorCommunityAPI; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.ParseException; + +import javax.security.auth.callback.Callback; +import java.io.IOException; +import java.net.MalformedURLException; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +public class DataStoreBot { + String storePath; + String apiURL; + APIPaser api; + DataStore store; + DataStoreBot(String storePath, String apiURL) throws MalformedURLException { + this.storePath = storePath; + this.apiURL = apiURL; + this.api = new APIPaser(this.apiURL); + this.store = new DataStore(); + } + public void setInterval(int seconds) { + ScheduledExecutorService ses = Executors.newScheduledThreadPool(1); + ses.schedule(()->{ + try { + this.setInterval(seconds); + this.storeLatestEntry(); + } catch (IOException | ParseException e) { + e.printStackTrace(); + } + }, seconds, TimeUnit.SECONDS); + ses.shutdown(); + } + public Runnable storeLatestEntry() throws IOException, ParseException { + JSONArray APIData = this.api.getJSONObject(); + JSONObject entry = (JSONObject) APIData.get(0); + this.storeEntry(entry); + return null; + } + public void storeEntry(JSONObject entry) throws IOException { + System.out.println("entry => "); + System.out.println(entry); + //get country + String country = ((JSONObject) entry.get("location")).get("country").toString(); + + //get sensorID + String sensorID = String.valueOf( + ((JSONObject) entry.get("sensor")).get("id") + ); + + //get timestamp + String timestamp = (String) entry.get("timestamp"); + + String path = this.storePath + country + "-" + sensorID + "-" + timestamp + ".json";; + this.store.storeJSON(path, entry); + } +} diff --git a/src/main/java/sh/adb/sensorCommunityAPI/Main.java b/src/main/java/sh/adb/sensorCommunityAPI/Main.java index cf244fa..f30de28 100644 --- a/src/main/java/sh/adb/sensorCommunityAPI/Main.java +++ b/src/main/java/sh/adb/sensorCommunityAPI/Main.java @@ -1,34 +1,10 @@ package sh.adb.sensorCommunityAPI; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.ParseException; - import java.io.IOException; public class Main { - public static void main(String[] args) throws IOException, ParseException { - String basePath = "./data"; - - APIPaser api = new APIPaser("https://data.sensor.community/airrohr/v1/sensor/35943/"); - JSONArray APIData = api.getJSONObject(); - JSONObject entry = (JSONObject) APIData.get(0); - System.out.println("entry => "); - System.out.println(entry); - - //get country - String country = ((JSONObject) entry.get("location")).get("country").toString(); - - //get sensorID - String sensorID = String.valueOf( - ((JSONObject) entry.get("sensor")).get("id") - ); - - //get timestamp - String timestamp = (String) entry.get("timestamp"); - - String path = basePath + country + "-" + sensorID + "-" + timestamp + ".json"; - DataStore store = new DataStore(); - store.storeJSON(path, entry); + public static void main(String[] args) throws IOException { + DataStoreBot bot = new DataStoreBot("./data/", "https://data.sensor.community/airrohr/v1/sensor/35943/"); + bot.setInterval(150); } }