⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readreportnotificationlistener.java

📁 关于 RFID 读写器的相关内容
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

						sourceInfo.setSourceName(rdSourceInfo.getSourceName());
						log.debug("Adding source name " + rdSourceInfo.getSourceName() + " when generating XML instance of notification");
					}
					else {
						log.debug("Neglecting source name when generating XML instance of notification");
					}
					sourceInfo.setSourceProtocol(rdSourceInfo.getSourceProtocol());
					sourceReport.setSourceInfo(sourceInfo);

				}

				//add all the tags
				List tagList = sourceReport.getTag();
				//Collection tags = report.getAllTags().values();
				Collection tags = rdSourceReport.getAllTags().values();
				Iterator tagIt = tags.iterator();
				while (tagIt.hasNext()) {
					org.fosstrak.reader.rprm.core.readreport.TagType rdTag = (org.fosstrak.reader.rprm.core.readreport.TagType) tagIt
							.next();
					org.fosstrak.reader.rprm.core.msg.notification.TagType tag = notificationFactory
							.createTagType();
					if (dataSelectorFields != null
							&& (dataSelectorFields.contains(FieldName.TAG_ID)
									|| dataSelectorFields.contains(FieldName.ALL)
									|| dataSelectorFields
											.contains(FieldName.ALL_TAG) || dataSelectorFields
									.contains(FieldName.ALL_SUPPORTED))) {
						tag.setTagID(HexUtil.hexToByteArray(rdTag.getId()));
					}
					tag.setTagIDAsPureURI(rdTag.getIdAsPureURI());
					tag.setTagIDAsTagURI(rdTag.getIdAsTagURI());
					tag.setTagType(rdTag.getTagType());
					getTagEvents(tag, rdTag);
					getTagFields(tag, rdTag);
					tagList.add(tag);
				}
				//add source report to the source list
				sourceList.add(sourceReport);
			}

			notification.getReadReport().add(reportItems);

			return notification;
		} catch (JAXBException e) {
			log.error(e);
			return null;
		}

	}


	/**
	 * Insert all the ReadReports from the ReaderDevice ReadReport into the
	 * ReadReport of the JAXB classes.
	 *
	 * @param msgReadReport
	 *            The JAXB ReadReport
	 * @param rdReadReport
	 *            The ReaderDevice ReadReport
	 * @param dataSelectorFields
	 * @return List with all JABX ReadReports
	 * @throws JAXBException
	 */
	private List getReadReports(List msgReadReport, ReadReport rdReadReport, CompareSet dataSelectorFields) throws JAXBException {
		ReadReportType reportItems = notificationFactory.createReadReportType();
		List sourceList = reportItems.getSourceReport();

		//get all Source reports
		Iterator sourceReportIt = rdReadReport.getSourceReports().values().iterator();
		while (sourceReportIt.hasNext()) {
			org.fosstrak.reader.rprm.core.msg.notification.ReadReportType.SourceReport sourceReport = notificationFactory.createReadReportTypeSourceReport();
			SourceReport rdSourceReport = (SourceReport) sourceReportIt.next();

			//add the source info
			if (rdSourceReport.containsSourceInfo()) {
				SourceInfoType sourceInfo = notificationFactory.createSourceInfoType();
				org.fosstrak.reader.rprm.core.readreport.SourceInfoType rdSourceInfo = rdSourceReport.getSourceInfo();
				if (rdSourceInfo.getSourceFrequency() >= 0) {
					sourceInfo.setSourceFrequency(Integer.toString(rdSourceInfo.getSourceFrequency()));
				}
				if (dataSelectorFields.contains(FieldName.SOURCE_NAME)) {
					sourceInfo.setSourceName(rdSourceInfo.getSourceName());
				}
				sourceInfo.setSourceProtocol(rdSourceInfo.getSourceProtocol());
				sourceReport.setSourceInfo(sourceInfo);
				//sourceList.add(sourceInfo);
			}

			//add all the tags
			List tagList = sourceReport.getTag();
			Collection tags = rdSourceReport.getAllTags().values();
			Iterator it = tags.iterator();
			while(it.hasNext()) {
				org.fosstrak.reader.rprm.core.readreport.TagType rdTag = (org.fosstrak.reader.rprm.core.readreport.TagType)it.next();
				TagType tag = notificationFactory.createTagType();
				if (dataSelectorFields != null && (dataSelectorFields.contains(FieldName.TAG_ID) || dataSelectorFields.contains(FieldName.ALL)  || dataSelectorFields.contains(FieldName.ALL_TAG)  || dataSelectorFields.contains(FieldName.ALL_SUPPORTED))) {
					tag.setTagID(HexUtil.hexToByteArray(rdTag.getId()));
				}
				tag.setTagIDAsPureURI(rdTag.getIdAsPureURI());
				tag.setTagIDAsTagURI(rdTag.getIdAsTagURI());
				tag.setTagType(rdTag.getTagType());
				tagList.add(tag);
			}

			//add source report the the sourceList
			sourceList.add(sourceReport);
		}
		//add read report to the report list
		msgReadReport.add(reportItems);
		return msgReadReport;
	}

	/**
	 * Insert all the tag events from the ReaderDevice TagType into the TagType
	 * of the JAXB classes.
	 *
	 * @param msgTag
	 *            The JAXB TagType
	 * @param rdTag
	 *            The ReaderDevice TagType
	 * @return List with all JAXB TagEvents
	 * @throws JAXBException
	 */
	private List getTagEvents(org.fosstrak.reader.rprm.core.msg.notification.TagType msgTag, org.fosstrak.reader.rprm.core.readreport.TagType rdTag) throws JAXBException {
		List eventList = msgTag.getTagEvent();
		Collection tagEvents = rdTag.getAllTagEvents().values();
		Iterator it = tagEvents.iterator();
		while(it.hasNext()) {
			org.fosstrak.reader.rprm.core.msg.notification.TagEventType tagEvent = notificationFactory.createTagEventType();
			org.fosstrak.reader.rprm.core.readreport.TagEventType rdEvent = (org.fosstrak.reader.rprm.core.readreport.TagEventType)it.next();
			tagEvent.setEventType(rdEvent.getEventType());

			org.fosstrak.reader.rprm.core.msg.notification.EventTimeType time = notificationFactory.createEventTimeType();
			if (rdEvent.getTimeTick() >= 0) {
				time.setEventTimeTick(Long.toString(rdEvent.getTimeTick()));
			}
			if (rdEvent.getTimeUTC() != null) {
				time.setEventTimeUTC(calendarToXMLGregorianCalendar(toCalendar(rdEvent.getTimeUTC())));
			}
			tagEvent.setTime(time);

			EventTriggers eventTriggers = notificationFactory.createTagEventTypeEventTriggers();
			List eventTriggerList = eventTriggers.getTrigger();
			Collection evTriggers = rdEvent.getEventTriggers().values();
			Iterator trgIt = evTriggers.iterator();

			if (!trgIt.hasNext()) {
				eventTriggerList.add("NoTrigger");
			}

			while(trgIt.hasNext()) {
				Trigger rdTrigger = (Trigger)trgIt.next();
				eventTriggerList.add(rdTrigger.getName());
			}

			tagEvent.setEventTriggers(eventTriggers);
			eventList.add(tagEvent);
		}
		return eventList;
	}

	/**
	 * Insert all the tag fields from the ReaderDevice TagField into the
	 * TagField of the JAXB classes.
	 *
	 * @param msgTag
	 *            The JAXB TagType
	 * @param rdTag
	 *            The ReaderDevice TagType
	 * @return List with all JAXB TagFields
	 * @throws JAXBException
	 */
	private List getTagFields(TagType msgTag, org.fosstrak.reader.rprm.core.readreport.TagType rdTag) throws JAXBException {
		List fieldList = msgTag.getTagFields();

		Collection coll = rdTag.getAllTagFields().values();
		Iterator it = coll.iterator();
		while(it.hasNext()) {
			TagFieldValueParamType tfvpReader = (TagFieldValueParamType)it.next();
			org.fosstrak.reader.rprm.core.msg.notification.TagFieldValueParamType tfvp = notificationFactory.createTagFieldValueParamType();
			tfvp.setTagFieldName(tfvpReader.getTagFieldName());
			tfvp.setTagFieldValue(tfvpReader.getTagFieldValue());
			fieldList.add(tfvp);
		}
		return fieldList;
	}

	//TODO: Veschieben in eigenen JUnit Test
	public static void serialisationTest() {
//		Marshal into a StringBuffer
		StringWriter sw = new StringWriter();
		Marshaller notificationMarshaller = null;
		JAXBContext notificationContext = null;
		String NOTIFICATION_PACKAGE = "org.fosstrak.reader.rprm.core.msg.notification";
		org.fosstrak.reader.rprm.core.msg.notification.ObjectFactory notificationFactory = new org.fosstrak.reader.rprm.core.msg.notification.ObjectFactory();

		try {
			notificationContext = JAXBContext.newInstance(NOTIFICATION_PACKAGE);

			notificationMarshaller = notificationContext.createMarshaller();
			notificationMarshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );

			Notification notification = notificationFactory.createNotification();

			notification.setId(Integer.toString(NotificationChannelConnections.nextMessageId()));
			notification.setNotifyChannelName("notifyChannelName");
			notification.setNotifyTriggerName("notifyTriggerName");

			ReaderType reader = notificationFactory.createReaderType();
			reader.setReaderName("Name des Readers");
			reader.setReaderRole("Seine Rolle");
			notification.setReader(reader);


			//Readreport kreiren
			List readReportList = notification.getReadReport();
			ReadReportType report = notificationFactory.createReadReportType();

			//SourceReport und Tag sind in einer xsd:choice, also kann es nur eines von beiden haben!!
			//SourceReport kreiren
			List sourceList = report.getSourceReport();
			ReadReportType.SourceReport source = notificationFactory.createReadReportTypeSourceReport();
			source.getTag(); // TODO: f黮len der tags welche zu dieser source geh鰎en
			SourceInfoType sourceInfo = notificationFactory.createSourceInfoType();
			sourceInfo.setSourceName("IRGENDEINE SOURCE");
			sourceInfo.setSourceProtocol("deren Protokoll");
			source.setSourceInfo(sourceInfo);
			sourceList.add(source);

			//TagReport kreiren
			List tagList = report.getTag();
			TagType tag = notificationFactory.createTagType();
			tag.setTagID(HexUtil.hexToByteArray("F0F0"));
			tag.setTagIDAsPureURI("PureURI");
			tag.setTagIDAsTagURI("PureTagURI");
			tagList.add(tag);



			readReportList.add(report);




			notificationMarshaller.marshal(notification,sw);
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		System.out.println(sw.getBuffer().toString());


	}

	/**
	 * Converts a <code>Date</code> into a <code>Calendar</code> object
	 * using the UTC timezone.
	 *
	 * @param d
	 *            The <code>Date</code>
	 * @return The corresponding <code>Calendar</code>
	 */
	private Calendar toCalendar(Date d) {
		Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+0"));
		cal.setTime(d);
		return cal;
	}

	private XMLGregorianCalendar calendarToXMLGregorianCalendar(Calendar cal) {
		GregorianCalendar gregCal = new GregorianCalendar();
		gregCal.setTimeInMillis(cal.getTimeInMillis());
		return new XMLGregorianCalendarImpl(gregCal);
	}

	public static void main(String[] args) {
		ReadReportNotificationListener.serialisationTest();
	}



}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -