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