@Override public IDeviceAssignment getDeviceAssignment(UUID id) throws SiteWhereException { return getDelegate().getDeviceAssignment(id); }
/** * Assert that a device assignment exists and throw an exception if not. * * @param token * @return * @throws SiteWhereException */ protected IDeviceAssignment assertDeviceAssignmentById(UUID id) throws SiteWhereException { IDeviceAssignment assignment = getDeviceManagement().getDeviceAssignment(id); if (assignment == null) { throw new SiteWhereSystemException(ErrorCode.InvalidDeviceAssignmentId, ErrorLevel.ERROR); } return assignment; }
/** * Assert that a device assignment exists and throw an exception if not. * * @param token * @return * @throws SiteWhereException */ protected IDeviceAssignment assertDeviceAssignmentById(UUID id) throws SiteWhereException { IDeviceAssignment assignment = getDeviceManagement().getDeviceAssignment(id); if (assignment == null) { throw new SiteWhereSystemException(ErrorCode.InvalidDeviceAssignmentId, ErrorLevel.ERROR); } return assignment; }
/** * Assert that a device assignment exists and throw an exception if not. * * @param token * @return * @throws SiteWhereException */ protected IDeviceAssignment assertDeviceAssignmentById(UUID id) throws SiteWhereException { IDeviceAssignment assignment = getDeviceManagement().getDeviceAssignment(id); if (assignment == null) { throw new SiteWhereSystemException(ErrorCode.InvalidDeviceAssignmentId, ErrorLevel.ERROR); } return assignment; }
/** * Assert that a device assignment exists and throw an exception if not. * * @param token * @return * @throws SiteWhereException */ protected IDeviceAssignment assertDeviceAssignmentById(UUID id) throws SiteWhereException { IDeviceAssignment assignment = getDeviceManagement().getDeviceAssignment(id); if (assignment == null) { throw new SiteWhereSystemException(ErrorCode.InvalidDeviceAssignmentId, ErrorLevel.ERROR); } return assignment; }
public AssignmentScope forSameAssignmentAs(IDeviceEvent event) throws SiteWhereException { return new AssignmentScope(getEventManagement(), getDeviceManagement().getDeviceAssignment(event.getDeviceAssignmentId())); }
IDeviceAssignment assignment = null; try { assignment = getDeviceManagement().getDeviceAssignment(device.getDeviceAssignmentId()); } finally { assignmentLookupTime.stop();
@Override public void getDeviceAssignment(GGetDeviceAssignmentRequest request, StreamObserver<GGetDeviceAssignmentResponse> responseObserver) { try { GrpcUtils.handleServerMethodEntry(this, DeviceManagementGrpc.getGetDeviceAssignmentMethod()); IDeviceAssignment apiResult = getDeviceManagement() .getDeviceAssignment(CommonModelConverter.asApiUuid(request.getId())); GGetDeviceAssignmentResponse.Builder response = GGetDeviceAssignmentResponse.newBuilder(); if (apiResult != null) { response.setAssignment(DeviceModelConverter.asGrpcDeviceAssignment(apiResult)); } responseObserver.onNext(response.build()); responseObserver.onCompleted(); } catch (Throwable e) { GrpcUtils.handleServerMethodException(DeviceManagementGrpc.getGetDeviceAssignmentMethod(), e, responseObserver); } finally { GrpcUtils.handleServerMethodExit(DeviceManagementGrpc.getGetDeviceAssignmentMethod()); } }
/** * Assure that a bucket is created for the given assignment. * * @param assignmentToken * @return * @throws SiteWhereException */ protected DeviceAssignment assureBucket(UUID assignmentId) throws SiteWhereException { DeviceAssignment cached = assignmentsById.get(assignmentId); if (cached != null) { return cached; } IDeviceAssignment assignment = getDeviceManagement().getDeviceAssignment(assignmentId); if (assignment == null) { throw new SiteWhereException("Assignment not found."); } DeviceAssignmentMarshalHelper helper = new DeviceAssignmentMarshalHelper(getDeviceManagement()); helper.setIncludeAsset(false); helper.setIncludeDevice(true); MarshaledDeviceAssignment converted = helper.convert(assignment, null); createBucket(converted.getToken(), converted.getAssetName() + " (" + converted.getDevice().getToken() + ")"); assignmentsById.put(assignmentId, converted); return converted; }
@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 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); } }
@Override public List<T> calculateRoutes(IDeviceEvent event, IDevice device, IDeviceAssignment assignment) throws SiteWhereException { List<T> routes = new ArrayList<T>(); IDeviceManagement dm = getDeviceManagement(getTenantEngine().getTenant()); for (IDevice targetDevice : matches) { IDeviceAssignment targetAssignment = dm.getDeviceAssignment(targetDevice.getDeviceAssignmentId()); Binding binding = new Binding(); binding.setVariable("logger", getLogger()); binding.setVariable("event", event); binding.setVariable("device", device); binding.setVariable("assignment", assignment); if (device.getDeviceAssignmentId() != null) { binding.setVariable("targetAssignment", targetAssignment); binding.setVariable("targetDevice", targetDevice); } try { Object result = run(binding); if (result != null) { routes.add(convertRoute(result)); } } catch (SiteWhereException e) { getLogger().error("Unable to run route calculator script.", e); } } return routes; }
@Override public void getDeviceAssignmentLabel(GGetDeviceAssignmentLabelRequest request, StreamObserver<GGetDeviceAssignmentLabelResponse> responseObserver) { try { GrpcUtils.handleServerMethodEntry(this, LabelGenerationGrpc.getGetDeviceAssignmentLabelMethod()); ILabelGenerator generator = getLabelGeneratorById(request.getGeneratorId()); IDeviceAssignment assignment = getDeviceManagement() .getDeviceAssignment(CommonModelConverter.asApiUuid(request.getDeviceAssignmentId())); byte[] content = generator.getDeviceAssignmentLabel(assignment, getEntityUriProvider()); Label label = new Label(); label.setContent(content); GGetDeviceAssignmentLabelResponse.Builder response = GGetDeviceAssignmentLabelResponse.newBuilder(); response.setLabel(LabelGenerationModelConverter.asGrpcLabel(label)); responseObserver.onNext(response.build()); responseObserver.onCompleted(); } catch (Throwable e) { GrpcUtils.handleServerMethodException(LabelGenerationGrpc.getGetDeviceAssignmentLabelMethod(), e, responseObserver); } finally { GrpcUtils.handleServerMethodExit(LabelGenerationGrpc.getGetDeviceAssignmentLabelMethod()); } }
/** * Get the current assignment or throw errors if it can not be resolved. * * @param deviceToken * @return * @throws SiteWhereException */ protected IDeviceAssignment getCurrentAssignment(String deviceToken) throws SiteWhereException { IDevice device = getDeviceManagement(getTenantEngine().getTenant()).getDeviceByToken(deviceToken); if (device == null) { throw new SiteWhereSystemException(ErrorCode.InvalidDeviceToken, ErrorLevel.ERROR); } if (device.getDeviceAssignmentId() == null) { throw new SiteWhereSystemException(ErrorCode.DeviceNotAssigned, ErrorLevel.ERROR); } return getDeviceManagement(getTenantEngine().getTenant()).getDeviceAssignment(device.getDeviceAssignmentId()); }
/** * 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); }
/** * 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()); } } }
/** * 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 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); }
IDeviceAssignment assignment = getDeviceManagement().getDeviceAssignment(event.getDeviceAssignmentId()); if (assignment == null) {
.getDeviceAssignment(source.getDeviceAssignmentId()); if (deviceAssignment != null) { result.setDeviceAssignment(deviceAssignment);