You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
3.4 KiB
Java
87 lines
3.4 KiB
Java
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.sql.Connection;
|
|
import java.sql.SQLException;
|
|
import java.util.Arrays;
|
|
import java.util.Iterator;
|
|
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;
|
|
DBHandler db;
|
|
DataStoreBot(String storePath, String apiURL, DBConfig dbConfig) throws MalformedURLException, SQLException {
|
|
this.storePath = storePath;
|
|
this.apiURL = apiURL;
|
|
this.api = new APIParser(this.apiURL);
|
|
this.store = new DataStore();
|
|
this.db = new DBHandler(dbConfig);
|
|
}
|
|
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")
|
|
);
|
|
int sensorID2 = Integer.parseInt(sensorID);
|
|
|
|
//get timestamp
|
|
String timestamp = (String) entry.get("timestamp");
|
|
|
|
//get value informations (6)
|
|
JSONArray value_array = (JSONArray) entry.get("sensordatavalues");
|
|
System.out.println("----------------------------");
|
|
Iterator iterator = value_array.iterator();
|
|
|
|
JSONObject firstArr = (JSONObject) iterator.next();
|
|
String value_type_p1 = (String) firstArr.get("value_type");
|
|
int id_p1 = (int) (long) firstArr.get("id");
|
|
float value_p1 = Float.parseFloat((String) firstArr.get("value")) ;
|
|
|
|
JSONObject secondArr = (JSONObject) iterator.next();
|
|
String value_type_p2 = (String) secondArr.get("value_type");
|
|
int id_p2 = (int) (long) secondArr.get("id");
|
|
float value_p2 = Float.parseFloat((String) firstArr.get("value")) ;
|
|
|
|
// Json to String
|
|
String allJSON = String.valueOf(entry);
|
|
|
|
String path = this.storePath + country + "-" + sensorID + "-" + timestamp + ".json";
|
|
path = path.replace(":", "-").replace(" ", "-");
|
|
this.store.storeJSON(path, entry);
|
|
//System.out.println(country + sensorID2 + timestamp + value_type_p1 + id_p1 + value_p1 + value_type_p2 + id_p2 + value_p2 + allJSON);
|
|
this.db.storeInDB(country, sensorID2, timestamp, value_type_p1, id_p1, value_p1, value_type_p2, id_p2, value_p2, allJSON);
|
|
//String country, int sensorID, String timeStamp, String value_type_p1, int id_p1, float value_p1, String value_type_p2, int id_p2, float value_p2, String allJSON
|
|
}
|
|
}
|