Skip to content
Snippets Groups Projects
Commit 85806798 authored by joalhelk's avatar joalhelk
Browse files

node and nodedata helper objects

parent a8f8b745
No related branches found
No related tags found
No related merge requests found
package com.joelhelkala.watcherGui.Node;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
public class Node {
private String description;
private String location;
private Float latitude;
private Float longitude;
private List<NodeData> data;
public Node(String description, Float latitude, Float longitude, String location) {
this.description = description;
this.latitude = latitude;
this.longitude = longitude;
this.location = location;
this.data = new ArrayList<NodeData>();
}
public Node(JSONArray arr) {
JSONObject node = arr.getJSONObject(0);
JSONObject parent = node.getJSONObject("parent");
this.description = parent.getString("description");
this.location = parent.getString("location");
this.latitude = parent.getFloat("latitude");
this.longitude = parent.getFloat("longitude");
this.data = new ArrayList<NodeData>();
for(int i = 0; i < arr.length(); i++) {
JSONObject measurements = arr.getJSONObject(i);
LocalDateTime measured_at = LocalDateTime.parse(measurements.getString("measured_at"));
Integer temperature = measurements.getInt("temperature");
Integer humidity = measurements.getInt("humidity");
Integer luminosity = measurements.getInt("luminosity");
this.data.add(new NodeData(measured_at, temperature, humidity, luminosity));
}
}
}
package com.joelhelkala.watcherGui.Node;
import java.time.LocalDateTime;
public class NodeData {
private final LocalDateTime measured_at;
private final Integer temperature;
private final Integer humidity;
private final Integer luminosity;
public NodeData(LocalDateTime measured_at, Integer temperature, Integer humidity, Integer luminosity) {
this.measured_at = measured_at;
this.temperature = temperature;
this.humidity = humidity;
this.luminosity = luminosity;
}
}
...@@ -3,11 +3,14 @@ package com.joelhelkala.watcherGui.frames; ...@@ -3,11 +3,14 @@ package com.joelhelkala.watcherGui.frames;
import java.awt.*; import java.awt.*;
import javax.swing.*; import javax.swing.*;
import org.json.JSONArray;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseListener; import java.awt.event.MouseListener;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.joelhelkala.watcherGui.Node.Node;
import com.joelhelkala.watcherGui.User.User; import com.joelhelkala.watcherGui.User.User;
import com.joelhelkala.watcherGui.frames.panels.LineChartPanel; import com.joelhelkala.watcherGui.frames.panels.LineChartPanel;
import com.joelhelkala.watcherGui.frames.panels.ProgressBarCirclePanel; import com.joelhelkala.watcherGui.frames.panels.ProgressBarCirclePanel;
...@@ -37,7 +40,7 @@ public class WelcomePage implements MouseListener { ...@@ -37,7 +40,7 @@ public class WelcomePage implements MouseListener {
private final JFrame frame; private final JFrame frame;
public WelcomePage(){ public WelcomePage(){
Map<String, String> temperatures = HttpRequests.getNodeData(1); Node node = new Node(HttpRequests.getNodeData(1));
JPanel topPanel = new JPanel(); JPanel topPanel = new JPanel();
topPanel.setBackground(dark); topPanel.setBackground(dark);
topPanel.setBounds(0,0,width, topPanelHeight); topPanel.setBounds(0,0,width, topPanelHeight);
......
...@@ -4,6 +4,8 @@ import javax.swing.JLabel; ...@@ -4,6 +4,8 @@ import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.SwingConstants; import javax.swing.SwingConstants;
import org.json.JSONArray;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.awt.Font; import java.awt.Font;
import java.text.Format; import java.text.Format;
......
...@@ -158,8 +158,8 @@ public class HttpRequests { ...@@ -158,8 +158,8 @@ public class HttpRequests {
* Gets all the history data from the given node_id. * Gets all the history data from the given node_id.
* TODO: Set a parameter that configures the amount of history data will be fetched * TODO: Set a parameter that configures the amount of history data will be fetched
*/ */
public static Map<String, String> getNodeData(Integer node_id) { public static JSONArray getNodeData(Integer node_id) {
Map<String, String> data = new HashMap<String, String>(); JSONArray data = new JSONArray();
try { try {
URL url = new URL(address + "/nodeData/" + node_id); URL url = new URL(address + "/nodeData/" + node_id);
StringBuilder response = new StringBuilder(); StringBuilder response = new StringBuilder();
...@@ -176,14 +176,14 @@ public class HttpRequests { ...@@ -176,14 +176,14 @@ public class HttpRequests {
response.append(responseLine.trim()); response.append(responseLine.trim());
} }
} }
System.out.println(response); data = new JSONArray(response.toString());
con.disconnect(); con.disconnect();
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
e.printStackTrace(); e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
return data; return data;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment