Weerstation.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. boolean wait;
  13. public Weerstation(){
  14. weerstation1 = new WeerstationConnector();
  15. meting1 = weerstation1.getMostRecentMeasurement();
  16. meting2 = weerstation1.getAllMeasurementsLast24h();
  17. currentScreen = 0;
  18. wait = true;
  19. IO.init();
  20. Timer timer = new Timer();
  21. //All the different screen classes
  22. final List<Object> lstScreens = new ArrayList<Object>();
  23. lstScreens.add(new AvgWindspeed(meting1, meting2));
  24. lstScreens.add(new RainRate(meting1, meting2));
  25. lstScreens.add(new BinnenTemperatuur(meting1, meting2));
  26. lstScreens.add(new BuitenTemperatuur(meting1, meting2));
  27. lstScreens.add(new InsideHum(meting1, meting2));
  28. lstScreens.add(new OutsideHum(meting1, meting2));
  29. //Screen switcher
  30. timer.scheduleAtFixedRate(new TimerTask() {
  31. public void run() {
  32. if(wait == false){
  33. currentScreen++;
  34. }
  35. if(currentScreen == lstScreens.size()){
  36. currentScreen = 0;
  37. }
  38. Object obj = lstScreens.get(currentScreen);
  39. try {
  40. obj.getClass().getMethod("display").invoke(obj);
  41. } catch (IllegalAccessException | IllegalArgumentException
  42. | InvocationTargetException | NoSuchMethodException
  43. | SecurityException e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. }, 0, 5*1000);
  48. //Update recent measurement every 60 seconds
  49. timer.scheduleAtFixedRate(new TimerTask() {
  50. public void run() {
  51. meting1 = weerstation1.getMostRecentMeasurement();
  52. for(Object obj : lstScreens){
  53. try {
  54. obj.getClass().getMethod("updateRecent", Measurement.class).invoke(obj, meting1 );
  55. } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. }
  60. }, 60*1000, 60*1000);
  61. //Update 24hours every 60 seconds
  62. timer.scheduleAtFixedRate(new TimerTask() {
  63. public void run() {
  64. meting2 = weerstation1.getAllMeasurementsLast24h();
  65. for(Object obj : lstScreens){
  66. try {
  67. obj.getClass().getMethod("update24Hour", ArrayList.class).invoke(obj, meting2 );
  68. } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
  69. e.printStackTrace();
  70. }
  71. }
  72. }
  73. }, 10*60*1000, 10*60*1000);
  74. //Button checker
  75. timer.scheduleAtFixedRate(new TimerTask() {
  76. public void run() {
  77. if(IO.readShort(0x100) == 1){
  78. if(IO.readShort(0x80) == 1){
  79. wait = true;
  80. }else if(IO.readShort(0x80) == 0){
  81. wait = false;
  82. }
  83. }else if(IO.readShort(0x100) == 0){
  84. wait = true;
  85. }
  86. }
  87. }, 0, 100);
  88. }
  89. }