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); } }