@Override public IDevice getDevice(UUID deviceId) throws SiteWhereException { return getDelegate().getDevice(deviceId); }
@Override public byte[] buildPayload(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 (byte[]) run(binding); } }
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); } }
IDevice device = getDeviceManagement().getDevice(source.getDeviceId()); if (device != null) { result.setDevice(getDeviceHelper().convert(device, assetManagement));
if (isIncludeDetails()) { if (source.getDeviceId() != null) { IDevice device = getDeviceManagement().getDevice(source.getDeviceId()); if (device != null) { Device inflated = getDeviceHelper().convert(device, assetManagement);
@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 void getDevice(GGetDeviceRequest request, StreamObserver<GGetDeviceResponse> responseObserver) { try { GrpcUtils.handleServerMethodEntry(this, DeviceManagementGrpc.getGetDeviceMethod()); IDevice apiResult = getDeviceManagement().getDevice(CommonModelConverter.asApiUuid(request.getId())); GGetDeviceResponse.Builder response = GGetDeviceResponse.newBuilder(); if (apiResult != null) { response.setDevice(DeviceModelConverter.asGrpcDevice(apiResult)); } responseObserver.onNext(response.build()); responseObserver.onCompleted(); } catch (Throwable e) { GrpcUtils.handleServerMethodException(DeviceManagementGrpc.getGetDeviceMethod(), e, responseObserver); } finally { GrpcUtils.handleServerMethodExit(DeviceManagementGrpc.getGetDeviceMethod()); } }
/** * Send an {@link IDeviceEvent} to the configured topic. * * @param event * @throws SiteWhereException */ protected void sendEvent(IDeviceEvent event) throws SiteWhereException { if (getMulticaster() != null) { IDeviceAssignment assignment = getDeviceManagement().getDeviceAssignment(event.getDeviceAssignmentId()); IDevice device = getDeviceManagement().getDevice(assignment.getDeviceId()); List<String> routes = getMulticaster().calculateRoutes(event, device, assignment); for (String route : routes) { publish(event, route); } } else { if (getRouteBuilder() != null) { IDeviceAssignment assignment = getDeviceManagement().getDeviceAssignment(event.getDeviceAssignmentId()); IDevice device = getDeviceManagement().getDevice(assignment.getDeviceId()); publish(event, getRouteBuilder().build(event, device, assignment)); } else { publish(event, getTopic()); } } }
@Override public void getDeviceLabel(GGetDeviceLabelRequest request, StreamObserver<GGetDeviceLabelResponse> responseObserver) { try { GrpcUtils.handleServerMethodEntry(this, LabelGenerationGrpc.getGetDeviceLabelMethod()); ILabelGenerator generator = getLabelGeneratorById(request.getGeneratorId()); IDevice device = getDeviceManagement().getDevice(CommonModelConverter.asApiUuid(request.getDeviceId())); byte[] content = generator.getDeviceLabel(device, getEntityUriProvider()); Label label = new Label(); label.setContent(content); GGetDeviceLabelResponse.Builder response = GGetDeviceLabelResponse.newBuilder(); response.setLabel(LabelGenerationModelConverter.asGrpcLabel(label)); responseObserver.onNext(response.build()); responseObserver.onCompleted(); } catch (Throwable e) { GrpcUtils.handleServerMethodException(LabelGenerationGrpc.getGetDeviceLabelMethod(), e, responseObserver); } finally { GrpcUtils.handleServerMethodExit(LabelGenerationGrpc.getGetDeviceLabelMethod()); } }
/** * Perform custom event processing in a Groovy script. * * @param context * @param event * @throws SiteWhereException */ protected void processEvent(IDeviceEventContext context, IDeviceEvent event) throws SiteWhereException { // These should be cached, so no performance hit. IDeviceAssignment assignment = getDeviceManagement().getDeviceAssignment(event.getDeviceAssignmentId()); IDevice device = 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_DEVICE_MANAGEMENT_BUILDER, deviceBuilder); binding.setVariable(IGroovyVariables.VAR_EVENT_MANAGEMENT_BUILDER, eventsBuilder); binding.setVariable(IGroovyVariables.VAR_LOGGER, getLogger()); getGroovyComponent().run(binding); }
/** * Convert the SPI into a model object based on marshaling parameters. * * @param source * @return * @throws SiteWhereException */ public MarshaledBatchElement convert(IBatchElement source, IDeviceManagement deviceManagement) throws SiteWhereException { if (source == null) { return null; } MarshaledBatchElement element = new MarshaledBatchElement(); element.setId(source.getId()); element.setBatchOperationId(source.getBatchOperationId()); element.setDeviceId(source.getDeviceId()); element.setProcessingStatus(source.getProcessingStatus()); if (isIncludeDevice()) { IDevice device = deviceManagement.getDevice(source.getDeviceId()); if (device != null) { element.setDevice(device); } else { LOGGER.warn("Invalid device reference in batch element."); } } PersistentEntity.copy(source, element); return element; }
/** * Send an {@link IDeviceEvent} to the configured topic. * * @param event * @throws SiteWhereException */ protected void sendEvent(IDeviceEvent event) throws SiteWhereException { IDeviceManagement dm = getDeviceManagement(getTenantEngine().getTenant()); IDeviceAssignment assignment = dm.getDeviceAssignment(event.getDeviceAssignmentId()); IDevice device = dm.getDevice(assignment.getDeviceId()); if (getMulticaster() != null) { List<String> routes = getMulticaster().calculateRoutes(event, device, assignment); for (String route : routes) { publish(event, route); } } else { if (getRouteBuilder() != null) { publish(event, getRouteBuilder().build(event, device, assignment)); } else { publish(event, getTopic()); } } }
@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); }
@Override public IDeviceState createDeviceState(IDeviceStateCreateRequest request) throws SiteWhereException { // Look up device. IDevice device = null; if (request.getDeviceId() != null) { device = getDeviceManagement().getDevice(request.getDeviceId()); } if (device == null) { getLogger().warn("Device state references invalid device id: " + request.getDeviceId()); throw new SiteWhereSystemException(ErrorCode.InvalidDeviceId, ErrorLevel.ERROR); } // Look up device assignment. IDeviceAssignment deviceAssignment = null; if (request.getDeviceAssignmentId() != null) { deviceAssignment = getDeviceManagement().getDeviceAssignment(request.getDeviceAssignmentId()); } if (deviceAssignment == null) { getLogger() .warn("Device state references invalid device assignment id: " + request.getDeviceAssignmentId()); throw new SiteWhereSystemException(ErrorCode.InvalidDeviceAssignmentId, ErrorLevel.ERROR); } // Use common logic so all backend implementations work the same. DeviceState state = DeviceStatePersistence.deviceStateCreateLogic(request, device, deviceAssignment); MongoCollection<Document> states = getMongoClient().getDeviceStatesCollection(); Document created = MongoDeviceState.toDocument(state); MongoPersistence.insert(states, created, ErrorCode.DuplicateId); return MongoDeviceState.fromDocument(created); }
IDevice parent = getDeviceManagement(tenant).getDevice(target.getParentDeviceId()); if (parent == null) { throw new SiteWhereException("Parent device reference points to device that does not exist.");
IDevice device = getDeviceManagement().getDevice(assignment.getDeviceId()); if (device == null) {
IDevice device = getDeviceManagement().getDevice(source.getDeviceId()); if (device != null) { result.setDevice(getDeviceHelper().convert(device, assetManagement));