Update server.js
This commit is contained in:
parent
f0bacd9cde
commit
612ba2bb24
64
server.js
64
server.js
@ -14,7 +14,12 @@ const PORT = process.env.PORT || 3000;
|
|||||||
const shiftCounters = {};
|
const shiftCounters = {};
|
||||||
|
|
||||||
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
||||||
// Format an epoch ms timestamp for Slack: "M/D/YY @HH:mm" in America/New_York
|
// pad two digits
|
||||||
|
function pad2(n) {
|
||||||
|
return n.toString().padStart(2, '0');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format an epoch‐ms timestamp for Slack: "M/D/YY @HH:mm" in America/New_York
|
||||||
function formatForSlack(epoch) {
|
function formatForSlack(epoch) {
|
||||||
return new Date(epoch).toLocaleString('en-US', {
|
return new Date(epoch).toLocaleString('en-US', {
|
||||||
timeZone: 'America/New_York',
|
timeZone: 'America/New_York',
|
||||||
@ -39,19 +44,22 @@ function computeHeatIndex(T, R) {
|
|||||||
return Math.round(HI * 100) / 100;
|
return Math.round(HI * 100) / 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine Day/Night shift and rolling period key
|
// Determine Day/Night shift & rolling period key based on a JS epoch
|
||||||
function getShiftInfo(epoch) {
|
function getShiftInfo(epoch) {
|
||||||
// first get a string in NY time, then parse back to Date
|
// convert epoch to an EST Date object by round‐trip through toLocaleString
|
||||||
const estString = new Date(epoch).toLocaleString('en-US', { timeZone: 'America/New_York' });
|
const estString = new Date(epoch)
|
||||||
|
.toLocaleString('en-US', { timeZone: 'America/New_York' });
|
||||||
const est = new Date(estString);
|
const est = new Date(estString);
|
||||||
const h = est.getHours(), m = est.getMinutes();
|
const h = est.getHours(), m = est.getMinutes();
|
||||||
let shift, start = new Date(est);
|
let shift, start = new Date(est);
|
||||||
|
|
||||||
if (h > 7 || (h === 7 && m >= 0)) {
|
if (h > 7 || (h === 7 && m >= 0)) {
|
||||||
if (h < 17 || (h === 17 && m < 30)) {
|
if (h < 17 || (h === 17 && m < 30)) {
|
||||||
shift = 'Day'; start.setHours(7,0,0,0);
|
shift = 'Day';
|
||||||
|
start.setHours(7, 0, 0, 0);
|
||||||
} else {
|
} else {
|
||||||
shift = 'Night'; start.setHours(17,30,0,0);
|
shift = 'Night';
|
||||||
|
start.setHours(17, 30, 0, 0);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
shift = 'Night';
|
shift = 'Night';
|
||||||
@ -95,8 +103,8 @@ const pool = mysql.createPool({
|
|||||||
connectTimeout: 10000
|
connectTimeout: 10000
|
||||||
});
|
});
|
||||||
|
|
||||||
// Ensure readings table exists (with epoch_ms instead of timestamp)
|
|
||||||
(async () => {
|
(async () => {
|
||||||
|
// Create readings table with epoch_ms instead of timestamp
|
||||||
await pool.execute(`
|
await pool.execute(`
|
||||||
CREATE TABLE IF NOT EXISTS readings (
|
CREATE TABLE IF NOT EXISTS readings (
|
||||||
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||||||
@ -150,13 +158,13 @@ app.post('/api/readings', async (req, res) => {
|
|||||||
shiftCounters[key] = (shiftCounters[key] || 0) + 1;
|
shiftCounters[key] = (shiftCounters[key] || 0) + 1;
|
||||||
const period = shiftCounters[key];
|
const period = shiftCounters[key];
|
||||||
|
|
||||||
// Insert inbound + outbound
|
// Insert inbound and outbound
|
||||||
const sql = `
|
const insertSQL = `
|
||||||
INSERT INTO readings(location,stationDockDoor,epoch_ms,temperature,humidity,heatIndex)
|
INSERT INTO readings(location,stationDockDoor,epoch_ms,temperature,humidity,heatIndex)
|
||||||
VALUES (?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?)
|
||||||
`;
|
`;
|
||||||
await pool.execute(sql, ['Inbound', String(inD), epoch, inT, inH, hiIn]);
|
await pool.execute(insertSQL, ['Inbound', String(inD), epoch, inT, inH, hiIn]);
|
||||||
await pool.execute(sql, ['Outbound', String(outD), epoch, outT, outH, hiOut]);
|
await pool.execute(insertSQL, ['Outbound', String(outD), epoch, outT, outH, hiOut]);
|
||||||
|
|
||||||
// Broadcast via SSE
|
// Broadcast via SSE
|
||||||
const slackTs = formatForSlack(epoch);
|
const slackTs = formatForSlack(epoch);
|
||||||
@ -177,15 +185,20 @@ app.post('/api/readings', async (req, res) => {
|
|||||||
heatIndex: hiOut
|
heatIndex: hiOut
|
||||||
});
|
});
|
||||||
|
|
||||||
// CSV upload for today's readings
|
// Upload today's CSV
|
||||||
const y = estNow.getFullYear(), m = pad2(estNow.getMonth()+1), d = pad2(estNow.getDate());
|
const y = estNow.getFullYear(), m = pad2(estNow.getMonth()+1), d = pad2(estNow.getDate());
|
||||||
const dateKey = `${y}${m}${d}`;
|
const dateKey = `${y}${m}${d}`;
|
||||||
const [rows] = await pool.execute(
|
const [rows] = await pool.execute(
|
||||||
`SELECT * FROM readings WHERE DATE(FROM_UNIXTIME(epoch_ms/1000)) = CURDATE() ORDER BY epoch_ms`
|
`SELECT * FROM readings
|
||||||
|
WHERE DATE(FROM_UNIXTIME(epoch_ms/1000)) = CURDATE()
|
||||||
|
ORDER BY epoch_ms`
|
||||||
);
|
);
|
||||||
let csvUrl = null;
|
let csvUrl = null;
|
||||||
try { csvUrl = await uploadTrendsCsv(dateKey, rows); }
|
try {
|
||||||
catch (e) { console.error('CSV upload error:', e); }
|
csvUrl = await uploadTrendsCsv(dateKey, rows);
|
||||||
|
} catch (e) {
|
||||||
|
console.error('CSV upload error:', e);
|
||||||
|
}
|
||||||
|
|
||||||
// Send Slack notification
|
// Send Slack notification
|
||||||
const weather = await fetchCurrentWeather();
|
const weather = await fetchCurrentWeather();
|
||||||
@ -245,11 +258,16 @@ app.post('/api/area-readings', async (req, res) => {
|
|||||||
const y = estNow.getFullYear(), m = pad2(estNow.getMonth()+1), d = pad2(estNow.getDate());
|
const y = estNow.getFullYear(), m = pad2(estNow.getMonth()+1), d = pad2(estNow.getDate());
|
||||||
const dateKey = `${y}${m}${d}`;
|
const dateKey = `${y}${m}${d}`;
|
||||||
const [rows] = await pool.execute(
|
const [rows] = await pool.execute(
|
||||||
`SELECT * FROM readings WHERE DATE(FROM_UNIXTIME(epoch_ms/1000)) = CURDATE() ORDER BY epoch_ms`
|
`SELECT * FROM readings
|
||||||
|
WHERE DATE(FROM_UNIXTIME(epoch_ms/1000)) = CURDATE()
|
||||||
|
ORDER BY epoch_ms`
|
||||||
);
|
);
|
||||||
let csvUrl = null;
|
let csvUrl = null;
|
||||||
try { csvUrl = await uploadTrendsCsv(dateKey, rows); }
|
try {
|
||||||
catch (e) { console.error('CSV upload error:', e); }
|
csvUrl = await uploadTrendsCsv(dateKey, rows);
|
||||||
|
} catch (e) {
|
||||||
|
console.error('CSV upload error:', e);
|
||||||
|
}
|
||||||
|
|
||||||
const weather = await fetchCurrentWeather();
|
const weather = await fetchCurrentWeather();
|
||||||
const text =
|
const text =
|
||||||
@ -292,7 +310,10 @@ app.get('/api/export', async (req, res) => {
|
|||||||
res.set('Content-Type', 'text/csv');
|
res.set('Content-Type', 'text/csv');
|
||||||
res.write('id,location,stationDockDoor,epoch_ms,temperature,humidity,heatIndex\n');
|
res.write('id,location,stationDockDoor,epoch_ms,temperature,humidity,heatIndex\n');
|
||||||
rows.forEach(r => {
|
rows.forEach(r => {
|
||||||
res.write(`${r.id},${r.location},${r.stationDockDoor},${r.epoch_ms},${r.temperature},${r.humidity},${r.heatIndex}\n`);
|
res.write(
|
||||||
|
`${r.id},${r.location},${r.stationDockDoor},${r.epoch_ms},` +
|
||||||
|
`${r.temperature},${r.humidity},${r.heatIndex}\n`
|
||||||
|
);
|
||||||
});
|
});
|
||||||
res.end();
|
res.end();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@ -305,8 +326,3 @@ app.get('/api/export', async (req, res) => {
|
|||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
console.log(`Server running on http://localhost:${PORT}`);
|
console.log(`Server running on http://localhost:${PORT}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Utility pad2 used above
|
|
||||||
function pad2(n) {
|
|
||||||
return n.toString().padStart(2,'0');
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user