View Javadoc

1   /*
2    *  
3    *  Fosstrak LLRP Commander (www.fosstrak.org)
4    * 
5    *  Copyright (C) 2008 ETH Zurich
6    *
7    *  This program is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  This program is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with this program.  If not, see <http://www.gnu.org/licenses/> 
19   *
20   */
21  
22  package org.fosstrak.llrp.client.repository.sql.roaccess;
23  
24  import java.sql.Timestamp;
25  import java.util.LinkedList;
26  import java.util.List;
27  
28  import org.apache.log4j.Logger;
29  import org.llrp.ltk.generated.interfaces.AirProtocolTagData;
30  import org.llrp.ltk.generated.interfaces.EPCParameter;
31  import org.llrp.ltk.generated.messages.RO_ACCESS_REPORT;
32  import org.llrp.ltk.generated.parameters.AccessSpecID;
33  import org.llrp.ltk.generated.parameters.AntennaID;
34  import org.llrp.ltk.generated.parameters.C1G2_CRC;
35  import org.llrp.ltk.generated.parameters.C1G2_PC;
36  import org.llrp.ltk.generated.parameters.ChannelIndex;
37  import org.llrp.ltk.generated.parameters.EPCData;
38  import org.llrp.ltk.generated.parameters.EPC_96;
39  import org.llrp.ltk.generated.parameters.FirstSeenTimestampUTC;
40  import org.llrp.ltk.generated.parameters.FirstSeenTimestampUptime;
41  import org.llrp.ltk.generated.parameters.InventoryParameterSpecID;
42  import org.llrp.ltk.generated.parameters.LastSeenTimestampUTC;
43  import org.llrp.ltk.generated.parameters.LastSeenTimestampUptime;
44  import org.llrp.ltk.generated.parameters.PeakRSSI;
45  import org.llrp.ltk.generated.parameters.ROSpecID;
46  import org.llrp.ltk.generated.parameters.SpecIndex;
47  import org.llrp.ltk.generated.parameters.TagReportData;
48  import org.llrp.ltk.generated.parameters.TagSeenCount;
49  import org.llrp.ltk.types.BitArray_HEX;
50  import org.llrp.ltk.types.Integer96_HEX;
51  
52  /**
53   * Helper class to access and maintain the content of RO_ACCESS_REPORTS.
54   * @author sawielan
55   *
56   */
57  public class ROAccessItem {
58  	
59  	// the log4j logger.
60  	private static Logger log = Logger.getLogger(ROAccessItem.class);
61  	
62  	// the log time.
63  	private Timestamp logTime;
64  	
65  	// the name of the adapter.
66  	private String adapterName;
67  	
68  	// the name of the reader.
69  	private String readerName;
70  	
71  	// the EPC.
72  	private String epc;
73  	
74  	// the ROSpecID
75  	private Long roSpecID;
76  	
77  	// spec index.
78  	private Integer specIndex;
79  	
80  	// inventory parameter spec ID.
81  	private Integer inventoryPrmSpecID;
82  	
83  	// antenna ID.
84  	private Integer antennaID;
85  	
86  	// peak RSSI.
87  	private Short peakRSSI;
88  	
89  	// channel index.
90  	private Integer channelIndex;
91  	
92  	// the first seen UTC time stamp.
93  	private Timestamp firstSeenUTC;
94  	
95  	// the first seen since uptime time stamp.
96  	private Timestamp firstSeenUptime;
97  	
98  	// the last seen time stamp UTC.
99  	private Timestamp lastSeenUTC;
100 	
101 	// the last seen time stamp since uptime.
102 	private Timestamp lastSeenUptime;
103 	
104 	// the tag count.
105 	private Integer tagSeenCount;
106 	
107 	// c1g2_crc
108 	private Integer c1g2_CRC;
109 	
110 	// c1g2_pc
111 	private Integer c1g2_PC;
112 	
113 	// the access spec ID.
114 	private Long accessSpecID;
115 	
116 	/**
117 	 * parses the entries of an RO_ACCESS_REPORTS into a list of ROAccessItems.
118 	 * @param report the RO_ACCESS_REPORTS message.
119 	 * @param adapterName the name of the adapter.
120 	 * @param readerName the name of the reader.
121 	 * @return a list of ROAccessItem.
122 	 */
123 	public static List<ROAccessItem> parse(
124 			RO_ACCESS_REPORT report, 
125 			String adapterName, 
126 			String readerName,
127 			long currentTime) {
128 		
129 		List<ROAccessItem> items = new LinkedList<ROAccessItem> ();
130 		
131 		List<TagReportData> tagDataList = report.getTagReportDataList();
132 		
133 		for (TagReportData tagData : tagDataList) {
134 			
135 			ROAccessItem item = new ROAccessItem();
136 			
137 			// log time.
138 			item.setLogTime(new Timestamp(currentTime));
139 			
140 			// adapter name.
141 			item.setAdapterName(adapterName);
142 			
143 			// reader name.
144 			item.setReaderName(readerName);
145 			
146 			// store the EPC as EPC96 or EPCData
147 			EPCParameter epcParameter = tagData.getEPCParameter();
148 			String epc = null;
149 			if (epcParameter instanceof EPC_96) {
150 				EPC_96 epc96 = (EPC_96) epcParameter;
151 				Integer96_HEX hex = epc96.getEPC();
152 				String hx = hex.toString();
153 				epc = hx;
154 			} else if (epcParameter instanceof EPCData){
155 				EPCData epcData = (EPCData) epcParameter;
156 				BitArray_HEX hex = epcData.getEPC();
157 				String hx = hex.toString();
158 				epc = hx;
159 			} else {
160 				log.error("Unknown EPCParameter encountered - ignoring.");
161 			}
162 			item.setEpc(epc);
163 			
164 			// RO Spec ID.
165 			ROSpecID roSpecID = tagData.getROSpecID();
166 			if ((null != roSpecID) && (null != roSpecID.getROSpecID())) {
167 				item.setRoSpecID(roSpecID.getROSpecID().toLong());
168 			}
169 			
170 			// spec index.
171 			SpecIndex specIndex = tagData.getSpecIndex();
172 			if ((null != specIndex) && (null != specIndex.getSpecIndex())) {
173 				item.setSpecIndex(specIndex.getSpecIndex().toInteger());
174 			}
175 			
176 			// inventory parameter spec ID.
177 			InventoryParameterSpecID inventoryPrmSpecID = 
178 				tagData.getInventoryParameterSpecID();
179 			if ((null != inventoryPrmSpecID) && 
180 					(null != inventoryPrmSpecID.getInventoryParameterSpecID())) {
181 				item.setInventoryPrmSpecID( 
182 						tagData.getInventoryParameterSpecID().
183 						getInventoryParameterSpecID().toInteger());	
184 			}
185 			
186 			// antenna ID.
187 			AntennaID antennaID = tagData.getAntennaID();
188 			if ((null != antennaID) && (null != antennaID.getAntennaID())) {
189 				item.setAntennaID(antennaID.getAntennaID().toInteger());
190 			}
191 			
192 			// peak RSSI.
193 			PeakRSSI peakRSSI = tagData.getPeakRSSI();
194 			if ((null != peakRSSI) && (null != peakRSSI.getPeakRSSI())) {
195 				item.setPeakRSSI(new Short(peakRSSI.getPeakRSSI().toByte()));
196 			}
197 			
198 			// channel index.
199 			ChannelIndex channelIndex = tagData.getChannelIndex();
200 			if ((null != channelIndex) && (null != channelIndex.getChannelIndex())) {
201 				item.setChannelIndex(channelIndex.getChannelIndex().toInteger());
202 			}
203 			
204 			// extract the first seen UTC time stamp.
205 			FirstSeenTimestampUTC frstSnUTC = 
206 				tagData.getFirstSeenTimestampUTC();
207 			if ((null != frstSnUTC) && (null != frstSnUTC.getMicroseconds())) {
208 				item.setFirstSeenUTC( 
209 						AbstractSQLROAccessReportsRepository.extractTimestamp(
210 								frstSnUTC.getMicroseconds()));
211 			}
212 			
213 			// extract the first seen since uptime time stamp.
214 			FirstSeenTimestampUptime frstSnUptime = 
215 				tagData.getFirstSeenTimestampUptime();
216 			if ((null != frstSnUptime) && (null != frstSnUptime.getMicroseconds())) {
217 				item.setFirstSeenUptime( 
218 						AbstractSQLROAccessReportsRepository.extractTimestamp(
219 								frstSnUptime.getMicroseconds()));
220 			} 
221 			
222 			// extract the last seen time stamp UTC.
223 			LastSeenTimestampUTC lstSnUTC = 
224 				tagData.getLastSeenTimestampUTC();
225 			if ((null != lstSnUTC) && (null != lstSnUTC.getMicroseconds())) {
226 				item.setLastSeenUTC(
227 						AbstractSQLROAccessReportsRepository.extractTimestamp(
228 								lstSnUTC.getMicroseconds()));
229 			}
230 			
231 			// extract the last seen time stamp since uptime.
232 			LastSeenTimestampUptime lstSnUptime = 
233 				tagData.getLastSeenTimestampUptime();
234 			if ((null != lstSnUptime) && (null != lstSnUptime.getMicroseconds())) {
235 				item.setLastSeenUptime(
236 						AbstractSQLROAccessReportsRepository.extractTimestamp(
237 								lstSnUptime.getMicroseconds()));
238 			}
239 			
240 			// extract the tag count.
241 			TagSeenCount tagSeenCount = tagData.getTagSeenCount();
242 			if ((null != tagSeenCount) && (null != tagSeenCount.getTagCount())) {
243 				item.setTagSeenCount(tagSeenCount.getTagCount().toInteger());
244 			}
245 			
246 			List<AirProtocolTagData> airProtoTagData = 
247 				tagData.getAirProtocolTagDataList();
248 			
249 			for (AirProtocolTagData aptd : airProtoTagData) {
250 				if (aptd instanceof C1G2_CRC) {
251 					C1G2_CRC c1g2_crc = (C1G2_CRC) aptd;
252 					if ((null != c1g2_crc) && (null != c1g2_crc.getCRC())) {
253 						item.setC1g2_CRC(c1g2_crc.getCRC().toInteger());
254 					}
255 				} else if (aptd instanceof C1G2_PC) {
256 					C1G2_PC c1g2_pc = (C1G2_PC) aptd;
257 					if ((null != c1g2_pc) && (null != c1g2_pc.getPC_Bits())) {
258 						item.setC1g2_PC(c1g2_pc.getPC_Bits().toInteger());
259 					}
260 				} else {
261 					log.error("Unknown AirProtocolTagData item encountered.");
262 				}						
263 			}
264 			
265 			// extract the access spec ID.
266 			AccessSpecID accessSpecID = tagData.getAccessSpecID();
267 			if ((null != accessSpecID) && 
268 					(null != accessSpecID.getAccessSpecID())) {
269 				item.setAccessSpecID(accessSpecID.getAccessSpecID().toLong());
270 			}
271 			
272 			items.add(item);
273 		}
274 		
275 		return items;
276 	}
277 
278 	/**
279 	 * @param logTime the logTime to set
280 	 */
281 	public void setLogTime(Timestamp logTime) {
282 		this.logTime = logTime;
283 	}
284 
285 	/**
286 	 * @return the logTime
287 	 */
288 	public Timestamp getLogTime() {
289 		return logTime;
290 	}
291 
292 	/**
293 	 * @param adapterName the adapterName to set
294 	 */
295 	public void setAdapterName(String adapterName) {
296 		this.adapterName = adapterName;
297 	}
298 
299 	/**
300 	 * @return the adapterName
301 	 */
302 	public String getAdapterName() {
303 		return adapterName;
304 	}
305 
306 	/**
307 	 * @param readerName the readerName to set
308 	 */
309 	public void setReaderName(String readerName) {
310 		this.readerName = readerName;
311 	}
312 
313 	/**
314 	 * @return the readerName
315 	 */
316 	public String getReaderName() {
317 		return readerName;
318 	}
319 
320 	/**
321 	 * @param epc the epc to set
322 	 */
323 	public void setEpc(String epc) {
324 		this.epc = epc;
325 	}
326 
327 	/**
328 	 * @return the epc
329 	 */
330 	public String getEpc() {
331 		return epc;
332 	}
333 
334 	/**
335 	 * @param roSpecID the roSpecID to set
336 	 */
337 	public void setRoSpecID(Long roSpecID) {
338 		this.roSpecID = roSpecID;
339 	}
340 
341 	/**
342 	 * @return the roSpecID
343 	 */
344 	public Long getRoSpecID() {
345 		return roSpecID;
346 	}
347 
348 	/**
349 	 * @param specIndex the specIndex to set
350 	 */
351 	public void setSpecIndex(Integer specIndex) {
352 		this.specIndex = specIndex;
353 	}
354 
355 	/**
356 	 * @return the specIndex
357 	 */
358 	public Integer getSpecIndex() {
359 		return specIndex;
360 	}
361 
362 	/**
363 	 * @param inventoryPrmSpecID the inventoryPrmSpecID to set
364 	 */
365 	public void setInventoryPrmSpecID(Integer inventoryPrmSpecID) {
366 		this.inventoryPrmSpecID = inventoryPrmSpecID;
367 	}
368 
369 	/**
370 	 * @return the inventoryPrmSpecID
371 	 */
372 	public Integer getInventoryPrmSpecID() {
373 		return inventoryPrmSpecID;
374 	}
375 
376 	/**
377 	 * @param antennaID the antennaID to set
378 	 */
379 	public void setAntennaID(Integer antennaID) {
380 		this.antennaID = antennaID;
381 	}
382 
383 	/**
384 	 * @return the antennaID
385 	 */
386 	public Integer getAntennaID() {
387 		return antennaID;
388 	}
389 
390 	/**
391 	 * @param peakRSSI the peakRSSI to set
392 	 */
393 	public void setPeakRSSI(Short peakRSSI) {
394 		this.peakRSSI = peakRSSI;
395 	}
396 
397 	/**
398 	 * @return the peakRSSI
399 	 */
400 	public Short getPeakRSSI() {
401 		return peakRSSI;
402 	}
403 
404 	/**
405 	 * @return the channelIndex
406 	 */
407 	public Integer getChannelIndex() {
408 		return channelIndex;
409 	}
410 
411 	/**
412 	 * @param channelIndex the channelIndex to set
413 	 */
414 	public void setChannelIndex(Integer channelIndex) {
415 		this.channelIndex = channelIndex;
416 	}
417 
418 	/**
419 	 * @return the firstSeenUTC
420 	 */
421 	public Timestamp getFirstSeenUTC() {
422 		return firstSeenUTC;
423 	}
424 
425 	/**
426 	 * @param firstSeenUTC the firstSeenUTC to set
427 	 */
428 	public void setFirstSeenUTC(Timestamp firstSeenUTC) {
429 		this.firstSeenUTC = firstSeenUTC;
430 	}
431 
432 	/**
433 	 * @return the firstSeenUptime
434 	 */
435 	public Timestamp getFirstSeenUptime() {
436 		return firstSeenUptime;
437 	}
438 
439 	/**
440 	 * @param firstSeenUptime the firstSeenUptime to set
441 	 */
442 	public void setFirstSeenUptime(Timestamp firstSeenUptime) {
443 		this.firstSeenUptime = firstSeenUptime;
444 	}
445 
446 	/**
447 	 * @return the lastSeenUTC
448 	 */
449 	public Timestamp getLastSeenUTC() {
450 		return lastSeenUTC;
451 	}
452 
453 	/**
454 	 * @param lastSeenUTC the lastSeenUTC to set
455 	 */
456 	public void setLastSeenUTC(Timestamp lastSeenUTC) {
457 		this.lastSeenUTC = lastSeenUTC;
458 	}
459 
460 	/**
461 	 * @return the lastSeenUptime
462 	 */
463 	public Timestamp getLastSeenUptime() {
464 		return lastSeenUptime;
465 	}
466 
467 	/**
468 	 * @param lastSeenUptime the lastSeenUptime to set
469 	 */
470 	public void setLastSeenUptime(Timestamp lastSeenUptime) {
471 		this.lastSeenUptime = lastSeenUptime;
472 	}
473 
474 	/**
475 	 * @return the tagSeenCount
476 	 */
477 	public Integer getTagSeenCount() {
478 		return tagSeenCount;
479 	}
480 
481 	/**
482 	 * @param tagSeenCount the tagSeenCount to set
483 	 */
484 	public void setTagSeenCount(Integer tagSeenCount) {
485 		this.tagSeenCount = tagSeenCount;
486 	}
487 
488 	/**
489 	 * @return the c1g2_CRC
490 	 */
491 	public Integer getC1g2_CRC() {
492 		return c1g2_CRC;
493 	}
494 
495 	/**
496 	 * @param c1g2CRC the c1g2_CRC to set
497 	 */
498 	public void setC1g2_CRC(Integer c1g2CRC) {
499 		c1g2_CRC = c1g2CRC;
500 	}
501 
502 	/**
503 	 * @return the c1g2_PC
504 	 */
505 	public Integer getC1g2_PC() {
506 		return c1g2_PC;
507 	}
508 
509 	/**
510 	 * @param c1g2PC the c1g2_PC to set
511 	 */
512 	public void setC1g2_PC(Integer c1g2PC) {
513 		c1g2_PC = c1g2PC;
514 	}
515 
516 	/**
517 	 * @return the accessSpecID
518 	 */
519 	public Long getAccessSpecID() {
520 		return accessSpecID;
521 	}
522 
523 	/**
524 	 * @param accessSpecID the accessSpecID to set
525 	 */
526 	public void setAccessSpecID(Long accessSpecID) {
527 		this.accessSpecID = accessSpecID;
528 	}
529 	
530 	/**
531 	 * returns the item requested by the column index used in 
532 	 * {@link AbstractSQLROAccessReportsRepository}. We recommend to use 
533 	 * those constants.
534 	 * @param index the index to use.
535 	 * @return the object requested.
536 	 */
537 	public Object get(int index) {
538 		switch (index) {
539 		case AbstractSQLROAccessReportsRepository.CINDEX_LOGTIME:
540 			return getLogTime();
541 		case AbstractSQLROAccessReportsRepository.CINDEX_ADAPTER:
542 			return getAdapterName();
543 		case AbstractSQLROAccessReportsRepository.CINDEX_READER:
544 			return getReaderName();
545 		case AbstractSQLROAccessReportsRepository.CINDEX_EPC:
546 			return getEpc();
547 		case AbstractSQLROAccessReportsRepository.CINDEX_ROSpecID:
548 			return getRoSpecID();
549 		case AbstractSQLROAccessReportsRepository.CINDEX_SpecIndex:
550 			return getSpecIndex();
551 		case AbstractSQLROAccessReportsRepository.CINDEX_InventoryParameterSpecID:
552 			return getInventoryPrmSpecID();
553 		case AbstractSQLROAccessReportsRepository.CINDEX_AntennaID:
554 			return getAntennaID();
555 		case AbstractSQLROAccessReportsRepository.CINDEX_PeakRSSI:
556 			return getPeakRSSI();
557 		case AbstractSQLROAccessReportsRepository.CINDEX_ChannelIndex:
558 			return getChannelIndex();
559 		case AbstractSQLROAccessReportsRepository.CINDEX_FirstSeenTimestampUTC:
560 			return getFirstSeenUTC();
561 		case AbstractSQLROAccessReportsRepository.CINDEX_FirstSeenTimestampUptime:
562 			return getFirstSeenUptime();
563 		case AbstractSQLROAccessReportsRepository.CINDEX_LastSeenTimestampUTC:
564 			return getLastSeenUTC();
565 		case AbstractSQLROAccessReportsRepository.CINDEX_LastSeenTimestampUptime:
566 			return getLastSeenUptime();
567 		case AbstractSQLROAccessReportsRepository.CINDEX_TagSeenCount:
568 			return getTagSeenCount();
569 		case AbstractSQLROAccessReportsRepository.CINDEX_C1G2_CRC:
570 			return getC1g2_CRC();
571 		case AbstractSQLROAccessReportsRepository.CINDEX_C1G2_PC:
572 			return getC1g2_PC();
573 		case AbstractSQLROAccessReportsRepository.CINDEX_AccessSpecID:
574 			return getAccessSpecID();
575 		}
576 		return null;
577 	}
578 	
579 	/**
580 	 * returns a string representation of the requested element. use the indices 
581 	 * defined in {@link AbstractSQLROAccessReportsRepository}.
582 	 * @param index the column index to use.
583 	 * @return the string representation or null if exception.
584 	 */
585 	public String getAsString(int index) {
586 		String ret;
587 		try {
588 			return get(index).toString();
589 		} catch (Exception e) {
590 			// null pointer if object is null
591 			ret = null;
592 		}
593 		return ret;
594 	}
595 	
596 	/**
597 	 * @return generates a comma-separated-values representation of this item.
598 	 */
599 	public String getAsCSV() {
600 		StringBuffer str = new StringBuffer();
601 		final int len = AbstractSQLROAccessReportsRepository.NUM_COLUMNS + 1;
602 		for (int i=1; i<len; i++) {
603 			String s = getAsString(i);
604 			if (null != s) {
605 				str.append(s);
606 			}
607 			str.append(",");
608 		}
609 		return str.toString();
610 	}
611 	
612 }