/** * Parse an {@link IDeviceMeasurements} record from a {@link SolrDocument}. * * @param document * @return * @throws SiteWhereException */ protected static IDeviceMeasurements parseMeasurementsFromDocument(SolrDocument document) throws SiteWhereException { DeviceMeasurements measurements = new DeviceMeasurements(); Iterator<String> names = document.getFieldNames().iterator(); int mxLength = ISolrFields.MEASUREMENT_PREFIX.length(); while (names.hasNext()) { String name = names.next(); if (name.startsWith(ISolrFields.MEASUREMENT_PREFIX)) { String metaName = name.substring(mxLength); Double metaValue = (Double) document.get(name); measurements.addOrReplaceMeasurement(metaName, metaValue); } } addFieldsFromEventDocument(document, measurements); return measurements; }
@Override public void onAlert(IDeviceEventContext context, IDeviceAlert alert) throws SiteWhereException { SolrInputDocument document = SiteWhereSolrFactory.createDocumentFromAlert(alert); try { queue.put(document); } catch (InterruptedException e) { throw new SiteWhereException("Interrupted during indexing.", e); } }
@Override public void onLocation(IDeviceEventContext context, IDeviceLocation location) throws SiteWhereException { SolrInputDocument document = SiteWhereSolrFactory.createDocumentFromLocation(location); try { queue.put(document); } catch (InterruptedException e) { throw new SiteWhereException("Interrupted during indexing.", e); } }
/** * Parses a {@link SolrDocument} into a {@link IDeviceEvent}. * * @param document * @return * @throws SiteWhereException */ public static IDeviceEvent parseDocument(SolrDocument document) throws SiteWhereException { String type = (String) document.getFieldValue(ISolrFields.EVENT_TYPE); if (type == null) { throw new SiteWhereException("Solr event does not contain an event type indicator."); } SolrEventType eventType = SolrEventType.valueOf(type); switch (eventType) { case Location: { return parseLocationFromDocument(document); } case Measurement: { return parseMeasurementFromDocument(document); } case Alert: { return parseAlertFromDocument(document); } default: { throw new SiteWhereException("Solr docuemnt contained unknown device event type."); } } }
/** * Parses a {@link SolrDocument} into a {@link IDeviceEvent}. * * @param document * @return * @throws SiteWhereException */ public static IDeviceEvent parseDocument(SolrDocument document) throws SiteWhereException { String type = (String) document.getFieldValue(ISolrFields.EVENT_TYPE); if (type == null) { throw new SiteWhereException("Solr event does not contain an event type indicator."); } SolrEventType eventType = SolrEventType.valueOf(type); switch (eventType) { case Location: { return parseLocationFromDocument(document); } case Measurements: { return parseMeasurementsFromDocument(document); } case Alert: { return parseAlertFromDocument(document); } default: { throw new SiteWhereException("Solr docuemnt contained unknown device event type."); } } }
/** * Create a {@link SolrInputDocument} based on information in * {@link IDeviceMeasurements}. * * @param measurements * @return * @throws SiteWhereException */ public static SolrInputDocument createDocumentFromMeasurements(IDeviceMeasurements measurements) throws SiteWhereException { SolrInputDocument document = new SolrInputDocument(); document.addField(ISolrFields.EVENT_TYPE, SolrEventType.Measurements.name()); addFieldsForEvent(document, measurements); for (String key : measurements.getMeasurements().keySet()) { document.addField(ISolrFields.MEASUREMENT_PREFIX + key, measurements.getMeasurement(key)); } return document; }
/** * Adds common fields from base SiteWhere {@link IDeviceEvent} object. * * @param document * @param event * @throws SiteWhereException */ protected static void addFieldsForEvent(SolrInputDocument document, IDeviceEvent event) throws SiteWhereException { document.addField(ISolrFields.EVENT_ID, event.getId()); document.addField(ISolrFields.DEVICE_ID, event.getDeviceId()); document.addField(ISolrFields.ASSIGNMENT_ID, event.getDeviceAssignmentId()); document.addField(ISolrFields.AREA_ID, event.getAreaId()); document.addField(ISolrFields.ASSET_ID, event.getAssetId()); document.addField(ISolrFields.EVENT_DATE, event.getEventDate()); document.addField(ISolrFields.RECEIVED_DATE, event.getReceivedDate()); addMetadata(document, event.getMetadata()); }
@Override public List<IDeviceEvent> executeQuery(String queryString) throws SiteWhereException { try { LOGGER.debug("About to execute Solr search with query string: " + queryString); List<IDeviceEvent> results = new ArrayList<IDeviceEvent>(); SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery(queryString); QueryResponse response = getSolr().getSolrClient().query(solrQuery); SolrDocumentList docs = response.getResults(); for (SolrDocument doc : docs) { results.add(SiteWhereSolrFactory.parseDocument(doc)); } return results; } catch (SolrServerException e) { throw new SiteWhereException("Unable to execute query.", e); } catch (IOException e) { throw new SiteWhereException("Unable to execute query.", e); } }
@Override public void onMeasurement(IDeviceEventContext context, IDeviceMeasurement mx) throws SiteWhereException { SolrInputDocument document = SiteWhereSolrFactory.createDocumentFromMeasurement(mx); try { queue.put(document); } catch (InterruptedException e) { throw new SiteWhereException("Interrupted during indexing.", e); } }
@Override public void onMeasurementsNotFiltered(IDeviceMeasurements measurements) throws SiteWhereException { SolrInputDocument document = SiteWhereSolrFactory.createDocumentFromMeasurements(measurements); try { queue.put(document); } catch (InterruptedException e) { throw new SiteWhereException("Interrupted during indexing.", e); } }
/** * Create a {@link SolrInputDocument} based on information in * {@link IDeviceMeasurement}. * * @param mx * @return * @throws SiteWhereException */ public static SolrInputDocument createDocumentFromMeasurement(IDeviceMeasurement mx) throws SiteWhereException { SolrInputDocument document = new SolrInputDocument(); document.addField(ISolrFields.EVENT_TYPE, SolrEventType.Measurement.name()); addFieldsForEvent(document, mx); document.addField(ISolrFields.MX_NAME, mx.getName()); document.addField(ISolrFields.MX_VALUE, mx.getValue()); return document; }
/** * Adds common fields from base SiteWhere {@link IDeviceEvent} object. * * @param document * @param event * @throws SiteWhereException */ protected static void addFieldsForEvent(SolrInputDocument document, IDeviceEvent event) throws SiteWhereException { document.addField(ISolrFields.EVENT_ID, event.getId()); document.addField(ISolrFields.ASSIGNMENT_TOKEN, event.getDeviceAssignmentToken()); document.addField(ISolrFields.ASSIGNMENT_TYPE, event.getAssignmentType().name()); document.addField(ISolrFields.ASSET_ID, event.getAssetId()); document.addField(ISolrFields.SITE_TOKEN, event.getSiteToken()); document.addField(ISolrFields.EVENT_DATE, event.getEventDate()); document.addField(ISolrFields.RECEIVED_DATE, event.getReceivedDate()); addMetadata(document, event.getMetadata()); }
@Override public List<IDeviceEvent> executeQuery(String queryString) throws SiteWhereException { try { getLogger().debug("About to execute Solr search with query string: " + queryString); List<IDeviceEvent> results = new ArrayList<IDeviceEvent>(); SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery(queryString); QueryResponse response = getSolrConnection().getSolrClient().query(solrQuery); SolrDocumentList docs = response.getResults(); for (SolrDocument doc : docs) { results.add(SiteWhereSolrFactory.parseDocument(doc)); } return results; } catch (SolrServerException e) { throw new SiteWhereException("Unable to execute query.", e); } catch (IOException e) { throw new SiteWhereException("Unable to execute query.", e); } }
/** * Create a {@link SolrInputDocument} based on information in * {@link IDeviceLocation}. * * @param location * @return * @throws SiteWhereException */ public static SolrInputDocument createDocumentFromLocation(IDeviceLocation location) throws SiteWhereException { SolrInputDocument document = new SolrInputDocument(); document.addField(ISolrFields.EVENT_TYPE, SolrEventType.Location.name()); addFieldsForEvent(document, location); String latLong = "" + location.getLatitude() + ", " + location.getLongitude(); document.addField(ISolrFields.LOCATION, latLong); document.addField(ISolrFields.ELEVATION, location.getElevation()); return document; }
/** * Parse an {@link IDeviceLocation} record from a {@link SolrDocument}. * * @param document * @return * @throws SiteWhereException */ @SuppressWarnings("unchecked") protected static IDeviceLocation parseLocationFromDocument(SolrDocument document) throws SiteWhereException { DeviceLocation location = new DeviceLocation(); List<String> latLong = (List<String>) document.get(ISolrFields.LOCATION); if (latLong == null) { throw new SiteWhereException("Invalid location document. No location data stored."); } String[] split = latLong.get(0).split("[,]"); location.setLatitude(Double.parseDouble(split[0].trim())); location.setLongitude(Double.parseDouble(split[1].trim())); Double elevation = (Double) document.get(ISolrFields.ELEVATION); if (elevation != null) { location.setElevation(elevation); } addFieldsFromEventDocument(document, location); return location; }
@Override public void onLocationNotFiltered(IDeviceLocation location) throws SiteWhereException { SolrInputDocument document = SiteWhereSolrFactory.createDocumentFromLocation(location); try { queue.put(document); } catch (InterruptedException e) { throw new SiteWhereException("Interrupted during indexing.", e); } }
@Override public void onAlertNotFiltered(IDeviceAlert alert) throws SiteWhereException { SolrInputDocument document = SiteWhereSolrFactory.createDocumentFromAlert(alert); try { queue.put(document); } catch (InterruptedException e) { throw new SiteWhereException("Interrupted during indexing.", e); } }
/** * Create a {@link SolrInputDocument} based on information in * {@link IDeviceLocation}. * * @param location * @return * @throws SiteWhereException */ public static SolrInputDocument createDocumentFromLocation(IDeviceLocation location) throws SiteWhereException { SolrInputDocument document = new SolrInputDocument(); document.addField(ISolrFields.EVENT_TYPE, SolrEventType.Location.name()); addFieldsForEvent(document, location); String latLong = "" + location.getLatitude() + ", " + location.getLongitude(); document.addField(ISolrFields.LOCATION, latLong); document.addField(ISolrFields.ELEVATION, location.getElevation()); return document; }
/** * Parse an {@link IDeviceLocation} record from a {@link SolrDocument}. * * @param document * @return * @throws SiteWhereException */ @SuppressWarnings("unchecked") protected static IDeviceLocation parseLocationFromDocument(SolrDocument document) throws SiteWhereException { DeviceLocation location = new DeviceLocation(); List<String> latLong = (List<String>) document.get(ISolrFields.LOCATION); if (latLong == null) { throw new SiteWhereException("Invalid location document. No location data stored."); } String[] split = latLong.get(0).split("[,]"); location.setLatitude(Double.parseDouble(split[0].trim())); location.setLongitude(Double.parseDouble(split[1].trim())); Double elevation = (Double) document.get(ISolrFields.ELEVATION); if (elevation != null) { location.setElevation(elevation); } addFieldsFromEventDocument(document, location); return location; }
/** * Create a {@link SolrInputDocument} based on information in * {@link IDeviceAlert}. * * @param alert * @return * @throws SiteWhereException */ public static SolrInputDocument createDocumentFromAlert(IDeviceAlert alert) throws SiteWhereException { SolrInputDocument document = new SolrInputDocument(); document.addField(ISolrFields.EVENT_TYPE, SolrEventType.Alert.name()); addFieldsForEvent(document, alert); document.addField(ISolrFields.ALERT_TYPE, alert.getType()); document.addField(ISolrFields.ALERT_MESSAGE, alert.getMessage()); document.addField(ISolrFields.ALERT_LEVEL, alert.getLevel().name()); document.addField(ISolrFields.ALERT_SOURCE, alert.getSource().name()); return document; }