65 lines
1.2 KiB
JavaScript
65 lines
1.2 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const PMEntry = require("../models/PMEntry");
|
|
const Status = require("../models/Status");
|
|
|
|
/* GET home page. */
|
|
router.get('/', function(req, res, next) {
|
|
res.render('index', { title: 'PM2.5 & PM10' });
|
|
});
|
|
|
|
|
|
|
|
router.get('/data/:timestamp', async (req,res) => {
|
|
let date = new Date(req.params.timestamp);
|
|
|
|
try {
|
|
let entries = await PMEntry.find({ updatedAt: {$gt: req.params.timestamp} });
|
|
res.json(entries);
|
|
} catch( e )
|
|
{
|
|
console.error(e);
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
router.post("/push", async(req, res) => {
|
|
console.log(req.body);
|
|
res.status(200);
|
|
res.send("ok");
|
|
res.end();
|
|
|
|
let entry = new PMEntry();
|
|
entry.pm10 = req.body.pm10;
|
|
entry.pm25 = req.body.pm25;
|
|
|
|
if(entry.pm10 > 500 || entry.pm25 > 500 )
|
|
return;
|
|
|
|
await entry.save();
|
|
|
|
});
|
|
|
|
|
|
router.get("/status", async(req,res) => {
|
|
let status = await Status.findOne({}).sort({createdAt: -1});
|
|
res.json(status);
|
|
});
|
|
|
|
router.post("/status", async(req,res) => {
|
|
console.log(req.body);
|
|
|
|
let status = new Status();
|
|
status.error = req.body.error;
|
|
status.ip = req.body.ip;
|
|
status.sleeping = req.body.sleeping;
|
|
|
|
await status.save();
|
|
res.status(200);
|
|
res.end();
|
|
});
|
|
|
|
module.exports = router;
|