···
logger = logging.getLogger('camera_server')
···
connected_clients = set()
51
-
# Create a simple HTML gallery template - using triple quotes properly
52
+
# 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; }}
60
-
.gallery {{ display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px; }}
61
-
.photo {{ border: 1px solid #ddd; padding: 5px; }}
61
+
.gallery {{ display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }}
62
+
.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; }}
66
+
@keyframes fadeIn {{ from {{ opacity: 0; }} to {{ opacity: 1; }} }}
67
+
@keyframes fadeOut {{ from {{ opacity: 1; }} to {{ opacity: 0; }} }}
67
-
const ws = new WebSocket('ws://' + window.location.hostname + ':8765');
68
-
ws.onmessage = function(event) {{
69
-
if(event.data === 'reload') {{
70
-
window.location.reload();
71
+
const RECONNECT_DELAY = 1000;
73
+
function connect() {{
74
+
ws = new WebSocket('ws://' + window.location.hostname + ':8765');
76
+
ws.onmessage = function(event) {{
77
+
const data = JSON.parse(event.data);
79
+
if (data.action === 'new_photo') {{
80
+
addPhoto(data.filename, data.timestamp);
81
+
}} else if (data.action === 'delete_photo') {{
82
+
removePhoto(data.filename);
86
+
ws.onclose = function() {{
87
+
console.log('WebSocket connection closed. Reconnecting...');
88
+
setTimeout(connect, RECONNECT_DELAY);
91
+
ws.onerror = function(err) {{
92
+
console.error('WebSocket error:', err);
99
+
function addPhoto(filename, timestamp) {{
100
+
const gallery = document.querySelector('.gallery');
101
+
const noPhotosMsg = gallery.querySelector('p');
102
+
if (noPhotosMsg) {{
103
+
noPhotosMsg.remove();
106
+
const photoDiv = document.createElement('div');
107
+
photoDiv.className = 'photo';
108
+
photoDiv.id = `photo-${{filename}}`;
110
+
photoDiv.innerHTML = `
111
+
<img src="/${{filename}}" alt="${{timestamp}}">
112
+
<div class="actions">
113
+
<a href="/${{filename}}" download>Download</a>
114
+
<a href="#" onclick="deletePhoto('${{filename}}'); return false;">Delete</a>
118
+
gallery.insertBefore(photoDiv, gallery.firstChild);
121
+
function removePhoto(filename) {{
122
+
const photoDiv = document.getElementById(`photo-${{filename}}`);
124
+
setTimeout(() => {{
126
+
const gallery = document.querySelector('.gallery');
127
+
if (gallery.children.length === 0) {{
128
+
const noPhotosMsg = document.createElement('p');
129
+
noPhotosMsg.style = 'text-align: center;';
130
+
noPhotosMsg.textContent = 'No photos yet. Press the button to take a photo!';
131
+
gallery.appendChild(noPhotosMsg);
function deletePhoto(filename) {{
75
-
if(confirm('Are you sure you want to delete this photo?')) {{
138
+
if (confirm('Are you sure you want to delete this photo?')) {{
fetch('/delete/' + filename, {{
80
-
window.location.reload();
143
+
removePhoto(filename);
···
if filename.lower().endswith(('.jpg', '.jpeg', '.png')):
timestamp = filename.replace('photo_', '').replace('.jpg', '')
117
-
<div class="photo">
180
+
<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")
150
-
asyncio.run(notify_clients())
213
+
asyncio.run(notify_clients('delete_photo', {'filename': filename}))
self.send_header('Content-type', 'text/plain')
···
connected_clients.remove(websocket)
173
-
async def notify_clients():
236
+
async def notify_clients(action, data):
176
-
*[client.send('reload') for client in connected_clients]
243
+
*[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")
197
-
filename = f"{Config.PHOTO_DIR}/photo_{timestamp}.jpg"
198
-
logger.info(f"Taking photo: {filename}")
264
+
filename = f"photo_{timestamp}.jpg"
265
+
filepath = os.path.join(Config.PHOTO_DIR, filename)
266
+
logger.info(f"Taking photo: {filepath}")
200
-
picam2.capture_file(filename)
268
+
picam2.capture_file(filepath)
logger.info("Photo taken successfully")
203
-
# Notify websocket clients to reload
204
-
asyncio.run(notify_clients())
271
+
# Notify websocket clients about new photo
272
+
asyncio.run(notify_clients('new_photo', {
273
+
'filename': filename,
274
+
'timestamp': timestamp
logger.error(f"IO Error while taking photo: {str(e)}")
···
logger.info("GPIO cleaned up")
if __name__ == "__main__":