From 629ab306689365e3b9906d80aa5d896b6317f858 Mon Sep 17 00:00:00 2001 From: adb Date: Wed, 3 Mar 2021 10:44:07 +0100 Subject: [PATCH] store sensor data to json files --- .../sh/adb/sensorCommunityAPI/APIPaser.java | 5 ++-- .../sh/adb/sensorCommunityAPI/DataStore.java | 21 ++++++++++++++++ .../java/sh/adb/sensorCommunityAPI/Main.java | 24 ++++++++++++++++++- .../sh/adb/sensorCommunityAPI/dataStore.java | 22 ----------------- 4 files changed, 47 insertions(+), 25 deletions(-) create mode 100644 src/main/java/sh/adb/sensorCommunityAPI/DataStore.java delete mode 100644 src/main/java/sh/adb/sensorCommunityAPI/dataStore.java diff --git a/src/main/java/sh/adb/sensorCommunityAPI/APIPaser.java b/src/main/java/sh/adb/sensorCommunityAPI/APIPaser.java index a40cd4f..423c2d3 100644 --- a/src/main/java/sh/adb/sensorCommunityAPI/APIPaser.java +++ b/src/main/java/sh/adb/sensorCommunityAPI/APIPaser.java @@ -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()) { diff --git a/src/main/java/sh/adb/sensorCommunityAPI/DataStore.java b/src/main/java/sh/adb/sensorCommunityAPI/DataStore.java new file mode 100644 index 0000000..5a96dee --- /dev/null +++ b/src/main/java/sh/adb/sensorCommunityAPI/DataStore.java @@ -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; + } +} \ 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 84ed97a..cf244fa 100644 --- a/src/main/java/sh/adb/sensorCommunityAPI/Main.java +++ b/src/main/java/sh/adb/sensorCommunityAPI/Main.java @@ -1,12 +1,34 @@ package sh.adb.sensorCommunityAPI; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; import org.json.simple.parser.ParseException; 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); } } diff --git a/src/main/java/sh/adb/sensorCommunityAPI/dataStore.java b/src/main/java/sh/adb/sensorCommunityAPI/dataStore.java deleted file mode 100644 index 90b2b17..0000000 --- a/src/main/java/sh/adb/sensorCommunityAPI/dataStore.java +++ /dev/null @@ -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; - } -} \ No newline at end of file