|
|
|
@ -6,6 +6,10 @@ 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;
|
|
|
|
@ -15,11 +19,13 @@ public class DataStoreBot {
|
|
|
|
|
String apiURL;
|
|
|
|
|
APIParser api;
|
|
|
|
|
DataStore store;
|
|
|
|
|
DataStoreBot(String storePath, String apiURL) throws MalformedURLException {
|
|
|
|
|
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);
|
|
|
|
@ -37,6 +43,7 @@ public class DataStoreBot {
|
|
|
|
|
this.storeEntry(entry);
|
|
|
|
|
}
|
|
|
|
|
public void storeEntry(JSONObject entry) throws IOException {
|
|
|
|
|
|
|
|
|
|
System.out.println("entry => ");
|
|
|
|
|
System.out.println(entry);
|
|
|
|
|
//get country
|
|
|
|
@ -46,12 +53,34 @@ public class DataStoreBot {
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|