@Override public IDeviceAssignment getDeviceAssignmentByToken(String token) throws SiteWhereException { return delegate.getDeviceAssignmentByToken(token); }
@Override public IDeviceAssignment getDeviceAssignment(UUID id) throws SiteWhereException { return getDelegate().getDeviceAssignment(id); }
@Override public IDevice getDevice(UUID deviceId) throws SiteWhereException { return getDelegate().getDevice(deviceId); }
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.getDeviceForAssignment(targetAssignment); List<IDeviceCommand> commands = deviceManagement.listDeviceCommands(targetDevice.getSpecificationToken(), false); IDeviceCommand match = null; for (IDeviceCommand command : commands) { 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); } }
@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); } }
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); } }
LOGGER.debug("Handling device registration request."); IDevice device = SiteWhere.getServer().getDeviceManagement(getTenant()) .getDeviceByHardwareId(request.getHardwareId()); IDeviceSpecification specification = SiteWhere.getServer().getDeviceManagement(getTenant()) .getDeviceSpecificationByToken(request.getSpecificationToken()); if (SiteWhere.getServer().getDeviceManagement(getTenant()).getSiteByToken(request.getSiteToken()) == null) { LOGGER.warn("Ignoring device registration request because of invalid site token."); return; deviceCreate.setComments("Device created by on-demand registration."); deviceCreate.setMetadata(request.getMetadata()); device = SiteWhere.getServer().getDeviceManagement(getTenant()).createDevice(deviceCreate); } else if (!device.getSpecificationToken().equals(request.getSpecificationToken())) { LOGGER.info("Found existing device registration, but specification does not match."); DeviceCreateRequest deviceUpdate = new DeviceCreateRequest(); deviceUpdate.setMetadata(request.getMetadata()); device = SiteWhere.getServer().getDeviceManagement(getTenant()).updateDevice(request.getHardwareId(), deviceUpdate); assnCreate.setDeviceHardwareId(device.getHardwareId()); assnCreate.setAssignmentType(DeviceAssignmentType.Unassociated); SiteWhere.getServer().getDeviceManagement(getTenant()).createDeviceAssignment(assnCreate);
/** * Indicates if an event is filtered. * * @param event * @return * @throws SiteWhereException */ protected boolean isFiltered(IDeviceEvent event) throws SiteWhereException { IDeviceAssignment assignment = deviceManagement.getDeviceAssignmentByToken(event.getDeviceAssignmentToken()); if (assignment == null) { throw new SiteWhereException("Device assignment for event not found."); } IDevice device = deviceManagement.getDeviceByHardwareId(assignment.getDeviceHardwareId()); if (device == null) { throw new SiteWhereException("Device assignment references unknown device."); } for (IDeviceEventFilter filter : filters) { if (filter.isFiltered(event, device, assignment)) { return true; } } return false; }
IDevice device = getDeviceManagement().getDevice(source.getDeviceId()); if (device != null) { result.setDevice(getDeviceHelper().convert(device, assetManagement)); IDeviceType deviceType = getDeviceManagement().getDeviceType(source.getDeviceTypeId()); if (deviceType != null) { result.setDeviceType(deviceType); .getDeviceAssignment(source.getDeviceAssignmentId()); if (deviceAssignment != null) { result.setDeviceAssignment(deviceAssignment); ICustomer customer = getDeviceManagement().getCustomer(source.getCustomerId()); if (customer == null) { customer = new InvalidCustomer(); IArea area = getDeviceManagement().getArea(source.getAreaId()); if (area == null) { area = new InvalidArea();
throw new SiteWhereSystemException(ErrorCode.InvalidDeviceId, ErrorLevel.ERROR); IDevice mapped = management.getDeviceByToken(request.getDeviceToken()); if (mapped == null) { throw new SiteWhereException("Device referenced by mapping does not exist."); IDeviceType deviceType = management.getDeviceType(device.getDeviceTypeId()); DeviceTypeUtils.getDeviceSlotByPath(deviceType, request.getDeviceElementSchemaPath()); management.updateDevice(mapped.getId(), nested); IDevice updated = management.updateDevice(device.getId(), update); return updated;
IDevice device = getDeviceManagement().getDeviceByToken(registration.getDeviceToken()); IDeviceCreateRequest request = registration.getDeviceRegistrationRequest(); if (device == null) { : "Device created by on-demand registration."); deviceCreate.setMetadata(request.getMetadata()); return getDeviceManagement().createDevice(deviceCreate); } else { getLogger().info("Found existing device registration. Updating device information."); return getDeviceManagement().updateDevice(device.getId(), request);
@Override public List<IDeviceCommand> listDeviceCommands(String token, boolean includeDeleted) throws SiteWhereException { return delegate.listDeviceCommands(token, includeDeleted); }
DeviceElementMapping cnvMapping = DeviceElementMapping.copy(mapping); if (isIncludeNested()) { IDevice device = getDeviceManagement().getDeviceByToken(mapping.getDeviceToken()); cnvMapping.setDevice(getNestedHelper().convert(device, assetManagement)); IDeviceType deviceType = getDeviceManagement().getDeviceType(source.getDeviceTypeId()); if (deviceType == null) { throw new SiteWhereException("Device references non-existent device type."); try { IDeviceAssignment assignment = getDeviceManagement() .getDeviceAssignment(source.getDeviceAssignmentId()); if (assignment == null) { throw new SiteWhereException("Device contains an invalid assignment reference.");
IDevice device = null; try { device = getDeviceManagement().getDeviceByToken(payload.getDeviceToken()); } finally { deviceLookupTime.stop(); IDeviceAssignment assignment = null; try { assignment = getDeviceManagement().getDeviceAssignment(device.getDeviceAssignmentId()); } finally { assignmentLookupTime.stop();
/** * Perform custom event processing in a Groovy script. * * @param event * @throws SiteWhereException */ protected void processEvent(IDeviceEvent event) throws SiteWhereException { // These should be cached, so no performance hit. IDeviceAssignment assignment = getDeviceManagement() .getDeviceAssignmentByToken(event.getDeviceAssignmentToken()); IDevice device = getDeviceManagement().getDeviceForAssignment(assignment); // Create Groovy binding with handles to everything. Binding binding = new Binding(); binding.setVariable("logger", getLogger()); binding.setVariable("event", new DeviceEventSupport(event)); binding.setVariable("assignment", assignment); binding.setVariable("device", device); binding.setVariable("deviceBuilder", deviceBuilder); binding.setVariable("eventBuilder", eventsBuilder); try { SiteWhere.getServer().getTenantGroovyConfiguration(getTenant()).getGroovyScriptEngine().run(getScriptPath(), binding); } catch (ResourceException e) { throw new SiteWhereException("Unable to access Groovy script. " + e.getMessage(), e); } catch (ScriptException e) { throw new SiteWhereException("Unable to run Groovy script.", e); } }
@Override public IDeviceAssignment createDeviceAssignment(IDeviceAssignmentCreateRequest request) throws SiteWhereException { return delegate.createDeviceAssignment(request); }
IDevice device = management.getDeviceByHardwareId(hardwareId); if (device == null) { throw new SiteWhereSystemException(ErrorCode.InvalidHardwareId, ErrorLevel.ERROR); IDevice mapped = management.getDeviceByHardwareId(request.getHardwareId()); if (mapped == null) { throw new SiteWhereException("Device referenced by mapping does not exist."); IDeviceSpecification specification = management.getDeviceSpecificationByToken(device.getSpecificationToken()); DeviceSpecificationUtils.getDeviceSlotByPath(specification, request.getDeviceElementSchemaPath()); management.updateDevice(mapped.getHardwareId(), nested); IDevice updated = management.updateDevice(hardwareId, update); return updated;
@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); }
IDevice mapped = management.getDeviceByToken(match.getDeviceToken()); if (mapped != null) { DeviceCreateRequest nested = new DeviceCreateRequest(); nested.setRemoveParentHardwareId(true); management.updateDevice(mapped.getId(), nested); IDevice updated = management.updateDevice(device.getId(), update); return updated;
@Override public IDevice updateDevice(String hardwareId, IDeviceCreateRequest request) throws SiteWhereException { return delegate.updateDevice(hardwareId, request); }