package sh.adb.sensorCommunityAPI; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.ParseException; 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; APIParser api; DataStore store; DataStoreBot(String storePath, String apiURL) throws MalformedURLException { this.storePath = storePath; this.apiURL = apiURL; this.api = new APIParser(this.apiURL); this.store = new DataStore(); } public void setInterval(int seconds) { ScheduledExecutorService ses = Executors.newScheduledThreadPool(1); ses.scheduleAtFixedRate(()->{ try { this.storeLatestEntry(); } catch (IOException | ParseException e) { e.printStackTrace(); } }, 0, seconds, TimeUnit.SECONDS); } public void storeLatestEntry() throws IOException, ParseException { JSONArray APIData = this.api.getJSONObject(); JSONObject entry = (JSONObject) APIData.get(0); this.storeEntry(entry); } 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"; path = path.replace(":", "-").replace(" ", "-"); this.store.storeJSON(path, entry); } }