···
logger = logging.getLogger('camera_server')
···
connected_clients = set()
+
# Create a simple HTML gallery template - using triple quotes properly and making sure to escape curly braces
HTML_TEMPLATE = """<!DOCTYPE html>
···
body {{ font-family: Arial; max-width: 800px; margin: 0 auto; padding: 20px; }}
h1 {{ text-align: center; }}
+
.gallery {{ display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }}
+
.photo {{ border: 1px solid #ddd; padding: 5px; animation: fadeIn 0.1s; flex: 0 1 200px; }}
.photo img {{ width: 100%; height: auto; }}
.photo .actions {{ text-align: center; margin-top: 5px; }}
.photo .actions a {{ margin: 0 5px; }}
+
@keyframes fadeIn {{ from {{ opacity: 0; }} to {{ opacity: 1; }} }}
+
@keyframes fadeOut {{ from {{ opacity: 1; }} to {{ opacity: 0; }} }}
+
const RECONNECT_DELAY = 1000;
+
ws = new WebSocket('ws://' + window.location.hostname + ':8765');
+
ws.onmessage = function(event) {{
+
const data = JSON.parse(event.data);
+
if (data.action === 'new_photo') {{
+
addPhoto(data.filename, data.timestamp);
+
}} else if (data.action === 'delete_photo') {{
+
removePhoto(data.filename);
+
ws.onclose = function() {{
+
console.log('WebSocket connection closed. Reconnecting...');
+
setTimeout(connect, RECONNECT_DELAY);
+
ws.onerror = function(err) {{
+
console.error('WebSocket error:', err);
+
function addPhoto(filename, timestamp) {{
+
const gallery = document.querySelector('.gallery');
+
const noPhotosMsg = gallery.querySelector('p');
+
const photoDiv = document.createElement('div');
+
photoDiv.className = 'photo';
+
photoDiv.id = `photo-${{filename}}`;
+
<img src="/${{filename}}" alt="${{timestamp}}">
+
<a href="/${{filename}}" download>Download</a>
+
<a href="#" onclick="deletePhoto('${{filename}}'); return false;">Delete</a>
+
gallery.insertBefore(photoDiv, gallery.firstChild);
+
function removePhoto(filename) {{
+
const photoDiv = document.getElementById(`photo-${{filename}}`);
+
const gallery = document.querySelector('.gallery');
+
if (gallery.children.length === 0) {{
+
const noPhotosMsg = document.createElement('p');
+
noPhotosMsg.style = 'text-align: center;';
+
noPhotosMsg.textContent = 'No photos yet. Press the button to take a photo!';
+
gallery.appendChild(noPhotosMsg);
function deletePhoto(filename) {{
+
if (confirm('Are you sure you want to delete this photo?')) {{
fetch('/delete/' + filename, {{
···
if filename.lower().endswith(('.jpg', '.jpeg', '.png')):
timestamp = filename.replace('photo_', '').replace('.jpg', '')
+
<div class="photo" id="photo-{filename}">
<img src="/{filename}" alt="{timestamp}">
<a href="/{filename}" download>Download</a>
···
self.send_header('Content-type', 'text/plain')
self.wfile.write(b"File deleted successfully")
+
asyncio.run(notify_clients('delete_photo', {'filename': filename}))
self.send_header('Content-type', 'text/plain')
···
connected_clients.remove(websocket)
+
async def notify_clients(action, data):
+
*[client.send(json.dumps(message)) for client in connected_clients]
···
time.sleep(Config.CAMERA_SETTLE_TIME)
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
+
filename = f"photo_{timestamp}.jpg"
+
filepath = os.path.join(Config.PHOTO_DIR, filename)
+
logger.info(f"Taking photo: {filepath}")
+
picam2.capture_file(filepath)
logger.info("Photo taken successfully")
+
# Notify websocket clients about new photo
+
asyncio.run(notify_clients('new_photo', {
logger.error(f"IO Error while taking photo: {str(e)}")
···
logger.info("GPIO cleaned up")
if __name__ == "__main__":