Compare commits

..

No commits in common. 'AddSQL_DB' and 'master' have entirely different histories.

2
.gitignore vendored

@ -1 +1 @@
config.json ./config.json

@ -23,10 +23,8 @@ repositories {
} }
dependencies { dependencies {
implementation 'org.jetbrains:annotations:20.1.0'
compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1' compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.13' compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.13'
compile group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '2.7.2'
testCompile group: 'junit', name: 'junit', version: '4.12' testCompile group: 'junit', name: 'junit', version: '4.12'
} }

@ -1,12 +1,5 @@
{ {
"storePath": "./data/", "storePath": "./data/",
"apiURL": "https://data.sensor.community/airrohr/v1/sensor/35943/", "apiURL": "https://data.sensor.community/airrohr/v1/sensor/35943/",
"interval": 150, "interval": 150
"dbConfig": {
"db": "",
"pw": "",
"user": "",
"port": 3306,
"server": "localhost"
}
} }

@ -1,35 +0,0 @@
package sh.adb.sensorCommunityAPI;
import org.json.simple.JSONObject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConfig {
String pw;
String db;
String user;
int port;
String server;
DBConfig(String pw, String db, String user, int port, String server){
this.pw = pw;
this.db = db;
this.user = user;
this.port = port;
this.server = server;
}
DBConfig(JSONObject config){
this.pw = (String) config.get("pw");
this.db = (String) config.get("db");
this.user = (String) config.get("user");
this.port = (int) (long) config.get("port");
this.server = (String) config.get("server");
}
Connection getDbConnection() throws SQLException {
return DriverManager.getConnection(
"jdbc:mariadb://"+this.server+":"+String.valueOf(this.port)+"/"+this.db+"?user="+this.user+"&password="+this.pw
);
}
}

@ -1,30 +0,0 @@
package sh.adb.sensorCommunityAPI;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class DBHandler {
Connection connection;
DBHandler(DBConfig config) throws SQLException {
this.connection = config.getDbConnection();
}
public void storeInDB(String SQL) {
try (Statement stmt = this.connection.createStatement()) {
stmt.executeUpdate(SQL);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void storeInDB(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) {
try (Statement stmt = this.connection.createStatement()) {
stmt.executeUpdate(
"INSERT INTO apidata (id, country, sensorID, Timestamp, value_type_p1, id_p1, value_p1, value_type_p2, id_p2, value_p2, allJSON )" +
"VALUES ( NULL , '" + country + "', '" + sensorID + "' , '" + timeStamp +"', '" + value_type_p1 + "', '" + id_p1 + "', '" + value_p1 + "', '" + value_type_p2 + "', '" + id_p2 + "' , '" + value_p2 + "', '" + allJSON + "' )"
);
} catch (SQLException e) {
e.printStackTrace();
}
}
}

@ -17,7 +17,7 @@ public class DataStore {
fw.close(); fw.close();
return true; return true;
}catch (IOException e){ }catch (IOException e){
System.out.println("error while storing data to: " + path); System.out.println("error while soring data to: " + path);
} }
return false; return false;
} }

@ -6,10 +6,6 @@ import org.json.simple.parser.ParseException;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException; 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.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -19,13 +15,11 @@ public class DataStoreBot {
String apiURL; String apiURL;
APIParser api; APIParser api;
DataStore store; DataStore store;
DBHandler db; DataStoreBot(String storePath, String apiURL) throws MalformedURLException {
DataStoreBot(String storePath, String apiURL, DBConfig dbConfig) throws MalformedURLException, SQLException {
this.storePath = storePath; this.storePath = storePath;
this.apiURL = apiURL; this.apiURL = apiURL;
this.api = new APIParser(this.apiURL); this.api = new APIParser(this.apiURL);
this.store = new DataStore(); this.store = new DataStore();
this.db = new DBHandler(dbConfig);
} }
public void setInterval(int seconds) { public void setInterval(int seconds) {
ScheduledExecutorService ses = Executors.newScheduledThreadPool(1); ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);
@ -43,7 +37,6 @@ public class DataStoreBot {
this.storeEntry(entry); this.storeEntry(entry);
} }
public void storeEntry(JSONObject entry) throws IOException { public void storeEntry(JSONObject entry) throws IOException {
System.out.println("entry => "); System.out.println("entry => ");
System.out.println(entry); System.out.println(entry);
//get country //get country
@ -53,34 +46,12 @@ public class DataStoreBot {
String sensorID = String.valueOf( String sensorID = String.valueOf(
((JSONObject) entry.get("sensor")).get("id") ((JSONObject) entry.get("sensor")).get("id")
); );
int sensorID2 = Integer.parseInt(sensorID);
//get timestamp //get timestamp
String timestamp = (String) entry.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"; String path = this.storePath + country + "-" + sensorID + "-" + timestamp + ".json";
path = path.replace(":", "-").replace(" ", "-"); path = path.replace(":", "-").replace(" ", "-");
this.store.storeJSON(path, entry); 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
} }
} }

@ -3,20 +3,16 @@ package sh.adb.sensorCommunityAPI;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException;
public class Main { public class Main {
public static void main(String[] args) throws IOException, SQLException { public static void main(String[] args) throws IOException {
DataStore store = new DataStore(); DataStore store = new DataStore();
JSONObject config = store.readJSON("config.json"); JSONObject config = store.readJSON("config.json");
if (config == null) config = store.readJSON("example.config.json"); if (config == null) config = store.readJSON("example.config.json");
DBConfig dbConfig = new DBConfig((JSONObject) config.get("dbConfig"));
DataStoreBot bot = new DataStoreBot( DataStoreBot bot = new DataStoreBot(
config.get("storePath").toString(), config.get("storePath").toString(),
config.get("apiURL").toString(), config.get("apiURL").toString()
dbConfig
); );
bot.setInterval((int)(long) config.get("interval")); bot.setInterval((int)(long) config.get("interval"));
} }

Loading…
Cancel
Save