From 83af6b9c9c821c5a6ad13c77dc70f08d30eb6e80 Mon Sep 17 00:00:00 2001 From: joalhelk <joalhelk@jyu.fi> Date: Mon, 17 Jan 2022 22:59:43 +0200 Subject: [PATCH] update node data to server --- .../watcherGui/Nodes/Node/Node.java | 9 +- .../watcherGui/frames/WelcomePage.java | 24 +++- .../frames/panels/TemperaturePanel.java | 2 +- .../frames/subframes/NodeDataFrame.java | 15 ++- .../frames/subframes/NodeSettingsFrame.java | 113 +++++++++++++++--- .../watcherGui/httpRequests/HttpRequests.java | 45 +++++++ 6 files changed, 175 insertions(+), 33 deletions(-) diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Nodes/Node/Node.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Nodes/Node/Node.java index 4f322f8..1c4df8a 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Nodes/Node/Node.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Nodes/Node/Node.java @@ -18,6 +18,7 @@ public class Node { private Integer id; private List<NodeData> data; + // Constructor with basic information public Node(String description, Float latitude, Float longitude, String location, Integer id) { this.description = description; this.latitude = latitude; @@ -27,6 +28,7 @@ public class Node { this.data = new ArrayList<NodeData>(); } + // Constructor with jsonarray public Node(JSONArray arr) { if(arr.length() == 0) { this.description = ""; @@ -96,7 +98,10 @@ public class Node { // Getters public Integer getId() { return id; } public String getLocation() { return location; } - + public String getDescription() { return description; } + public Float getLatitude() { return latitude; } + public Float getLongitude() { return longitude; } + public void updateData(JSONArray nodeData) { data = new ArrayList<NodeData>(); if(nodeData.length() == 0) return; @@ -109,5 +114,5 @@ public class Node { 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 8a25eef..c643474 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java @@ -49,12 +49,12 @@ public class WelcomePage implements MouseListener, ActionListener { private final JLabel helpLabel; private final JLabel settingsLabel; private final JLabel logoutLabel; - private final JFrame frame; + private static JFrame frame; private static JLabel chosen_label; 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); + private static NodeSettingsFrame nodeSettingFrame = new NodeSettingsFrame(width-leftPanelWidth, height-topPanelHeight-bottomPanelHeight); public WelcomePage(){ Nodes.AddNodes(HttpRequests.getNodes()); @@ -189,7 +189,9 @@ public class WelcomePage implements MouseListener, ActionListener { stagePanel.add(second, "2"); stagePanel.add(third, "3"); cl.show(stagePanel, "1"); - frame.add(stagePanel); + frame.add(stagePanel); + + nodeSettingFrame.updateInformation(Nodes.findByLocation(comboBox.getSelectedItem().toString())); frame.setVisible(true); } @@ -281,7 +283,18 @@ public class WelcomePage implements MouseListener, ActionListener { LoginPage login = new LoginPage(); } } - + + /* + * Opens a dialog that asks if the user wants to save + */ + public static boolean handleSave() { + String message = "Are you sure you want to save?"; + String title = "SAVE?"; + int result = JOptionPane.showConfirmDialog(frame, message, title, + JOptionPane.YES_NO_OPTION, + JOptionPane.QUESTION_MESSAGE); + return result == JOptionPane.YES_OPTION; + } /* * This is prompted when user selects a item from node combobox @@ -296,6 +309,7 @@ public class WelcomePage implements MouseListener, ActionListener { if(node == null) return; node.updateData(HttpRequests.getNodeData(node.getId())); - nodeDataFrame.updateData(node); + nodeDataFrame.updateData(node); + nodeSettingFrame.updateInformation(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 e426291..75b803f 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 @@ -16,7 +16,7 @@ public class TemperaturePanel extends AbstractValuePanel { private static final Character symbol = 0xB0; private static final String title = "Temperature"; - static DateTimeFormatter formatter = DateTimeFormatter.ISO_WEEK_DATE; + public TemperaturePanel() { super(symbol, title); } 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 7bbbce7..cc60be6 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 @@ -22,7 +22,8 @@ public class NodeDataFrame extends JPanel { private static final Color lightgray = new Color(73, 73, 73); private TemperaturePanel tempCard; - private JPanel humidCard; + private HumidityPanel humidCard; + private LuminosityPanel lumiCard; private LineChartPanel chartCard; public NodeDataFrame(int x, int y, int width, int height) { @@ -48,10 +49,10 @@ public class NodeDataFrame extends JPanel { humidCard.setBounds(cardWidth+30, 10, cardWidth, cardPanelHeight-20); cardPanel.add(humidCard); - JPanel card3 = new LuminosityPanel(node.getRecentLuminosity()); - card3.setBackground(lightgray); - card3.setBounds(cardWidth*2+45, 10, cardWidth, cardPanelHeight-20); - cardPanel.add(card3); + lumiCard = new LuminosityPanel(node.getRecentLuminosity()); + lumiCard.setBackground(lightgray); + lumiCard.setBounds(cardWidth*2+45, 10, cardWidth, cardPanelHeight-20); + cardPanel.add(lumiCard); JPanel chartCardPanel = new JPanel(); chartCardPanel.setBackground(gray); @@ -70,7 +71,9 @@ public class NodeDataFrame extends JPanel { // Updates the frames data panels with given node information public void updateData(Node node) { tempCard.updateData(node.getRecentTemperature()); + humidCard.updateData(node.getRecentHumidity()); + lumiCard.updateData(node.getRecentLuminosity()); chartCard.updateData(node.getData()); - // TODO: perhaps change the humidity to normal label since the updating is hard + } } diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/NodeSettingsFrame.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/NodeSettingsFrame.java index 5ad79be..91a4e4a 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/NodeSettingsFrame.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/NodeSettingsFrame.java @@ -2,43 +2,118 @@ package com.joelhelkala.watcherGui.frames.subframes; import java.awt.Color; + import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JSplitPane; +import javax.swing.JTextField; + +import com.joelhelkala.watcherGui.Nodes.Node.Node; +import com.joelhelkala.watcherGui.User.User; +import com.joelhelkala.watcherGui.frames.LoginPage; +import com.joelhelkala.watcherGui.frames.WelcomePage; +import com.joelhelkala.watcherGui.httpRequests.HttpRequests; import java.awt.GridLayout; import java.awt.BorderLayout; +import java.awt.event.*; -public class NodeSettingsFrame extends JPanel { +public class NodeSettingsFrame extends JPanel implements ActionListener{ private static final Color gray = new Color(45, 45, 45); + private JTextField descField = new JTextField(); + private JTextField locationField = new JTextField(); + private JTextField latField = new JTextField(); + private JTextField lonField = new JTextField(); + private Node current_node = null; + + private JButton reset_btn; + private JButton save_btn; - public NodeSettingsFrame(int x, int y, int width, int height) { - setLayout(new BorderLayout()); + // Costructor + public NodeSettingsFrame(int width, int height) { + setLayout(null); setBounds(0,0,width,height); setOpaque(true); setBackground(gray); - JPanel grid = new JPanel(); - grid.setLayout(new GridLayout(2,2)); + JLabel descLabel = new JLabel("Description"); + descLabel.setForeground(Color.white); + descLabel.setBounds(10,10, 100, 30); + descField.setBounds(120, 10, 200, 30); + add(descLabel); + add(descField); - JPanel descPanel = new JPanel(); - JPanel locationPanel = new JPanel(); - JPanel longPanel = new JPanel(); - JPanel latPanel = new JPanel(); + JLabel locationLabel = new JLabel("Location"); + locationLabel.setForeground(Color.white); + locationLabel.setBounds(10,50, 100, 30); + locationField.setBounds(120, 50, 200, 30); + add(locationLabel); + add(locationField); - JButton reset_btn = new JButton("Reset"); - JButton save_btn = new JButton("Save"); + JLabel latLabel = new JLabel("Latitude"); + latLabel.setForeground(Color.white); + latLabel.setBounds(10,90, 100, 30); + latField.setBounds(120, 90, 200, 30); + add(latLabel); + add(latField); - grid.add(descPanel); - grid.add(locationPanel); - grid.add(latPanel); - grid.add(longPanel); + JLabel lonLabel = new JLabel("Longitude"); + lonLabel.setForeground(Color.white); + lonLabel.setBounds(10,130, 100, 30); + lonField.setBounds(120, 130, 200, 30); + add(lonLabel); + add(lonField); - add(grid, BorderLayout.CENTER); - - JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, reset_btn, save_btn); - add(split, BorderLayout.SOUTH); + reset_btn = new JButton("Reset"); + save_btn = new JButton("Save"); + reset_btn.addActionListener(this); + save_btn.addActionListener(this); + reset_btn.setBounds(width/4, height-200, 50, 20); + save_btn.setBounds(width/2, height-200, 50, 20); + add(reset_btn); + add(save_btn); + } + + // Updates the textfields according to the given node + public void updateInformation(Node node) { + current_node = node; + descField.setText(node.getDescription()); + locationField.setText(node.getLocation()); + latField.setText(node.getLatitude().toString()); + lonField.setText(node.getLongitude().toString()); + } + + // Saves the current information from the textlabels + // A confirmation window is prompted + private void saveInformation() { + boolean save = WelcomePage.handleSave(); + if(save) HttpRequests.updateNode(new Node(descField.getText(), + Float.parseFloat(latField.getText()), + Float.parseFloat(lonField.getText()), + locationField.getText(), + current_node.getId())); + } + + /* + * Validates all the textfields and returns + * a boolean based on if all are valid + */ + private boolean validFields() { + // TODO: create implementation + return true; + } + + // This handles the reset and save button clicks + @Override + public void actionPerformed(ActionEvent e) { + if(e.getSource() == reset_btn) { + updateInformation(current_node); + } else if(e.getSource() == save_btn) { + if(validFields()) saveInformation(); + } } } \ No newline at end of file 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 7f271b8..930029b 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/httpRequests/HttpRequests.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/httpRequests/HttpRequests.java @@ -198,6 +198,7 @@ public class HttpRequests { return nodes; } + // Sends a basic GET request to the given url and returns a JSONArray from response private static JSONArray basicGetRequest(String uri) { JSONArray data = new JSONArray(); URL url; @@ -226,4 +227,48 @@ public class HttpRequests { } return data; } + + /* + * Send a PUT request to update a node + */ + public static boolean updateNode(Node node) { + boolean success = false; + JSONObject response_json = new JSONObject(); + URL url; + try { + url = new URL(address + "/node/" + node.getId()); + + HttpURLConnection con = (HttpURLConnection)url.openConnection(); + con.setRequestMethod("PUT"); + con.setRequestProperty("Content-Type", "application/json; utf-8"); + con.setRequestProperty("Accept", "application/json"); + con.setDoOutput(true); + + JSONObject obj = new JSONObject(); + obj.put("id", node.getId()); + obj.put("location", node.getLocation()); + obj.put("description", node.getDescription()); + obj.put("latitude", node.getLatitude()); + obj.put("longitude", node.getLongitude()); + + String jsonString = obj.toString(); + + try(OutputStream os = con.getOutputStream()) { + byte[] input = jsonString.getBytes("utf-8"); + os.write(input, 0, input.length); + } + + int status = con.getResponseCode(); + if(status < 300) success = true; + + con.disconnect(); + + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return success; + } } -- GitLab