store sensor data to json files
This commit is contained in:
parent
7007787848
commit
629ab30668
@ -1,6 +1,7 @@
|
|||||||
package sh.adb.sensorCommunityAPI;
|
package sh.adb.sensorCommunityAPI;
|
||||||
|
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.json.simple.JSONArray;
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
import org.json.simple.parser.JSONParser;
|
import org.json.simple.parser.JSONParser;
|
||||||
|
|
||||||
@ -19,9 +20,9 @@ public class APIPaser {
|
|||||||
public APIPaser(String urlString) throws MalformedURLException {
|
public APIPaser(String urlString) throws MalformedURLException {
|
||||||
this.url = urlString;
|
this.url = urlString;
|
||||||
}
|
}
|
||||||
public Object getJSON() throws IOException, ParseException {
|
public JSONArray getJSONObject() throws IOException, ParseException {
|
||||||
JSONParser parser = new JSONParser();
|
JSONParser parser = new JSONParser();
|
||||||
return parser.parse(this.getRequest());
|
return (JSONArray) parser.parse(this.getRequest());
|
||||||
}
|
}
|
||||||
public String getRequest() throws IOException {
|
public String getRequest() throws IOException {
|
||||||
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
|
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
|
||||||
|
21
src/main/java/sh/adb/sensorCommunityAPI/DataStore.java
Normal file
21
src/main/java/sh/adb/sensorCommunityAPI/DataStore.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,12 +1,34 @@
|
|||||||
package sh.adb.sensorCommunityAPI;
|
package sh.adb.sensorCommunityAPI;
|
||||||
|
|
||||||
|
import org.json.simple.JSONArray;
|
||||||
|
import org.json.simple.JSONObject;
|
||||||
import org.json.simple.parser.ParseException;
|
import org.json.simple.parser.ParseException;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) throws IOException, ParseException {
|
public static void main(String[] args) throws IOException, ParseException {
|
||||||
|
String basePath = "./data";
|
||||||
|
|
||||||
APIPaser api = new APIPaser("https://data.sensor.community/airrohr/v1/sensor/35943/");
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,22 +0,0 @@
|
|||||||
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 {
|
|
||||||
boolean check = false;
|
|
||||||
|
|
||||||
FileWriter file = new FileWriter(path);
|
|
||||||
try {
|
|
||||||
file.write(json.toJSONString());
|
|
||||||
check = true;
|
|
||||||
}catch (IOException e){
|
|
||||||
System.out.println("Sorry cant write in file" + path);
|
|
||||||
}
|
|
||||||
return check;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user