/** * Convert command initiator from API to GRPC. * * @param api * @return * @throws SiteWhereException */ public static GDeviceCommandInitiator asGrpcCommandInitiator(CommandInitiator api) throws SiteWhereException { switch (api) { case BatchOperation: return GDeviceCommandInitiator.CMD_INITIATOR_BATCH_OPERATION; case REST: return GDeviceCommandInitiator.CMD_INITIATOR_REST; case Scheduler: return GDeviceCommandInitiator.CMD_INITIATOR_SCHEDULER; case Script: return GDeviceCommandInitiator.CMD_INITIATOR_SCRIPT; } throw new SiteWhereException("Unknown command initiator: " + api.name()); }
/** * Load fields from value map. * * @param event * @param values * @throws SiteWhereException */ public static void loadFromMap(DeviceCommandInvocation event, Map<String, Object> values) throws SiteWhereException { event.setEventType(DeviceEventType.CommandInvocation); event.setInitiator(CommandInitiator.valueOf(InfluxDbDeviceEvent.find(values, CMD_INITIATOR))); event.setInitiatorId(InfluxDbDeviceEvent.find(values, CMD_INITIATOR_ID, true)); event.setTarget(CommandTarget.valueOf(InfluxDbDeviceEvent.find(values, CMD_TARGET))); event.setTargetId(InfluxDbDeviceEvent.find(values, CMD_TARGET_ID, true)); event.setCommandToken(InfluxDbDeviceEvent.find(values, CMD_COMMAND_TOKEN)); // Copy parameter values. for (String key : values.keySet()) { if (key.startsWith(CMD_PARAMETER_VALUE_PREFIX)) { String name = key.substring(CMD_PARAMETER_VALUE_PREFIX.length()); String value = InfluxDbDeviceEvent.find(values, key); event.getParameterValues().put(name, value); } } InfluxDbDeviceEvent.loadFromMap(event, values); }
target.setInitiator(CommandInitiator.valueOf(initiatorName));
/** * Get indicator value for command initiator. * * @param initiator * @return * @throws SiteWhereException */ public static Byte getIndicatorForCommandInitiator(CommandInitiator initiator) throws SiteWhereException { switch (initiator) { case REST: { return 0; } case Script: { return 1; } case BatchOperation: { return 2; } case Scheduler: { return 3; } default: { throw new SiteWhereException("Unsupported command initiator: " + initiator.name()); } } }
target.setInitiator(CommandInitiator.valueOf(initiatorName));
/** * Copy information from SPI into Mongo {@link Document}. * * @param source * @param target */ public static void toDocument(IDeviceCommandInvocation source, Document target) { MongoDeviceEvent.toDocument(source, target, false); target.append(PROP_INITIATOR, source.getInitiator().name()); target.append(PROP_INITIATOR_ID, source.getInitiatorId()); target.append(PROP_TARGET, source.getTarget().name()); target.append(PROP_TARGET_ID, source.getTargetId()); target.append(PROP_COMMAND_TOKEN, source.getCommandToken()); Document params = new Document(); for (String key : source.getParameterValues().keySet()) { params.append(key, source.getParameterValues().get(key)); } target.append(PROP_PARAMETER_VALUES, params); }
@Override public DeviceCommandInvocation decodeDeviceCommandInvocation(byte[] payload) throws SiteWhereException { ByteArrayInputStream stream = new ByteArrayInputStream(payload); try { ProtobufMarshaler.DeviceCommandInvocation pb = ProtobufMarshaler.DeviceCommandInvocation.parseFrom(stream); DeviceCommandInvocation invocation = new DeviceCommandInvocation(); invocation.setInitiator(CommandInitiator.valueOf(pb.getInitiator())); if (pb.hasInitiatorId()) { invocation.setInitiatorId(pb.getInitiatorId()); } invocation.setTarget(CommandTarget.valueOf(pb.getTarget())); if (pb.hasTargetId()) { invocation.setTargetId(pb.getTargetId()); } invocation.setCommandToken(pb.getCommandToken()); for (ProtobufMarshaler.MetadataEntry entry : pb.getParameterValuesList()) { invocation.getParameterValues().put(entry.getName(), entry.getValue()); } invocation.setStatus(CommandStatus.valueOf(pb.getStatus())); loadDeviceEventData(invocation, pb.getEventData()); return invocation; } catch (IOException e) { throw new SiteWhereException("Unable to unmarshal command invocation.", e); } }
/** * Copy information from SPI into Mongo {@link Document}. * * @param source * @param target */ public static void toDocument(IDeviceCommandInvocation source, Document target) { MongoDeviceEvent.toDocument(source, target, false); target.append(PROP_INITIATOR, source.getInitiator().name()); target.append(PROP_INITIATOR_ID, source.getInitiatorId()); target.append(PROP_TARGET, source.getTarget().name()); target.append(PROP_TARGET_ID, source.getTargetId()); target.append(PROP_COMMAND_TOKEN, source.getCommandToken()); target.append(PROP_STATUS, source.getStatus().name()); Document params = new Document(); for (String key : source.getParameterValues().keySet()) { params.append(key, source.getParameterValues().get(key)); } target.append(PROP_PARAMETER_VALUES, params); }
/** * Save fields to builder. * * @param event * @param builder * @throws SiteWhereException */ public static void saveToBuilder(DeviceCommandInvocation event, Point.Builder builder) throws SiteWhereException { builder.tag(CMD_INITIATOR, event.getInitiator().name()); if (event.getInitiatorId() != null) { builder.tag(CMD_INITIATOR_ID, event.getInitiatorId()); } builder.tag(CMD_TARGET, event.getTarget().name()); if (event.getTargetId() != null) { builder.tag(CMD_TARGET_ID, event.getTargetId()); } builder.tag(CMD_COMMAND_TOKEN, event.getCommandToken()); for (String key : event.getParameterValues().keySet()) { String value = event.getParameterValues().get(key); builder.addField(CMD_PARAMETER_VALUE_PREFIX + key, value); } InfluxDbDeviceEvent.saveToBuilder(event, builder); } }
@Override public byte[] encodeDeviceCommandInvocation(IDeviceCommandInvocation invocation) throws SiteWhereException { try { ProtobufMarshaler.DeviceCommandInvocation.Builder builder = ProtobufMarshaler.DeviceCommandInvocation .newBuilder(); builder.setInitiator(invocation.getInitiator().name()); if (invocation.getInitiatorId() != null) { builder.setInitiatorId(invocation.getInitiatorId()); } builder.setTarget(invocation.getTarget().name()); if (invocation.getTargetId() != null) { builder.setTargetId(invocation.getTargetId()); } builder.setCommandToken(invocation.getCommandToken()); for (String key : invocation.getParameterValues().keySet()) { ProtobufMarshaler.MetadataEntry.Builder mbuilder = ProtobufMarshaler.MetadataEntry.newBuilder(); mbuilder.setName(key); mbuilder.setValue(invocation.getParameterValues().get(key)); builder.addParameterValues(mbuilder.build()); } builder.setStatus(invocation.getStatus().name()); builder.setEventData(createDeviceEventData(invocation)); ByteArrayOutputStream out = new ByteArrayOutputStream(); builder.build().writeTo(out); return out.toByteArray(); } catch (IOException e) { throw new SiteWhereException("Unable to marshal command invocation.", e); } }