@Override public void initialize(ILifecycleProgressMonitor monitor) throws SiteWhereException { // Use parameters to build connection string. this.connectionStringBuilder = new ConnectionStringBuilder().setNamespaceName(namespaceNameParameter.getValue()) .setEventHubName(eventHubNameParameter.getValue()).setSasKeyName(sasKeyNameParameter.getValue()) .setSasKey(sasKeyParameter.getValue()); // Use parameters to build event processor host. this.eventProcessorHost = new EventProcessorHost( EventProcessorHost.createHostName(hostNamePrefixParameter.getValue()), eventHubNameParameter.getValue(), consumerGroupNameParameter.getValue(), connectionStringBuilder.toString(), storageConnectionStringParameter.getValue(), storageContainerNameParameter.getValue()); }
/** * Perform validations on configured parameters. * * @throws SiteWhereException */ protected void validateParameters() throws SiteWhereException { for (ILifecycleComponentParameter<?> parameter : getParameters()) { // Validate that required parameters were provided. if ((parameter.isRequired()) && (parameter.getValue() == null)) { throw new SiteWhereException("No value provided for required parameter '" + parameter.getName() + "'. Unable to initialize component."); } } }
/** * Get MongoDB database client. * * @return * @throws SiteWhereException */ public MongoDatabase getDatabase() throws SiteWhereException { return getMongoClient().getDatabase(getDatabaseName().getValue()); }
/** * Parse hostname(s) and port(s) into {@link ServerAddress} entries. * * @return * @throws SiteWhereException */ protected List<ServerAddress> parseServerAddresses() throws SiteWhereException { String[] hosts = getHostname().getValue().split(","); String[] ports = getConfiguration().getPort().split(","); if (hosts.length != ports.length) { throw new SiteWhereException("Number of hosts does not match number of ports. Hosts(" + arrayAsString(hosts) + ") Ports(" + arrayAsString(ports) + ")."); } List<ServerAddress> addresses = new ArrayList<ServerAddress>(); for (int i = 0; i < hosts.length; i++) { try { addresses.add(new ServerAddress(hosts[i].trim(), Integer.parseInt(ports[i].trim()))); } catch (NumberFormatException e) { throw new SiteWhereException("Non-numeric port number specified for MQTT broker."); } } return addresses; }
/** * Get an event by unique id. * * @param eventId * @param client * @return * @throws SiteWhereException */ public static IDeviceEvent getEventById(UUID eventId, InfluxDbClient client) throws SiteWhereException { Query query = new Query( "SELECT * FROM " + InfluxDbDeviceEvent.COLLECTION_EVENTS + " where " + EVENT_ID + "='" + eventId + "'", client.getDatabase().getValue()); QueryResult response = client.getInflux().query(query, TimeUnit.MILLISECONDS); List<IDeviceEvent> results = InfluxDbDeviceEvent.eventsOfType(response, IDeviceEvent.class); if (results.size() > 0) { return results.get(0); } return null; }
/** * Get an event by alternate id. * * @param alternateId * @param client * @return * @throws SiteWhereException */ public static IDeviceEvent getEventByAlternateId(String alternateId, InfluxDbClient client) throws SiteWhereException { Query query = new Query("SELECT * FROM " + InfluxDbDeviceEvent.COLLECTION_EVENTS + " where " + ALTERNATE_ID + "='" + alternateId + "'", client.getDatabase().getValue()); QueryResult response = client.getInflux().query(query, TimeUnit.MILLISECONDS); List<IDeviceEvent> results = InfluxDbDeviceEvent.eventsOfType(response, IDeviceEvent.class); if (results.size() > 0) { return results.get(0); } return null; }
@Override public ISearchResults<IDeviceCommandResponse> listDeviceCommandInvocationResponses(UUID invocationId) throws SiteWhereException { return InfluxDbDeviceCommandResponse.getResponsesForInvocation(invocationId, getClient().getInflux(), getClient().getDatabase().getValue()); }
/** * Search for of events of a given type associated with one or more entities for * a given index. * * @param index * @param entityIds * @param type * @param criteria * @param client * @param clazz * @return * @throws SiteWhereException */ public static <T> SearchResults<T> searchByIndex(DeviceEventIndex index, List<UUID> entityIds, DeviceEventType type, ISearchCriteria criteria, InfluxDbClient client, Class<T> clazz) throws SiteWhereException { Query query = InfluxDbDeviceEvent.queryEventsOfTypeForIndex(index, type, entityIds, criteria, client.getDatabase().getValue()); LOGGER.debug("Query: " + query.getCommand()); QueryResult response = client.getInflux().query(query, TimeUnit.MILLISECONDS); List<T> results = InfluxDbDeviceEvent.eventsOfType(response, clazz); Query countQuery = InfluxDbDeviceEvent.queryEventsOfTypeForIndexCount(index, type, entityIds, criteria, client.getDatabase().getValue()); LOGGER.debug("Count: " + countQuery.getCommand()); QueryResult countResponse = client.getInflux().query(countQuery); long count = parseCount(countResponse); return new SearchResults<T>(results, count); }
@Override public List<IDeviceMeasurement> addDeviceMeasurements(UUID deviceAssignmentId, IDeviceMeasurementCreateRequest... requests) throws SiteWhereException { List<IDeviceMeasurement> result = new ArrayList<>(); IDeviceAssignment assignment = assertDeviceAssignmentById(deviceAssignmentId); for (IDeviceMeasurementCreateRequest request : requests) { DeviceMeasurement mxs = DeviceEventManagementPersistence.deviceMeasurementCreateLogic(request, assignment); Point.Builder builder = InfluxDbDeviceEvent.createBuilder(); InfluxDbDeviceMeasurements.saveToBuilder(mxs, builder); addUserDefinedTags(assignment, builder); getClient().getInflux().write(getClient().getDatabase().getValue(), getAssignmentSpecificRetentionPolicy(assignment), builder.build()); result.add(mxs); } return result; }
@Override public List<IDeviceLocation> addDeviceLocations(UUID deviceAssignmentId, IDeviceLocationCreateRequest... requests) throws SiteWhereException { List<IDeviceLocation> result = new ArrayList<>(); IDeviceAssignment assignment = assertDeviceAssignmentById(deviceAssignmentId); for (IDeviceLocationCreateRequest request : requests) { DeviceLocation location = DeviceEventManagementPersistence.deviceLocationCreateLogic(assignment, request); Point.Builder builder = InfluxDbDeviceEvent.createBuilder(); InfluxDbDeviceLocation.saveToBuilder(location, builder); addUserDefinedTags(assignment, builder); getClient().getInflux().write(getClient().getDatabase().getValue(), getAssignmentSpecificRetentionPolicy(assignment), builder.build()); result.add(location); } return result; }
@Override public List<IDeviceAlert> addDeviceAlerts(UUID deviceAssignmentId, IDeviceAlertCreateRequest... requests) throws SiteWhereException { List<IDeviceAlert> result = new ArrayList<>(); IDeviceAssignment assignment = assertDeviceAssignmentById(deviceAssignmentId); for (IDeviceAlertCreateRequest request : requests) { DeviceAlert alert = DeviceEventManagementPersistence.deviceAlertCreateLogic(assignment, request); Point.Builder builder = InfluxDbDeviceEvent.createBuilder(); InfluxDbDeviceAlert.saveToBuilder(alert, builder); addUserDefinedTags(assignment, builder); getClient().getInflux().write(getClient().getDatabase().getValue(), getAssignmentSpecificRetentionPolicy(assignment), builder.build()); result.add(alert); } return result; }
@Override public List<IDeviceStateChange> addDeviceStateChanges(UUID deviceAssignmentId, IDeviceStateChangeCreateRequest... requests) throws SiteWhereException { List<IDeviceStateChange> result = new ArrayList<>(); IDeviceAssignment assignment = assertDeviceAssignmentById(deviceAssignmentId); for (IDeviceStateChangeCreateRequest request : requests) { DeviceStateChange sc = DeviceEventManagementPersistence.deviceStateChangeCreateLogic(assignment, request); Point.Builder builder = InfluxDbDeviceEvent.createBuilder(); InfluxDbDeviceStateChange.saveToBuilder(sc, builder); addUserDefinedTags(assignment, builder); getClient().getInflux().write(getClient().getDatabase().getValue(), getAssignmentSpecificRetentionPolicy(assignment), builder.build()); result.add(sc); } return result; }
@Override public List<IDeviceCommandResponse> addDeviceCommandResponses(UUID deviceAssignmentId, IDeviceCommandResponseCreateRequest... requests) throws SiteWhereException { List<IDeviceCommandResponse> result = new ArrayList<>(); IDeviceAssignment assignment = assertDeviceAssignmentById(deviceAssignmentId); for (IDeviceCommandResponseCreateRequest request : requests) { DeviceCommandResponse cr = DeviceEventManagementPersistence.deviceCommandResponseCreateLogic(assignment, request); Point.Builder builder = InfluxDbDeviceEvent.createBuilder(); InfluxDbDeviceCommandResponse.saveToBuilder(cr, builder); addUserDefinedTags(assignment, builder); getClient().getInflux().write(getClient().getDatabase().getValue(), getAssignmentSpecificRetentionPolicy(assignment), builder.build()); result.add(cr); } return result; }
@Override public List<IDeviceCommandInvocation> addDeviceCommandInvocations(UUID deviceAssignmentId, IDeviceCommandInvocationCreateRequest... requests) throws SiteWhereException { List<IDeviceCommandInvocation> result = new ArrayList<>(); IDeviceAssignment assignment = assertDeviceAssignmentById(deviceAssignmentId); for (IDeviceCommandInvocationCreateRequest request : requests) { DeviceCommandInvocation ci = DeviceEventManagementPersistence.deviceCommandInvocationCreateLogic(assignment, request); Point.Builder builder = InfluxDbDeviceEvent.createBuilder(); InfluxDbDeviceCommandInvocation.saveToBuilder(ci, builder); addUserDefinedTags(assignment, builder); getClient().getInflux().write(getClient().getDatabase().getValue(), getAssignmentSpecificRetentionPolicy(assignment), builder.build()); result.add(ci); } return result; }
+ getClient().getKeyspace().getValue() + ".events_by_id (device_id, event_id, alt_id, event_type, assignment_id, customer_id, area_id, asset_id, event_date, received_date, location, measurement, alert, invocation, response, state_change) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); this.insertDeviceEventByAltId = getClient().getSession().prepare("insert into " + getClient().getKeyspace().getValue() + ".events_by_alt_id (device_id, event_id, alt_id, event_type, assignment_id, customer_id, area_id, asset_id, event_date, received_date, location, measurement, alert, invocation, response, state_change) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); this.insertDeviceEventByAssignment = getClient().getSession().prepare("insert into " + getClient().getKeyspace().getValue() + ".events_by_assignment (device_id, bucket, event_id, alt_id, event_type, assignment_id, customer_id, area_id, asset_id, event_date, received_date, location, measurement, alert, invocation, response, state_change) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); this.insertDeviceEventByCustomer = getClient().getSession().prepare("insert into " + getClient().getKeyspace().getValue() + ".events_by_customer (device_id, bucket, event_id, alt_id, event_type, assignment_id, customer_id, area_id, asset_id, event_date, received_date, location, measurement, alert, invocation, response, state_change) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); this.insertDeviceEventByArea = getClient().getSession().prepare("insert into " + getClient().getKeyspace().getValue() + ".events_by_area (device_id, bucket, event_id, alt_id, event_type, assignment_id, customer_id, area_id, asset_id, event_date, received_date, location, measurement, alert, invocation, response, state_change) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); this.insertDeviceEventByAsset = getClient().getSession().prepare("insert into " + getClient().getKeyspace().getValue() + ".events_by_asset (device_id, bucket, event_id, alt_id, event_type, assignment_id, customer_id, area_id, asset_id, event_date, received_date, location, measurement, alert, invocation, response, state_change) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); this.selectEventsByAssignmentForType = getClient().getSession().prepare("select * from " + getClient().getKeyspace().getValue() + ".events_by_assignment where assignment_id=? and event_type=? and bucket=? and event_date >= ? and event_date <= ?"); this.selectEventsByCustomerForType = getClient().getSession().prepare("select * from " + getClient().getKeyspace().getValue() + ".events_by_customer where customer_id=? and event_type=? and bucket=? and event_date >= ? and event_date <= ?"); this.selectEventsByAreaForType = getClient().getSession().prepare("select * from " + getClient().getKeyspace().getValue() + ".events_by_area where area_id=? and event_type=? and bucket=? and event_date >= ? and event_date <= ?"); this.selectEventsByAssetForType = getClient().getSession().prepare("select * from " + getClient().getKeyspace().getValue() + ".events_by_asset where asset_id=? and event_type=? and bucket=? and event_date >= ? and event_date <= ?");
@Override public void initialize(ILifecycleProgressMonitor monitor) throws SiteWhereException { super.start(monitor); // Verify that contact points were specified. String[] contactPoints = getContactPoints().getValue().split(","); if (contactPoints.length == 0) { throw new SiteWhereException("No contact points specified for Cassandra cluster."); } Cluster.Builder builder = Cluster.builder(); for (String contactPoint : contactPoints) { builder.addContactPoint(contactPoint.trim()); } PoolingOptions pooling = new PoolingOptions(); pooling.setMaxRequestsPerConnection(HostDistance.LOCAL, 32768); pooling.setMaxRequestsPerConnection(HostDistance.REMOTE, 32768); pooling.setMaxQueueSize(32768); builder.withPoolingOptions(pooling); this.cluster = builder.build(); this.session = getCluster().connect(); }
getClient().execute("CREATE KEYSPACE IF NOT EXISTS " + getClient().getKeyspace().getValue() + " WITH replication = {'class':'SimpleStrategy','replication_factor':'1'}"); getClient().execute("USE " + getClient().getKeyspace().getValue() + ";"); getClient().execute("CREATE TYPE IF NOT EXISTS " + getClient().getKeyspace().getValue() + ".sw_location (latitude double, longitude double, elevation double);"); this.locationType = getClient().getSession().getCluster().getMetadata() .getKeyspace(getClient().getKeyspace().getValue()).getUserType("sw_location"); getClient().execute("CREATE TYPE IF NOT EXISTS " + getClient().getKeyspace().getValue() + ".sw_measurement (mxname text, mxvalue double);"); this.measurementType = getClient().getSession().getCluster().getMetadata() .getKeyspace(getClient().getKeyspace().getValue()).getUserType("sw_measurement"); getClient().execute("CREATE TYPE IF NOT EXISTS " + getClient().getKeyspace().getValue() + ".sw_alert (source tinyint, level tinyint, type text, message text);"); this.alertType = getClient().getSession().getCluster().getMetadata() .getKeyspace(getClient().getKeyspace().getValue()).getUserType("sw_alert"); getClient().execute("CREATE TYPE IF NOT EXISTS " + getClient().getKeyspace().getValue() + ".sw_invocation (initiator tinyint, initiator_id text, target tinyint, target_id text, command_token text, command_params map<text, text>);"); this.invocationType = getClient().getSession().getCluster().getMetadata() .getKeyspace(getClient().getKeyspace().getValue()).getUserType("sw_invocation"); getClient().execute("CREATE TYPE IF NOT EXISTS " + getClient().getKeyspace().getValue() + ".sw_response (orig_event_id uuid, resp_event_id uuid, response text);"); this.responseType = getClient().getSession().getCluster().getMetadata() .getKeyspace(getClient().getKeyspace().getValue()).getUserType("sw_response");
builder.connectionsPerHost(10); getLogger().info("MongoDB Connection: hosts=" + getHostname().getValue() + " ports=" + getConfiguration().getPort() + " replicaSet=" + getConfiguration().getReplicaSetName()); } catch (MongoTimeoutException e) { getLogger().warn("Timed out connecting to MongoDB. Will attempt to reconnect to " + getHostname().getValue() + ":" + getConfiguration().getPort() + ".", e);
@Override public void initialize(ILifecycleProgressMonitor monitor) throws SiteWhereException { super.start(monitor); String connectionUrl = "http://" + getHostname().getValue() + ":" + getConfiguration().getPort(); this.influx = InfluxDBFactory.connect(connectionUrl, getConfiguration().getUsername(), getConfiguration().getPassword()); influx.createDatabase(getDatabase().getValue()); if (getConfiguration().isEnableBatch()) { influx.enableBatch(getConfiguration().getBatchChunkSize(), getConfiguration().getBatchIntervalMs(), TimeUnit.MILLISECONDS); } influx.setLogLevel(convertLogLevel(getConfiguration().getLogLevel())); }