@Override public void start(ILifecycleProgressMonitor monitor) throws SiteWhereException { LOGGER.info("Solr initializing with URL: " + getSolrServerUrl()); setSolrClient(new HttpSolrClient.Builder(getSolrServerUrl()).build()); }
/** * 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."); } } }
@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); } }
/** * 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; }
/** * 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."); } } }
@Override public void start(ILifecycleProgressMonitor monitor) throws SiteWhereException { // Required for filters. super.start(monitor); if (getSolr() == null) { throw new SiteWhereException("No Solr configuration provided to " + getClass().getName()); } try { LOGGER.info("Attempting to ping Solr server to verify availability..."); SolrPingResponse response = getSolr().getSolrClient().ping(); int pingTime = response.getQTime(); LOGGER.info("Solr server location verified. Ping responded in " + pingTime + " ms."); } catch (SolrServerException e) { throw new SiteWhereException("Ping failed. Verify that Solr server is available.", e); } catch (IOException e) { throw new SiteWhereException("Exception in ping. Verify that Solr server is available.", e); } LOGGER.info("Solr event processor indexing events to server at: " + getSolr().getSolrServerUrl()); executor.execute(new SolrDocumentQueueProcessor()); }
@Override public void start(ILifecycleProgressMonitor monitor) throws SiteWhereException { getLogger().info("Solr initializing with URL: " + getSolrConfiguration().getSolrServerUrl()); setSolrClient(new HttpSolrClient.Builder(getSolrConfiguration().getSolrServerUrl()).build()); }
@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); } }
/** * 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()); }
/** * Parse an {@link IDeviceMeasurement} record from a {@link SolrDocument}. * * @param document * @return * @throws SiteWhereException */ protected static IDeviceMeasurement parseMeasurementFromDocument(SolrDocument document) throws SiteWhereException { DeviceMeasurement mx = new DeviceMeasurement(); String name = (String) document.get(ISolrFields.MX_NAME); Double value = (Double) document.get(ISolrFields.MX_VALUE); mx.setName(name); mx.setValue(value); addFieldsFromEventDocument(document, mx); return mx; }
@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; }
@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); } }
/** * 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()); }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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; }