Fix: Convert timestamp to WIB (UTC+7) timezone

This commit is contained in:
flems 2026-05-29 04:26:44 +07:00
parent 5dd7cb1e1a
commit 59d89ae7a5
2 changed files with 23 additions and 4 deletions

View File

@ -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;

View File

@ -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