本文整理汇总了Java中com.amazonaws.services.iot.client.AWSIotMessage类的典型用法代码示例。如果您正苦于以下问题:Java AWSIotMessage类的具体用法?Java AWSIotMessage怎么用?Java AWSIotMessage使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AWSIotMessage类属于com.amazonaws.services.iot.client包,在下文中一共展示了AWSIotMessage类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: runCommand
import com.amazonaws.services.iot.client.AWSIotMessage; //导入依赖的package包/类
public String runCommand(Command command, AWSIotMessage request, long commandTimeout, boolean isAsync)
throws AWSIotException, AWSIotTimeoutException {
String commandId = newCommandId();
appendCommandId(request, commandId);
request.setTopic(getTopic(command, null));
AwsIotDeviceCommand deviceCommand = new AwsIotDeviceCommand(this, command, commandId, request, commandTimeout,
isAsync);
pendingCommands.put(commandId, deviceCommand);
LOGGER.fine("Number of pending commands: " + pendingCommands.size());
try {
deviceCommand.put(device);
} catch (AWSIotException e) {
// if exception happens during publish, we remove the command
// from the pending list as we'll never get ack for it.
pendingCommands.remove(commandId);
throw e;
}
return deviceCommand.get(device);
}
示例2: onCommandAck
import com.amazonaws.services.iot.client.AWSIotMessage; //导入依赖的package包/类
public void onCommandAck(AWSIotMessage response) {
if (response == null || response.getTopic() == null) {
return;
}
AwsIotDeviceCommand command = getPendingCommand(response);
if (command == null) {
LOGGER.warning("Unknown command received from topic " + response.getTopic());
return;
}
boolean success = response.getTopic().endsWith(COMMAND_ACK_PATHS.get(CommandAck.ACCEPTED));
if (!success
&& (Command.DELETE.equals(command.getCommand()) && AWSIotDeviceErrorCode.NOT_FOUND.equals(command
.getErrorCode()))) {
// Ignore empty document error (NOT_FOUND) for delete command
success = true;
}
if (success) {
command.setResponse(response);
command.onSuccess();
} else {
command.onFailure();
}
}
示例3: appendCommandId
import com.amazonaws.services.iot.client.AWSIotMessage; //导入依赖的package包/类
private void appendCommandId(AWSIotMessage message, String commandId) throws AWSIotException {
String payload = message.getStringPayload();
if (payload == null) {
payload = "{}";
}
try {
JsonNode jsonNode = objectMapper.readTree(payload);
if (!jsonNode.isObject()) {
throw new AWSIotException("Invalid Json string in payload");
}
((ObjectNode) jsonNode).put(COMMAND_ID_FIELD, commandId);
message.setStringPayload(jsonNode.toString());
} catch (IOException e) {
throw new AWSIotException(e);
}
}
示例4: onFailure
import com.amazonaws.services.iot.client.AWSIotMessage; //导入依赖的package包/类
@Override
public void onFailure(IMqttToken token, Throwable cause) {
final AWSIotMessage message = (AWSIotMessage) token.getUserContext();
if (message == null) {
LOGGER.warning("Request failed: " + token.getException());
return;
}
LOGGER.warning("Request failed for topic " + message.getTopic() + ": " + token.getException());
client.scheduleTask(new Runnable() {
@Override
public void run() {
message.onFailure();
}
});
}
示例5: publishMessage
import com.amazonaws.services.iot.client.AWSIotMessage; //导入依赖的package包/类
@Override
public void publishMessage(AWSIotMessage message) throws AWSIotException, AwsIotRetryableException {
String topic = message.getTopic();
MqttMessage mqttMessage = new MqttMessage(message.getPayload());
mqttMessage.setQos(message.getQos().getValue());
try {
mqttClient.publish(topic, mqttMessage, message, messageListener);
} catch (MqttException e) {
if (e.getReasonCode() == MqttException.REASON_CODE_CLIENT_NOT_CONNECTED) {
throw new AwsIotRetryableException(e);
} else {
throw new AWSIotException(e);
}
}
}
示例6: buildMqttConnectOptions
import com.amazonaws.services.iot.client.AWSIotMessage; //导入依赖的package包/类
private MqttConnectOptions buildMqttConnectOptions(AbstractAwsIotClient client, SocketFactory socketFactory) {
MqttConnectOptions options = new MqttConnectOptions();
options.setSocketFactory(socketFactory);
options.setCleanSession(true);
options.setConnectionTimeout(client.getConnectionTimeout() / 1000);
options.setKeepAliveInterval(client.getKeepAliveInterval() / 1000);
Set<String> serverUris = getServerUris();
if (serverUris != null && !serverUris.isEmpty()) {
String[] uriArray = new String[serverUris.size()];
serverUris.toArray(uriArray);
options.setServerURIs(uriArray);
}
if (client.getWillMessage() != null) {
AWSIotMessage message = client.getWillMessage();
options.setWill(message.getTopic(), message.getPayload(), message.getQos().getValue(), false);
}
return options;
}
示例7: dispatch
import com.amazonaws.services.iot.client.AWSIotMessage; //导入依赖的package包/类
public void dispatch(final AWSIotMessage message) {
boolean matches = false;
for (String topicFilter : subscriptions.keySet()) {
if (topicFilterMatch(topicFilter, message.getTopic())) {
final AWSIotTopic topic = subscriptions.get(topicFilter);
scheduleTask(new Runnable() {
@Override
public void run() {
topic.onMessage(message);
}
});
matches = true;
}
}
if (!matches) {
LOGGER.warning("Unexpected message received from topic " + message.getTopic());
}
}
示例8: main
import com.amazonaws.services.iot.client.AWSIotMessage; //导入依赖的package包/类
public static void main(String args[]) throws InterruptedException, AWSIotException {
CommandArguments arguments = CommandArguments.parse(args);
initClient(arguments);
awsIotClient.setWillMessage(new AWSIotMessage("client/disconnect", AWSIotQos.QOS0, awsIotClient.getClientId()));
String thingName = arguments.getNotNull("thingName", SampleUtil.getConfig("thingName"));
ConnectedWindow connectedWindow = new ConnectedWindow(thingName);
awsIotClient.attach(connectedWindow);
awsIotClient.connect();
// Delete existing document if any
connectedWindow.delete();
AWSIotConnectionStatus status = AWSIotConnectionStatus.DISCONNECTED;
while (true) {
AWSIotConnectionStatus newStatus = awsIotClient.getConnectionStatus();
if (!status.equals(newStatus)) {
System.out.println(System.currentTimeMillis() + " Connection status changed to " + newStatus);
status = newStatus;
}
Thread.sleep(1000);
}
}
示例9: onMessage
import com.amazonaws.services.iot.client.AWSIotMessage; //导入依赖的package包/类
@Override
public void onMessage(AWSIotMessage message) {
String messageId = UUID.randomUUID().toString();
MessageNode node = new MessageNode(messagesNode, messageId, message.getPayload());
messagesNode.addChildren(node);
}
示例10: AwsIotDeviceCommand
import com.amazonaws.services.iot.client.AWSIotMessage; //导入依赖的package包/类
public AwsIotDeviceCommand(AwsIotDeviceCommandManager commandManager, Command command, String commandId,
AWSIotMessage request, long commandTimeout, boolean isAsync) {
super(request, commandTimeout, isAsync);
this.commandManager = commandManager;
this.command = command;
this.commandId = commandId;
this.requestSent = false;
}
示例11: runCommandSync
import com.amazonaws.services.iot.client.AWSIotMessage; //导入依赖的package包/类
public String runCommandSync(Command command, AWSIotMessage request) throws AWSIotException {
try {
return runCommand(command, request, 0, false);
} catch (AWSIotTimeoutException e) {
// We shouldn't get timeout exception because timeout is 0
throw new AwsIotRuntimeException(e);
}
}
示例12: getPendingCommand
import com.amazonaws.services.iot.client.AWSIotMessage; //导入依赖的package包/类
private AwsIotDeviceCommand getPendingCommand(AWSIotMessage message) {
String payload = message.getStringPayload();
if (payload == null) {
return null;
}
try {
JsonNode jsonNode = objectMapper.readTree(payload);
if (!jsonNode.isObject()) {
return null;
}
JsonNode node = jsonNode.get(COMMAND_ID_FIELD);
if (node == null) {
return null;
}
String commandId = node.textValue();
AwsIotDeviceCommand command = pendingCommands.remove(commandId);
if (command == null) {
return null;
}
node = jsonNode.get(ERROR_CODE_FIELD);
if (node != null) {
command.setErrorCode(AWSIotDeviceErrorCode.valueOf(node.longValue()));
}
node = jsonNode.get(ERROR_MESSAGE_FIELD);
if (node != null) {
command.setErrorMessage(node.textValue());
}
return command;
} catch (IOException e) {
return null;
}
}
示例13: onSuccess
import com.amazonaws.services.iot.client.AWSIotMessage; //导入依赖的package包/类
@Override
public void onSuccess(IMqttToken token) {
final AWSIotMessage message = (AWSIotMessage) token.getUserContext();
if (message == null) {
return;
}
boolean forceFailure = false;
if (token.getResponse() instanceof MqttSuback) {
MqttSuback subAck = (MqttSuback) token.getResponse();
int qos[] = subAck.getGrantedQos();
for (int i = 0; i < qos.length; i++) {
if (qos[i] == SUB_ACK_RETURN_CODE_FAILURE) {
LOGGER.warning("Request failed: likely due to too many subscriptions or policy violations");
forceFailure = true;
break;
}
}
}
final boolean isSuccess = !forceFailure;
client.scheduleTask(new Runnable() {
@Override
public void run() {
if (isSuccess) {
message.onSuccess();
} else {
message.onFailure();
}
}
});
}
示例14: subscribeTopic
import com.amazonaws.services.iot.client.AWSIotMessage; //导入依赖的package包/类
@Override
public void subscribeTopic(AWSIotMessage message) throws AWSIotException, AwsIotRetryableException {
try {
mqttClient.subscribe(message.getTopic(), message.getQos().getValue(), message, messageListener);
} catch (MqttException e) {
if (e.getReasonCode() == MqttException.REASON_CODE_CLIENT_NOT_CONNECTED) {
throw new AwsIotRetryableException(e);
} else {
throw new AWSIotException(e);
}
}
}
示例15: unsubscribeTopic
import com.amazonaws.services.iot.client.AWSIotMessage; //导入依赖的package包/类
@Override
public void unsubscribeTopic(AWSIotMessage message) throws AWSIotException, AwsIotRetryableException {
try {
mqttClient.unsubscribe(message.getTopic(), message, messageListener);
} catch (MqttException e) {
if (e.getReasonCode() == MqttException.REASON_CODE_CLIENT_NOT_CONNECTED) {
throw new AwsIotRetryableException(e);
} else {
throw new AWSIotException(e);
}
}
}