Weerstation.java 7.1 KB

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