-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
227 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
__version__ = "1.3.0" | ||
__version__ = "1.3.1" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
''' | ||
/* | ||
* Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
''' | ||
|
||
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient | ||
import logging | ||
import time | ||
import argparse | ||
|
||
|
||
class CallbackContainer(object): | ||
|
||
def __init__(self, client): | ||
self._client = client | ||
|
||
def messagePrint(self, client, userdata, message): | ||
print("Received a new message: ") | ||
print(message.payload) | ||
print("from topic: ") | ||
print(message.topic) | ||
print("--------------\n\n") | ||
|
||
def messageForward(self, client, userdata, message): | ||
topicRepublish = message.topic + "/republish" | ||
print("Forwarding message from: %s to %s" % (message.topic, topicRepublish)) | ||
print("--------------\n\n") | ||
self._client.publishAsync(topicRepublish, str(message.payload), 1, self.pubackCallback) | ||
|
||
def pubackCallback(self, mid): | ||
print("Received PUBACK packet id: ") | ||
print(mid) | ||
print("++++++++++++++\n\n") | ||
|
||
def subackCallback(self, mid, data): | ||
print("Received SUBACK packet id: ") | ||
print(mid) | ||
print("Granted QoS: ") | ||
print(data) | ||
print("++++++++++++++\n\n") | ||
|
||
|
||
# Read in command-line parameters | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("-e", "--endpoint", action="store", required=True, dest="host", help="Your AWS IoT custom endpoint") | ||
parser.add_argument("-r", "--rootCA", action="store", required=True, dest="rootCAPath", help="Root CA file path") | ||
parser.add_argument("-c", "--cert", action="store", dest="certificatePath", help="Certificate file path") | ||
parser.add_argument("-k", "--key", action="store", dest="privateKeyPath", help="Private key file path") | ||
parser.add_argument("-w", "--websocket", action="store_true", dest="useWebsocket", default=False, | ||
help="Use MQTT over WebSocket") | ||
parser.add_argument("-id", "--clientId", action="store", dest="clientId", default="basicPubSub", | ||
help="Targeted client id") | ||
parser.add_argument("-t", "--topic", action="store", dest="topic", default="sdk/test/Python", help="Targeted topic") | ||
|
||
args = parser.parse_args() | ||
host = args.host | ||
rootCAPath = args.rootCAPath | ||
certificatePath = args.certificatePath | ||
privateKeyPath = args.privateKeyPath | ||
useWebsocket = args.useWebsocket | ||
clientId = args.clientId | ||
topic = args.topic | ||
|
||
if args.useWebsocket and args.certificatePath and args.privateKeyPath: | ||
parser.error("X.509 cert authentication and WebSocket are mutual exclusive. Please pick one.") | ||
exit(2) | ||
|
||
if not args.useWebsocket and (not args.certificatePath or not args.privateKeyPath): | ||
parser.error("Missing credentials for authentication.") | ||
exit(2) | ||
|
||
# Configure logging | ||
logger = logging.getLogger("AWSIoTPythonSDK.core") | ||
logger.setLevel(logging.DEBUG) | ||
streamHandler = logging.StreamHandler() | ||
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | ||
streamHandler.setFormatter(formatter) | ||
logger.addHandler(streamHandler) | ||
|
||
# Init AWSIoTMQTTClient | ||
myAWSIoTMQTTClient = None | ||
if useWebsocket: | ||
myAWSIoTMQTTClient = AWSIoTMQTTClient(clientId, useWebsocket=True) | ||
myAWSIoTMQTTClient.configureEndpoint(host, 443) | ||
myAWSIoTMQTTClient.configureCredentials(rootCAPath) | ||
else: | ||
myAWSIoTMQTTClient = AWSIoTMQTTClient(clientId) | ||
myAWSIoTMQTTClient.configureEndpoint(host, 8883) | ||
myAWSIoTMQTTClient.configureCredentials(rootCAPath, privateKeyPath, certificatePath) | ||
|
||
# AWSIoTMQTTClient connection configuration | ||
myAWSIoTMQTTClient.configureAutoReconnectBackoffTime(1, 32, 20) | ||
myAWSIoTMQTTClient.configureOfflinePublishQueueing(-1) # Infinite offline Publish queueing | ||
myAWSIoTMQTTClient.configureDrainingFrequency(2) # Draining: 2 Hz | ||
myAWSIoTMQTTClient.configureConnectDisconnectTimeout(10) # 10 sec | ||
myAWSIoTMQTTClient.configureMQTTOperationTimeout(5) # 5 sec | ||
|
||
myCallbackContainer = CallbackContainer(myAWSIoTMQTTClient) | ||
|
||
# Connect and subscribe to AWS IoT | ||
myAWSIoTMQTTClient.connect() | ||
|
||
# Perform synchronous subscribes | ||
myAWSIoTMQTTClient.subscribe(topic, 1, myCallbackContainer.messageForward) | ||
myAWSIoTMQTTClient.subscribe(topic + "/republish", 1, myCallbackContainer.messagePrint) | ||
time.sleep(2) | ||
|
||
# Publish to the same topic in a loop forever | ||
loopCount = 0 | ||
while True: | ||
myAWSIoTMQTTClient.publishAsync(topic, "New Message " + str(loopCount), 1, ackCallback=myCallbackContainer.pubackCallback) | ||
loopCount += 1 | ||
time.sleep(1) |