Weerstation.java 8.8 KB

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