retropilot-server/src/server/websocket/web/commands.js

66 lines
2.3 KiB
JavaScript
Raw Normal View History

2022-01-08 16:28:15 -07:00
// eslint-disable-next-line no-unused-vars
2022-03-21 04:56:42 -06:00
import authenticationController from '../../controllers/authentication';
2022-01-12 08:02:30 -07:00
2022-03-21 04:56:42 -06:00
import deviceController from '../../controllers/devices';
import athenaRealtime from '../athena';
// Checks if device is currently online in Athena
async function isDongleOnline(ws, msg) {
2022-01-07 18:35:55 -07:00
// Checking if the user is authorised to access dongle, this will be used later
// allowing users to delegate access.
const isAuthorised = await deviceController.isUserAuthorised(ws.account.id, msg.data.dongleId);
if (isAuthorised && isAuthorised.success === true) {
ws.send(JSON.stringify({
command: msg.command,
success: true,
id: msg.id || null,
2022-01-08 13:43:57 -07:00
data: athenaRealtime.isDeviceConnected(ws.account.id, null, msg.data.dongleId),
2022-01-07 18:35:55 -07:00
}));
2022-01-08 13:43:57 -07:00
} else {
2022-01-07 18:35:55 -07:00
ws.send(JSON.stringify({
2022-01-08 13:43:57 -07:00
command: msg.command, success: false, id: msg.id || null, msg: 'not_authorised',
2022-01-07 18:35:55 -07:00
}));
}
}
// Checks if device is currently online in Athena
async function rebootDongle(ws, msg) {
2022-01-07 18:35:55 -07:00
// Checking if the user is authorised to access dongle, this will be used later
// allowing users to delegate access.
const isAuthorised = await deviceController.isUserAuthorised(ws.account.id, msg.data.dongleId);
console.log('is auth', isAuthorised);
if (isAuthorised && isAuthorised.success === true) {
await athenaRealtime.invoke('reboot', null, msg.data.dongleId, ws.account.id, msg.id || null);
ws.send(JSON.stringify({
2022-01-08 13:43:57 -07:00
command: msg.command, success: true, id: msg.id || null, data: { command_issued: true },
2022-01-07 18:35:55 -07:00
}));
2022-01-08 13:43:57 -07:00
} else {
2022-01-07 18:35:55 -07:00
ws.send(JSON.stringify({
2022-01-08 13:43:57 -07:00
command: msg.command, success: false, id: msg.id || null, msg: 'not_authorised',
2022-01-07 18:35:55 -07:00
}));
}
}
async function takeSnapshot(ws, msg) {
2022-01-07 18:35:55 -07:00
const isAuthorised = await deviceController.isUserAuthorised(ws.account.id, msg.data.dongleId);
console.log('is auth', isAuthorised);
if (isAuthorised && isAuthorised.success === true) {
await athenaRealtime.invoke('takeSnapshot', null, msg.data.dongleId, ws.account.id, msg.id || null);
ws.send(JSON.stringify({
2022-01-08 13:43:57 -07:00
command: msg.command, success: true, id: msg.id || null, data: { command_issued: true },
2022-01-07 18:35:55 -07:00
}));
2022-01-08 13:43:57 -07:00
} else {
2022-01-07 18:35:55 -07:00
ws.send(JSON.stringify({
2022-01-08 13:43:57 -07:00
command: msg.command, success: false, id: msg.id || null, msg: 'not_authorised',
2022-01-07 18:35:55 -07:00
}));
}
}
2022-01-12 08:02:30 -07:00
export default {
2022-01-07 18:35:55 -07:00
isDongleOnline,
rebootDongle,
2022-01-08 13:43:57 -07:00
takeSnapshot,
2022-01-07 18:35:55 -07:00
};