Implement the same for centers
Should really standardize this... Wish JS had interfaces...
This commit is contained in:
112
main.js
112
main.js
@@ -496,6 +496,18 @@ class CenterSession {
|
|||||||
sessions = [];
|
sessions = [];
|
||||||
nextSession = 0;
|
nextSession = 0;
|
||||||
mode = CenterMode.DEBUG;
|
mode = CenterMode.DEBUG;
|
||||||
|
configuredMessageJob = {
|
||||||
|
source: "",
|
||||||
|
destination: "",
|
||||||
|
message: "",
|
||||||
|
};
|
||||||
|
configuredMultiMessageJob = {
|
||||||
|
source: "",
|
||||||
|
destination: "",
|
||||||
|
message: "",
|
||||||
|
interval: 1000,
|
||||||
|
count: 1,
|
||||||
|
};
|
||||||
|
|
||||||
disconnectingPromise = {
|
disconnectingPromise = {
|
||||||
promise: null,
|
promise: null,
|
||||||
@@ -618,6 +630,18 @@ class CenterSession {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
configNotify(source, destination, message) {
|
||||||
|
this.configuredMessageJob = {
|
||||||
|
source: source,
|
||||||
|
destination: destination,
|
||||||
|
message: message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
notifyConfig() {
|
||||||
|
this.notify(this.configuredMessageJob.source, this.configuredMessageJob.destination, this.configuredMessageJob.message);
|
||||||
|
}
|
||||||
|
|
||||||
notify(source, destination, message) {
|
notify(source, destination, message) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!this.canSend()) {
|
if (!this.canSend()) {
|
||||||
@@ -636,6 +660,21 @@ class CenterSession {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
configManyNotify(source, destination, message, interval, count) {
|
||||||
|
this.configuredMultiMessageJob = {
|
||||||
|
source: source,
|
||||||
|
destination: destination,
|
||||||
|
message: message,
|
||||||
|
interval: interval,
|
||||||
|
count: count
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
manyNotifyConfig() {
|
||||||
|
this.notifyOnInterval(this.configuredMultiMessageJob.source, this.configuredMultiMessageJob.destination, this.configuredMultiMessageJob.message,
|
||||||
|
this.configuredMultiMessageJob.interval, this.configuredMultiMessageJob.count);
|
||||||
|
}
|
||||||
|
|
||||||
notifyOnInterval(source, destination, message, interval, count) {
|
notifyOnInterval(source, destination, message, interval, count) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!this.canSend() || this.busy) {
|
if (!this.canSend() || this.busy) {
|
||||||
@@ -764,6 +803,8 @@ class CenterSession {
|
|||||||
status: this.status,
|
status: this.status,
|
||||||
activeSessions: this.sessions.length,
|
activeSessions: this.sessions.length,
|
||||||
mode: this.mode,
|
mode: this.mode,
|
||||||
|
configuredMessageJob: this.configuredMessageJob,
|
||||||
|
configuredMultiMessageJob: this.configuredMultiMessageJob,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -831,6 +872,8 @@ class CenterSessionManager {
|
|||||||
if (!!server.mode) {
|
if (!!server.mode) {
|
||||||
createdServer.mode = server.mode;
|
createdServer.mode = server.mode;
|
||||||
}
|
}
|
||||||
|
createdServer.configuredMessageJob = server.configuredMessageJob;
|
||||||
|
createdServer.configuredMultiMessageJob = server.configuredMultiMessageJob;
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.log1(`Error loading centers from ${CLIENT_SESSIONS_FILE}: ${e}`);
|
this.logger.log1(`Error loading centers from ${CLIENT_SESSIONS_FILE}: ${e}`);
|
||||||
@@ -883,7 +926,11 @@ class HTTPServer {
|
|||||||
app.delete('/api/center/:id/session/:sessionId', this.closeCenterServerSessionById.bind(this));
|
app.delete('/api/center/:id/session/:sessionId', this.closeCenterServerSessionById.bind(this));
|
||||||
app.delete('/api/center/:id/session/:sessionId/destroy', this.deleteCenterServerSessionById.bind(this));
|
app.delete('/api/center/:id/session/:sessionId/destroy', this.deleteCenterServerSessionById.bind(this));
|
||||||
app.patch('/api/center/:id', this.patchCenterServer.bind(this));
|
app.patch('/api/center/:id', this.patchCenterServer.bind(this));
|
||||||
|
app.put('/api/center/:id/send', this.configNotify.bind(this));
|
||||||
|
app.post('/api/center/:id/send/config', this.notifyConfig.bind(this));
|
||||||
app.post('/api/center/:id/send', this.notify.bind(this));
|
app.post('/api/center/:id/send', this.notify.bind(this));
|
||||||
|
app.put('/api/center/:id/sendMany', this.configNotifyMany.bind(this));
|
||||||
|
app.post('/api/center/:id/sendMany/config', this.notifyManyConfig.bind(this));
|
||||||
app.post('/api/center/:id/sendMany', this.notifyMany.bind(this));
|
app.post('/api/center/:id/sendMany', this.notifyMany.bind(this));
|
||||||
app.delete('/api/center/:id/sendMany', this.cancelNotifyMany.bind(this));
|
app.delete('/api/center/:id/sendMany', this.cancelNotifyMany.bind(this));
|
||||||
app.delete('/api/center/:id/connect', this.disconnectCenterSession.bind(this));
|
app.delete('/api/center/:id/connect', this.disconnectCenterSession.bind(this));
|
||||||
@@ -1201,12 +1248,40 @@ class HTTPServer {
|
|||||||
res.send(centerSessionManager.getAvailableCenterModes());
|
res.send(centerSessionManager.getAvailableCenterModes());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
configNotify(req, res) {
|
||||||
|
let server = centerSessionManager.getSession(req.params.id);
|
||||||
|
let source = req.body.source;
|
||||||
|
let destination = req.body.destination;
|
||||||
|
let message = req.body.message;
|
||||||
|
this.logger.log1(`Setting default message from ${source} to ${destination} with message ${message} on server with ID ${req.params.id}`)
|
||||||
|
if (server) {
|
||||||
|
server.configureDefault(source, destination, message);
|
||||||
|
res.send(server.serialize());
|
||||||
|
} else {
|
||||||
|
this.logger.log1(`No server found with ID ${req.params.id}`);
|
||||||
|
res.status(404).send();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
notifyConfig(req, res) {
|
||||||
|
let server = centerSessionManager.getSession(req.params.id);
|
||||||
|
this.logger.log1(`Sending pre-configured message on server with ID ${req.params.id}`)
|
||||||
|
if (server) {
|
||||||
|
server.sendDefault()
|
||||||
|
.then(pdu => res.send(pdu))
|
||||||
|
.catch(err => res.status(400).send(JSON.stringify(err)));
|
||||||
|
} else {
|
||||||
|
this.logger.log1(`No server found with ID ${req.params.id}`);
|
||||||
|
res.status(404).send();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
let destination = req.body.destination;
|
let destination = req.body.destination;
|
||||||
let message = req.body.message;
|
let message = req.body.message;
|
||||||
this.logger.log1(`Sending notify message from ${source} to ${destination} with message ${message} on session with ID ${req.params.id}`)
|
this.logger.log1(`Sending notify message from ${source} to ${destination} with message ${message} on server with ID ${req.params.id}`)
|
||||||
if (server) {
|
if (server) {
|
||||||
server.notify(source, destination, message)
|
server.notify(source, destination, message)
|
||||||
.then(pdu => res.send(pdu))
|
.then(pdu => res.send(pdu))
|
||||||
@@ -1217,6 +1292,41 @@ class HTTPServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
configNotifyMany(req, res) {
|
||||||
|
let server = centerSessionManager.getSession(req.params.id);
|
||||||
|
let source = req.body.source;
|
||||||
|
let destination = req.body.destination;
|
||||||
|
let message = req.body.message;
|
||||||
|
let interval = req.body.interval / 1000;
|
||||||
|
let count = req.body.count;
|
||||||
|
if (!!req.body.perSecond) {
|
||||||
|
interval = 1 / req.body.perSecond;
|
||||||
|
}
|
||||||
|
let perSecond = 1 / interval;
|
||||||
|
this.logger.log1(
|
||||||
|
`Setting default ${count} messages from ${source} to ${destination} with message ${message} on server with ID ${req.params.id} at a rate of ${perSecond} per second.`);
|
||||||
|
if (server) {
|
||||||
|
server.configureDefaultInterval(source, destination, message, interval, count);
|
||||||
|
res.send(server.serialize());
|
||||||
|
} else {
|
||||||
|
this.logger.log1(`No server found with ID ${req.params.id}`);
|
||||||
|
res.status(404).send();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
notifyManyConfig(req, res) {
|
||||||
|
let server = centerSessionManager.getSession(req.params.id);
|
||||||
|
this.logger.log1(`Sending pre-configured messages on server with ID ${req.params.id}`)
|
||||||
|
if (server) {
|
||||||
|
server.sendDefaultInterval()
|
||||||
|
.then(pdu => res.send(pdu))
|
||||||
|
.catch(err => res.status(400).send(JSON.stringify(err)));
|
||||||
|
} else {
|
||||||
|
this.logger.log1(`No server found with ID ${req.params.id}`);
|
||||||
|
res.status(404).send();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
notifyMany(req, res) {
|
notifyMany(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;
|
||||||
|
Reference in New Issue
Block a user