/** * Create an unassociated assignment for device with the given hardware id. * This allows events to be written when a device does not have an existing * assignment. * * @param hardwareId * @return * @throws SiteWhereException */ protected IDeviceAssignment createUnassociatedAssignmentFor(String hardwareId) throws SiteWhereException { LOGGER.debug("Creating unassociated assignment for {}.", hardwareId); DeviceAssignmentCreateRequest assnCreate = new DeviceAssignmentCreateRequest(); assnCreate.setDeviceHardwareId(hardwareId); assnCreate.setAssignmentType(DeviceAssignmentType.Unassociated); return SiteWhere.getServer().getDeviceManagement(getTenant()).createDeviceAssignment(assnCreate); }
@Override public void handleDeviceRegistration(IDeviceRegistrationPayload registration) throws SiteWhereException { Authentication previous = SecurityContextHolder.getContext().getAuthentication(); try { Authentication system = getMicroservice().getSystemUser() .getAuthenticationForTenant(getTenantEngine().getTenant()); SecurityContextHolder.getContext().setAuthentication(system); IDevice device = getOrCreateDevice(registration); // Find assignment metadata that should be associated. ICustomer customer = getCustomerFor(registration); IArea area = getAreaFor(registration); // Make sure device is assigned. if (device.getDeviceAssignmentId() == null) { getLogger().debug("Handling unassigned device for registration."); DeviceAssignmentCreateRequest assnCreate = new DeviceAssignmentCreateRequest(); assnCreate.setDeviceToken(device.getToken()); if (customer != null) { assnCreate.setCustomerToken(customer.getToken()); } if (area != null) { assnCreate.setAreaToken(area.getToken()); } getDeviceManagement().createDeviceAssignment(assnCreate); } boolean isNewRegistration = (device != null); sendRegistrationAck(registration.getDeviceToken(), isNewRegistration); } finally { SecurityContextHolder.getContext().setAuthentication(previous); } }
DeviceAssignmentCreateRequest assnCreate = new DeviceAssignmentCreateRequest(); assnCreate.setDeviceHardwareId(device.getHardwareId()); assnCreate.setAssignmentType(DeviceAssignmentType.Unassociated);
/** * Convert a device assignment create request from GRPC to API. * * @param grpc * @return * @throws SiteWhereException */ public static DeviceAssignmentCreateRequest asApiDeviceAssignmentCreateRequest(GDeviceAssignmentCreateRequest grpc) throws SiteWhereException { DeviceAssignmentCreateRequest api = new DeviceAssignmentCreateRequest(); api.setToken(grpc.hasToken() ? grpc.getToken().getValue() : null); api.setDeviceToken(grpc.hasDeviceToken() ? grpc.getDeviceToken().getValue() : null); api.setCustomerToken(grpc.hasCustomerToken() ? grpc.getCustomerToken().getValue() : null); api.setAreaToken(grpc.hasAreaToken() ? grpc.getAreaToken().getValue() : null); api.setAssetToken(grpc.hasAssetToken() ? grpc.getAssetToken().getValue() : null); api.setStatus(CommonModelConverter.asApiDeviceAssignmentStatus(grpc.getStatus())); api.setMetadata(grpc.getMetadataMap()); return api; }