retropilot-server/src/server/router/api/realtime.js

166 lines
4.9 KiB
JavaScript
Raw Normal View History

2022-01-12 08:02:30 -07:00
import express from 'express';
2022-03-21 17:38:56 -06:00
import { AthenaReturnedData } from '../../../models';
import authenticationController from '../../controllers/authentication';
2022-01-12 08:02:30 -07:00
import deviceController from '../../controllers/devices';
import { requireAuthenticated } from '../../middlewares/authentication';
2022-03-21 17:38:56 -06:00
2022-03-22 07:03:17 -06:00
// /api/realtime
2022-01-12 08:02:30 -07:00
const router = express.Router();
2022-03-21 17:38:56 -06:00
const whitelistParams = {
2022-01-07 18:35:55 -07:00
getmessage: true,
getversion: true,
setnavdestination: true,
listdatadirectory: true,
reboot: true,
uploadfiletourl: true,
listuploadqueue: true,
cancelupload: true,
primeactivated: true,
getpublickey: true,
getsshauthorizedkeys: true,
getsiminfo: true,
getnetworktype: true,
getnetworks: true,
2022-01-08 13:43:57 -07:00
takesnapshot: true,
2022-01-07 18:35:55 -07:00
};
2021-10-29 14:45:15 -06:00
2022-03-22 07:03:17 -06:00
// TODO: use middleware to get device from dongle id
router.get('/:dongleId/connected', requireAuthenticated, async (req, res) => {
2022-03-22 07:03:17 -06:00
const { account, params: { dongleId } } = req;
2022-01-08 16:07:09 -07:00
2022-03-22 09:14:08 -06:00
const device = await deviceController.getDeviceFromDongleId(dongleId);
2022-01-07 18:35:55 -07:00
if (!device) {
2022-01-08 16:07:09 -07:00
return res.status(400).json({
error: true,
errorMsg: 'no_dongle',
errorObject: { authenticated: true, dongle_exists: false },
});
2022-01-07 18:35:55 -07:00
}
2022-01-08 16:07:09 -07:00
// TODO support delegation of access
2022-01-07 18:35:55 -07:00
// TODO remove indication of dongle existing
if (device.account_id !== account.id) {
2022-01-08 16:07:09 -07:00
return res.status(403).json({
error: true,
errorMsg: 'unauthorised',
errorObject: { authenticated: true, dongle_exists: true, authorised_user: false },
});
2022-01-07 18:35:55 -07:00
}
2022-01-08 19:22:44 -07:00
// eslint-disable-next-line max-len
const isConnected = await req.athenaWebsocketTemp.isDeviceConnected(account.id, device.id, dongleId);
2022-01-07 18:35:55 -07:00
return res.status(200).json({
2022-01-08 19:22:44 -07:00
success: true,
dongle_id: device.dongle_id,
data: isConnected,
2022-01-07 18:35:55 -07:00
});
});
2022-03-22 07:03:17 -06:00
// TODO: change to POST request
router.get('/:dongleId/send/:method/', requireAuthenticated, async (req, res) => {
2022-03-22 07:03:17 -06:00
const { account, params: { dongleId, method } } = req;
2022-01-08 19:22:44 -07:00
if (!whitelistParams[method.toLowerCase()]) {
return res.status(409).json({
error: true,
errorMsg: 'invalid_method',
errorObject: { method },
});
}
2022-03-22 09:14:08 -06:00
const device = await deviceController.getDeviceFromDongleId(dongleId);
2022-01-07 18:35:55 -07:00
if (!device) {
2022-01-08 19:22:44 -07:00
return res.status(400).json({
error: true,
errorMsg: 'no_dongle',
errorObject: { authenticated: true, dongle_exists: false },
});
2022-01-07 18:35:55 -07:00
}
2022-01-08 19:22:44 -07:00
// TODO support delegation of access
2022-01-07 18:35:55 -07:00
// TODO remove indication of dongle existing
if (device.account_id !== account.id) {
2022-01-08 19:22:44 -07:00
return res.status(403).json({
error: true,
errorMsg: 'unauthorised',
errorObject: { authenticated: true, dongle_exists: true, authorised_user: false },
});
2022-01-07 18:35:55 -07:00
}
2022-01-08 19:22:44 -07:00
const data = await req.athenaWebsocketTemp.invoke(method, null, dongleId, account.id);
2022-01-07 18:35:55 -07:00
return res.status(200).json({
2022-01-08 19:22:44 -07:00
success: true,
dongle_id: dongleId,
method,
data,
2022-01-07 18:35:55 -07:00
});
});
2022-03-22 07:03:17 -06:00
router.get('/:dongle_id/get', async (req, res) => {
2022-01-08 17:38:41 -07:00
const account = await authenticationController.getAuthenticatedAccount(req);
2022-01-07 18:35:55 -07:00
if (account == null) {
2022-01-08 19:22:44 -07:00
return res.status(403).json({
error: true,
errorMsg: 'Unauthenticated',
errorObject: { authenticated: false },
});
2022-01-07 18:35:55 -07:00
}
2022-03-22 09:14:08 -06:00
const device = await deviceController.getDeviceFromDongleId(req.params.dongle_id);
2022-01-07 18:35:55 -07:00
if (!device) {
2022-01-08 19:22:44 -07:00
return res.status(400).json({
error: true,
errorMsg: 'no_dongle',
errorObject: {
authenticated: true,
dongle_exists: false,
},
});
2022-01-07 18:35:55 -07:00
}
if (device.account_id !== account.id) {
2022-01-08 19:22:44 -07:00
return res.status(403).json({
error: true,
errorMsg: 'unauthorised',
errorObject: {
authenticated: true,
dongle_exists: true,
authorised_user: false,
},
});
2022-01-07 18:35:55 -07:00
}
2022-03-21 17:38:56 -06:00
return res.json(await AthenaReturnedData.findAll({
2022-01-08 19:22:44 -07:00
where: { device_id: device.id },
}));
2022-01-07 18:35:55 -07:00
});
2022-03-22 07:03:17 -06:00
// TODO: change to POST request
router.get('/:dongle_id/temp/nav/:lat/:long', async (req, res) => {
2022-01-07 18:35:55 -07:00
if (!req.params.lat || !req.params.long) {
return res.status(403).json({ error: true, errorMsg: 'Malformed_Request', errorObject: { malformed: true } });
}
2022-01-08 17:38:41 -07:00
const account = await authenticationController.getAuthenticatedAccount(req);
2022-01-07 18:35:55 -07:00
if (account == null) {
return res.status(403).json({ error: true, errorMsg: 'Unauthenticated', errorObject: { authenticated: false } });
}
2022-03-22 09:14:08 -06:00
const device = await deviceController.getDeviceFromDongleId(req.params.dongle_id);
2022-01-07 18:35:55 -07:00
if (!device) {
return res.status(400).json({ error: true, errorMsg: 'no_dongle', errorObject: { authenticated: true, dongle_exists: false } });
}
if (device.account_id !== account.id) {
return res.status(403).json({ error: true, errorMsg: 'unauthorised', errorObject: { authenticated: true, dongle_exists: true, authorised_user: false } });
}
const data = await req.athenaWebsocketTemp.invoke('setNavDestination', { latitude: req.params.lat, longitude: req.params.long }, device.dongle_id, account.id);
return res.status(200).json({
2022-01-08 13:43:57 -07:00
success: true, dongle_id: device.dongle_id, method: req.params.method, data,
2022-01-07 18:35:55 -07:00
});
});
2022-01-12 08:02:30 -07:00
export default router;