Implement getAllModes endpoint

This commit is contained in:
David Majdandžić
2023-03-25 16:44:24 +01:00
parent 390ad046c2
commit 4ec3e9fed0
2 changed files with 27 additions and 2 deletions

File diff suppressed because one or more lines are too long

27
main.js
View File

@@ -440,6 +440,7 @@ class CenterSessionStatus {
// TODO: Implement endpoint for modifying modes // TODO: Implement endpoint for modifying modes
// TODO: Implement mode modification on centerSession // TODO: Implement mode modification on centerSession
// TODO: Implement writing and reading mode from file
class CenterMode { class CenterMode {
static DEBUG = "DEBUG"; static DEBUG = "DEBUG";
static ECHO = "ECHO"; static ECHO = "ECHO";
@@ -463,6 +464,7 @@ class CenterSession {
} }
static STATUS_CHANGED_EVENT = "statusChanged"; static STATUS_CHANGED_EVENT = "statusChanged";
static MODE_CHANGED_EVENT = "modeChanged";
static ANY_PDU_EVENT = "*"; static ANY_PDU_EVENT = "*";
static MESSAGE_SEND_COUNTER_UPDATE_EVENT = "messageSendCounterUpdate"; static MESSAGE_SEND_COUNTER_UPDATE_EVENT = "messageSendCounterUpdate";
@@ -502,8 +504,14 @@ class CenterSession {
this.refresh(); this.refresh();
} }
setMode(mode) {
this.mode = Object.values(CenterMode)[mode];
this.eventEmitter.emit(CenterSession.MODE_CHANGED_EVENT, mode);
}
refresh() { refresh() {
this.close().catch(err => {}); this.close().catch(err => {
});
} }
error(error) { error(error) {
@@ -738,6 +746,14 @@ class CenterSessionManager {
this.logger.log1(`Error loading sessions from ${CLIENT_SESSIONS_FILE}: ${e}`); this.logger.log1(`Error loading sessions from ${CLIENT_SESSIONS_FILE}: ${e}`);
} }
} }
getAvailableCenterModes() {
let modes = Object.values(CenterMode);
return modes.reduce((acc, curr, idx) => {
acc[idx] = curr;
return acc;
}, {});
}
} }
class HTTPServer { class HTTPServer {
@@ -760,6 +776,7 @@ class HTTPServer {
app.get('/api/center', this.getCenterSessions.bind(this)); app.get('/api/center', this.getCenterSessions.bind(this));
app.post('/api/center', this.createCenterSession.bind(this)); app.post('/api/center', this.createCenterSession.bind(this));
app.get('/api/center/modes', this.getAvailableModes.bind(this));
app.get('/api/center/:id', this.getCenterSessionById.bind(this)); app.get('/api/center/:id', this.getCenterSessionById.bind(this));
app.patch('/api/center/:id', this.patchCenterServer.bind(this)); app.patch('/api/center/:id', this.patchCenterServer.bind(this));
app.post('/api/center/:id/send', this.notify.bind(this)); app.post('/api/center/:id/send', this.notify.bind(this));
@@ -967,6 +984,9 @@ class HTTPServer {
if (!!req.body.password && req.body.password !== server.password) { if (!!req.body.password && req.body.password !== server.password) {
server.setPassword(req.body.password); server.setPassword(req.body.password);
} }
if (!!req.body.mode) {
server.setMode(req.body.mode);
}
res.send(server.serialize()); res.send(server.serialize());
} else { } else {
this.logger.log1(`No center server found with ID ${req.params.id}`); this.logger.log1(`No center server found with ID ${req.params.id}`);
@@ -974,6 +994,11 @@ class HTTPServer {
} }
} }
getAvailableModes(req, res) {
this.logger.log1("Getting available modes");
res.send(centerSessionManager.getAvailableCenterModes());
}
notify(req, res) { notify(req, res) {
let server = centerSessionManager.getSession(req.params.id); let server = centerSessionManager.getSession(req.params.id);
let source = req.body.source; let source = req.body.source;