@Override public IDeviceCommand createDeviceCommand(IDeviceSpecification spec, IDeviceCommandCreateRequest request) throws SiteWhereException { // Note: This allows duplicates if duplicate was marked deleted. List<IDeviceCommand> existing = listDeviceCommands(spec.getToken(), false); // Use common logic so all backend implementations work the same. String uuid = ((request.getToken() != null) ? request.getToken() : UUID.randomUUID().toString()); DeviceCommand command = SiteWherePersistence.deviceCommandCreateLogic(spec, request, uuid, existing); MongoCollection<Document> commands = getMongoClient().getDeviceCommandsCollection(getTenant()); Document created = MongoDeviceCommand.toDocument(command); MongoPersistence.insert(commands, created, ErrorCode.DeviceCommandExists); return MongoDeviceCommand.fromDocument(created); }
/** * Create a new device command for an existing device specification. * * @param context * @param spec * @param request * @return * @throws SiteWhereException */ public static IDeviceCommand createDeviceCommand(IHBaseContext context, IDeviceSpecification spec, IDeviceCommandCreateRequest request) throws SiteWhereException { Long specId = context.getDeviceIdManager().getSpecificationKeys().getValue(spec.getToken()); if (specId == null) { throw new SiteWhereSystemException(ErrorCode.InvalidDeviceSpecificationToken, ErrorLevel.ERROR); } String uuid = ((request.getToken() != null) ? request.getToken() : UUID.randomUUID().toString()); // Use common logic so all backend implementations work the same. List<IDeviceCommand> existing = listDeviceCommands(context, spec.getToken(), false); DeviceCommand command = SiteWherePersistence.deviceCommandCreateLogic(spec, request, uuid, existing); // Create unique row for new device. Long nextId = HBaseDeviceSpecification.allocateNextCommandId(context, specId); byte[] rowkey = HBaseDeviceSpecification.getDeviceCommandRowKey(specId, nextId); context.getDeviceIdManager().getCommandKeys().create(uuid, rowkey); return putDeviceCommandPayload(context, command); }