-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAvailabilityChecker.js
More file actions
53 lines (44 loc) · 1.58 KB
/
Copy pathAvailabilityChecker.js
File metadata and controls
53 lines (44 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require("dotenv").config();
/** Required libraries */
const mongoose = require("mongoose");
/** Import the Mqtt file which connects to the broker and provide client,as well as publishing and subscribing functions */
const mqtt = require("./Mqtt");
module.exports.mqtt = mqtt;
/** Subscribed topics */
const checkBookingTopic = "Team5/Dentistimo/Check/Booking"; //Booking information from frontend - confirm - should include issuance
const getTimeslotTopic = "/Team5/Dentistimo/TimeSlots";
const clientErrorTopic = "/Team5/Dentistimo/Client/Error";
const topicsToSubscribeTo = [
checkBookingTopic,
getTimeslotTopic
]
module.exports.listOfTopics = topicsToSubscribeTo;
/** Import the database. Connection happens in the Database.js file */
const database = require("./Database");
const timeslotsController = require('./Timeslots.js');
const bookingsController = require('./Booking.js');
mqtt.subscribeToAll(topicsToSubscribeTo);
/** Listen to messages below */
mqtt.client.on("message", function (topic, message) {
switch (topic) {
case checkBookingTopic:
try {
bookingsController.enqueueBooking(JSON.parse(message));
bookingsController.validateBooking();
} catch (error) {
console.log(error)
mqtt.client.publish(clientErrorTopic, JSON.stringify(error));
}
break;
case getTimeslotTopic:
try {
timeslotsController.checkTimeslots(JSON.parse(message));
} catch (error) {
console.log(error)
mqtt.client.publish(clientErrorTopic, JSON.stringify(error));
}
break;
default:
break;
}
});