Weerstation.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. stopAnimatie();
  85. while(startup)
  86. {
  87. try
  88. {
  89. Thread.sleep(1);
  90. }
  91. catch(InterruptedException e)
  92. {
  93. e.printStackTrace();
  94. }
  95. }
  96. GUIboard.clearTop();
  97. GUIboard.clearLeft();
  98. GUIboard.clearRight();
  99. //Screen switcher
  100. timer.scheduleAtFixedRate(new TimerTask() {
  101. public void run() {
  102. if(!wait){
  103. currentScreen++;
  104. }
  105. if(currentScreen == lstScreens.size()){
  106. currentScreen = 0;
  107. }
  108. Grootheid obj = lstScreens.get(currentScreen);
  109. if(graph && (graph != currentGraph))
  110. {
  111. obj.displayGraph();
  112. currentGraph = true;
  113. }
  114. else if(!graph)
  115. {
  116. obj.display();
  117. }
  118. }
  119. }, 0, 5*1000);
  120. //Update recent measurement every 60 seconds
  121. timer.scheduleAtFixedRate(new TimerTask() {
  122. public void run() {
  123. meting1 = weerstation1.getMostRecentMeasurement();
  124. for(Grootheid obj : lstScreens){
  125. obj.updateRecent(meting1);
  126. }
  127. }
  128. }, 60*1000, 60*1000);
  129. //Update 24hours every 60 seconds
  130. timer.scheduleAtFixedRate(new TimerTask() {
  131. public void run() {
  132. meting2 = weerstation1.getAllMeasurementsLast24h();
  133. for(Grootheid obj : lstScreens){
  134. obj.updatePeriod(meting2);
  135. }
  136. }
  137. }, 10*60*1000, 10*60*1000);
  138. //Button checker
  139. timer.scheduleAtFixedRate(new TimerTask() {
  140. public void run() {
  141. if(IO.readShort(0x100) == 1){
  142. if(IO.readShort(0x80) == 1){
  143. wait = true;
  144. if(!graph)
  145. {
  146. Grootheid obj = lstScreens.get(currentScreen);
  147. obj.displayGraph();
  148. }
  149. graph = true;
  150. }else if(IO.readShort(0x80) == 0){
  151. wait = false;
  152. if(graph)
  153. {
  154. Grootheid obj = lstScreens.get(currentScreen);
  155. obj.display();
  156. }
  157. graph = false;
  158. }
  159. }else if(IO.readShort(0x100) == 0){
  160. if(IO.readShort(0x80) == 1){
  161. if(!graph)
  162. {
  163. Grootheid obj = lstScreens.get(currentScreen);
  164. obj.displayGraph();
  165. }
  166. graph = true;
  167. }else if(IO.readShort(0x80) == 0){
  168. if(graph)
  169. {
  170. Grootheid obj = lstScreens.get(currentScreen);
  171. obj.display();
  172. }
  173. graph = false;
  174. }
  175. wait = true;
  176. }
  177. }
  178. }, 0, 100);
  179. }
  180. public void startAnimatie()
  181. {
  182. starter.scheduleAtFixedRate(new TimerTask() {
  183. public void run() {
  184. startup = true;
  185. GUIboard.clearBottom();
  186. GUIboard.clearTop();
  187. GUIboard.clearLeft();
  188. GUIboard.clearRight();
  189. char[] charray;
  190. if(startupState)
  191. {
  192. charray = " Weerstation Breda\n Connecting".toCharArray();
  193. }
  194. else
  195. {
  196. charray = " Weerstation Breda\n Calculating".toCharArray();
  197. }
  198. for(char ch : charray)
  199. {
  200. IO.writeShort(0x40, ch);
  201. }
  202. int p = 1;
  203. for(int i=0; i<128;i++)
  204. {
  205. IO.writeShort(0x42, 1 << 12 | i << 5 | 26);
  206. if(i%21==0)
  207. {
  208. p = p << 1;
  209. if(p>32)
  210. {
  211. p=1;
  212. }
  213. for(int q=0x10; q<0x40; q+=0x02)
  214. {
  215. IO.writeShort(q, 0x100 | p);
  216. }
  217. }
  218. IO.delay(6);
  219. }
  220. startup = false;
  221. }
  222. }, 0, 128*6 + 2);
  223. }
  224. public void stopAnimatie()
  225. {
  226. starter.cancel();
  227. }
  228. public void checkData(){
  229. Iterator<Measurement> it = meting2.iterator();
  230. while( it.hasNext() )
  231. {
  232. Measurement m = it.next();
  233. if(m.getRawOutsideTemp() >= 30000)
  234. {
  235. it.remove();
  236. }
  237. }
  238. }
  239. }