Weerstation.java 8.4 KB

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