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

Tu 482 #14

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
133 changes: 126 additions & 7 deletions src/controllers/messages.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { UUIDV4 } from 'sequelize';
import { messagesDB } from '../models/db.index';
import { Message } from '../models/messages.model';
import { Message, Measurement } from '../models/messages.model';
import { RequestHandler } from 'express';

export const getMessages: RequestHandler = async (req, res) => {
try {
const messages = await messagesDB.findAll().then((messages) => messages.map((message) => {
const messages = await messagesDB.findAll({ where: { type: 'MESSAGE' } }).then((messages) => messages.map((message) => {
const jsonMessage:Message = message.toJSON();
return jsonMessage;
})) as Message[];
Expand All @@ -15,19 +15,52 @@ export const getMessages: RequestHandler = async (req, res) => {
}
};

export const getMeasurements: RequestHandler = async (req, res) => {
try {
const measurements = await messagesDB.findAll({ where: { type: 'MEASUREMENT' } }).then((measurements) => measurements.map((measurements) => {
const jsonMeasurement:Measurement = measurements.toJSON();
return jsonMeasurement;
})) as Measurement[];
res.status(200).json({ measurements });
} catch (e) {
res.status(500).json({ message: e });
}
};

export const getLastMessages: RequestHandler = async (req, res) => {
try {
const numMsgs = (req.params.numMsgs as unknown as number) || 10; //cast to number, or default to 10
const messages = await messagesDB.findAll({
where: { type: 'MESSAGE' },
limit: numMsgs,
order: [['time', 'DESC']],
});
}).then((messages) => messages.map((messages) => {
const jsonMessages:Message = messages.toJSON();
return jsonMessages;
})) as Message[];
res.status(200).json(messages);
} catch (e) {
res.status(500).json({ message: e });
}
};

export const getLastMeasurements: RequestHandler = async (req, res) => {
try {
const numMsgs = (req.params.numMsgs as unknown as number) || 10; //cast to number, or default to 10
const measurements = await messagesDB.findAll({
where: { type: 'MEASUREMENT' },
limit: numMsgs,
order: [['time', 'DESC']],
}).then((measurements) => measurements.map((measurements) => {
const jsonMeasurements:Measurement = measurements.toJSON();
return jsonMeasurements;
})) as Measurement[];
res.status(200).json(measurements);
} catch (e) {
res.status(500).json({ message: e });
}
};

export const getMessage: RequestHandler = async (req, res) => {
try {
const messageID = req.params.id;
Expand All @@ -36,7 +69,7 @@ export const getMessage: RequestHandler = async (req, res) => {
return;
}

const message = await messagesDB.findOne({ where: { id: messageID } }).then((message) => {
const message = await messagesDB.findOne({ where: { id: messageID, type: 'MESSAGE' } }).then((message) => {
const jsonMachine:Message|undefined = message?.toJSON();
return jsonMachine;
});
Expand All @@ -51,6 +84,29 @@ export const getMessage: RequestHandler = async (req, res) => {
}
};

export const getMeasurementsByID: RequestHandler = async (req, res) => {
try {
const measurementID = req.params.id;
if (!measurementID) {
res.status(400).json({ message: 'Missing required fields!' });
return;
}

const measurements = await messagesDB.findAll({ where: { id: measurementID, type: 'MEASUREMENT' } }).then((measurements) => measurements.map((measurements) => {
const jsonMeasurement:Measurement = measurements.toJSON();
return jsonMeasurement;
})) as Measurement[];

if (!measurements) {
res.status(400).json({ message: 'Measurements not found!' });
}

res.status(200).json(measurements);
} catch (e) {
res.status(500).json({ message: e });
}
};

interface CreateMessageBody {
message:Partial<Message>
}
Expand All @@ -64,21 +120,42 @@ export const createMessage: RequestHandler = async (req, res) => {
id: createMessageBody.message.id ? UUIDV4() : null,
});

const message = await messagesDB.findOne({ where: { name: createMessageBody.message.id } }).then((message) => message?.toJSON()) as Message;
const message = await messagesDB.findOne({ where: { name: createMessageBody.message.id, type: 'MESSAGE' } }).then((message) => message?.toJSON()) as Message;

res.status(201).json({ message });
} catch (e) {
res.status(500).json({ message: e });
}
};

interface CreateMeasurementBody {
measurement:Partial<Measurement>
}

export const createMeasurement: RequestHandler = async (req, res) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should meet to discuss message/measurement creation (its a special topic. Lets omit of any message creation routes for this PR

try {
const createMeasurementBody:CreateMeasurementBody = req.body;

await messagesDB.create({
...createMeasurementBody.measurement,
id: createMeasurementBody.measurement.id ? UUIDV4() : null,
});

const measurement = await messagesDB.findOne({ where: { name: createMeasurementBody.measurement.id, type: 'MEASUREMENT' } }).then((measurement) => measurement?.toJSON()) as Measurement;

res.status(201).json({ measurement });
} catch (e) {
res.status(500).json({ message: e });
}
};

export const findMessagesByNodeID: RequestHandler = async (req, res) => {
try {
const nodeID = req.params.nodeID;
if (!nodeID) {
res.status(400).json({ message: 'Missing required fields!' });
}
const message = await messagesDB.findAll({ where: { nodeID: nodeID } }).then((messages) => messages.map((message) => {
const message = await messagesDB.findAll({ where: { nodeID: nodeID, type: 'MESSAGE' } }).then((messages) => messages.map((message) => {
const jsonMessage:Message = message.toJSON();
return jsonMessage;
})) as Message[];
Expand All @@ -93,13 +170,34 @@ export const findMessagesByNodeID: RequestHandler = async (req, res) => {
}
};

export const findMeasurementsByNodeID: RequestHandler = async (req, res) => {
try {
const nodeID = req.params.nodeID;
if (!nodeID) {
res.status(400).json({ message: 'Missing required fields!' });
}
const measurement = await messagesDB.findAll({ where: { nodeID: nodeID, type: 'MEASUREMENT' } }).then((measurement) => measurement.map((measurement) => {
const jsonMeasurement:Measurement = measurement.toJSON();
return jsonMeasurement;
})) as Measurement[];

if (!measurement){
res.status(400).json({ message: 'Measurement not found' });
return;
}
res.status(200).json(measurement);
} catch (e) {
res.status(500).json({ message: e });
}
};

export const findMessagesByPlotID: RequestHandler = async (req, res) => {
try {
const plotID = req.params.plotID;
if (!plotID) {
res.status(400).json({ message: 'Missing required fields!' });
}
const message = await messagesDB.findAll({ where: { plotID: plotID } }).then((messages) => messages.map((message) => {
const message = await messagesDB.findAll({ where: { plotID: plotID, type: 'MESSAGE' } }).then((messages) => messages.map((message) => {
const jsonMessage:Message = message.toJSON();
return jsonMessage;
})) as Message[];
Expand All @@ -112,4 +210,25 @@ export const findMessagesByPlotID: RequestHandler = async (req, res) => {
} catch (e) {
res.status(500).json({ message: e });
}
};

export const findMeasurementsByPlotID: RequestHandler = async (req, res) => {
try {
const plotID = req.params.plotID;
if (!plotID) {
res.status(400).json({ message: 'Missing required fields!' });
}
const measurement = await messagesDB.findAll({ where: { plotID: plotID, type: 'MEASUREMENT' } }).then((measurement) => measurement.map((measurement) => {
const jsonMeasurement:Measurement = measurement.toJSON();
return jsonMeasurement;
})) as Measurement[];

if (!measurement){
res.status(400).json({ message: 'Measurement not found' });
return;
}
res.status(200).json(measurement);
} catch (e) {
res.status(500).json({ message: e });
}
};
108 changes: 108 additions & 0 deletions src/controllers/sensors.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { UUIDV4 } from 'sequelize';
import { sensorsDB } from '../models/db.index';
import { Sensor } from '../models/sensors.model';
import { RequestHandler } from 'express';

export const getSensors: RequestHandler = async (req, res) => {
try {
const sensors = await sensorsDB.findAll().then((sensors) => sensors.map((sensors) => {
const jsonSensors:Sensor = sensors.toJSON();
return jsonSensors;
})) as Sensor[];
res.status(200).json({ sensors });
} catch (e) {
res.status(500).json({ message: e });
}
};

export const getSensor: RequestHandler = async (req, res) => {
try {
const id = req.params.id;
if (!id) {
res.status(400).json({ message: 'Missing required fields' });
return;
}
const sensor = await sensorsDB.findOne({ where: { id: id } }).then((sensor) => {
const jsonSensor:Sensor|undefined = sensor?.toJSON();
return jsonSensor;
});
if (!sensor){
res.status(400).json({ message: 'Sensor not found' });
return;
}
res.status(200).json({ sensor });
} catch (e) {
res.status(500).json({ message: e });
}
};

interface CreateSensorBody {
sensor:Partial<Sensor>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are leaving this partial, but which fields are allowed to be omitted? Maybe we type this more explicitly, as well as check in the route if req fields are missing and reply with an error if so.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also do we want to check for auth? verifying a usertype is what they said earlier?

}

export const createSensor: RequestHandler = async (req, res) => {
try {
const createSensorBody:CreateSensorBody = req.body;
const thisID = UUIDV4();
await sensorsDB.create({
...createSensorBody.sensor,
id: createSensorBody.sensor.id ? thisID : null,
});
const sensor = await sensorsDB.findOne({ where: { name: thisID } }).then((sensor) => sensor?.toJSON()) as Sensor;
res.status(200).json({ sensor });
} catch (e) {
res.status(500).json({ message: e });
}
};

interface UpdateSensorBody {
sensor?:Partial<Sensor>;
}

export const updateSensor: RequestHandler = async(req, res) => {
try {
const updateSensorBody:UpdateSensorBody = req.body;
const id = req.params.id;
if (!id) {
res.status(400).json({ message: 'Missing required fields' });
return;
}
if (!updateSensorBody.sensor){
res.status(400).json({ message: 'Missing required fields' });
return;
}
const sensor = await sensorsDB.findOne({ where: { id: id } }).then((sensor) => sensor?.toJSON()) as Sensor;
if (!sensor){
res.status(400).json({ message: 'Sensor not found' });
return;
}
sensorsDB.update({
...updateSensorBody.sensor,
}, { where: { id: id },
});
const updatedSensor = await sensorsDB.findOne({ where: { id: id } }).then((sensor) => sensor?.toJSON()) as Sensor;

res.status(200).json({ machine: updatedSensor, message: 'Sensor updated' });
} catch (e) {
res.status(500).json({ message: e });
}
};

export const deleteSensor: RequestHandler = async (req, res) => {
try {
const id = req.params.id;
if (!id) {
res.status(400).json({ message: 'Missing required fields' });
return;
}
const sensor = await sensorsDB.findOne({ where: { id: id } }).then((sensor) => sensor?.toJSON()) as Sensor;
if (!sensor){
res.status(400).json({ message: 'Sensor not found' });
return;
}
await sensorsDB.destroy({ where: { id: id } });
res.status(200).json({ message: 'Sensor deleted' });
} catch (e) {
res.status(500).json({ message: e });
}
};
4 changes: 3 additions & 1 deletion src/models/db.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { messagesModel } from './messages.model';
import { usersModel } from './users.model';
import { plotsModel } from './plots.model';
import { sessionsModel } from './sessions.model';
import { sensorsModel } from './sensors.model';

export const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
host: dbConfig.HOST,
Expand All @@ -23,4 +24,5 @@ export const nodesDB = sequelize.define('nodes', nodesModel);
export const messagesDB = sequelize.define('messages', messagesModel);
export const logDB = sequelize.define('log', logModel);
export const plotsDB = sequelize.define('plots', plotsModel);
export const sessionsDB = sequelize.define('sessions', sessionsModel);
export const sessionsDB = sequelize.define('sessions', sessionsModel);
export const sensorsDB = sequelize.define('sensors', sensorsModel);
Loading