From 3a60036edc17c1de4902c717f512d14c28ebbc4c Mon Sep 17 00:00:00 2001 From: "Gambit Communications, Inc" Date: Mon, 20 Apr 2020 10:46:51 -0400 Subject: [PATCH 1/5] Add files via upload --- list_things.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/list_things.py b/list_things.py index e6185e6..c08e5b1 100644 --- a/list_things.py +++ b/list_things.py @@ -26,12 +26,14 @@ ########################################################################### class MyApp: def __init__(self): + self.region = None self.access = None self.secret = None self.verbose = False def usage(self): print ("Usage: listthings.py") + print ("\t-r|--region region-name AWS_REGION") print ("\t-a|--access access-key AWS_ACCESS_KEY_ID") print ("\t-s|--secret secret-key AWS_SECRET_ACCESS_KEY") print ("\t[-v|--verbose] verbose output") @@ -39,7 +41,7 @@ def usage(self): def command_line(self): try: - opts, args = getopt.getopt(sys.argv[1:], "a:s:v", ["access=", "secret=", "verbose"]) + opts, args = getopt.getopt(sys.argv[1:], "h:p:r:a:s:v", ["host=", "port=", "region=", "access=", "secret=", "verbose"]) except getopt.GetoptError as err: # print help information and exit: print (str(err)) # will print something like "option -a not recognized" @@ -49,13 +51,23 @@ def command_line(self): for o, a in opts: if o in ("-v", "--verbose"): self.verbose = True + elif o in ("-r", "--region"): + self.region = a elif o in ("-a", "--access"): self.access = a elif o in ("-s", "--secret"): self.secret = a + elif o in ("-h", "--host"): + dummy = a + elif o in ("-p", "--port"): + dummy = a else: assert False, "unhandled option" + if self.region == None: + self.usage() + sys.exit(1) + if self.access == None: self.usage() sys.exit(1) @@ -69,6 +81,7 @@ def start(self): client = boto3.client( 'iot', + region_name = self.region, aws_access_key_id = self.access, aws_secret_access_key = self.secret ) From a4f2765e805880707af13d3599c76fcc308a854b Mon Sep 17 00:00:00 2001 From: "Gambit Communications, Inc" Date: Mon, 20 Apr 2020 10:47:11 -0400 Subject: [PATCH 2/5] Add files via upload --- get_thing_shadow.py | 115 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 get_thing_shadow.py diff --git a/get_thing_shadow.py b/get_thing_shadow.py new file mode 100644 index 0000000..6e588e4 --- /dev/null +++ b/get_thing_shadow.py @@ -0,0 +1,115 @@ +# Copyright 2019. Gambit Communications, Inc. All Rights Reserved. +# Copyright 2013. Amazon Web Services, Inc. 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License 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. + +# Import the SDK +import boto3 +import uuid + +import json + +import os +import getopt +import sys + +# use https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iot-data.html#get_thing_shadow +# requires AWSIoTDataAccess policy for the user, else get permission exception +# +# also https://forums.aws.amazon.com/thread.jspa?threadID=221549 + +########################################################################### +class MyApp: + def __init__(self): + self.region = None + self.access = None + self.secret = None + self.thing = None + self.verbose = False + + def usage(self): + print ("Usage: listthings.py") + print ("\t-r|--region region-name AWS_REGION") + print ("\t-a|--access access-key AWS_ACCESS_KEY_ID") + print ("\t-s|--secret secret-key AWS_SECRET_ACCESS_KEY") + print ("\t-t|--thing thing thing name") + print ("\t[-v|--verbose] verbose output") + return + + def command_line(self): + try: + opts, args = getopt.getopt(sys.argv[1:], "h:p:r:a:s:t:v", ["host=", "port=", "region=", "access=", "secret=", "thing=", "verbose"]) + except getopt.GetoptError as err: + # print help information and exit: + print (str(err)) # will print something like "option -a not recognized" + self.usage() + sys.exit(1) + + for o, a in opts: + if o in ("-v", "--verbose"): + self.verbose = True + elif o in ("-r", "--region"): + self.region = a + elif o in ("-a", "--access"): + self.access = a + elif o in ("-s", "--secret"): + self.secret = a + elif o in ("-t", "--thing"): + self.thing = a + elif o in ("-h", "--host"): + dummy = a + elif o in ("-p", "--port"): + dummy = a + else: + assert False, "unhandled option" + + if self.region == None: + self.usage() + sys.exit(1) + + if self.access == None: + self.usage() + sys.exit(1) + + if self.secret == None: + self.usage() + sys.exit(1) + + if self.thing == None: + self.usage() + sys.exit(1) + + def start(self): + self.command_line() + + client = boto3.client( + 'iot-data', + region_name = self.region, + aws_access_key_id = self.access, + aws_secret_access_key = self.secret + ) + + response = client.get_thing_shadow( + thingName = self.thing + ) + + streamingBody = response["payload"] + rawDataBytes = streamingBody.read() # rawDataBytes is of type 'bytes' in, Python 3.x specific + rawDataString = rawDataBytes.decode('utf-8') # Python 3.x specific + jsonState = json.loads(rawDataString) + print jsonState + +########################################################################### +if __name__ == "__main__": + main = MyApp() + main.start() From d37c2142708027a7683f0d140d407e3ba7f664ef Mon Sep 17 00:00:00 2001 From: "Gambit Communications, Inc" Date: Mon, 20 Apr 2020 10:49:37 -0400 Subject: [PATCH 3/5] Update README.md --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9786a82..299ae85 100644 --- a/README.md +++ b/README.md @@ -7,12 +7,17 @@ large-scale prototypes with MIMIC MQTT Simulator https://www.gambitcomm.com/site % python list_things.py Usage: listthings.py + -r|--region region-name AWS_REGION -a|--access access-key AWS_ACCESS_KEY_ID -s|--secret secret-key AWS_SECRET_ACCESS_KEY [-v|--verbose] verbose output - % python list_things.py -a 'XXXXX' -s 'XXXX' + % python list_things.py -r us-east-2 -a 'XXXXX' -s 'XXXX' {u'things': [{u'thingArn': u'arn:aws:iot:us-east-1:409128494776:thing/mimic-10', u'version': 1, u'thingName': u'mimic-10', u'attributes': {}}, {u'thingArn': u'arn:aws:iot:us-east-1:409128494776:thing/mimic-9', u'version': 1, u'thingName': u'mimic-9', u'attributes': {}},... + + % python get_thing_shadow.py -r us-east-2 -a 'xxxxx' -s 'xxxxx' -t mimic-1 + {u'timestamp': 1587393680, u'state': {u'reported': {u'color': u'yellow', u'mem': 25, u'temp': 50000, u'power': u'on', u'light': 4157}}, u'version': 3765, u'metadata': {u'reported': {u'color': {u'timestamp': 1584978911}, u'mem': {u'timestamp': 1584978911}, u'temp': {u'timestamp': 1584978911}, u'power': {u'timestamp': 1584978911}, u'light': {u'timestamp': 1584978911}}}} + From 24d0c6336b14400d1e4e9806dc30b5b3857927df Mon Sep 17 00:00:00 2001 From: "Gambit Communications, Inc" Date: Mon, 20 Apr 2020 10:57:03 -0400 Subject: [PATCH 4/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 299ae85..5fc0493 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ large-scale prototypes with MIMIC MQTT Simulator https://www.gambitcomm.com/site -s|--secret secret-key AWS_SECRET_ACCESS_KEY [-v|--verbose] verbose output - % python list_things.py -r us-east-2 -a 'XXXXX' -s 'XXXX' + % python list_things.py -r us-east-1 -a 'XXXXX' -s 'XXXX' {u'things': [{u'thingArn': u'arn:aws:iot:us-east-1:409128494776:thing/mimic-10', u'version': 1, u'thingName': u'mimic-10', u'attributes': {}}, {u'thingArn': u'arn:aws:iot:us-east-1:409128494776:thing/mimic-9', u'version': 1, u'thingName': u'mimic-9', u'attributes': {}},... From 7ee6c2caa8503c91741ca81cca73e77f36be1211 Mon Sep 17 00:00:00 2001 From: "Gambit Communications, Inc" Date: Sat, 1 Jul 2023 09:02:47 -0400 Subject: [PATCH 5/5] Add files via upload --- creatething.py | 101 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 creatething.py diff --git a/creatething.py b/creatething.py new file mode 100644 index 0000000..a27ce31 --- /dev/null +++ b/creatething.py @@ -0,0 +1,101 @@ +# Copyright 2019. Gambit Communications, Inc. All Rights Reserved. +# Copyright 2013. Amazon Web Services, Inc. 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License 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. + +# Import the SDK +import boto3 +import uuid + +import os +import getopt +import sys + +# use https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iot.html#IoT.Client.create_thing + +########################################################################### +class MyApp: + def __init__(self): + self.access = None + self.secret = None + self.prefix = None + self.count = 1 + self.verbose = False + + def usage(self): + print ("Usage: creatething.py") + print ("\t-a|--access access-key AWS_ACCESS_KEY_ID") + print ("\t-s|--secret secret-key AWS_SECRET_ACCESS_KEY") + print ("\t-p|--prefix thing-prefix thing name prefix") + print ("\t[-c|--count count] count of things to be created, default 1") + print ("\t[-v|--verbose] verbose output") + return + + def command_line(self): + try: + opts, args = getopt.getopt(sys.argv[1:], "a:s:p:c:v", ["access=", "secret=", "prefix=", "count=", "verbose"]) + except getopt.GetoptError as err: + # print help information and exit: + print (str(err)) # will print something like "option -a not recognized" + self.usage() + sys.exit(1) + + for o, a in opts: + if o in ("-v", "--verbose"): + self.verbose = True + elif o in ("-a", "--access"): + self.access = a + elif o in ("-s", "--secret"): + self.secret = a + elif o in ("-p", "--prefix"): + self.prefix = a + elif o in ("-c", "--count"): + self.count = int(a) + else: + assert False, "unhandled option" + + if self.access == None: + self.usage() + sys.exit(1) + + if self.secret == None: + self.usage() + sys.exit(1) + + if self.prefix == None: + self.usage() + sys.exit(1) + + if self.count == None: + self.count = 1 + + def start(self): + self.command_line() + + client = boto3.client( + 'iot', + aws_access_key_id = self.access, + aws_secret_access_key = self.secret + ) + + for i in range(1, self.count + 1): + thingname = self.prefix + '-' + str(i) + response = client.create_thing( + thingName = thingname + ) + print response + +########################################################################### +if __name__ == "__main__": + main = MyApp() + main.start()