diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d105ded --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +./config.json \ No newline at end of file diff --git a/example.config.json b/example.config.json new file mode 100644 index 0000000..0ca1f73 --- /dev/null +++ b/example.config.json @@ -0,0 +1,5 @@ +{ + "storePath": "./data/", + "apiURL": "https://data.sensor.community/airrohr/v1/sensor/35943/", + "interval": 150 +} \ No newline at end of file diff --git a/src/main/java/sh/adb/sensorCommunityAPI/APIParser.java b/src/main/java/sh/adb/sensorCommunityAPI/APIParser.java index 36b6191..944cc65 100644 --- a/src/main/java/sh/adb/sensorCommunityAPI/APIParser.java +++ b/src/main/java/sh/adb/sensorCommunityAPI/APIParser.java @@ -2,14 +2,11 @@ package sh.adb.sensorCommunityAPI; import org.apache.http.client.methods.HttpGet; import org.json.simple.JSONArray; -import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import java.io.IOException; -import java.net.MalformedURLException; import org.apache.http.HttpResponse; -import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; @@ -17,7 +14,7 @@ import org.json.simple.parser.ParseException; public class APIParser { String url; - public APIParser(String urlString) throws MalformedURLException { + public APIParser(String urlString) { this.url = urlString; } public JSONArray getJSONObject() throws IOException, ParseException { diff --git a/src/main/java/sh/adb/sensorCommunityAPI/DataStore.java b/src/main/java/sh/adb/sensorCommunityAPI/DataStore.java index 5a96dee..e2b9798 100644 --- a/src/main/java/sh/adb/sensorCommunityAPI/DataStore.java +++ b/src/main/java/sh/adb/sensorCommunityAPI/DataStore.java @@ -1,7 +1,9 @@ package sh.adb.sensorCommunityAPI; import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; @@ -9,13 +11,25 @@ public class DataStore { public boolean storeJSON(String path, JSONObject json) throws IOException { FileWriter fw = new FileWriter(path); try { + System.out.println("store data to: " + path + " => "); System.out.println(json.toString()); fw.write(json.toString()); fw.close(); return true; }catch (IOException e){ - System.out.println("error wile soring data to: " + path); + System.out.println("error while soring data to: " + path); } return false; } + public JSONObject readJSON(String path){ + JSONParser parser = new JSONParser(); + + try { + System.out.println("reading file: " + path); + return (JSONObject) parser.parse(new FileReader(path)); + } catch (Exception e) { + System.out.println("error while reading file: " + path); + } + return null; + } } \ No newline at end of file diff --git a/src/main/java/sh/adb/sensorCommunityAPI/Main.java b/src/main/java/sh/adb/sensorCommunityAPI/Main.java index f30de28..3fd31fd 100644 --- a/src/main/java/sh/adb/sensorCommunityAPI/Main.java +++ b/src/main/java/sh/adb/sensorCommunityAPI/Main.java @@ -1,10 +1,23 @@ package sh.adb.sensorCommunityAPI; +import org.json.simple.JSONObject; + import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { - DataStoreBot bot = new DataStoreBot("./data/", "https://data.sensor.community/airrohr/v1/sensor/35943/"); - bot.setInterval(150); + DataStore store = new DataStore(); + JSONObject config = store.readJSON("config.json"); + if (config == null){ + config = store.readJSON("example.config.json"); + } + + DataStoreBot bot = new DataStoreBot( + config.get("storePath").toString(), + config.get("apiURL").toString() + ); + bot.setInterval(Integer.parseInt( + config.get("interval").toString() + )); } }