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; } }
@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); } }
@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); }
@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); }
create.setTarget(CommandTarget.Assignment); create.setTargetId(getAssignmentToken()); create.setEventDate(new Date()); events.addDeviceCommandInvocation(assignmentToken, command, create); LOGGER.info("Executed command invocation job.");