Weerstation.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. import java.util.Calendar;
  7. public class Weerstation {
  8. WeerstationConnector weerstation1;
  9. Calendar now;
  10. Measurement meting1;
  11. Periode periodeDag;
  12. ArrayList<Measurement> meting2;
  13. Timer starter;
  14. int currentScreen;
  15. boolean wait;
  16. boolean graph;
  17. boolean startup;
  18. public Weerstation(){
  19. weerstation1 = new WeerstationConnector();
  20. now = Calendar.getInstance();
  21. periodeDag = new Periode(Calendar.YEAR,Calendar.MONTH,Calendar.DAY_OF_MONTH,Calendar.YEAR,Calendar.MONTH,Calendar.DAY_OF_MONTH);
  22. GUIboard.init();
  23. starter = new Timer();
  24. startAnimatie();
  25. meting1 = weerstation1.getMostRecentMeasurement();
  26. meting2 = weerstation1.getAllMeasurementsBetween(periodeDag.getBeginPeriode(), periodeDag.getEindePeriode());
  27. stopAnimatie();
  28. while(startup)
  29. {
  30. try
  31. {
  32. Thread.sleep(1);
  33. }
  34. catch(InterruptedException e)
  35. {
  36. e.printStackTrace();
  37. }
  38. }
  39. currentScreen = 0;
  40. wait = true;
  41. graph = false;
  42. Timer timer = new Timer();
  43. //All the different screen classes
  44. final List<Grootheid> lstScreens = new ArrayList<Grootheid>();
  45. lstScreens.add(new WindDirection(meting1, meting2));
  46. lstScreens.add(new OutsideTemp(meting1, meting2));
  47. lstScreens.add(new WindChill(meting1, meting2));
  48. lstScreens.add(new OutsideHum(meting1, meting2));
  49. lstScreens.add(new Barometer(meting1, meting2));
  50. lstScreens.add(new AvgWindSpeed(meting1, meting2));
  51. lstScreens.add(new RainRate(meting1, meting2));
  52. lstScreens.add(new InsideTemp(meting1, meting2));
  53. lstScreens.add(new InsideHum(meting1, meting2));
  54. lstScreens.add(new CloudHeight(meting1, meting2));
  55. lstScreens.add(new UVLevel(meting1, meting2));
  56. lstScreens.add(new Zonsterkte(meting1, meting2));
  57. lstScreens.add(new DewPoint(meting1, meting2));
  58. lstScreens.add(new Sun(meting1));
  59. //Screen switcher
  60. timer.scheduleAtFixedRate(new TimerTask() {
  61. public void run() {
  62. if(!wait){
  63. currentScreen++;
  64. }
  65. if(currentScreen == lstScreens.size()){
  66. currentScreen = 0;
  67. }
  68. Grootheid obj = lstScreens.get(currentScreen);
  69. if(graph)
  70. {
  71. obj.displayGraph();
  72. }
  73. else
  74. {
  75. obj.display();
  76. }
  77. }
  78. }, 0, 5*1000);
  79. //Update recent measurement every 60 seconds
  80. timer.scheduleAtFixedRate(new TimerTask() {
  81. public void run() {
  82. meting1 = weerstation1.getMostRecentMeasurement();
  83. for(Grootheid obj : lstScreens){
  84. obj.updateRecent(meting1);
  85. }
  86. }
  87. }, 60*1000, 60*1000);
  88. //Update 24hours every 60 seconds
  89. timer.scheduleAtFixedRate(new TimerTask() {
  90. public void run() {
  91. meting2 = weerstation1.getAllMeasurementsLast24h();
  92. for(Grootheid obj : lstScreens){
  93. obj.update24Hour(meting2);
  94. }
  95. }
  96. }, 10*60*1000, 10*60*1000);
  97. //Button checker
  98. timer.scheduleAtFixedRate(new TimerTask() {
  99. public void run() {
  100. if(IO.readShort(0x100) == 1){
  101. if(IO.readShort(0x80) == 1){
  102. wait = true;
  103. if(!graph)
  104. {
  105. Grootheid obj = lstScreens.get(currentScreen);
  106. obj.displayGraph();
  107. }
  108. graph = true;
  109. }else if(IO.readShort(0x80) == 0){
  110. wait = false;
  111. if(graph)
  112. {
  113. Grootheid obj = lstScreens.get(currentScreen);
  114. obj.display();
  115. }
  116. graph = false;
  117. }
  118. }else if(IO.readShort(0x100) == 0){
  119. if(IO.readShort(0x80) == 1){
  120. if(!graph)
  121. {
  122. Grootheid obj = lstScreens.get(currentScreen);
  123. obj.displayGraph();
  124. }
  125. graph = true;
  126. }else if(IO.readShort(0x80) == 0){
  127. if(graph)
  128. {
  129. Grootheid obj = lstScreens.get(currentScreen);
  130. obj.display();
  131. }
  132. graph = false;
  133. }
  134. wait = true;
  135. }
  136. }
  137. }, 0, 100);
  138. }
  139. public void startAnimatie()
  140. {
  141. starter.scheduleAtFixedRate(new TimerTask() {
  142. public void run() {
  143. startup = true;
  144. GUIboard.clearBottom();
  145. for(int i=1; i<128;i+=2)
  146. {
  147. for(int n=0; n<32;n++)
  148. {
  149. IO.writeShort(0x42, 1 << 12 | i-1 << 5 | n);
  150. IO.writeShort(0x42, 1 << 12 | i << 5 | n);
  151. IO.delay(1);
  152. }
  153. }
  154. startup = false;
  155. }
  156. }, 0, 128*32);
  157. }
  158. public void stopAnimatie()
  159. {
  160. starter.cancel();
  161. }
  162. }