/** * Convert command target from API to GRPC. * * @param api * @return * @throws SiteWhereException */ public static GDeviceCommandTarget asGrpcCommandTarget(CommandTarget api) throws SiteWhereException { switch (api) { case Assignment: return GDeviceCommandTarget.CMD_TARGET_ASSIGNMENT; } throw new SiteWhereException("Unknown command target: " + 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.setTarget(CommandTarget.valueOf(targetName));
/** * Get indicator value for command target. * * @param target * @return * @throws SiteWhereException */ public static Byte getIndicatorForCommandTarget(CommandTarget target) throws SiteWhereException { switch (target) { case Assignment: { return 0; } default: { throw new SiteWhereException("Unsupported command target: " + target.name()); } } }
target.setTarget(CommandTarget.valueOf(targetName));
/** * 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); } }