feat: working version
This commit is contained in:
parent
ab8fcfd665
commit
a0710f69d1
3
Pipfile
3
Pipfile
@ -8,8 +8,9 @@ awsiotsdk = "*"
|
|||||||
boto3 = "*"
|
boto3 = "*"
|
||||||
opencv-python = "*"
|
opencv-python = "*"
|
||||||
requests = "*"
|
requests = "*"
|
||||||
|
"rpi.gpio" = "*"
|
||||||
|
|
||||||
[dev-packages]
|
[dev-packages]
|
||||||
|
|
||||||
[requires]
|
[requires]
|
||||||
python_version = "3.10"
|
python_version = "3.9"
|
||||||
|
@ -48,6 +48,6 @@ def detect_faces(photo, bucket):
|
|||||||
def iot_publish(payload):
|
def iot_publish(payload):
|
||||||
iot_data.publish(
|
iot_data.publish(
|
||||||
topic=TOPIC,
|
topic=TOPIC,
|
||||||
qos=1,
|
qos=0,
|
||||||
payload=json.dumps(payload)
|
payload=json.dumps(payload)
|
||||||
)
|
)
|
@ -15,15 +15,15 @@ def lambda_handler(event, _context):
|
|||||||
filename = event['filename']
|
filename = event['filename']
|
||||||
|
|
||||||
try:
|
try:
|
||||||
url = s3.generate_presigned_post(BUCKET, filename)
|
result = s3.generate_presigned_put(BUCKET, filename)
|
||||||
print("Got presigned URL: %s", url)
|
print("Got presigned URL: {}".format(result['url']))
|
||||||
except ClientError as e:
|
except ClientError as e:
|
||||||
print(e)
|
print(e)
|
||||||
print("Couldn't get a presigned URL")
|
print("Couldn't get a presigned URL")
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
try:
|
try:
|
||||||
iot_publish({ "url": url })
|
iot_publish(result)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
print(f"Error while publishing to MQTT topic {TOPIC}.")
|
print(f"Error while publishing to MQTT topic {TOPIC}.")
|
||||||
@ -35,6 +35,6 @@ def lambda_handler(event, _context):
|
|||||||
def iot_publish(payload):
|
def iot_publish(payload):
|
||||||
iot_data.publish(
|
iot_data.publish(
|
||||||
topic=TOPIC,
|
topic=TOPIC,
|
||||||
qos=1,
|
qos=0,
|
||||||
payload=json.dumps(payload)
|
payload=json.dumps(payload)
|
||||||
)
|
)
|
@ -26,7 +26,7 @@ LED_TOPIC = "cloudprog/hw4/team02/led"
|
|||||||
S3_UPLOAD_TOPIC = "cloudprog/hw4/team02/s3/upload"
|
S3_UPLOAD_TOPIC = "cloudprog/hw4/team02/s3/upload"
|
||||||
S3_URL_TOPIC = "cloudprog/hw4/team02/s3/url"
|
S3_URL_TOPIC = "cloudprog/hw4/team02/s3/url"
|
||||||
|
|
||||||
FORMAT = "jpg"
|
FORMAT = ".jpg"
|
||||||
|
|
||||||
filename = None
|
filename = None
|
||||||
cam = cv2.VideoCapture(0)
|
cam = cv2.VideoCapture(0)
|
||||||
@ -95,24 +95,25 @@ def led_control(light):
|
|||||||
def parse_s3_url_payload(payload):
|
def parse_s3_url_payload(payload):
|
||||||
data = json.loads(payload.decode())
|
data = json.loads(payload.decode())
|
||||||
|
|
||||||
post_presigned_s3_url(data["url"], take_picture())
|
post_presigned_s3_url(data, take_picture())
|
||||||
|
|
||||||
|
|
||||||
def take_picture():
|
def take_picture():
|
||||||
cam.open()
|
#cam.open()
|
||||||
ret, frame = cam.read()
|
ret, frame = cam.read()
|
||||||
ret, img = cv2.imencode(FORMAT, frame)
|
ret, img = cv2.imencode(FORMAT, frame)
|
||||||
cam.release()
|
cam.release()
|
||||||
return img.tobytes()
|
return img.tobytes()
|
||||||
|
|
||||||
|
|
||||||
def post_presigned_s3_url(url, img_bytes):
|
def post_presigned_s3_url(data, img_bytes):
|
||||||
res = requests.post(url, data=img_bytes)
|
fn = data['fields']['key']
|
||||||
|
res = requests.post(data['url'], data=data['fields'], files={ "file": (fn, img_bytes) })
|
||||||
print(f"Put to S3. status code: {res.status_code}")
|
print(f"Put to S3. status code: {res.status_code}")
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main(loop):
|
||||||
global filename
|
global filename
|
||||||
# create MQTT connection
|
# create MQTT connection
|
||||||
mqtt_connection = mqtt_connection_builder.mtls_from_path(
|
mqtt_connection = mqtt_connection_builder.mtls_from_path(
|
||||||
@ -133,7 +134,7 @@ async def main():
|
|||||||
# subscribe to MQTT topic
|
# subscribe to MQTT topic
|
||||||
subscribe_future, packet_id = mqtt_connection.subscribe(
|
subscribe_future, packet_id = mqtt_connection.subscribe(
|
||||||
topic=LED_TOPIC,
|
topic=LED_TOPIC,
|
||||||
qos=mqtt.QoS.AT_LEAST_ONCE,
|
qos=mqtt.QoS.AT_MOST_ONCE,
|
||||||
callback=on_message_received
|
callback=on_message_received
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -143,7 +144,7 @@ async def main():
|
|||||||
|
|
||||||
subscribe_future, packet_id = mqtt_connection.subscribe(
|
subscribe_future, packet_id = mqtt_connection.subscribe(
|
||||||
topic=S3_URL_TOPIC,
|
topic=S3_URL_TOPIC,
|
||||||
qos=mqtt.QoS.AT_LEAST_ONCE,
|
qos=mqtt.QoS.AT_MOST_ONCE,
|
||||||
callback=on_message_received
|
callback=on_message_received
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -155,17 +156,18 @@ async def main():
|
|||||||
command = input("> ")
|
command = input("> ")
|
||||||
|
|
||||||
if command == "p":
|
if command == "p":
|
||||||
filename = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d-%H-%M-%S") + "." + FORMAT
|
filename = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d-%H-%M-%S") + FORMAT
|
||||||
publish_future, packet_id = mqtt_connection.publish(
|
publish_future, packet_id = mqtt_connection.publish(
|
||||||
topic=S3_UPLOAD_TOPIC,
|
topic=S3_UPLOAD_TOPIC,
|
||||||
payload=json.dumps({ "filename": filename }),
|
payload=json.dumps({ "filename": filename }),
|
||||||
qos=mqtt.QoS.AT_LEAST_ONCE
|
qos=mqtt.QoS.AT_MOST_ONCE
|
||||||
)
|
)
|
||||||
publish_result = publish_future.result()
|
publish_result = publish_future.result()
|
||||||
print(publish_result)
|
print(publish_result)
|
||||||
print(f"Upload request {filename} sent!")
|
print(f"Upload request {filename} sent!")
|
||||||
elif command == "q":
|
elif command == "q":
|
||||||
break
|
break
|
||||||
|
loop.stop()
|
||||||
|
|
||||||
|
|
||||||
def handler(loop, context):
|
def handler(loop, context):
|
||||||
@ -180,6 +182,6 @@ def handler(loop, context):
|
|||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
loop.set_exception_handler(handler)
|
loop.set_exception_handler(handler)
|
||||||
|
|
||||||
loop.create_task(main())
|
loop.create_task(main(loop))
|
||||||
|
|
||||||
loop.run_forever()
|
loop.run_forever()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user