This commit is contained in:
104
server-node/index.ts
Normal file
104
server-node/index.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
import dotenv from 'dotenv'
|
||||
import express from 'express'
|
||||
import type { Request, Response } from 'express'
|
||||
import mongo from 'mongodb'
|
||||
import path from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
dotenv.config()
|
||||
|
||||
console.log('Starting with parameters:')
|
||||
console.table(
|
||||
['MONGO_SERVER', 'MONGO_DB'].map((parameter) => {
|
||||
return {
|
||||
parameter,
|
||||
value: process.env[parameter]
|
||||
}
|
||||
})
|
||||
)
|
||||
const app = express()
|
||||
|
||||
app.listen(3000, () => {
|
||||
console.log('Listening on ' + 3000)
|
||||
})
|
||||
|
||||
let db: mongo.Db
|
||||
|
||||
async function connectToMongo() {
|
||||
try {
|
||||
const mongoUrl = `mongodb://${process.env.MONGO_SERVER}:27017`
|
||||
const client = await mongo.MongoClient.connect(mongoUrl)
|
||||
db = client.db(process.env.MONGO_DB)
|
||||
console.log('Connected succesfully to server')
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
setTimeout(connectToMongo, 5000)
|
||||
}
|
||||
}
|
||||
|
||||
connectToMongo()
|
||||
|
||||
function objectIdFromDate(date: Date) {
|
||||
return new mongo.ObjectId(Math.floor(date.getTime() / 1000).toString(16) + '0000000000000000')
|
||||
}
|
||||
|
||||
app.use(function (req: Request, res: Response, next) {
|
||||
res.header('Access-Control-Allow-Origin', '*')
|
||||
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
|
||||
next()
|
||||
})
|
||||
|
||||
app.get('/type/:type/startDate/:startDate/endDate/:endDate/sample/:sample', async (req: Request, res: Response) => {
|
||||
const startDate = new Date(parseInt(req.params.startDate) * 1000)
|
||||
const endDate = new Date(parseInt(req.params.endDate) * 1000)
|
||||
const sample = parseInt(req.params.sample)
|
||||
const type = req.params.type
|
||||
const agg = [
|
||||
{
|
||||
$match: {
|
||||
$and: [
|
||||
{ _id: { $gt: objectIdFromDate(startDate), $lt: objectIdFromDate(endDate) } },
|
||||
{ type },
|
||||
{ value: { $ne: NaN } }
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: {
|
||||
$toDate: {
|
||||
$subtract: [
|
||||
{ $toLong: { $toDate: '$_id' } },
|
||||
{ $mod: [{ $toLong: { $toDate: '$_id' } }, 1000 * 60 * sample] }
|
||||
]
|
||||
}
|
||||
},
|
||||
value: { $avg: '$value' },
|
||||
date: { $min: { $toDate: '$_id' } }
|
||||
}
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 0,
|
||||
value: { $round: ['$value', 1] },
|
||||
date: { $toLong: '$date' }
|
||||
}
|
||||
},
|
||||
{ $sort: { date: 1 } }
|
||||
]
|
||||
try {
|
||||
const docs = await db.collection('dht22').aggregate(agg).toArray()
|
||||
res.json(docs)
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
})
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url)
|
||||
const __dirname = path.dirname(__filename)
|
||||
|
||||
app.use(express.static(path.join(__dirname, '../dist')))
|
||||
|
||||
app.get('/*', function (req: Request, res: Response) {
|
||||
res.sendFile(path.join(__dirname, '../dist/index.html'))
|
||||
})
|
||||
Reference in New Issue
Block a user