add config file

master
adb 3 years ago
parent 540bb7b778
commit 4bc35cf70e

1
.gitignore vendored

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

@ -0,0 +1,5 @@
{
"storePath": "./data/",
"apiURL": "https://data.sensor.community/airrohr/v1/sensor/35943/",
"interval": 150
}

@ -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 {

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

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

Loading…
Cancel
Save