Advertisement
Guest User

Untitled

a guest
Jan 31st, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.net.*;
  5. import java.text.ParseException;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8. import java.util.Timer;
  9. import java.util.TimerTask;
  10.  
  11. import javafx.scene.chart.LineChart;
  12.  
  13. public class PasswordURLAuthenticatorTimerArduino extends TimerTask {
  14.  
  15. static HygroSample higroExt = new HygroSample();
  16.  
  17. public void run() {
  18. System.out.println("Hello World! - B.Exterior");
  19. Date date1 = new Date();
  20.  
  21. if (higroExt.first==null){
  22. System.out.println("First Day - B.Exterior");
  23. // Create data sample if inexisting
  24. double[] timeS=new double[288];
  25. double[] tempS=new double[288];
  26. double[] humiS=new double[288];
  27. String[] stateS=new String[288];
  28. higroExt.add(date1,timeS,tempS,humiS,stateS);
  29. }
  30.  
  31. //Access website and feed vector
  32. try {
  33. // Sets the authenticator that will be used by the networking code
  34. // when a proxy or an HTTP server asks for authentication.
  35. Authenticator.setDefault(new CustomAuthenticator());
  36. URL url = new URL("http://meteo.tecnico.ulisboa.pt/obs/livejson");
  37. // read text returned by server
  38. BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
  39. String line;
  40. String[] data;
  41.  
  42. while ((line = in.readLine()) != null) {
  43. // System.out.println(line); // Print line read
  44. data=line.split(",");
  45. System.out.println(data[1].substring(12, 22) + " " + data[1].substring(23,28) + " " + data[2].substring(7) + " " + data[4].substring(5));
  46.  
  47. //Date
  48. SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
  49. date1 = higroExt.last.day;
  50. Date date2 = new Date();
  51.  
  52. SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  53. System.out.println(sdf2.format(date2));
  54.  
  55. //String dateInString1 = data[1].substring(12, 22);
  56. //String dateInString2 = data[1].substring(12, 28);
  57.  
  58. // Convert Date date2 to Calendar --> Avoid Deprecation!
  59.  
  60. //Time
  61. @SuppressWarnings("deprecation")
  62. // Use Calendar.HOUR_OF_DAY instead of date2.getHours()
  63. //double time = calendar.get(Calendar.HOUR_OF_DAY)*1.0+calendar.get(Calendar.MINUTE)*1.0/60;
  64. double time = date2.getHours()*1.0+date2.getMinutes()*1.0/60;
  65.  
  66. double temp = Double.parseDouble(data[2].substring(7));
  67. double humi = Double.parseDouble(data[4].substring(5));
  68. String state = "NA";
  69.  
  70. // Create new node if day changes
  71. if (sdf1.format(date2).equals(sdf1.format(date1))!=true){
  72. System.out.println(sdf1.format(date2)+ " vs " + sdf1.format(date1));
  73. // or
  74. // Verify if date is different of date of last node
  75. double[] timeS=new double[288];
  76. double[] tempS=new double[288];
  77. double[] humiS=new double[288];
  78. String[] stateS=new String[288];
  79. higroExt.add(date2,timeS,tempS,humiS,stateS);
  80. System.out.println("New Day");
  81. higroExt.toCSV(higroExt.last, "Ext");
  82. //higroExt.toHTML(higroExt.last, "Ext");
  83. }
  84. // Update last instant data
  85. higroExt.feed(higroExt.last, time, temp, humi, state);
  86. }
  87. in.close();
  88.  
  89. // Print for debug purposes
  90. higroExt.printL();
  91. //higroExt.toHTML(higroExt.last, "Ext");
  92. higroExt.toCSV(higroExt.last, "Ext");
  93. // System.out.println(" Goodbye World!");
  94. }
  95.  
  96. catch (MalformedURLException e) {
  97. System.out.println("Malformed URL: " + e.getMessage());
  98. }
  99. catch (IOException e) {
  100. System.out.println("I/O Error: " + e.getMessage());
  101. }
  102.  
  103.  
  104. }
  105.  
  106. public static void main(String[] args){
  107.  
  108. Timer timer = new Timer();
  109. timer.schedule(new PasswordURLAuthenticatorTimerArduino(), 0,1000*60*4);
  110.  
  111. //AnimatedTimeLineChart.launch(AnimatedTimeLineChart.class,args);
  112.  
  113. }
  114.  
  115. // Authenticator
  116. public static class CustomAuthenticator extends Authenticator {
  117. // Called when password authorization is needed
  118. protected PasswordAuthentication getPasswordAuthentication() {
  119. // Get information about the request
  120. String prompt = getRequestingPrompt();
  121. String hostname = getRequestingHost();
  122. InetAddress ipaddr = getRequestingSite();
  123. int port = getRequestingPort();
  124. String username = "junitec";
  125. String password = "ema.ist";
  126.  
  127. // Return the information (a data holder that is used by Authenticator)
  128. return new PasswordAuthentication(username, password.toCharArray());
  129.  
  130. }
  131.  
  132. }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement