attribute automatically the port

This commit is contained in:
Lei PAN
2023-05-19 13:22:22 +02:00
parent 2685191be3
commit 7717a04b43
3 changed files with 40 additions and 18 deletions
+1 -1
View File
@@ -20,7 +20,7 @@
"node-wget": "^0.4.3",
"node-xlsx": "^0.21.0",
"playwright": "^1.32.1",
"puppeteer": "^1.13.0",
"puppeteer": "^1.20.0",
"ws": "^6.2.0",
"read-ini-file": "^3.0.1",
"uuid": "^9.0.0",
+32 -9
View File
@@ -4,6 +4,7 @@ const CommandorPage = require("./workers/CommandorPage");
const {MongoManager, formatDate} = require("./workers/mongo_manager");
const alert = require('alert');
const schedule = require("node-schedule");
const {execSync} = require("child_process");
const mongoManager = new MongoManager();
const SEVEN_DAYS_IN_S = 3600 * 24 * 7;
@@ -12,7 +13,11 @@ let excelUtil = new ExcelUtil();
let collectionName = formatDate(new Date())
// device_to_excludes = ["47e7e36b", "e30eb015"]
// device_to_excludes = ["47e7e36b"]
// device_to_excludes = ["J4AXB761H2322WJ"]
device_to_excludes = []
attributedPorts = []
const device_port_info = new Map();
startPort = 9000
async function filterAlreadyBookedContacts(contactList) {
let alreadyBookedContacts = await mongoManager.getAllSuccessfulItemsForDay(collectionName);
@@ -25,7 +30,7 @@ async function filterAlreadyBookedContacts(contactList) {
return contactsToBook;
}
async function filterAlreadyAccepteddContacts(contactList) {
async function filterAlreadyAcceptedContacts(contactList) {
let alreadyBookedContacts = await mongoManager.getAllAcceptedAppointments();
let contactsToBook = [];
contactList.forEach((contact) => {
@@ -112,20 +117,19 @@ async function needToBook(contact, mongoManager) {
return needToBook
}
async function startBook(contactPojo, device, selectedStore, audioAnalyse, alertBeep) {
async function startBook(contactPojo, device, selectedStore, audioAnalyse, alertBeep, port) {
console.log(`model: ${device.model()}`);
console.log(`serial: ${device.serial()}`);
if (await needToBook(contactPojo, mongoManager)) {
let commandor = new CommandorPage(contactPojo, device, mongoManager, selectedStore, audioAnalyse, alertBeep);
let commandor = new CommandorPage(contactPojo, device, mongoManager, selectedStore, audioAnalyse, alertBeep, port);
//read contacts form excel
let port = 9222;
return await commandor.loadPage(port);
return await commandor.loadPage();
} else {
console.log("do not send request --> skip")
}
}
async function startWithList(contacts, device, selectedStore, audioAnalyse, alertBeep) {
async function startWithList(contacts, device, selectedStore, audioAnalyse, alertBeep, port) {
let duplicatedList = [].concat(contacts).concat(contacts).concat(contacts)
// let duplicatedList = [].concat(contacts)
await duplicatedList.reduce(async (promise, contactPojo) => {
@@ -133,7 +137,7 @@ async function startWithList(contacts, device, selectedStore, audioAnalyse, aler
// The first iteration uses an already resolved Promise
// so, it will immediately continue.
await promise;
const contents = await startBook(contactPojo, device, selectedStore, audioAnalyse, alertBeep);
const contents = await startBook(contactPojo, device, selectedStore, audioAnalyse, alertBeep, port);
console.log(contents);
}, Promise.resolve());
}
@@ -161,9 +165,26 @@ async function startBookWithNumbers(startNumber, endNumber, selectedStore, pathT
alert("联系人数为0")
return
}
function startForwordingForDevice(device) {
const execSync = require('child_process').execSync;
// get attributed port
let attributedPort = device_port_info[device.serial()]
if (attributedPort) {
const output = execSync('adb -s ' + device.serial() + " forward tcp:" + attributedPort + " localabstract:chrome_devtools_remote", {encoding: 'utf-8'}); // the default is 'buffer'
console.log('Output was:\n', output);
} else {
attributedPort = startPort;
startPort++;
const output = execSync('adb -s ' + device.serial() + " forward tcp:" + attributedPort + " localabstract:chrome_devtools_remote", {encoding: 'utf-8'}); // the default is 'buffer'
console.log('Output was:\n', output);
}
return attributedPort
}
mongoManager.connect().then(r => {
filterAlreadyBookedContacts(contactList).then((listToBook) => {
filterAlreadyAccepteddContacts(listToBook).then(notAcceptedContacts => {
filterAlreadyAcceptedContacts(listToBook).then(notAcceptedContacts => {
filterBlacklistedContacts(notAcceptedContacts).then(listWithoutBlackContact => {
console.log("number of contacts to book:" + listWithoutBlackContact.length)
android.devices().then((devices) => {
@@ -176,7 +197,9 @@ async function startBookWithNumbers(startNumber, endNumber, selectedStore, pathT
console.log("connected device number:" + filteredDeviceList.length)
console.log("segmentNumber:" + segmentNumber)
for (let i = 0; i < filteredDeviceList.length; i++) {
startWithList(listWithoutBlackContact.slice(i * segmentNumber, segmentNumber * (i + 1)), filteredDeviceList[i], selectedStore, audioAnalyse, alertBeep);
let device = filteredDeviceList[i];
let port = startForwordingForDevice(device)
startWithList(listWithoutBlackContact.slice(i * segmentNumber, segmentNumber * (i + 1)), device, selectedStore, audioAnalyse, alertBeep, port);
}
})
})
+7 -8
View File
@@ -68,12 +68,13 @@ function logWithDevice(message, device) {
}
class CommandorPage {
constructor(contact, device, mongoManager, selectedStore = DEFAULT_STORE, audioAnalyse = false, alertBeep = false) {
constructor(contact, device, mongoManager, selectedStore = DEFAULT_STORE, audioAnalyse = false, alertBeep = false, port = 9000) {
this.contact = contact;
this.device = device;
this.mongoManager = mongoManager;
this.selectedStore = selectedStore;
this.choosedStore = selectedStore
this.choosedStore = selectedStore;
this.port = port;
this.isFillingFields = false;
this.isTerminated = false;
this.audioAnalyse = audioAnalyse;
@@ -86,12 +87,12 @@ class CommandorPage {
}
async loadPage(port) {
async loadPage() {
// Connect to the device.
log("loadPage() called");
log("loadPage() called, with port:" + this.port);
try {
this.browser = await puppeteer.connect({
browserWSEndpoint: 'ws://127.0.0.1:9002/devtools/browser',
browserWSEndpoint: "ws://127.0.0.1:" + this.port + "/devtools/browser",
headless: false, defaultViewport: null
})
this.page = await this.browser.newPage();
@@ -126,8 +127,6 @@ class CommandorPage {
log(e)
}
try {
// let element = await this.page.$(':nth-match(:text("rendezvousparis"), 1)')
// let element = await this.page.$(':nth-match(:text("rendezvousparis"), 1)')
const [button] = await this.page.$x("//a[contains(., 'rendezvousparis')]");
console.log("button is " + button)
if (button) {
@@ -365,7 +364,7 @@ class CommandorPage {
await delay(1000)
this.captchaSolver = new SolveCaptcha(page);
await this.captchaSolver.start((solution) => {
log("solution is: " + solution);
logWithDevice("solution is: " + solution, this.device);
if (solution !== ERROR_CAPTCHA_UNSOLVABLE && solution !== TWO_CAPTCHA_CONNECTION_FAILED) {
try {
if (!page.isClosed()) {