diff --git a/backend/database.js b/backend/database.js index 2665209..c093a7a 100644 --- a/backend/database.js +++ b/backend/database.js @@ -173,7 +173,12 @@ async function insertSensorData(data) { async function getLatestSensorData() { try { const [rows] = await pool.query( - 'SELECT * FROM sensor_data ORDER BY timestamp DESC LIMIT 1' + `SELECT + id, suhu, berat, target, relay1, relay2, relay3, relay4, status, + CONVERT_TZ(timestamp, '+00:00', '+07:00') as timestamp + FROM sensor_data + ORDER BY timestamp DESC + LIMIT 1` ); return rows[0] || null; } catch (error) { @@ -186,7 +191,12 @@ async function getLatestSensorData() { async function getSensorDataHistory(limit = 50) { try { const [rows] = await pool.query( - 'SELECT * FROM sensor_data ORDER BY timestamp DESC LIMIT ?', + `SELECT + id, suhu, berat, target, relay1, relay2, relay3, relay4, status, + CONVERT_TZ(timestamp, '+00:00', '+07:00') as timestamp + FROM sensor_data + ORDER BY timestamp DESC + LIMIT ?`, [limit] ); return rows; @@ -218,7 +228,12 @@ async function insertStatusHistory(message) { async function getStatusHistory(limit = 50) { try { const [rows] = await pool.query( - 'SELECT * FROM status_history ORDER BY timestamp DESC LIMIT ?', + `SELECT + id, message, + CONVERT_TZ(timestamp, '+00:00', '+07:00') as timestamp + FROM status_history + ORDER BY timestamp DESC + LIMIT ?`, [limit] ); return rows; diff --git a/backend/server.js b/backend/server.js index ea2e036..2f4dd02 100644 --- a/backend/server.js +++ b/backend/server.js @@ -146,6 +146,10 @@ mqttClient.on('message', async (topic, message) => { try { const data = JSON.parse(msg); + // Get current time in WIB (UTC+7) + const now = new Date(); + const wibTime = new Date(now.getTime() + (7 * 60 * 60 * 1000)); + // Update latestData dengan data dari ESP32 latestData = { suhu: data.suhu || 0, @@ -156,7 +160,7 @@ mqttClient.on('message', async (topic, message) => { relay3: latestData.relay3 || false, relay4: latestData.relay4 || false, status: latestData.status, // Keep current status - timestamp: new Date().toISOString() + timestamp: wibTime.toISOString() }; // Save to database