Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jflight/vario/BrVario.java


1   /*
2   Project name:   JFlight
3   Hosted at:     www.sourceforge.net
4   Homepage:    jflight.sourceforge.net
5   Licence:    GNU public licence (GPL)
6   Filename:    BrVario.java
7   Package:    jflight
8    */
9   
10  package jflight.vario;
11  
12  import java.io.*;
13  import java.util.*;
14  import java.text.*;
15  import java.awt.*;
16  import java.awt.event.*;
17  import java.lang.System;
18  import jflight.model.*;
19  
20  /**
21   * Support for the Braeuniger Vario-devices.
22   *
23   *
24   *
25   * @since       JDK1.1.x
26   * @author      Rüdiger Bien
27   *
28   * CVS-section:
29   * @file_version  $Revision: 1.9 $
30   *
31   *
32   * tbd:
33   * - test with marks in the barogram
34   *
35   */
36  public class BrVario
37  extends Vario {
38      
39      //private OutputStream os;
40      //private InputStream is;
41      
42      private int pid;
43      private int bufSize;
44      private int varioCnt;
45      private Date today;
46      private byte msg[];
47      
48      private final int RX_TIMEOUT = 10000; // # of ms
49      //private final int MAX_INT = 2147483647;
50      public final static int SUCCESS             = 0;
51      public final static int RX_ERROR           = -1;
52      public final static int IO_NOT_INITIALIZED  = -2;
53      public final static int NOT_AVAILABLE       = -3;
54      
55      
56      
57  /*
58  Constructor BrVario
59   
60   */
61      public BrVario() {
62          
63          super();
64          
65      }
66      
67      
68      
69      
70      
71      /* Implementation of the abstract base-class. */
72      public int getBarogram(int flightNumber) throws IOException {
73          int cnt = 0;
74          int state = 0;
75          int  b; // in byte
76          int ret = SUCCESS;
77          long t1;
78          long time1=0,
79          time2=0,
80          diff;
81          int num = 0,
82          numBytes = 0,
83          sync=0;
84          int  startTime=0,
85          stopTime=0,
86          startYear=0,
87          startDate=0,
88          month = 0,
89          day = 0,
90          hour = 0,
91          min = 0,
92          height=0,
93          speed,
94          interval=1;
95          int i,
96          oldSpeed=0,
97          newSpeed = 0,
98          speedCnt=0;
99          boolean speedDataAvail = false;
100         Date d;
101         Calendar calTime = new GregorianCalendar();
102         Calendar calDate = new GregorianCalendar();
103         
104         VarioConfig vConfig = new VarioConfig();
105         VarioData vData;
106         baroCont.clearSingleVarioBuffer(0);
107         
108         if( !ioInit ) {
109             // printMessage("BrVario::getFlight(): Internal error: I/O not initialised");
110             return IO_NOT_INITIALIZED;
111         }
112         
113         // printMessage("BrVario::getFlight(): getFlight ...");
114         
115         t1 = System.currentTimeMillis();
116         while( state != 99 ) {
117             // if((debug&0x01)!=0) printDebug(" [" + state + "] " );
118             
119             b = is.read();
120             if (b == -1 ) {
121                 if ((System.currentTimeMillis() - t1) > RX_TIMEOUT ) {
122                     // printMessage("BrVario::getFlight(): timeout!");
123                     ret = RX_ERROR;
124                     state = 99;
125                     continue;
126                     //throw new VarioTimeoutException("Timeout: Vario doesn't reply");
127                 } else {
128                     delay(1);
129                     continue;
130                 }
131             }
132             else cnt++;
133             
134             //if(((debug&0x01)!=0) && (numBytes < 20) ) printDebug(" *" + b);
135             
136             switch (state) {
137                 case 0:
138                     if( (b==48) || (b==49) || (b==50) || (b==51) || (b==52) ) state = 1;
139                     break;
140                 case 1: // sync byte received; wait for 53
141                     if( b==53 ) {
142                         state = 2;
143                         // printMessage("data synched");
144                     }
145                     break;
146                 case 2:
147                     // printDebug("flight number: " + b );
148                     state = 3;
149                     break;
150                 case 3: // bytes to come, high
151                     numBytes = b << 8;
152                     state = 4;
153                     break;
154                 case 4:
155                     numBytes |= b;
156                     numBytes -= 2;
157                     // printMessage("bytes to come: " + numBytes);
158                     state = 5;
159                     num = 0;
160                     break;
161                 case 5: // ser. number, pilot name; drop that for now
162                     num++;
163                     if( num == 27 ) state = 10;
164                     break;
165                 case 10: // startdate high / low
166                     startDate = b << 8;
167                     state = 11;
168                     break;
169                 case 11:
170                     startDate |= b;
171                     // if((debug&0x01)!=0) printDebug("start date: " + startDate);
172                     day = startDate/100;
173                     month = startDate - (day*100);
174                     state = 12;
175                     break;
176                 case 12: // startyear high / low
177                     startYear = b << 8;
178                     state = 13;
179                     break;
180                 case 13:
181                     startYear |= b;
182                     // if((debug&0x01)!=0) printDebug("start year: " + startYear);
183                     calDate.set(startYear,month-1,day,0,0,0);
184                     state = 14;
185                     num = 0;
186                     break;
187                 case 14: // drops
188                     num++;
189                     if( num == 8 ) state = 20;
190                     break;
191                 case 20:
192                     interval = b;
193                     vConfig.interval = interval;
194                     baroCont.setObject(flightNumber, (Object)vConfig);
195                     // if((debug&0x01)!=0) printDebug("interval: " + interval );
196                     state = 21;
197                     break;
198                 case 21: // startTime high / low
199                     startTime = b << 8;
200                     state = 22;
201                     break;
202                 case 22:
203                     startTime |= b;
204                     time1 = (startTime/100)*3600 + (startTime-((startTime/100)*100))*60;
205                     // // if((debug&0x01)!=0) printDebug("start time: " + startTime);
206                     hour = startTime / 100;
207                     min = startTime - (hour*100);
208                     // printMessage("Barogram date " + startYear + " " + month + " " + day + " " + hour + " " + min );
209                     calTime.clear();
210                     calTime.set(startYear,month-1,day,hour,min,0);
211                     d = calTime.getTime();
212                     // printMessage("Barogram starts at (UTC) " + d.toString() );
213                     state = 23;
214                     break;
215                 case 23: // stopTime high / low
216                     stopTime = b << 8;
217                     state = 24;
218                     break;
219                 case 24:
220                     stopTime |= b;
221                     time2 = (stopTime/100)*3600 + (stopTime-((stopTime/100)*100))*60;
222                     diff = time2 - time1; // flight duration in s
223                     // if((debug&0x01)!=0) printDebug("stop time: " + stopTime);
224                     // if((debug&0x01)!=0) printDebug("flight duration: " + diff +"s");
225                     // if((debug&0x01)!=0) printDebug("bytes read so far: " + cnt);
226                     
227                     // now I have to check if it's extended data format or not:
228                     // use flight-time, if interval >= 5s, otherwise the possible error
229                     // is more than the approx. 100 bytes of the new data-format.
230                     if( interval >= 5 ) {
231                         // if((debug&0x01)!=0) printDebug("Data format test 1:");
232                         if( (((numBytes-44)/3)-(diff/interval))<30 ) {
233                             state = 40; // old format
234                             numBytes -= 44;
235                             // if((debug&0x01)!=0) printDebug(">>> pure data following! <<<");
236                         } else {
237                             state = 30; // ext. format
238                             num = 0;
239                             // if((debug&0x01)!=0) printDebug(">>> Probably new data-format! <<<");
240                         }
241                     } else {
242                         // if((debug&0x01)!=0) printDebug("Data format test 2:");
243                         // the number of the remaining bytes has to be a multiple of 3
244                         // 44 bytes read so far since number-of-bytes read:
245                         if( ((numBytes - 46 ) % 3) == 0 ) { //last speed-val. is always missing !
246                             state = 40; // old format
247                             numBytes -= 44;
248                             // if((debug&0x01)!=0) printDebug(">>> pure data following! <<<");
249                         } else {
250                             state = 30; // ext. format
251                             num = 0;
252                             // if((debug&0x01)!=0) printDebug(">>> Probably new data-format! <<<");
253                         }
254                     }
255                     break;
256                     
257                 case 30: // ext. format
258                     // if((debug&0x01)!=0) printDebug(" *" + b);
259                     num++;
260                     if( num == 100 ) {
261                         state = 40;
262                         numBytes -= 144;
263                     }
264                     break;
265                 case 40: // height- and speed-data:
266                     height = b << 8;
267                     numBytes--;
268                     if( numBytes > 0 ) state = 41;
269                     else state = 99;
270                     break;
271                 case 41:
272                     height |= b;
273                     numBytes--;
274                     if( ((height > 6000) && (height < 20000)) ||(height > 26000) ) {
275                         // assuming heights > 6000MSL very unlikely ...
276                         // marked values have an offset of 20000 m
277                         state = 40; // un-synchronised
278                         sync++;
279                         continue;
280                     }
281                     // if((debug&0x01)!=0) printDebug("height: " + height);
282                     if( numBytes > 0 ) state = 42;
283                     else state = 99;
284                     break;
285                 case 42:
286                     speed = b;
287                     // if((debug&0x01)!=0) printDebug("speed: " + speed);
288                     vData = new VarioData();
289                     vData.height = height;
290                     vData.speed = speed;
291                     baroCont.setObject(flightNumber, (Object)vData);
292                     numBytes--;
293                 /* Now I have to check if there are speed-data available
294                 Simple: It seems that in this case all but the first speedvalues
295                 are set to 45; check this; to be improved maybe
296                  */
297                     speedCnt++;
298                     if( (speedCnt > 1) && (speed != 45) ) {
299                         speedDataAvail = true;
300                     }
301                     if( numBytes > 0 ) state = 40;
302                     else state = 99;
303                     break;
304             } // switch
305             
306         } // while
307         
308         // if((debug&0x01)!=0) printDebug("- flight end -");
309         
310         if( baroCont.varioDataAvailable(0) ) {
311             // update config-data:
312             vConfig = baroCont.getConfigFromList(flightNumber);
313             vConfig.time = calTime.getTime().getTime();
314             vConfig.date = calDate.getTime().getTime();
315             // printMessage("time / date = " + vConfig.time + " / " + vConfig.date );
316             /*
317              if( speedDataAvail == false ) {
318                 // if((debug&0x01)!=0) {
319                 //printDebug("No speeddata avilable");
320                 //}
321             } else {
322                 // if((debug&0x01)!=0) printDebug("Speeddata detected");
323             }
324             */
325         }
326         // baroCont.removeNonflightData(flightNumber);
327         
328         // printMessage("bytes left to read: " + numBytes + " bytes really read: = " + (cnt-9) + " sync: " + sync );
329         // printMessage("BrVario::getFlight() Finished normally with ret = " + ret);
330         
331         return ret;
332     }
333     
334     
335     /* Implementation of the abstract base-class. */
336     public int getFlightBook() {
337         // printDebug("getFlightBook(): tbd");
338         return NOT_AVAILABLE;
339     }
340     
341     
342 } // end BrVario