add DataStoreBot and execute Interval

master
adb 3 years ago
parent 4172dccee7
commit a0b0ca53ac

@ -0,0 +1,2 @@
## data store directory
here you will find the saved json files

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

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

Loading…
Cancel
Save