Weerstation.java 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package weerstation;
  2. import java.lang.reflect.InvocationTargetException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Timer;
  6. import java.util.TimerTask;
  7. public class Weerstation {
  8. WeerstationConnector weerstation1;
  9. Measurement meting1;
  10. ArrayList<Measurement> meting2;
  11. int currentScreen;
  12. public Weerstation(){
  13. weerstation1 = new WeerstationConnector();
  14. meting1 = weerstation1.getMostRecentMeasurement();
  15. meting2 = weerstation1.getAllMeasurementsLast24h();
  16. currentScreen = -1;
  17. IO.init();
  18. Timer timer = new Timer();
  19. //All the different screen classes
  20. final List<Object> lstScreens = new ArrayList<Object>();
  21. lstScreens.add(new AvgWindspeed(meting1, meting2));
  22. lstScreens.add(new RainRate(meting1, meting2));
  23. //Screen switcher
  24. timer.scheduleAtFixedRate(new TimerTask() {
  25. public void run() {
  26. currentScreen++;
  27. if(currentScreen == lstScreens.size()){
  28. currentScreen = 0;
  29. }
  30. Object obj = lstScreens.get(currentScreen);
  31. try {
  32. obj.getClass().getMethod("display").invoke(obj);
  33. } catch (IllegalAccessException | IllegalArgumentException
  34. | InvocationTargetException | NoSuchMethodException
  35. | SecurityException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }, 0, 5*1000);
  40. //Update recent measurement every 60 seconds
  41. timer.scheduleAtFixedRate(new TimerTask() {
  42. public void run() {
  43. meting1 = weerstation1.getMostRecentMeasurement();
  44. for(Object obj : lstScreens){
  45. try {
  46. obj.getClass().getMethod("updateRecent", Measurement.class).invoke(obj, meting1 );
  47. } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. }
  52. }, 60*1000, 60*1000);
  53. //Update 24hours every 60 seconds
  54. timer.scheduleAtFixedRate(new TimerTask() {
  55. public void run() {
  56. meting2 = weerstation1.getAllMeasurementsLast24h();
  57. for(Object obj : lstScreens){
  58. try {
  59. obj.getClass().getMethod("update24Hour", ArrayList.class).invoke(obj, meting2 );
  60. } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. }
  65. }, 10*60*1000, 10*60*1000);
  66. //Button checker
  67. timer.scheduleAtFixedRate(new TimerTask() {
  68. public void run() {
  69. }
  70. }, 0, 60*1000);
  71. }
  72. }