Weerstation.java 2.7 KB

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