Weerstation.java 8.1 KB

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