Spaces:
Sleeping
Sleeping
Upload 7 files
Browse files- app.py +9 -1
- static/js/script.js +4 -9
app.py
CHANGED
|
@@ -2,7 +2,15 @@ from flask import Flask, render_template, jsonify, request
|
|
| 2 |
from flask_cors import CORS
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
| 5 |
-
CORS(app)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Trạng thái đèn (lưu trong memory, có thể thay bằng database)
|
| 8 |
light_status = {
|
|
|
|
| 2 |
from flask_cors import CORS
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
| 5 |
+
CORS(app, resources={r"/api/*": {"origins": "*"}})
|
| 6 |
+
|
| 7 |
+
# Thêm security headers cho HF Spaces
|
| 8 |
+
@app.after_request
|
| 9 |
+
def after_request(response):
|
| 10 |
+
response.headers.add('Access-Control-Allow-Origin', '*')
|
| 11 |
+
response.headers.add('Access-Control-Allow-Headers', 'Content-Type')
|
| 12 |
+
response.headers.add('Access-Control-Allow-Methods', 'GET,POST,OPTIONS')
|
| 13 |
+
return response
|
| 14 |
|
| 15 |
# Trạng thái đèn (lưu trong memory, có thể thay bằng database)
|
| 16 |
light_status = {
|
static/js/script.js
CHANGED
|
@@ -2,22 +2,15 @@
|
|
| 2 |
function updateUI(isOn) {
|
| 3 |
const bulb = document.getElementById('bulb');
|
| 4 |
const statusText = document.getElementById('statusText');
|
| 5 |
-
const currentStatus = document.getElementById('currentStatus');
|
| 6 |
|
| 7 |
if (isOn) {
|
| 8 |
bulb.classList.add('on');
|
| 9 |
statusText.classList.add('on');
|
| 10 |
statusText.textContent = 'Đèn đang bật';
|
| 11 |
-
currentStatus.textContent = 'BẬT';
|
| 12 |
-
currentStatus.classList.remove('off');
|
| 13 |
-
currentStatus.classList.add('on');
|
| 14 |
} else {
|
| 15 |
bulb.classList.remove('on');
|
| 16 |
statusText.classList.remove('on');
|
| 17 |
statusText.textContent = 'Đèn đang tắt';
|
| 18 |
-
currentStatus.textContent = 'TẮT';
|
| 19 |
-
currentStatus.classList.remove('on');
|
| 20 |
-
currentStatus.classList.add('off');
|
| 21 |
}
|
| 22 |
}
|
| 23 |
|
|
@@ -25,11 +18,12 @@ function updateUI(isOn) {
|
|
| 25 |
async function getStatus() {
|
| 26 |
try {
|
| 27 |
const response = await fetch('/api/light/status');
|
|
|
|
| 28 |
const data = await response.json();
|
| 29 |
updateUI(data.on);
|
| 30 |
} catch (error) {
|
| 31 |
console.error('Lỗi khi lấy trạng thái:', error);
|
| 32 |
-
|
| 33 |
}
|
| 34 |
}
|
| 35 |
|
|
@@ -42,12 +36,13 @@ async function toggleLight() {
|
|
| 42 |
'Content-Type': 'application/json'
|
| 43 |
}
|
| 44 |
});
|
|
|
|
| 45 |
const data = await response.json();
|
| 46 |
updateUI(data.on);
|
| 47 |
showNotification(data.message);
|
| 48 |
} catch (error) {
|
| 49 |
console.error('Lỗi khi chuyển đổi đèn:', error);
|
| 50 |
-
|
| 51 |
}
|
| 52 |
}
|
| 53 |
|
|
|
|
| 2 |
function updateUI(isOn) {
|
| 3 |
const bulb = document.getElementById('bulb');
|
| 4 |
const statusText = document.getElementById('statusText');
|
|
|
|
| 5 |
|
| 6 |
if (isOn) {
|
| 7 |
bulb.classList.add('on');
|
| 8 |
statusText.classList.add('on');
|
| 9 |
statusText.textContent = 'Đèn đang bật';
|
|
|
|
|
|
|
|
|
|
| 10 |
} else {
|
| 11 |
bulb.classList.remove('on');
|
| 12 |
statusText.classList.remove('on');
|
| 13 |
statusText.textContent = 'Đèn đang tắt';
|
|
|
|
|
|
|
|
|
|
| 14 |
}
|
| 15 |
}
|
| 16 |
|
|
|
|
| 18 |
async function getStatus() {
|
| 19 |
try {
|
| 20 |
const response = await fetch('/api/light/status');
|
| 21 |
+
if (!response.ok) throw new Error('Server error');
|
| 22 |
const data = await response.json();
|
| 23 |
updateUI(data.on);
|
| 24 |
} catch (error) {
|
| 25 |
console.error('Lỗi khi lấy trạng thái:', error);
|
| 26 |
+
// Không hiện alert, chỉ log lỗi
|
| 27 |
}
|
| 28 |
}
|
| 29 |
|
|
|
|
| 36 |
'Content-Type': 'application/json'
|
| 37 |
}
|
| 38 |
});
|
| 39 |
+
if (!response.ok) throw new Error('Server error');
|
| 40 |
const data = await response.json();
|
| 41 |
updateUI(data.on);
|
| 42 |
showNotification(data.message);
|
| 43 |
} catch (error) {
|
| 44 |
console.error('Lỗi khi chuyển đổi đèn:', error);
|
| 45 |
+
showNotification('⚠️ Lỗi kết nối server!');
|
| 46 |
}
|
| 47 |
}
|
| 48 |
|