public Object generate() throws SiteWhereException { DeviceCommandInvocationCreateRequest request = new DeviceCommandInvocationCreateRequest(); request.setInitiator(ExampleData.INVOCATION_SET_RPT_INTV.getInitiator()); request.setInitiatorId(ExampleData.INVOCATION_SET_RPT_INTV.getInitiatorId()); request.setTarget(ExampleData.INVOCATION_SET_RPT_INTV.getTarget()); request.setCommandToken(ExampleData.INVOCATION_SET_RPT_INTV.getCommandToken()); request.getParameterValues().putAll(ExampleData.INVOCATION_SET_RPT_INTV.getParameterValues()); request.setEventDate(ExampleData.INVOCATION_SET_RPT_INTV.getEventDate()); return request; } }
public Builder(String commandToken, String target) throws SiteWhereException { request.setCommandToken(commandToken); request.setInitiator(CommandInitiator.Script); request.setTarget(CommandTarget.Assignment); request.setTargetId(target); }
public Builder metadata(String name, String value) { if (request.getMetadata() == null) { request.setMetadata(new HashMap<String, String>()); } request.getMetadata().put(name, value); return this; }
/** * Convert device command invocation create request from GRPC to API. * * @param grpc * @return * @throws SiteWhereException */ public static DeviceCommandInvocationCreateRequest asApiDeviceCommandInvocationCreateRequest( GDeviceCommandInvocationCreateRequest grpc) throws SiteWhereException { DeviceCommandInvocationCreateRequest api = new DeviceCommandInvocationCreateRequest(); api.setInitiator(EventModelConverter.asApiCommandInitiator(grpc.getInitiator())); api.setInitiatorId(grpc.getInitiatorId()); api.setTarget(EventModelConverter.asApiCommandTarget(grpc.getTarget())); if (grpc.hasTargetId()) { api.setTargetId(grpc.getTargetId().getValue()); } api.setCommandToken(grpc.getCommandToken()); api.setParameterValues(grpc.getParameterValuesMap()); EventModelConverter.copyApiDeviceEventCreateRequest(grpc.getEvent(), api); return api; }
@RequestMapping(value = "/{token}/invocations/schedules/{scheduleToken}", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "Schedule command invocation") @Secured({ SiteWhereRoles.REST }) @Documented(examples = { @Example(stage = Stage.Request, json = Assignments.CreateCommandInvocationRequest.class, description = "scheduleCommandInvocationRequest.md"), @Example(stage = Stage.Response, json = Schedules.CreateScheduledJobResponse.class, description = "scheduleCommandInvocationResponse.md") }) public IScheduledJob scheduleCommandInvocation(@RequestBody DeviceCommandInvocationCreateRequest request, @ApiParam(value = "Assignment token", required = true) @PathVariable String token, @ApiParam(value = "Schedule token", required = true) @PathVariable String scheduleToken, HttpServletRequest servletRequest) throws SiteWhereException { Tracer.start(TracerCategory.RestApiCall, "scheduleCommandInvocation", LOGGER); try { assureDeviceCommand(request.getCommandToken(), servletRequest); IScheduledJobCreateRequest job = ScheduledJobHelper.createCommandInvocationJob(UUID.randomUUID().toString(), token, request.getCommandToken(), request.getParameterValues(), scheduleToken); return SiteWhere.getServer().getScheduleManagement(getTenant(servletRequest)).createScheduledJob(job); } finally { Tracer.stop(LOGGER); } }
public Builder parameterValue(String name, String value) { request.getParameterValues().put(name, value); return this; }
@Override public void execute(JobExecutionContext context) throws JobExecutionException { parse(context); if (getAssignmentToken() == null) { throw new JobExecutionException("Assignment token not provided."); } if (getCommandToken() == null) { throw new JobExecutionException("Command token not provided."); } try { ITenant tenant = getTenantManagement().getTenantByToken(context.getScheduler().getSchedulerName()); IDeviceEventManagement events = getDeviceEventManagement(tenant); DeviceCommandInvocationCreateRequest create = new DeviceCommandInvocationCreateRequest(); create.setCommandToken(getCommandToken()); create.setParameterValues(getParameters()); create.setInitiator(CommandInitiator.Scheduler); create.setInitiatorId(context.getJobDetail().getKey().getName()); create.setTarget(CommandTarget.Assignment); create.setTargetId(getAssignmentToken()); create.setEventDate(new Date()); events.addDeviceCommandInvocations(null, create); LOGGER.info("Executed command invocation job."); } catch (SiteWhereException e) { throw new JobExecutionException("Unable to create command invocation.", e); } catch (SchedulerException e) { throw new JobExecutionException("Unable to get scheduler name.", e); } }
public Builder parameterValue(String name, String value) { request.getParameterValues().put(name, value); return this; }
@Override public void sendCommand(String assignmentToken, String commandName, Map<String, String> parameters) throws SiteWhereException { IDeviceAssignment assignment = getDeviceManagement().getDeviceAssignmentByToken(assignmentToken); if (assignment == null) { throw new SiteWhereException("Command not executed. Assignment not found: " + assignmentToken); } IDevice device = getDeviceManagement().getDeviceForAssignment(assignment); List<IDeviceCommand> commands = getDeviceManagement().listDeviceCommands(device.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); } 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().addDeviceCommandInvocation(assignment.getToken(), match, create); }
public Builder(String commandToken, String target) throws SiteWhereException { request.setCommandToken(commandToken); request.setInitiator(CommandInitiator.Script); request.setTarget(CommandTarget.Assignment); request.setTargetId(target); }
public Builder metadata(String name, String value) { if (request.getMetadata() == null) { request.setMetadata(new HashMap<String, String>()); } request.getMetadata().put(name, value); return this; }
@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); }
IDeviceEventManagement events = SiteWhere.getServer().getDeviceEventManagement(tenant); IDeviceCommand command = devices.getDeviceCommandByToken(getCommandToken()); DeviceCommandInvocationCreateRequest create = new DeviceCommandInvocationCreateRequest(); create.setCommandToken(getCommandToken()); create.setParameterValues(getParameters()); create.setInitiator(CommandInitiator.Scheduler); create.setInitiatorId(context.getJobDetail().getKey().getName()); create.setTarget(CommandTarget.Assignment); create.setTargetId(getAssignmentToken()); create.setEventDate(new Date()); events.addDeviceCommandInvocation(assignmentToken, command, create); LOGGER.info("Executed command invocation job.");
DeviceCommandInvocationCreateRequest request = new DeviceCommandInvocationCreateRequest(); request.setCommandToken(command.getToken()); request.setInitiator(CommandInitiator.BatchOperation); 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>(); metadata.put(IBatchOperationCreateRequest.META_BATCH_OPERATION_TOKEN, operation.getToken()); request.setMetadata(metadata);
DeviceCommandInvocationCreateRequest request = new DeviceCommandInvocationCreateRequest(); request.setCommandToken(commandToken); request.setInitiator(CommandInitiator.BatchOperation); request.setInitiatorId(systemUser.getName()); request.setTarget(CommandTarget.Assignment); request.setTargetId(assignment.getToken()); request.setParameterValues(operation.getMetadata()); Map<String, String> metadata = new HashMap<String, String>(); metadata.put(IBatchOperationCreateRequest.META_BATCH_OPERATION_ID, operation.getToken()); request.setMetadata(metadata);