UART & Sensors
This commit is contained in:
+13
-10
@@ -20,9 +20,9 @@ db = client["microwave_network_db"]
|
||||
cooking_collection = db["cooking_parameters"]
|
||||
device_network_collection = db["device_network"]
|
||||
|
||||
# Ensure the photo storage directory exists when the app starts
|
||||
PHOTO_DIR = "storage/dishPhotos"
|
||||
os.makedirs(PHOTO_DIR, exist_ok=True)
|
||||
# Ensure the camera image storage directory exists when the app starts
|
||||
CAMERA_IMAGE_DIR = "storage/dishCameraImages"
|
||||
os.makedirs(CAMERA_IMAGE_DIR, exist_ok=True)
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# Routes
|
||||
@@ -40,24 +40,24 @@ def cooking_params():
|
||||
if not data:
|
||||
return jsonify({"error": "Invalid or missing JSON payload"}), 400
|
||||
|
||||
# 1. Handle the Photo
|
||||
photo_b64 = data.get("photo")
|
||||
if photo_b64:
|
||||
# 1. Handle the Camera Image
|
||||
camera_image_b64 = data.get("camera_image")
|
||||
if camera_image_b64:
|
||||
# Generate a unique filename using UUID to avoid overwriting
|
||||
filename = f"dish_{uuid.uuid4().hex}.jpg"
|
||||
filepath = os.path.join(PHOTO_DIR, filename)
|
||||
filepath = os.path.join(CAMERA_IMAGE_DIR, filename)
|
||||
|
||||
try:
|
||||
# Decode the base64 string and save it as a binary file
|
||||
with open(filepath, "wb") as f:
|
||||
f.write(base64.b64decode(photo_b64))
|
||||
f.write(base64.b64decode(camera_image_b64))
|
||||
|
||||
# Replace the giant base64 string in the dictionary with the local file path
|
||||
# so we don't bloat the MongoDB document
|
||||
data["photo"] = filepath
|
||||
data["camera_image"] = filepath
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({"error": f"Failed to save photo: {str(e)}"}), 500
|
||||
return jsonify({"error": f"Failed to save camera image: {str(e)}"}), 500
|
||||
|
||||
# 2. Save to MongoDB
|
||||
try:
|
||||
@@ -70,6 +70,9 @@ def cooking_params():
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({"error": f"Database error: {str(e)}"}), 500
|
||||
|
||||
# 3. Returns with the cooking parameters
|
||||
|
||||
|
||||
|
||||
@app.route("/device-network", methods=["POST"])
|
||||
|
||||
Reference in New Issue
Block a user