Update server.js
updates
This commit is contained in:
parent
997f7c34e2
commit
5f029f492a
71
server.js
71
server.js
@ -47,11 +47,9 @@ 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';
|
||||
@ -64,13 +62,13 @@ function getShiftInfo(now) {
|
||||
}
|
||||
|
||||
async function fetchCurrentWeather() {
|
||||
const key = process.env.WEATHER_API_KEY;
|
||||
const zip = process.env.ZIP_CODE;
|
||||
const key = process.env.WEATHER_API_KEY, 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;
|
||||
@ -81,7 +79,7 @@ async function fetchCurrentWeather() {
|
||||
}
|
||||
}
|
||||
|
||||
// MariaDB connection pool
|
||||
// MariaDB pool
|
||||
const pool = mysql.createPool({
|
||||
host: process.env.DB_HOST,
|
||||
port: parseInt(process.env.DB_PORT,10) || 3306,
|
||||
@ -96,7 +94,7 @@ const pool = mysql.createPool({
|
||||
|
||||
// Ensure readings table exists
|
||||
(async () => {
|
||||
const createSQL = `
|
||||
const sql = `
|
||||
CREATE TABLE IF NOT EXISTS readings (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
location VARCHAR(20) NOT NULL,
|
||||
@ -105,12 +103,11 @@ const pool = mysql.createPool({
|
||||
temperature DOUBLE,
|
||||
humidity DOUBLE,
|
||||
heatIndex DOUBLE
|
||||
);
|
||||
`;
|
||||
await pool.execute(createSQL);
|
||||
);`;
|
||||
await pool.execute(sql);
|
||||
})();
|
||||
|
||||
// Middleware & static files (serve heatmap.html at '/')
|
||||
// Middleware & static (serve heatmap.html at '/')
|
||||
app.use(bodyParser.json());
|
||||
const publicDir = path.join(__dirname, 'public');
|
||||
app.use(express.static(publicDir, { index: 'heatmap.html' }));
|
||||
@ -132,7 +129,7 @@ function broadcast(event, data) {
|
||||
clients.forEach(c => c.write(msg));
|
||||
}
|
||||
|
||||
// Dual dock‐door readings endpoint
|
||||
// === Dual dock-door readings endpoint ===
|
||||
app.post('/api/readings', async (req, res) => {
|
||||
try {
|
||||
const { inbound={}, outbound={} } = req.body;
|
||||
@ -152,15 +149,14 @@ app.post('/api/readings', async (req, res) => {
|
||||
const sqlTs = formatDateEST(estNow);
|
||||
const shortTs = shortEST(estNow);
|
||||
|
||||
// Insert readings
|
||||
const insertSQL = `
|
||||
// Insert inbound + outbound
|
||||
const ins = `
|
||||
INSERT INTO readings(location,stationDockDoor,timestamp,temperature,humidity,heatIndex)
|
||||
VALUES(?,?,?,?,?,?)
|
||||
`;
|
||||
await pool.execute(insertSQL, ['Inbound', String(inD), sqlTs, inT, inH, hiIn]);
|
||||
await pool.execute(insertSQL, ['Outbound', String(outD), sqlTs, outT, outH, hiOut]);
|
||||
VALUES(?,?,?,?,?,?)`;
|
||||
await pool.execute(ins, ['Inbound', String(inD), sqlTs, inT, inH, hiIn]);
|
||||
await pool.execute(ins, ['Outbound', String(outD), sqlTs, outT, outH, hiOut]);
|
||||
|
||||
// Broadcast SSE
|
||||
// SSE broadcast
|
||||
broadcast('new-reading', {
|
||||
location: 'Inbound',
|
||||
stationDockDoor: String(inD),
|
||||
@ -178,7 +174,7 @@ app.post('/api/readings', async (req, res) => {
|
||||
heatIndex: hiOut
|
||||
});
|
||||
|
||||
// Upload CSV of today’s readings
|
||||
// Generate/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(
|
||||
@ -204,10 +200,10 @@ app.post('/api/readings', async (req, res) => {
|
||||
`*_Humidity:_* ${outH} % 💦\n` +
|
||||
`*_Heat Index:_* ${hiOut} °F 🥵`;
|
||||
|
||||
// Trigger Slack workflow via Inputs map
|
||||
// Send JSON with top-level "text" field
|
||||
await axios.post(
|
||||
process.env.SLACK_WEBHOOK_URL,
|
||||
{ inputs: { message: text } },
|
||||
{ text, shift, period, timestamp: shortTs },
|
||||
{ headers: { 'Content-Type': 'application/json' } }
|
||||
);
|
||||
|
||||
@ -218,7 +214,7 @@ app.post('/api/readings', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Area/Mod station readings endpoint
|
||||
// === Area/mod readings endpoint ===
|
||||
app.post('/api/area-readings', async (req, res) => {
|
||||
try {
|
||||
const { area, stationCode, temperature: T, humidity: H } = req.body;
|
||||
@ -229,18 +225,16 @@ app.post('/api/area-readings', async (req, res) => {
|
||||
const hi = computeHeatIndex(T, H);
|
||||
const now = new Date();
|
||||
const { shift, estNow } = getShiftInfo(now);
|
||||
|
||||
const sqlTs = formatDateEST(estNow);
|
||||
const shortTs = shortEST(estNow);
|
||||
|
||||
// Insert reading
|
||||
const insertSQL = `
|
||||
// Insert
|
||||
const ins = `
|
||||
INSERT INTO readings(location,stationDockDoor,timestamp,temperature,humidity,heatIndex)
|
||||
VALUES(?,?,?,?,?,?)
|
||||
`;
|
||||
await pool.execute(insertSQL, [area, stationCode, sqlTs, T, H, hi]);
|
||||
VALUES(?,?,?,?,?,?)`;
|
||||
await pool.execute(ins, [area, stationCode, sqlTs, T, H, hi]);
|
||||
|
||||
// Broadcast SSE
|
||||
// SSE
|
||||
broadcast('new-area-reading', {
|
||||
location: area,
|
||||
stationDockDoor: stationCode,
|
||||
@ -250,7 +244,7 @@ app.post('/api/area-readings', async (req, res) => {
|
||||
heatIndex: hi
|
||||
});
|
||||
|
||||
// Upload CSV
|
||||
// CSV
|
||||
const y = estNow.getFullYear(), m = pad2(estNow.getMonth()+1), d = pad2(estNow.getDate());
|
||||
const dateKey = `${y}${m}${d}`;
|
||||
const [rows] = await pool.execute(
|
||||
@ -272,10 +266,10 @@ app.post('/api/area-readings', async (req, res) => {
|
||||
`*_Humidity:_* ${H} % 💦\n` +
|
||||
`*_Heat Index:_* ${hi} °F 🥵`;
|
||||
|
||||
// Trigger Slack workflow via Inputs map
|
||||
// Send JSON with top-level "text" field
|
||||
await axios.post(
|
||||
process.env.SLACK_WEBHOOK_URL,
|
||||
{ inputs: { message: text } },
|
||||
{ text, location: area, station_dock_door: stationCode, temperature: T, humidity: H, heat_index: hi },
|
||||
{ headers: { 'Content-Type': 'application/json' } }
|
||||
);
|
||||
|
||||
@ -305,7 +299,7 @@ app.get('/api/export', async (req, res) => {
|
||||
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;
|
||||
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();
|
||||
@ -319,3 +313,4 @@ app.get('/api/export', async (req, res) => {
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Server running on http://localhost:${PORT}`);
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user