diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Node/Node.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Node/Node.java index d0b57610696b4d8a99374fc28053a631f58d9f05..f34b378284dddffc906787a613fd13f3c0356ec9 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Node/Node.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Node/Node.java @@ -15,13 +15,15 @@ public class Node { private String location; private Float latitude; private Float longitude; + private Integer id; private List<NodeData> data; - public Node(String description, Float latitude, Float longitude, String location) { + public Node(String description, Float latitude, Float longitude, String location, Integer id) { this.description = description; this.latitude = latitude; this.longitude = longitude; this.location = location; + this.id = id; this.data = new ArrayList<NodeData>(); } @@ -31,6 +33,7 @@ public class Node { this.location = ""; this.latitude = 0f; this.longitude = 0f; + this.id = null; this.data = new ArrayList<NodeData>(); } else { JSONObject node = arr.getJSONObject(0); @@ -80,9 +83,23 @@ public class Node { } return newest; } + + // Getters + public Integer getId() { return id; } + public String getLocation() { return location; } - public String getLocation() { - return location; + public void updateData(JSONArray nodeData) { + data = new ArrayList<NodeData>(); + if(nodeData.length() == 0) return; + + for(int i = 0; i < nodeData.length(); i++) { + JSONObject obj = nodeData.getJSONObject(i); + LocalDateTime measured_at = LocalDateTime.parse(obj.get("measured_at").toString()); + Integer temp = obj.getInt("temperature"); + Integer humidity = obj.getInt("humidity"); + Integer lux = obj.getInt("luminosity"); + data.add(new NodeData(measured_at, temp, humidity, lux)); + } } } diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java index cd582aaa06b07b3957dea1b7385d53bdb7c82981..029662bdbdb3e26a884e390c69084fa07cd12e45 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java @@ -51,9 +51,13 @@ public class WelcomePage implements MouseListener, ActionListener { private final JFrame frame; private static JLabel chosen_label; + private static Node nodes[]; + + private static NodeDataFrame nodeDataFrame = new NodeDataFrame(leftPanelWidth, topPanelHeight, width-leftPanelWidth, height-topPanelHeight-bottomPanelHeight); + private static NodeSettingsFrame nodeSettingFrame = new NodeSettingsFrame(leftPanelWidth, topPanelHeight, width-leftPanelWidth, height-topPanelHeight-bottomPanelHeight); public WelcomePage(){ - Node nodes[] = HttpRequests.getNodes(); + nodes = HttpRequests.getNodes(); frame = new JFrame(); frame.getContentPane().setBackground( Color.MAGENTA ); @@ -176,8 +180,8 @@ public class WelcomePage implements MouseListener, ActionListener { stagePanel = new JPanel(); stagePanel.setBounds(leftPanelWidth, topPanelHeight, width-leftPanelWidth, height-topPanelHeight-bottomPanelHeight); stagePanel.setBackground(Color.red); - JPanel first = new NodeDataFrame(leftPanelWidth, topPanelHeight, width-leftPanelWidth, height-topPanelHeight-bottomPanelHeight); - JPanel second = new NodeSettingsFrame(leftPanelWidth, topPanelHeight, width-leftPanelWidth, height-topPanelHeight-bottomPanelHeight); + JPanel first = nodeDataFrame; + JPanel second = nodeSettingFrame; JPanel third = new FriendsFrame(leftPanelWidth, topPanelHeight, width-leftPanelWidth, height-topPanelHeight-bottomPanelHeight); stagePanel.setLayout(cl); stagePanel.add(first, "1"); @@ -277,8 +281,26 @@ public class WelcomePage implements MouseListener, ActionListener { } } + + /* + * This is prompted when user selects a item from node combobox + * Find the node object based on name and then fetch the data for that node from the server + * This data will be displayed on the data page + */ @Override public void actionPerformed(java.awt.event.ActionEvent e) { - System.out.println(((JComboBox)e.getSource()).getSelectedItem()); + String node_name = ((JComboBox)e.getSource()).getSelectedItem().toString(); + Node node = null; + for(Node n : nodes) { + if(n.getLocation() == node_name) { + node = n; + break; + } + } + + if(node == null) return; + + node.updateData(HttpRequests.getNodeData(node.getId())); + nodeDataFrame.updateData(node); } } \ No newline at end of file diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/panels/TemperaturePanel.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/panels/TemperaturePanel.java index 6f35a102a914b8f1c3ea3e724d10c1ddafd3866d..7b267c05583fa7f7e2082578621c68005a50b3c9 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/panels/TemperaturePanel.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/panels/TemperaturePanel.java @@ -18,21 +18,23 @@ import java.util.Date; public class TemperaturePanel extends JPanel { + private JLabel temp; + private static final Character degree = 0xB0; + public TemperaturePanel() { - this.setLayout(new BorderLayout()); + setLayout(new BorderLayout()); JLabel day = new JLabel(getDay()); day.setForeground(java.awt.Color.white); day.setHorizontalAlignment(SwingConstants.CENTER); - this.add(day, BorderLayout.NORTH); + add(day, BorderLayout.NORTH); - Character degree = 0xB0; String temperature = "-17" + degree; JLabel temp = new JLabel(temperature); temp.setForeground(java.awt.Color.white); temp.setHorizontalAlignment(SwingConstants.CENTER); temp.setFont(new Font(null, Font.BOLD, 40)); - this.add(temp, BorderLayout.CENTER); + add(temp, BorderLayout.CENTER); } public TemperaturePanel(Node node) { @@ -43,21 +45,20 @@ public class TemperaturePanel extends JPanel { LocalDateTime datetime = node.getRecentDate(); if (datetime != null) date = datetime.toString(); } - this.setLayout(new BorderLayout()); + setLayout(new BorderLayout()); JLabel day = new JLabel(date); day.setForeground(java.awt.Color.white); day.setHorizontalAlignment(SwingConstants.CENTER); - this.add(day, BorderLayout.NORTH); + add(day, BorderLayout.NORTH); - Character degree = 0xB0; String temperature = "No value"; if(temp_value != null) temperature = temp_value + "" + degree; - JLabel temp = new JLabel(temperature); + temp = new JLabel(temperature); temp.setForeground(java.awt.Color.white); temp.setHorizontalAlignment(SwingConstants.CENTER); temp.setFont(new Font(null, Font.BOLD, 40)); - this.add(temp, BorderLayout.CENTER); + add(temp, BorderLayout.CENTER); } private String getDay() { @@ -67,4 +68,10 @@ public class TemperaturePanel extends JPanel { String date = f.format(new Date()) + " " + sdf.format(cal.getTime()); return date; } + + // Updates the temperature value to most recent node temp + public void updateData(Node node) { + String text = node.getRecentTemperature().toString() + "" + degree; + temp.setText(text); + } } diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/NodeDataFrame.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/NodeDataFrame.java index 1bcaa2866b2cb36260625b7d0900ff8c0f572de2..c3f0f57894c6953c8fb0dc77fe39a7f57cda92be 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/NodeDataFrame.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/NodeDataFrame.java @@ -2,6 +2,7 @@ package com.joelhelkala.watcherGui.frames.subframes; import java.awt.Color; +import javax.swing.JLabel; import javax.swing.JPanel; import com.joelhelkala.watcherGui.Node.Node; @@ -19,6 +20,9 @@ public class NodeDataFrame extends JPanel { private static final Color gray = new Color(45, 45, 45); private static final Color lightgray = new Color(73, 73, 73); + private TemperaturePanel tempCard; + private JPanel humidCard; + public NodeDataFrame(int x, int y, int width, int height) { setLayout(null); setBounds(x,y,width,height); @@ -30,15 +34,15 @@ public class NodeDataFrame extends JPanel { cardPanel.setBounds(0, 0, width, cardPanelHeight); cardPanel.setLayout(null); - JPanel card1 = new TemperaturePanel(node); - card1.setBackground(lightgray); - card1.setBounds(15, 10, cardWidth, cardPanelHeight-20); - cardPanel.add(card1); + tempCard = new TemperaturePanel(node); + tempCard.setBackground(lightgray); + tempCard.setBounds(15, 10, cardWidth, cardPanelHeight-20); + cardPanel.add(tempCard); - JPanel card2 = new ProgressBarCirclePanel().makeUI("Humidity", node.getRecentHumidity()); - card2.setBackground(lightgray); - card2.setBounds(cardWidth+30, 10, cardWidth, cardPanelHeight-20); - cardPanel.add(card2); + humidCard = new ProgressBarCirclePanel().makeUI("Humidity", node.getRecentHumidity()); + humidCard.setBackground(lightgray); + humidCard.setBounds(cardWidth+30, 10, cardWidth, cardPanelHeight-20); + cardPanel.add(humidCard); JPanel card3 = new JPanel(); card3.setBackground(lightgray); @@ -57,6 +61,11 @@ public class NodeDataFrame extends JPanel { add(cardPanel); add(chartPanel); - + } + + // Updates the frames data panels with given node information + public void updateData(Node node) { + tempCard.updateData(node); + // TODO: perhaps change the humidity to normal label since the updating is hard } } diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/httpRequests/HttpRequests.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/httpRequests/HttpRequests.java index bb0ca2682a9883060e1a3be60abcc67b1249c22e..282a00df723974ce44b48cafef0ffa6393e3513e 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/httpRequests/HttpRequests.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/httpRequests/HttpRequests.java @@ -190,7 +190,8 @@ public class HttpRequests { String location = obj.getString("location"); Float latitude = obj.getFloat("latitude"); Float longitude = obj.getFloat("longitude"); - nodes[i] = new Node(description, latitude, longitude, location); + Integer id = obj.getInt("id"); + nodes[i] = new Node(description, latitude, longitude, location, id); } return nodes; }