Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fill level change api #8

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import express from 'express';
import express, { response } from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import mongoose from 'mongoose';
Expand All @@ -13,13 +13,17 @@ import projectRouter from './routes/project';
import trashbinRouter from './routes/trashbin';
import sensorRouter from './routes/sensor';
import trashCollectorRouter from './routes/trashCollector';
import { updateFillLevelChangesCore } from './controllers/trashbin';
import noiseRouter from './routes/noise';
import historyRouter from './routes/history';
import { initializeMQTT } from './mqttClient';

const mqtt = require('mqtt');


const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const PORT = process.env.PORT || 5001;

dotenv.config();
Expand Down Expand Up @@ -53,7 +57,7 @@ initializeMQTT(globalEventEmitter);

// Middleware
app.use(bodyParser.json());

app.use(express.json());
// Update CORS policy to whitelist every client domain
app.use((req: any, res: any, next: any) => {
res.setHeader('Access-Control-Allow-Origin', '*');
Expand All @@ -77,8 +81,13 @@ app.use('/api/v1/history', historyRouter);
// Connect to MongoDB
mongoose
.connect(process.env.MONGO_DB_URL || '', {})
.then(() => console.log('MongoDB connected'))
.catch((err) => console.log(err));
.then(async () => {
console.log('MongoDB connected');
})
.catch((err) => {
console.error('MongoDB connection error:', err);
});


// Define a route
app.get('/', (req, res) => {
Expand Down
5 changes: 3 additions & 2 deletions src/controllers/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,9 @@ export const updateProject = async (req: any, res: any) => {
if (updatedProject.users?.length > 0) {
updatedProject.users.forEach(async (userId: any) => {
const user: any = await User.findById(userId);
if (!user.projects.includes(updatedProject._id)) {
user.projects.push(updatedProject._id);
if (!user?.projects.includes(updatedProject._id ) && user) {
console.log("users",user)
user?.projects.push(updatedProject._id);
await user.save();
}
});
Expand Down
95 changes: 93 additions & 2 deletions src/controllers/trashbin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { generateUniqueTrashbinIdentifier } from '../service/trashbin';
import { Project } from '../models/project';
import { Trashbin } from '../models/trashbin';
import { History } from '../models/history';
import { Types } from 'mongoose';

import mongoose from 'mongoose';

export const createTrashItem = async (req: any, res: any, next: any) => {
try {
console.log('Inside Trash Can');
const projectId = req.body.project;
const trashcanName = req.body.name;
const longitude = req.body.longitude;
Expand Down Expand Up @@ -39,7 +41,7 @@ export const createTrashItem = async (req: any, res: any, next: any) => {

// Fetch location string from the longitude and latitude

console.log('Coming inside line # 41');


const trashbin = new Trashbin({
name: trashcanName,
Expand Down Expand Up @@ -171,20 +173,32 @@ export const getAllTrashItems = async (req: any, res: any, next: any) => {
const projectQuery = req.query.project;
let trashbins;
let count;
console.log("Received project query:", projectQuery);
if(!projectQuery){
return res.status(400).json({ message: "Project query parameter is required" });
}

if (projectQuery) {
// Check if the projectQuery is a valid ObjectId
if (mongoose.Types.ObjectId.isValid(projectQuery)) {
// console.log("Querying trashbins by ObjectId:", projectQuery);
trashbins = await Trashbin.find({ project: projectQuery })
.populate('assignee')
.populate('project');
} else {
//console.log("Querying trashbins by project identifier:", projectQuery);
// If not a valid ObjectId, assume it's an identifier
const project = await Project.findOne({ identifier: projectQuery });
if (!project) {
console.error("Project not found for identifier:", projectQuery);
return res.status(404).json({ message: "Project not found" });
}
if (project) {
trashbins = await Trashbin.find({ project: project._id })
.populate('assignee')
.populate('project');
// console.log("Fetched trashbins:", trashbins);

} else {
return res.status(404).json({ message: 'Project not found' });
}
Expand Down Expand Up @@ -360,3 +374,80 @@ export const addMultipleTrashItems = async (req: any, res: any, next: any) => {
res.status(500).json({ message: error.message });
}
};

export const updateFillLevelChangesCore = async (req: any, res: any): Promise<void> => {
try {
const { hours } = req.body;
// Validate the `hours` input
if (hours !== undefined && (typeof hours !== 'number' || hours < 0)) {
return res.status(400).json({ error: '`hours` must be a positive number or null.' });
}

const now: Date = new Date();
const cutoffDate: Date = hours
? new Date(now.getTime() - hours * 60 * 60 * 1000)
: new Date(0); // Default to epoch if hours is null
// Fetch all trashbins
const trashbins = await Trashbin.find();
if (!trashbins.length) {
console.warn('No trashbins found.');
return res.status(404).json({ message: 'No trashbins found.' });
}

// Iterate over each trashbin
for (const trashbin of trashbins) {
let totalFillLevelChange = 0;

if (!trashbin.sensors || trashbin.sensors.length === 0) {
continue;
}

// Process each sensor for the current trashbin
for (const sensorId of trashbin.sensors) {
// Query to find all history, converting string dates dynamically
const histories = await History.aggregate([
{
$match: {
sensor: new Types.ObjectId(sensorId),
measureType: 'fill_level',
$expr: {
$and: [
{ $gte: [{ $toDate: '$createdAt' }, cutoffDate] },
{ $lte: [{ $toDate: '$createdAt' }, now] },
],
},
},
},
{ $sort: { createdAt: 1 } }, // Sort by createdAt ascending
]);

if (histories.length >= 1) {
const oldestMeasurement = histories[0].measurement; // First entry
const newestMeasurement = histories[histories.length - 1].measurement; // Last entry
const fillLevelChange = newestMeasurement - oldestMeasurement;

totalFillLevelChange += fillLevelChange;
} else {
console.warn(`No valid history data found for Sensor: ${sensorId}`);
}
}

// Update trashbin's total fill-level change
if (totalFillLevelChange !== 0) {
await Trashbin.findByIdAndUpdate(trashbin._id, { fillLevelChange: totalFillLevelChange });
} else {
console.log(`No changes to update for Trashbin: ${trashbin._id}`);
}
}

res.status(200).json({ message: 'Fill-level changes updated successfully for all trashbins.' });
} catch (error) {
console.error('Error updating fill-level changes:', error);
res.status(500).json({ error: 'An error occurred while updating fill-level changes.' });
}
};





3 changes: 3 additions & 0 deletions src/routes/trashbin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getTrashItemById,
createTrashItem,
updateTrashItem,
updateFillLevelChangesCore,
addMultipleTrashItems,
} from '../controllers/trashbin';
import { authenticateToken } from '../middleware/authenticate';
Expand All @@ -17,6 +18,8 @@ router.get('/', authenticateToken, getAllTrashItems);

// Create a new trash item
router.post('/', authenticateToken, createTrashItem);
//update fill level change
router.put('/updateFillLevelChanges', updateFillLevelChangesCore);

router.patch('/:id', authenticateToken, updateTrashItem);

Expand Down
Loading