mirror of
https://github.com/franjsco/lscanbot
synced 2025-02-19 21:10:35 +01:00
refactoring promises
This commit is contained in:
parent
9c600000d3
commit
5824f3abc2
15
index.js
15
index.js
@ -24,15 +24,8 @@ bot.on('/help', (msg) => {
|
|||||||
bot.on('/scan', (msg) => {
|
bot.on('/scan', (msg) => {
|
||||||
if (utils.isAuthorizedUser(msg.from.id)) {
|
if (utils.isAuthorizedUser(msg.from.id)) {
|
||||||
scanner.scanNetwork(msg.from.id)
|
scanner.scanNetwork(msg.from.id)
|
||||||
.then(() => {
|
.then((devices) => {
|
||||||
scanner.getDevicesNetwork(msg.from.id)
|
bot.sendMessage(msg.from.id, utils.templateDevicesList(devices), { parseMode: 'Markdown' } );
|
||||||
.then((devices) => {
|
|
||||||
bot.sendMessage(msg.from.id, utils.templateDevicesList(devices), { parseMode: 'Markdown' });
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
msg.reply.text('Error, check the error.log');
|
|
||||||
logger.logError(err);
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
msg.reply.text('Error, check the error.log');
|
msg.reply.text('Error, check the error.log');
|
||||||
@ -70,8 +63,8 @@ bot.on(/^\/remove (.+)$/, (msg, props) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
bot.on('/getUID', (msg) => {
|
bot.on('/me', (msg) => {
|
||||||
msg.reply.text(`⚠️ UID: ${msg.from.id}`);
|
msg.reply.text(`⚠️ My UID: ${msg.from.id}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
scanner.initScannerDB();
|
scanner.initScannerDB();
|
||||||
|
44
scanner.js
44
scanner.js
@ -2,52 +2,31 @@ const arpScanner = require('arpscan/promise');
|
|||||||
const appConfig = require('./config.json');
|
const appConfig = require('./config.json');
|
||||||
const db = require('./db');
|
const db = require('./db');
|
||||||
|
|
||||||
function scannerResults(data) {
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
function returnDevices(data) {
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
function returnDevicesError(err) {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
function returnInventory(data) {
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
function returnInventoryError(err) {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
function scannerError(err) {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
function scanNetwork(userId) {
|
function scanNetwork(userId) {
|
||||||
db.initScan(userId);
|
db.initScan(userId);
|
||||||
return (
|
return (
|
||||||
arpScanner(appConfig.scanner)
|
arpScanner(appConfig.scanner)
|
||||||
.then(scannerResults)
|
.then(data => {
|
||||||
.then(data => db.addDevices(userId, data))
|
db.addDevices(userId, data)
|
||||||
.catch(scannerError)
|
return userId
|
||||||
|
})
|
||||||
|
.then(getDevicesNetwork)
|
||||||
|
.catch((err) => { return err})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDevicesNetwork(userId) {
|
function getDevicesNetwork(userId) {
|
||||||
return (db.getDevices(userId)
|
return (db.getDevices(userId)
|
||||||
.then(returnDevices)
|
.then(data => {return data})
|
||||||
.catch(returnDevicesError)
|
.catch(err => {return err})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getInventoryNetwork() {
|
function getInventoryNetwork() {
|
||||||
return (
|
return (
|
||||||
db.getInventory()
|
db.getInventory()
|
||||||
.then(returnInventory)
|
.then(data => {return data})
|
||||||
.catch(returnInventoryError)
|
.catch(err => {return err})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,7 +39,7 @@ function addDeviceToTrack(data) {
|
|||||||
const devArg = {
|
const devArg = {
|
||||||
device: data[0],
|
device: data[0],
|
||||||
owner: data[1],
|
owner: data[1],
|
||||||
mac: data[2],
|
mac: data[2].toUpperCase(),
|
||||||
};
|
};
|
||||||
db.addDeviceToInventory(devArg);
|
db.addDeviceToInventory(devArg);
|
||||||
}
|
}
|
||||||
@ -71,7 +50,6 @@ function removeDeviceToTrack(mac) {
|
|||||||
|
|
||||||
module.exports.initScannerDB = initScannerDB;
|
module.exports.initScannerDB = initScannerDB;
|
||||||
module.exports.scanNetwork = scanNetwork;
|
module.exports.scanNetwork = scanNetwork;
|
||||||
module.exports.getDevicesNetwork = getDevicesNetwork;
|
|
||||||
module.exports.getInventoryNetwork = getInventoryNetwork;
|
module.exports.getInventoryNetwork = getInventoryNetwork;
|
||||||
module.exports.addDeviceToTrack = addDeviceToTrack;
|
module.exports.addDeviceToTrack = addDeviceToTrack;
|
||||||
module.exports.removeDeviceToTrack = removeDeviceToTrack;
|
module.exports.removeDeviceToTrack = removeDeviceToTrack;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user