/** * Copy information from SPI into Mongo {@link Document}. * * @param source * @param target */ public static void toDocument(IDeviceAssignment source, Document target) { if (source.getActiveDate() != null) { target.append(PROP_ACTIVE_DATE, source.getActiveDate()); } target.append(PROP_ASSET_MODULE_ID, source.getAssetModuleId()); target.append(PROP_ASSET_ID, source.getAssetId()); if (source.getAssignmentType() != null) { target.append(PROP_ASSIGNMENT_TYPE, source.getAssignmentType().name()); } if (source.getReleasedDate() != null) { target.append(PROP_RELEASED_DATE, source.getReleasedDate()); } if (source.getStatus() != null) { target.append(PROP_STATUS, source.getStatus().name()); } target.append(PROP_TOKEN, source.getToken()); target.append(PROP_DEVICE_HARDWARE_ID, source.getDeviceHardwareId()); target.append(PROP_SITE_TOKEN, source.getSiteToken()); if (source.getState() != null) { setState(source.getState(), target); } MongoSiteWhereEntity.toDocument(source, target); MongoMetadataProvider.toDocument(source, target); }
/** * Copy information from SPI into Mongo {@link Document}. * * @param source * @param target */ public static void toDocument(IDeviceAssignment source, Document target) { target.append(PROP_DEVICE_ID, source.getDeviceId()); target.append(PROP_DEVICE_TYPE_ID, source.getDeviceTypeId()); target.append(PROP_CUSTOMER_ID, source.getCustomerId()); target.append(PROP_AREA_ID, source.getAreaId()); target.append(PROP_ASSET_ID, source.getAssetId()); if (source.getActiveDate() != null) { target.append(PROP_ACTIVE_DATE, source.getActiveDate()); } if (source.getReleasedDate() != null) { target.append(PROP_RELEASED_DATE, source.getReleasedDate()); } if (source.getStatus() != null) { target.append(PROP_STATUS, source.getStatus().name()); } MongoPersistentEntity.toDocument(source, target); }
/** * Common creation logic for all device events. * * @param request * @param assignment * @param target * @throws SiteWhereException */ public static void deviceEventCreateLogic(IDeviceEventCreateRequest request, IDeviceAssignment assignment, DeviceEvent target) throws SiteWhereException { target.setId(UUID.randomUUID()); target.setAlternateId(request.getAlternateId()); target.setDeviceId(assignment.getDeviceId()); target.setDeviceAssignmentId(assignment.getId()); target.setCustomerId(assignment.getCustomerId()); target.setAreaId(assignment.getAreaId()); target.setAssetId(assignment.getAssetId()); if (request.getEventDate() != null) { target.setEventDate(request.getEventDate()); } else { target.setEventDate(new Date()); } target.setReceivedDate(new Date()); MetadataProvider.copy(request.getMetadata(), target); }
public Builder(IDeviceAssignment api) { request.setToken(api.getToken()); request.setDeviceHardwareId(api.getDeviceHardwareId()); request.setAssetModuleId(api.getAssetModuleId()); request.setAssetId(api.getAssetId()); request.setAssignmentType(api.getAssignmentType()); if (api.getMetadata() != null) { request.setMetadata(new HashMap<String, String>()); request.getMetadata().putAll(api.getMetadata()); } }
@Override public URI getDeviceAssignmentIdentifier(IDeviceAssignment assignment) throws SiteWhereException { return createUri(SITEWHERE_PROTOCOL + "assignment/" + assignment.getToken()); }
/** * Common creation logic for all device events. * * @param request * @param assignment * @param target * @throws SiteWhereException */ public static void deviceEventCreateLogic(IDeviceEventCreateRequest request, IDeviceAssignment assignment, DeviceEvent target) throws SiteWhereException { target.setAlternateId(request.getAlternateId()); target.setSiteToken(assignment.getSiteToken()); target.setDeviceAssignmentToken(assignment.getToken()); target.setAssignmentType(assignment.getAssignmentType()); target.setAssetModuleId(assignment.getAssetModuleId()); target.setAssetId(assignment.getAssetId()); if (request.getEventDate() != null) { target.setEventDate(request.getEventDate()); } else { target.setEventDate(new Date()); } target.setReceivedDate(new Date()); MetadataProvider.copy(request.getMetadata(), target); }
/** * Persist multiple alert events. * * @param builders * @return * @throws SiteWhereException */ public List<IDeviceAlert> persistAlerts(List<DeviceAlertCreateRequest.Builder> builders) throws SiteWhereException { List<DeviceAlertCreateRequest> requests = new ArrayList<>(); for (DeviceAlertCreateRequest.Builder builder : builders) { DeviceAlertCreateRequest request = builder.build(); requests.add(request); } return events.addDeviceAlerts(getDeviceAssignment().getId(), requests.toArray(new DeviceAlertCreateRequest[0])); }
@Override public void sendCommand(IDeviceAssignment assignment, String commandName, Map<String, String> parameters) throws SiteWhereException { IDevice device = getDeviceManagement().getDevice(assignment.getDeviceId()); DeviceCommandSearchCriteria criteria = new DeviceCommandSearchCriteria(1, 0); criteria.setDeviceTypeId(device.getDeviceTypeId()); ISearchResults<IDeviceCommand> commands = getDeviceManagement().listDeviceCommands(criteria); IDeviceCommand match = null; for (IDeviceCommand command : commands.getResults()) { if (command.getName().equals(commandName)) { match = command; } } if (match == null) { throw new SiteWhereException("Command not executed. No command found matching: " + commandName); } DeviceCommandInvocationCreateRequest create = new DeviceCommandInvocationCreateRequest(); create.setCommandToken(match.getToken()); create.setParameterValues(parameters); create.setInitiator(CommandInitiator.Script); create.setTarget(CommandTarget.Assignment); create.setTargetId(assignment.getToken()); create.setEventDate(new Date()); getDeviceEventManagement().addDeviceCommandInvocations(assignment.getId(), create); }
public DeviceCommandInvocationCreateRequest.Builder newCommandInvocation(String commandName, String target) { try { IDeviceAssignment targetAssignment = deviceManagement.getDeviceAssignmentByToken(target); if (targetAssignment == null) { throw new SiteWhereException("Target assignment not found: " + target); } IDevice targetDevice = deviceManagement.getDevice(targetAssignment.getDeviceId()); DeviceCommandSearchCriteria criteria = new DeviceCommandSearchCriteria(1, 0); criteria.setDeviceTypeId(targetDevice.getDeviceTypeId()); ISearchResults<IDeviceCommand> commands = deviceManagement.listDeviceCommands(criteria); IDeviceCommand match = null; for (IDeviceCommand command : commands.getResults()) { if (command.getName().equals(commandName)) { match = command; } } if (match == null) { throw new SiteWhereException("Command not executed. No command found matching: " + commandName); } return new DeviceCommandInvocationCreateRequest.Builder(match.getToken(), target); } catch (SiteWhereException e) { throw new RuntimeException(e); } }
IDeviceAssignment assignment = (IDeviceAssignment) PayloadMarshalerResolver.getInstance() .getMarshaler(payloadType).decode(payload, DeviceAssignment.class); boolean sameAssetModule = assetModuleId.equals(assignment.getAssetModuleId()); boolean sameAssetId = assetId.equals(assignment.getAssetId()); boolean matchingStatus = (criteria.getStatus() == null) || (criteria.getStatus() == assignment.getStatus()); if (sameAssetModule && sameAssetId && matchingStatus) { pager.process(assignment);
IDevice device = getDeviceManagement().getDevice(assignment.getDeviceId()); if (device == null) { context.setDeviceStatus(device.getStatus()); context.setDeviceMetadata(device.getMetadata()); context.setAssignmentStatus(assignment.getStatus()); context.setAssignmentMetadata(assignment.getMetadata());
/** * Forward the given event to the Kafka persisted events topic. * * @param deviceAssignmentId * @param event * @return * @throws SiteWhereException */ protected <T extends IDeviceEvent> List<T> forwardEvents(UUID deviceAssignmentId, List<T> events) throws SiteWhereException { IDeviceAssignment assignment = assertDeviceAssignmentById(deviceAssignmentId); for (T event : events) { PersistedEventPayload api = new PersistedEventPayload(); api.setDeviceId(assignment.getDeviceId()); api.setEvent(event); GPersistedEventPayload payload = EventModelConverter.asGrpcPersistedEventPayload(api); getTenantEngine().getInboundPersistedEventsProducer().send(assignment.getId().toString(), EventModelMarshaler.buildPersistedEventPayloadMessage(payload)); } return events; }
request.setInitiatorId(operation.getId().toString()); request.setTarget(CommandTarget.Assignment); request.setTargetId(assignment.getToken()); request.setParameterValues(operation.getMetadata()); Map<String, String> metadata = new HashMap<String, String>(); .addDeviceCommandInvocations(assignment.getId(), request).get(0); updated.getMetadata().put(IBatchCommandInvocationRequest.META_INVOCATION_EVENT_ID, invocation.getId().toString());
@Override public IDevice getDeviceForAssignment(IDeviceAssignment assignment) throws SiteWhereException { return getDeviceByHardwareId(assignment.getDeviceHardwareId()); }
if (assignment.getCustomerId() != null) { BoundStatement eventByCustomer = getCassandraEventManagementClient().getInsertDeviceEventByCustomer() .bind(); if (assignment.getAreaId() != null) { BoundStatement eventByArea = getCassandraEventManagementClient().getInsertDeviceEventByArea().bind(); binder.bind(getCassandraEventManagementClient(), eventByArea, event); if (assignment.getAssetId() != null) { BoundStatement eventByAsset = getCassandraEventManagementClient().getInsertDeviceEventByAsset().bind(); binder.bind(getCassandraEventManagementClient(), eventByAsset, event);
/** * Logic applied before deleting a device assignment. * * @param assignment * @throws SiteWhereException */ public static void deviceAssignmentDeleteLogic(IDeviceAssignment assignment) throws SiteWhereException { if (assignment.getReleasedDate() == null) { throw new SiteWhereSystemException(ErrorCode.CanNotDeleteActiveAssignment, ErrorLevel.ERROR); } }
@Override public URI getDeviceAssignmentIdentifier(IDeviceAssignment assignment) throws SiteWhereException { return createUri(SITEWHERE_PROTOCOL + "assignment/" + assignment.getToken()); }
/** * Persist multiple measurement events. * * @param builders * @return * @throws SiteWhereException */ public List<IDeviceMeasurement> persistMeasurements(List<DeviceMeasurementCreateRequest.Builder> builders) throws SiteWhereException { List<DeviceMeasurementCreateRequest> requests = new ArrayList<>(); for (DeviceMeasurementCreateRequest.Builder builder : builders) { DeviceMeasurementCreateRequest request = builder.build(); requests.add(request); } return events.addDeviceMeasurements(getDeviceAssignment().getId(), requests.toArray(new DeviceMeasurementCreateRequest[0])); }
@Override public String buildUri(IOutboundConnector connector, IDeviceEventContext context, IDeviceEvent event) throws SiteWhereException { // These should be cached, so no performance hit. IDeviceAssignment assignment = connector.getDeviceManagement() .getDeviceAssignment(event.getDeviceAssignmentId()); IDevice device = connector.getDeviceManagement().getDevice(assignment.getDeviceId()); // Create Groovy binding with handles to everything. Binding binding = new Binding(); binding.setVariable(IGroovyVariables.VAR_EVENT_CONTEXT, context); binding.setVariable(IGroovyVariables.VAR_EVENT, event); binding.setVariable(IGroovyVariables.VAR_ASSIGNMENT, assignment); binding.setVariable(IGroovyVariables.VAR_DEVICE, device); binding.setVariable(IGroovyVariables.VAR_LOGGER, getLogger()); return (String) run(binding); } }
@Override public IDevice getDeviceForAssignment(IDeviceAssignment assignment) throws SiteWhereException { return getDeviceByHardwareId(assignment.getDeviceHardwareId()); }