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 > 7 || (h === 7 && m >= 0)) {
|
||||||
if (h < 17 || (h === 17 && m < 30)) {
|
if (h < 17 || (h === 17 && m < 30)) {
|
||||||
shift = 'Day';
|
shift = 'Day'; start.setHours(7,0,0,0);
|
||||||
start.setHours(7,0,0,0);
|
|
||||||
} else {
|
} else {
|
||||||
shift = 'Night';
|
shift = 'Night'; start.setHours(17,30,0,0);
|
||||||
start.setHours(17,30,0,0);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
shift = 'Night';
|
shift = 'Night';
|
||||||
@ -64,13 +62,13 @@ function getShiftInfo(now) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function fetchCurrentWeather() {
|
async function fetchCurrentWeather() {
|
||||||
const key = process.env.WEATHER_API_KEY;
|
const key = process.env.WEATHER_API_KEY, zip = process.env.ZIP_CODE;
|
||||||
const zip = process.env.ZIP_CODE;
|
|
||||||
if (!key || !zip) return 'Unavailable';
|
if (!key || !zip) return 'Unavailable';
|
||||||
try {
|
try {
|
||||||
const { data } = await axios.get('https://api.openweathermap.org/data/2.5/weather', {
|
const { data } = await axios.get(
|
||||||
params: { zip: `${zip},us`, appid: key, units: 'imperial' }
|
'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 desc = data.weather[0].description.replace(/^\w/,c=>c.toUpperCase());
|
||||||
const hi = Math.round(data.main.temp_max);
|
const hi = Math.round(data.main.temp_max);
|
||||||
const hum = data.main.humidity;
|
const hum = data.main.humidity;
|
||||||
@ -81,7 +79,7 @@ async function fetchCurrentWeather() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MariaDB connection pool
|
// MariaDB pool
|
||||||
const pool = mysql.createPool({
|
const pool = mysql.createPool({
|
||||||
host: process.env.DB_HOST,
|
host: process.env.DB_HOST,
|
||||||
port: parseInt(process.env.DB_PORT,10) || 3306,
|
port: parseInt(process.env.DB_PORT,10) || 3306,
|
||||||
@ -96,7 +94,7 @@ const pool = mysql.createPool({
|
|||||||
|
|
||||||
// Ensure readings table exists
|
// Ensure readings table exists
|
||||||
(async () => {
|
(async () => {
|
||||||
const createSQL = `
|
const sql = `
|
||||||
CREATE TABLE IF NOT EXISTS readings (
|
CREATE TABLE IF NOT EXISTS readings (
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
location VARCHAR(20) NOT NULL,
|
location VARCHAR(20) NOT NULL,
|
||||||
@ -105,12 +103,11 @@ const pool = mysql.createPool({
|
|||||||
temperature DOUBLE,
|
temperature DOUBLE,
|
||||||
humidity DOUBLE,
|
humidity DOUBLE,
|
||||||
heatIndex DOUBLE
|
heatIndex DOUBLE
|
||||||
);
|
);`;
|
||||||
`;
|
await pool.execute(sql);
|
||||||
await pool.execute(createSQL);
|
|
||||||
})();
|
})();
|
||||||
|
|
||||||
// Middleware & static files (serve heatmap.html at '/')
|
// Middleware & static (serve heatmap.html at '/')
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
const publicDir = path.join(__dirname, 'public');
|
const publicDir = path.join(__dirname, 'public');
|
||||||
app.use(express.static(publicDir, { index: 'heatmap.html' }));
|
app.use(express.static(publicDir, { index: 'heatmap.html' }));
|
||||||
@ -132,7 +129,7 @@ function broadcast(event, data) {
|
|||||||
clients.forEach(c => c.write(msg));
|
clients.forEach(c => c.write(msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dual dock‐door readings endpoint
|
// === Dual dock-door readings endpoint ===
|
||||||
app.post('/api/readings', async (req, res) => {
|
app.post('/api/readings', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { inbound={}, outbound={} } = req.body;
|
const { inbound={}, outbound={} } = req.body;
|
||||||
@ -152,15 +149,14 @@ app.post('/api/readings', async (req, res) => {
|
|||||||
const sqlTs = formatDateEST(estNow);
|
const sqlTs = formatDateEST(estNow);
|
||||||
const shortTs = shortEST(estNow);
|
const shortTs = shortEST(estNow);
|
||||||
|
|
||||||
// Insert readings
|
// Insert inbound + outbound
|
||||||
const insertSQL = `
|
const ins = `
|
||||||
INSERT INTO readings(location,stationDockDoor,timestamp,temperature,humidity,heatIndex)
|
INSERT INTO readings(location,stationDockDoor,timestamp,temperature,humidity,heatIndex)
|
||||||
VALUES(?,?,?,?,?,?)
|
VALUES(?,?,?,?,?,?)`;
|
||||||
`;
|
await pool.execute(ins, ['Inbound', String(inD), sqlTs, inT, inH, hiIn]);
|
||||||
await pool.execute(insertSQL, ['Inbound', String(inD), sqlTs, inT, inH, hiIn]);
|
await pool.execute(ins, ['Outbound', String(outD), sqlTs, outT, outH, hiOut]);
|
||||||
await pool.execute(insertSQL, ['Outbound', String(outD), sqlTs, outT, outH, hiOut]);
|
|
||||||
|
|
||||||
// Broadcast SSE
|
// SSE broadcast
|
||||||
broadcast('new-reading', {
|
broadcast('new-reading', {
|
||||||
location: 'Inbound',
|
location: 'Inbound',
|
||||||
stationDockDoor: String(inD),
|
stationDockDoor: String(inD),
|
||||||
@ -178,7 +174,7 @@ app.post('/api/readings', async (req, res) => {
|
|||||||
heatIndex: hiOut
|
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 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(
|
||||||
@ -204,10 +200,10 @@ app.post('/api/readings', async (req, res) => {
|
|||||||
`*_Humidity:_* ${outH} % 💦\n` +
|
`*_Humidity:_* ${outH} % 💦\n` +
|
||||||
`*_Heat Index:_* ${hiOut} °F 🥵`;
|
`*_Heat Index:_* ${hiOut} °F 🥵`;
|
||||||
|
|
||||||
// Trigger Slack workflow via Inputs map
|
// Send JSON with top-level "text" field
|
||||||
await axios.post(
|
await axios.post(
|
||||||
process.env.SLACK_WEBHOOK_URL,
|
process.env.SLACK_WEBHOOK_URL,
|
||||||
{ inputs: { message: text } },
|
{ text, shift, period, timestamp: shortTs },
|
||||||
{ headers: { 'Content-Type': 'application/json' } }
|
{ 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) => {
|
app.post('/api/area-readings', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { area, stationCode, temperature: T, humidity: H } = req.body;
|
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 hi = computeHeatIndex(T, H);
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const { shift, estNow } = getShiftInfo(now);
|
const { shift, estNow } = getShiftInfo(now);
|
||||||
|
|
||||||
const sqlTs = formatDateEST(estNow);
|
const sqlTs = formatDateEST(estNow);
|
||||||
const shortTs = shortEST(estNow);
|
const shortTs = shortEST(estNow);
|
||||||
|
|
||||||
// Insert reading
|
// Insert
|
||||||
const insertSQL = `
|
const ins = `
|
||||||
INSERT INTO readings(location,stationDockDoor,timestamp,temperature,humidity,heatIndex)
|
INSERT INTO readings(location,stationDockDoor,timestamp,temperature,humidity,heatIndex)
|
||||||
VALUES(?,?,?,?,?,?)
|
VALUES(?,?,?,?,?,?)`;
|
||||||
`;
|
await pool.execute(ins, [area, stationCode, sqlTs, T, H, hi]);
|
||||||
await pool.execute(insertSQL, [area, stationCode, sqlTs, T, H, hi]);
|
|
||||||
|
|
||||||
// Broadcast SSE
|
// SSE
|
||||||
broadcast('new-area-reading', {
|
broadcast('new-area-reading', {
|
||||||
location: area,
|
location: area,
|
||||||
stationDockDoor: stationCode,
|
stationDockDoor: stationCode,
|
||||||
@ -250,7 +244,7 @@ app.post('/api/area-readings', async (req, res) => {
|
|||||||
heatIndex: hi
|
heatIndex: hi
|
||||||
});
|
});
|
||||||
|
|
||||||
// Upload CSV
|
// 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(
|
||||||
@ -272,10 +266,10 @@ app.post('/api/area-readings', async (req, res) => {
|
|||||||
`*_Humidity:_* ${H} % 💦\n` +
|
`*_Humidity:_* ${H} % 💦\n` +
|
||||||
`*_Heat Index:_* ${hi} °F 🥵`;
|
`*_Heat Index:_* ${hi} °F 🥵`;
|
||||||
|
|
||||||
// Trigger Slack workflow via Inputs map
|
// Send JSON with top-level "text" field
|
||||||
await axios.post(
|
await axios.post(
|
||||||
process.env.SLACK_WEBHOOK_URL,
|
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' } }
|
{ headers: { 'Content-Type': 'application/json' } }
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -305,7 +299,7 @@ app.get('/api/export', async (req, res) => {
|
|||||||
res.set('Content-Type', 'text/csv');
|
res.set('Content-Type', 'text/csv');
|
||||||
res.write('id,location,stationDockDoor,timestamp,temperature,humidity,heatIndex\n');
|
res.write('id,location,stationDockDoor,timestamp,temperature,humidity,heatIndex\n');
|
||||||
rows.forEach(r => {
|
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.write(`${r.id},${r.location},${r.stationDockDoor},${ts},${r.temperature},${r.humidity},${r.heatIndex}\n`);
|
||||||
});
|
});
|
||||||
res.end();
|
res.end();
|
||||||
@ -319,3 +313,4 @@ 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}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user