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

get node history data

parent 1f183cdc
No related branches found
No related tags found
No related merge requests found
...@@ -5,11 +5,14 @@ import javax.swing.*; ...@@ -5,11 +5,14 @@ import javax.swing.*;
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.Map;
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;
import com.joelhelkala.watcherGui.frames.panels.TemperaturePanel; import com.joelhelkala.watcherGui.frames.panels.TemperaturePanel;
import com.joelhelkala.watcherGui.httpRequests.HttpRequests;
public class WelcomePage implements MouseListener { public class WelcomePage implements MouseListener {
...@@ -28,8 +31,11 @@ public class WelcomePage implements MouseListener { ...@@ -28,8 +31,11 @@ public class WelcomePage implements MouseListener {
private final JLabel nodeDataLabel; private final JLabel nodeDataLabel;
private final JLabel nodeSettingsLabel; private final JLabel nodeSettingsLabel;
private final JLabel friendsLabel; private final JLabel friendsLabel;
private final JLabel helpLabel;
private final JLabel settingsLabel;
public WelcomePage(){ public WelcomePage(){
Map<String, String> temperatures = 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);
...@@ -126,18 +132,22 @@ public class WelcomePage implements MouseListener { ...@@ -126,18 +132,22 @@ public class WelcomePage implements MouseListener {
friendsLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); friendsLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
leftPanel.add(friendsLabel); leftPanel.add(friendsLabel);
JLabel helpLabel = new JLabel("Help"); helpLabel = new JLabel("Help");
helpLabel.setHorizontalAlignment(SwingConstants.CENTER); helpLabel.setHorizontalAlignment(SwingConstants.CENTER);
helpLabel.setForeground(Color.WHITE); helpLabel.setForeground(Color.WHITE);
helpLabel.setFont(new Font("Apple SD Gothic Neo", Font.PLAIN, 16)); helpLabel.setFont(new Font("Apple SD Gothic Neo", Font.PLAIN, 16));
helpLabel.setBounds(20, 615, 98, 16); helpLabel.setBounds(20, 615, 98, 16);
helpLabel.addMouseListener(this);
helpLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
leftPanel.add(helpLabel); leftPanel.add(helpLabel);
JLabel settingsLabel = new JLabel("Settings"); settingsLabel = new JLabel("Settings");
settingsLabel.setHorizontalAlignment(SwingConstants.CENTER); settingsLabel.setHorizontalAlignment(SwingConstants.CENTER);
settingsLabel.setForeground(Color.WHITE); settingsLabel.setForeground(Color.WHITE);
settingsLabel.setFont(new Font("Apple SD Gothic Neo", Font.PLAIN, 16)); settingsLabel.setFont(new Font("Apple SD Gothic Neo", Font.PLAIN, 16));
settingsLabel.setBounds(123, 615, 98, 16); settingsLabel.setBounds(123, 615, 98, 16);
settingsLabel.addMouseListener(this);
settingsLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
leftPanel.add(settingsLabel); leftPanel.add(settingsLabel);
frame.getContentPane().add(cardPanel); frame.getContentPane().add(cardPanel);
frame.getContentPane().add(chartPanel); frame.getContentPane().add(chartPanel);
...@@ -158,7 +168,8 @@ public class WelcomePage implements MouseListener { ...@@ -158,7 +168,8 @@ public class WelcomePage implements MouseListener {
if (target == nodeDataLabel) nodeDataLabel.setForeground(lightgray); if (target == nodeDataLabel) nodeDataLabel.setForeground(lightgray);
else if (target == nodeSettingsLabel) nodeSettingsLabel.setForeground(lightgray); else if (target == nodeSettingsLabel) nodeSettingsLabel.setForeground(lightgray);
else if (target == friendsLabel) friendsLabel.setForeground(lightgray); else if (target == friendsLabel) friendsLabel.setForeground(lightgray);
else if (target == helpLabel) helpLabel.setForeground(lightgray);
else if (target == settingsLabel) settingsLabel.setForeground(lightgray);
} }
@Override @Override
...@@ -167,7 +178,8 @@ public class WelcomePage implements MouseListener { ...@@ -167,7 +178,8 @@ public class WelcomePage implements MouseListener {
if (target == nodeDataLabel) nodeDataLabel.setForeground(Color.WHITE); if (target == nodeDataLabel) nodeDataLabel.setForeground(Color.WHITE);
else if (target == nodeSettingsLabel) nodeSettingsLabel.setForeground(Color.WHITE); else if (target == nodeSettingsLabel) nodeSettingsLabel.setForeground(Color.WHITE);
else if (target == friendsLabel) friendsLabel.setForeground(Color.WHITE); else if (target == friendsLabel) friendsLabel.setForeground(Color.WHITE);
else if (target == helpLabel) helpLabel.setForeground(Color.WHITE);
else if (target == settingsLabel) settingsLabel.setForeground(Color.WHITE);
} }
@Override @Override
......
...@@ -9,6 +9,8 @@ import java.net.HttpURLConnection; ...@@ -9,6 +9,8 @@ import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
...@@ -135,8 +137,11 @@ public class HttpRequests { ...@@ -135,8 +137,11 @@ public class HttpRequests {
response.append(responseLine.trim()); response.append(responseLine.trim());
} }
} }
// Get the values from the response and set them to the user
JSONObject object = new JSONObject(response.toString()); JSONObject object = new JSONObject(response.toString());
User.setToken(object.get("access_token").toString()); User.setToken(object.get("access_token").toString());
User.setName(object.get("name").toString());
User.setEmail(object.get("email").toString());
con.disconnect(); con.disconnect();
return true; return true;
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
...@@ -149,6 +154,39 @@ public class HttpRequests { ...@@ -149,6 +154,39 @@ public class HttpRequests {
return false; return false;
} }
/*
* Gets all the history data from the given node_id.
* TODO: Set a parameter that configures the amount of history data will be fetched
*/
public static Map<String, String> getNodeData(Integer node_id) {
Map<String, String> data = new HashMap<String, String>();
try {
URL url = new URL(address + "/nodeData/" + node_id);
StringBuilder response = new StringBuilder();
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Content-Type", "application/json; utf-8");
con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);
try(BufferedReader br = new BufferedReader(
new InputStreamReader(con.getInputStream(), "utf-8"))) {
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
}
System.out.println(response);
con.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
public static JSONArray parse(String responseBody) { public static JSONArray parse(String responseBody) {
JSONArray body = new JSONArray(responseBody); JSONArray body = new JSONArray(responseBody);
for(int i = 0; i < body.length(); i++) { for(int i = 0; i < body.length(); i++) {
......
...@@ -71,10 +71,11 @@ public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFi ...@@ -71,10 +71,11 @@ public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFi
.withClaim("role", user.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList())) .withClaim("role", user.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList()))
.sign(algorithm); .sign(algorithm);
Map<String, String> tokens = new HashMap<>(); Map<String, String> json_resp = new HashMap<>();
tokens.put("access_token", access_token); json_resp.put("access_token", access_token);
json_resp.put("name", user.getName());
json_resp.put("email", user.getEmail());
response.setContentType("application/json"); response.setContentType("application/json");
// TODO: perhaps return user info and token, not just token new ObjectMapper().writeValue(response.getOutputStream(), json_resp);
new ObjectMapper().writeValue(response.getOutputStream(), tokens);
} }
} }
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