Compare commits

...

2 Commits

Author SHA1 Message Date
adb
c2b24cc896 Merge remote-tracking branch 'origin/master'
# Conflicts:
#	src/main/java/sh/adb/sensorCommunityAPI/dataStore.java
2021-03-03 10:45:20 +01:00
adb
629ab30668 store sensor data to json files 2021-03-03 10:44:07 +01:00
4 changed files with 47 additions and 46 deletions

View File

@ -1,6 +1,7 @@
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;
@ -19,9 +20,9 @@ public class APIPaser {
public APIPaser(String urlString) throws MalformedURLException {
this.url = urlString;
}
public Object getJSON() throws IOException, ParseException {
public JSONArray getJSONObject() throws IOException, ParseException {
JSONParser parser = new JSONParser();
return parser.parse(this.getRequest());
return (JSONArray) parser.parse(this.getRequest());
}
public String getRequest() throws IOException {
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {

View File

@ -0,0 +1,21 @@
package sh.adb.sensorCommunityAPI;
import org.json.simple.JSONObject;
import java.io.FileWriter;
import java.io.IOException;
public class DataStore {
public boolean storeJSON(String path, JSONObject json) throws IOException {
FileWriter fw = new FileWriter(path);
try {
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);
}
return false;
}
}

View File

@ -1,5 +1,7 @@
package sh.adb.sensorCommunityAPI;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
import java.io.IOException;
@ -16,7 +18,27 @@ import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException, ParseException {
String basePath = "./data";
APIPaser api = new APIPaser("https://data.sensor.community/airrohr/v1/sensor/35943/");
api.getJSON();
JSONArray APIData = api.getJSONObject();
JSONObject entry = (JSONObject) APIData.get(0);
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")
);
//get timestamp
String timestamp = (String) entry.get("timestamp");
String path = basePath + country + "-" + sensorID + "-" + timestamp + ".json";
DataStore store = new DataStore();
store.storeJSON(path, entry);
}
}

View File

@ -1,43 +0,0 @@
package sh.adb.sensorCommunityAPI;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class dataStore {
public boolean storeJson(String path, JSONObject json) throws IOException {
boolean check = false;
FileWriter file = new FileWriter(path);
try {
file.write(json.toJSONString());
check = true;
}catch (IOException e){
System.out.println("Can not write in file: " + path);
e.printStackTrace();
}
return check;
}
public JSONObject getjson(String path) throws IOException, ParseException {
JSONParser parser = new JSONParser();
JSONObject jsonObject = null;
try {
Object obj = parser.parse(new FileReader(path));
jsonObject = (JSONObject) obj;
} catch (Exception e) {
System.out.println("Can not create Jsonobjekt from path: " + path);
e.printStackTrace();
}
return jsonObject;
}
}