diff --git a/server.js b/server.js index a0b8222..d91eebe 100644 --- a/server.js +++ b/server.js @@ -14,17 +14,17 @@ const PORT = process.env.PORT || 3000; const shiftCounters = {}; // Helpers -const pad2 = n => n.toString().padStart(2, '0'); +const pad2 = n => n.toString().padStart(2,'0'); -function shortEST(d) { - const est = new Date(d.toLocaleString('en-US', { timeZone: 'America/New_York' })); - const M = est.getMonth() + 1, D = est.getDate(), YY = String(est.getFullYear()).slice(-2); +function shortEST(date) { + const est = new Date(date.toLocaleString('en-US', { timeZone: 'America/New_York' })); + const M = est.getMonth()+1, D = est.getDate(), YY = String(est.getFullYear()).slice(-2); const hh = pad2(est.getHours()), mm = pad2(est.getMinutes()); return `${M}/${D}/${YY} @${hh}:${mm}`; } -function formatDateEST(d) { - const est = new Date(d.toLocaleString('en-US', { timeZone: 'America/New_York' })); +function formatDateEST(date) { + const est = new Date(date.toLocaleString('en-US', { timeZone: 'America/New_York' })); const y = est.getFullYear(), M = pad2(est.getMonth()+1), D = pad2(est.getDate()); const hh = pad2(est.getHours()), mm = pad2(est.getMinutes()), ss = pad2(est.getSeconds()); return `${y}-${M}-${D} ${hh}:${mm}:${ss}`; @@ -37,7 +37,7 @@ function computeHeatIndex(T, R) { const HI = c1 + c2*T + c3*R + c4*T*R + c5*T*T + c6*R*R + c7*T*T*R + c8*T*R*R + c9*T*T*R*R; - return Math.round(HI * 100) / 100; + return Math.round(HI*100)/100; } function getShiftInfo(now) { @@ -47,13 +47,15 @@ function getShiftInfo(now) { if (h > 7 || (h === 7 && m >= 0)) { if (h < 17 || (h === 17 && m < 30)) { - shift = 'Day'; start.setHours(7,0,0,0); + shift = 'Day'; + start.setHours(7,0,0,0); } else { - shift = 'Night'; start.setHours(17,30,0,0); + shift = 'Night'; + start.setHours(17,30,0,0); } } else { shift = 'Night'; - start.setDate(start.getDate()-1); + start.setDate(start.getDate() - 1); start.setHours(17,30,0,0); } @@ -62,27 +64,27 @@ function getShiftInfo(now) { } async function fetchCurrentWeather() { - const key = process.env.WEATHER_API_KEY, zip = process.env.ZIP_CODE; + const key = process.env.WEATHER_API_KEY; + const zip = process.env.ZIP_CODE; if (!key || !zip) return 'Unavailable'; try { - const { data } = await axios.get( - 'https://api.openweathermap.org/data/2.5/weather', - { params:{ zip:`${zip},us`, appid:key, units:'imperial' } } - ); + const { data } = await axios.get('https://api.openweathermap.org/data/2.5/weather', { + params: { zip: `${zip},us`, appid: key, units: 'imperial' } + }); const desc = data.weather[0].description.replace(/^\w/, c => c.toUpperCase()); const hi = Math.round(data.main.temp_max); const hum = data.main.humidity; return `${desc}. Hi of ${hi}, Humidity ${hum}%`; - } catch (e) { - console.error('Weather API error:', e.message); + } catch (err) { + console.error('Weather API error:', err.message); return 'Unavailable'; } } -// MariaDB pool +// MariaDB connection pool const pool = mysql.createPool({ host: process.env.DB_HOST, - port: parseInt(process.env.DB_PORT,10)||3306, + port: parseInt(process.env.DB_PORT,10) || 3306, user: process.env.DB_USER, password: process.env.DB_PASSWORD, database: process.env.DB_NAME, @@ -92,196 +94,228 @@ const pool = mysql.createPool({ connectTimeout: 10000 }); -// Ensure table exists -(async()=>{ - const sql=` - CREATE TABLE IF NOT EXISTS readings( - id INT AUTO_INCREMENT PRIMARY KEY, - location VARCHAR(20) NOT NULL, - stationDockDoor VARCHAR(10) NOT NULL, - timestamp DATETIME NOT NULL, - temperature DOUBLE, - humidity DOUBLE, - heatIndex DOUBLE - );`; - await pool.execute(sql); +// Ensure readings table exists +(async () => { + const createSQL = ` + CREATE TABLE IF NOT EXISTS readings ( + id INT AUTO_INCREMENT PRIMARY KEY, + location VARCHAR(20) NOT NULL, + stationDockDoor VARCHAR(10) NOT NULL, + timestamp DATETIME NOT NULL, + temperature DOUBLE, + humidity DOUBLE, + heatIndex DOUBLE + ); + `; + await pool.execute(createSQL); })(); -// Middleware & static (serve heatmap.html as index) +// Middleware & static files (serve heatmap.html at '/') app.use(bodyParser.json()); -const publicDir = path.join(__dirname,'public'); +const publicDir = path.join(__dirname, 'public'); app.use(express.static(publicDir, { index: 'heatmap.html' })); // SSE setup let clients = []; -app.get('/api/stream',(req,res)=>{ +app.get('/api/stream', (req, res) => { res.set({ - 'Content-Type':'text/event-stream', - 'Cache-Control':'no-cache', - Connection:'keep-alive' + 'Content-Type': 'text/event-stream', + 'Cache-Control': 'no-cache', + Connection: 'keep-alive' }); res.flushHeaders(); clients.push(res); - req.on('close',()=>{ clients=clients.filter(c=>c!==res) }); + req.on('close', () => { clients = clients.filter(c => c !== res); }); }); -function broadcast(evt,data){ - const msg = `event: ${evt}\ndata: ${JSON.stringify(data)}\n\n`; - clients.forEach(c=>c.write(msg)); +function broadcast(event, data) { + const msg = `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`; + clients.forEach(c => c.write(msg)); } -// ---- Dual dock-door endpoint ---- -app.post('/api/readings', async (req,res)=>{ +// Dual dock‐door readings endpoint +app.post('/api/readings', async (req, res) => { try { - const { inbound={}, outbound={} }=req.body; + const { inbound = {}, outbound = {} } = req.body; const { dockDoor: inD, temperature: inT, humidity: inH } = inbound; const { dockDoor: outD, temperature: outT, humidity: outH } = outbound; - if ([inD,inT,inH,outD,outT,outH].some(v=>v==null)) - return res.status(400).json({ error:'Missing fields' }); + if ([inD, inT, inH, outD, outT, outH].some(v => v == null)) { + return res.status(400).json({ error: 'Missing fields' }); + } - const hiIn = computeHeatIndex(inT,inH); - const hiOut = computeHeatIndex(outT,outH); + const hiIn = computeHeatIndex(inT, inH); + const hiOut = computeHeatIndex(outT, outH); const now = new Date(); const { shift, key, estNow } = getShiftInfo(now); - shiftCounters[key]=(shiftCounters[key]||0)+1; - const period = shiftCounters[key]; + shiftCounters[key] = (shiftCounters[key] || 0) + 1; + const period = shiftCounters[key]; + const sqlTs = formatDateEST(estNow); const shortTs = shortEST(estNow); - // insert - const ins = ` + // Insert readings + const insertSQL = ` INSERT INTO readings(location,stationDockDoor,timestamp,temperature,humidity,heatIndex) - VALUES(?,?,?,?,?,?)`; - await pool.execute(ins,['Inbound',String(inD),sqlTs,inT,inH,hiIn]); - await pool.execute(ins,['Outbound',String(outD),sqlTs,outT,outH,hiOut]); + VALUES(?,?,?,?,?,?) + `; + await pool.execute(insertSQL, ['Inbound', String(inD), sqlTs, inT, inH, hiIn]); + await pool.execute(insertSQL, ['Outbound', String(outD), sqlTs, outT, outH, hiOut]); - // SSE - broadcast('new-reading',{ location:'Inbound',stationDockDoor:String(inD),timestamp:shortTs,temperature:inT,humidity:inH,heatIndex:hiIn }); - broadcast('new-reading',{ location:'Outbound',stationDockDoor:String(outD),timestamp:shortTs,temperature:outT,humidity:outH,heatIndex:hiOut }); + // Broadcast SSE + broadcast('new-reading', { + location: 'Inbound', + stationDockDoor: String(inD), + timestamp: shortTs, + temperature: inT, + humidity: inH, + heatIndex: hiIn + }); + broadcast('new-reading', { + location: 'Outbound', + stationDockDoor: String(outD), + timestamp: shortTs, + temperature: outT, + humidity: outH, + heatIndex: hiOut + }); - // CSV - const y=estNow.getFullYear(), m=pad2(estNow.getMonth()+1), d=pad2(estNow.getDate()); - const dateKey=`${y}${m}${d}`; - const [rows] = await pool.execute( + // Upload CSV of today’s readings + const y = estNow.getFullYear(), m = pad2(estNow.getMonth()+1), d = pad2(estNow.getDate()); + const dateKey = `${y}${m}${d}`; + const [rows] = await pool.execute( `SELECT * FROM readings WHERE DATE(timestamp)=CURDATE() ORDER BY timestamp` ); - let csvUrl=null; - try{ csvUrl=await uploadTrendsCsv(dateKey,rows) }catch(e){console.error(e)} + let csvUrl = null; + try { csvUrl = await uploadTrendsCsv(dateKey, rows); } + catch (e) { console.error('CSV upload error:', e); } - // weather + // Fetch weather const weather = await fetchCurrentWeather(); - // build text + // Build Slack message text const text = - `*_${shift} shift Period ${period} dock/ trailer temperature checks for ${shortTs}_*\n`+ - `*_Current Weather Forecast for Baltimore: ${weather}_*\n\n`+ - `*_⬇️ Inbound Dock Door 🚛 :_* ${inD}\n`+ - `*_Temp:_* ${inT} °F 🌡️\n`+ - `*_Humidity:_* ${inH} % 💦\n`+ - `*_Heat Index:_* ${hiIn} °F 🥵\n\n`+ - `*_⬆️ Outbound Dock Door 🚛 :_* ${outD}\n`+ - `*_Temp:_* ${outT} °F 🌡️\n`+ - `*_Humidity:_* ${outH} % 💦\n`+ + `*_${shift} shift Period ${period} dock/ trailer temperature checks for ${shortTs}_*\n` + + `*_Current Weather Forecast for Baltimore: ${weather}_*\n\n` + + `*_⬇️ Inbound Dock Door 🚛 :_* ${inD}\n` + + `*_Temp:_* ${inT} °F 🌡️\n` + + `*_Humidity:_* ${inH} % 💦\n` + + `*_Heat Index:_* ${hiIn} °F 🥵\n\n` + + `*_⬆️ Outbound Dock Door 🚛 :_* ${outD}\n` + + `*_Temp:_* ${outT} °F 🌡️\n` + + `*_Humidity:_* ${outH} % 💦\n` + `*_Heat Index:_* ${hiOut} °F 🥵`; - const payload = { text }; + // Trigger Slack workflow via Inputs map + await axios.post( + process.env.SLACK_WEBHOOK_URL, + { inputs: { message: text } }, + { headers: { 'Content-Type': 'application/json' } } + ); - // send as url-encoded string - const body = `payload=${encodeURIComponent(JSON.stringify(payload))}`; - await axios.post(process.env.SLACK_WEBHOOK_URL, body, { - headers:{ 'Content-Type':'application/x-www-form-urlencoded' } - }); - - res.json({ success:true, shift, period, csvUrl }); - } catch(err) { - console.error('POST /api/readings error:',err); + res.json({ success: true, shift, period, csvUrl }); + } catch (err) { + console.error('POST /api/readings error:', err); res.status(500).json({ error: err.message }); } }); -// ---- Area/Mod endpoint ---- -app.post('/api/area-readings', async (req,res)=>{ +// Area/Mod station readings endpoint +app.post('/api/area-readings', async (req, res) => { try { const { area, stationCode, temperature: T, humidity: H } = req.body; - if (!area||!stationCode||T==null||H==null) - return res.status(400).json({ error:'Missing fields' }); + if (!area || !stationCode || T == null || H == null) { + return res.status(400).json({ error: 'Missing fields' }); + } - const hi = computeHeatIndex(T,H); + const hi = computeHeatIndex(T, H); const now = new Date(); const { shift, estNow } = getShiftInfo(now); + const sqlTs = formatDateEST(estNow); const shortTs = shortEST(estNow); - // insert - const ins = ` + // Insert reading + const insertSQL = ` INSERT INTO readings(location,stationDockDoor,timestamp,temperature,humidity,heatIndex) - VALUES(?,?,?,?,?,?)`; - await pool.execute(ins,[area,stationCode,sqlTs,T,H,hi]); + VALUES(?,?,?,?,?,?) + `; + await pool.execute(insertSQL, [area, stationCode, sqlTs, T, H, hi]); - // SSE - broadcast('new-area-reading',{ location:area,stationDockDoor:stationCode,timestamp:shortTs,temperature:T,humidity:H,heatIndex:hi }); - - // CSV - const y=estNow.getFullYear(), m=pad2(estNow.getMonth()+1), d=pad2(estNow.getDate()); - const dateKey=`${y}${m}${d}`; - const [rows] = await pool.execute( - `SELECT * FROM readings WHERE DATE(timestamp)=CURDATE() ORDER BY timestamp` - ); - let csvUrl=null; - try{ csvUrl=await uploadTrendsCsv(dateKey,rows) }catch(e){console.error(e)} - - // weather - const weather = await fetchCurrentWeather(); - - // build text - const text = - `*_${shift} shift ${area} temp check for ${shortTs}_*\n`+ - `*_Current Weather Forecast for Baltimore: ${weather}_*\n\n`+ - `*_${area.toUpperCase()} station:_* ${stationCode}\n`+ - `*_Temp:_* ${T} °F 🌡️\n`+ - `*_Humidity:_* ${H} % 💦\n`+ - `*_Heat Index:_* ${hi} °F 🥵`; - - const payload = { text }; - const body = `payload=${encodeURIComponent(JSON.stringify(payload))}`; - await axios.post(process.env.SLACK_WEBHOOK_URL, body, { - headers:{ 'Content-Type':'application/x-www-form-urlencoded' } + // Broadcast SSE + broadcast('new-area-reading', { + location: area, + stationDockDoor: stationCode, + timestamp: shortTs, + temperature: T, + humidity: H, + heatIndex: hi }); - res.json({ success:true, csvUrl }); - } catch(err) { - console.error('POST /api/area-readings error:',err); + // Upload CSV + const y = estNow.getFullYear(), m = pad2(estNow.getMonth()+1), d = pad2(estNow.getDate()); + const dateKey = `${y}${m}${d}`; + const [rows] = await pool.execute( + `SELECT * FROM readings WHERE DATE(timestamp)=CURDATE() ORDER BY timestamp` + ); + let csvUrl = null; + try { csvUrl = await uploadTrendsCsv(dateKey, rows); } + catch (e) { console.error('CSV upload error:', e); } + + // Fetch weather + const weather = await fetchCurrentWeather(); + + // Build Slack message text + const text = + `*_${shift} shift ${area} temp check for ${shortTs}_*\n` + + `*_Current Weather Forecast for Baltimore: ${weather}_*\n\n` + + `*_${area.toUpperCase()} station:_* ${stationCode}\n` + + `*_Temp:_* ${T} °F 🌡️\n` + + `*_Humidity:_* ${H} % 💦\n` + + `*_Heat Index:_* ${hi} °F 🥵`; + + // Trigger Slack workflow via Inputs map + await axios.post( + process.env.SLACK_WEBHOOK_URL, + { inputs: { message: text } }, + { headers: { 'Content-Type': 'application/json' } } + ); + + res.json({ success: true, csvUrl }); + } catch (err) { + console.error('POST /api/area-readings error:', err); res.status(500).json({ error: err.message }); } }); -// ---- GET all readings ---- -app.get('/api/readings', async (req,res)=>{ +// GET all readings +app.get('/api/readings', async (req, res) => { try { const [rows] = await pool.execute(`SELECT * FROM readings ORDER BY timestamp`); res.json(rows); - } catch(err) { - console.error('GET /api/readings error:',err); + } catch (err) { + console.error('GET /api/readings error:', err); res.status(500).json({ error: err.message }); } }); -// ---- CSV export ---- -app.get('/api/export', async (req,res)=>{ +// Export CSV +app.get('/api/export', async (req, res) => { try { const [rows] = await pool.execute(`SELECT * FROM readings ORDER BY timestamp`); - res.setHeader('Content-disposition','attachment; filename=readings.csv'); - res.set('Content-Type','text/csv'); + res.setHeader('Content-disposition', 'attachment; filename=readings.csv'); + res.set('Content-Type', 'text/csv'); res.write('id,location,stationDockDoor,timestamp,temperature,humidity,heatIndex\n'); - rows.forEach(r=>{ - const ts = r.timestamp instanceof Date ? formatDateEST(r.timestamp) : r.timestamp; + rows.forEach(r => { + const ts = (r.timestamp instanceof Date) ? formatDateEST(r.timestamp) : r.timestamp; res.write(`${r.id},${r.location},${r.stationDockDoor},${ts},${r.temperature},${r.humidity},${r.heatIndex}\n`); }); res.end(); - } catch(err) { - console.error('GET /api/export error:',err); + } catch (err) { + console.error('GET /api/export error:', err); res.status(500).send(err.message); } }); // Start server -app.listen(PORT, ()=>console.log(`Server running on http://localhost:${PORT}`)); +app.listen(PORT, () => { + console.log(`Server running on http://localhost:${PORT}`); +});