Weerstation.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. GUIboard.clearTop();
  79. GUIboard.clearLeft();
  80. GUIboard.clearRight();
  81. //Screen switcher
  82. timer.scheduleAtFixedRate(new TimerTask() {
  83. public void run() {
  84. if(!wait){
  85. currentScreen++;
  86. }
  87. if(currentScreen == lstScreens.size()){
  88. currentScreen = 0;
  89. }
  90. Grootheid obj = lstScreens.get(currentScreen);
  91. if(graph)
  92. {
  93. obj.displayGraph();
  94. }
  95. else
  96. {
  97. obj.display();
  98. }
  99. }
  100. }, 0, 5*1000);
  101. //Update recent measurement every 60 seconds
  102. timer.scheduleAtFixedRate(new TimerTask() {
  103. public void run() {
  104. meting1 = weerstation1.getMostRecentMeasurement();
  105. for(Grootheid obj : lstScreens){
  106. obj.updateRecent(meting1);
  107. }
  108. }
  109. }, 60*1000, 60*1000);
  110. //Update 24hours every 60 seconds
  111. timer.scheduleAtFixedRate(new TimerTask() {
  112. public void run() {
  113. meting2 = weerstation1.getAllMeasurementsLast24h();
  114. for(Grootheid obj : lstScreens){
  115. obj.update24Hour(meting2);
  116. }
  117. }
  118. }, 10*60*1000, 10*60*1000);
  119. //Button checker
  120. timer.scheduleAtFixedRate(new TimerTask() {
  121. public void run() {
  122. if(IO.readShort(0x100) == 1){
  123. if(IO.readShort(0x80) == 1){
  124. wait = true;
  125. if(!graph)
  126. {
  127. Grootheid obj = lstScreens.get(currentScreen);
  128. obj.displayGraph();
  129. }
  130. graph = true;
  131. }else if(IO.readShort(0x80) == 0){
  132. wait = false;
  133. if(graph)
  134. {
  135. Grootheid obj = lstScreens.get(currentScreen);
  136. obj.display();
  137. }
  138. graph = false;
  139. }
  140. }else if(IO.readShort(0x100) == 0){
  141. if(IO.readShort(0x80) == 1){
  142. if(!graph)
  143. {
  144. Grootheid obj = lstScreens.get(currentScreen);
  145. obj.displayGraph();
  146. }
  147. graph = true;
  148. }else if(IO.readShort(0x80) == 0){
  149. if(graph)
  150. {
  151. Grootheid obj = lstScreens.get(currentScreen);
  152. obj.display();
  153. }
  154. graph = false;
  155. }
  156. wait = true;
  157. }
  158. }
  159. }, 0, 100);
  160. }
  161. public void startAnimatie()
  162. {
  163. starter.scheduleAtFixedRate(new TimerTask() {
  164. public void run() {
  165. startup = true;
  166. GUIboard.clearBottom();
  167. GUIboard.clearTop();
  168. GUIboard.clearLeft();
  169. GUIboard.clearRight();
  170. char[] charray;
  171. if(startupState)
  172. {
  173. charray = " Weerstation Breda\n Connecting".toCharArray();
  174. }
  175. else
  176. {
  177. charray = " Weerstation Breda\n Calculating".toCharArray();
  178. }
  179. for(char ch : charray)
  180. {
  181. IO.writeShort(0x40, ch);
  182. }
  183. int p = 1;
  184. for(int i=0; i<128;i++)
  185. {
  186. IO.writeShort(0x42, 1 << 12 | i << 5 | 26);
  187. if(i%21==0)
  188. {
  189. p = p << 1;
  190. if(p>32)
  191. {
  192. p=1;
  193. }
  194. for(int q=0x10; q<0x40; q+=0x02)
  195. {
  196. IO.writeShort(q, 0x100 | p);
  197. }
  198. }
  199. IO.delay(6);
  200. }
  201. startup = false;
  202. }
  203. }, 0, 128*6 + 2);
  204. }
  205. public void stopAnimatie()
  206. {
  207. starter.cancel();
  208. }
  209. }