Basic support for debug info.
Returning status of each source as part of vehicle model.
This commit is contained in:
parent
d1ebe4b2f4
commit
5de45af0e5
13
.vscode/launch.json
vendored
13
.vscode/launch.json
vendored
@ -4,6 +4,19 @@
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Launch via NPM",
|
||||
"request": "launch",
|
||||
"runtimeArgs": [
|
||||
"run",
|
||||
"server"
|
||||
],
|
||||
"runtimeExecutable": "npm",
|
||||
"skipFiles": [
|
||||
"<node_internals>/**"
|
||||
],
|
||||
"type": "pwa-node"
|
||||
},
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
|
||||
@ -2,6 +2,7 @@ const crypto = require('crypto');
|
||||
const fetch = require('node-fetch');
|
||||
const Vehicle = require('../models/vehicle');
|
||||
const PubNub = require('pubnub');
|
||||
const { SSL_OP_SSLEAY_080_CLIENT_DH_BUG } = require('constants');
|
||||
|
||||
const baseUrl = 'https://avtocod.ru/api/v3';
|
||||
let deviceToken = crypto.createHash('sha256').update(Date.now().toString()).digest().toString('hex');
|
||||
@ -74,28 +75,38 @@ function waitForReport(pubnubConfig, channel) {
|
||||
|
||||
class AvtocodProvider {
|
||||
static async getReport(number) {
|
||||
let url = `${baseUrl}/auto/generate?number=${encodeURIComponent(number)}&device_token=${deviceToken}`;
|
||||
let resp = await getJson(url);
|
||||
|
||||
let html = await getPage(resp.report_uri);
|
||||
let result = html.match(/<meta name="app-version-hash" content="(.*?)" \/>/);
|
||||
if(result == null) {
|
||||
throw Error('Error getting api version hash');
|
||||
try {
|
||||
let url = `${baseUrl}/auto/generate?number=${encodeURIComponent(number)}&device_token=${deviceToken}`;
|
||||
let resp = await getJson(url);
|
||||
|
||||
let html = await getPage(resp.report_uri);
|
||||
let result = html.match(/<meta name="app-version-hash" content="(.*?)" \/>/);
|
||||
if(result == null) {
|
||||
throw Error('Error getting api version hash');
|
||||
}
|
||||
let hash = result[1];
|
||||
|
||||
result = html.match(/value:(.*report_container.*)/);
|
||||
if(result == null) {
|
||||
throw Error('Error getting report data');
|
||||
}
|
||||
let mainData = JSON.parse(result[1]);
|
||||
let report = decryptReport(mainData.report_container.report, hash);
|
||||
if(report == null) {
|
||||
report = await waitForReport(mainData.pubnub, mainData.report_container.channel);
|
||||
}
|
||||
let vehicle = Vehicle.fromAvtocod(report);
|
||||
console.log('Avtocod found vehicle: ', vehicle?.brand?.name?.original);
|
||||
return vehicle;
|
||||
} catch(ex) {
|
||||
ex.debugInfo = {
|
||||
autocod: {
|
||||
fields: 0n,
|
||||
error: ex.message
|
||||
}
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
let hash = result[1];
|
||||
|
||||
result = html.match(/value:(.*report_container.*)/);
|
||||
if(result == null) {
|
||||
throw Error('Error getting report data');
|
||||
}
|
||||
let mainData = JSON.parse(result[1]);
|
||||
let report = decryptReport(mainData.report_container.report, hash);
|
||||
if(report == null) {
|
||||
report = await waitForReport(mainData.pubnub, mainData.report_container.channel);
|
||||
}
|
||||
let vehicle = Vehicle.fromAvtocod(report);
|
||||
console.log('Avtocod found vehicle: ', vehicle?.brand?.name?.original);
|
||||
return vehicle;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -54,62 +54,87 @@ class Vin01Provider {
|
||||
}
|
||||
|
||||
static async getReport(number, token) {
|
||||
let vin = await Vin01Provider.getVin(number, token);
|
||||
console.log('vin01 found VIN: ', vin);
|
||||
|
||||
let checks = [Vin01Provider.runCheck('base', vin, token), Vin01Provider.runCheck('history', vin, token)];
|
||||
let [base, history] = (await Promise.allSettled(checks)).map(Vin01Provider.checkErrors);
|
||||
if(base.status == 'rejected' && history.status == 'rejected') {
|
||||
console.log('Vin01 base error: ', base.reason);
|
||||
console.log('Vin01 history error: ', history.reason);
|
||||
let vehicle = new Vehicle();
|
||||
vehicle.vin1 = Utils.cyrillicToLatin(vin);
|
||||
return vehicle;
|
||||
} else if(base.status == 'rejected') {
|
||||
console.log('vin01 found history');
|
||||
let vehicle = Vehicle.fromVin01History(history.value);
|
||||
vehicle.number = number;
|
||||
return vehicle;
|
||||
} else if(history.status == 'rejected') {
|
||||
console.log('vin01 found base info');
|
||||
let vehicle = Vehicle.fromVin01Base(base.value);
|
||||
vehicle.number = number;
|
||||
return vehicle;
|
||||
} else {
|
||||
// Both history and base reports were successfully received, merge them in one report
|
||||
let baseVehicle = Vehicle.fromVin01Base(base.value);
|
||||
let historyVehicle = Vehicle.fromVin01History(history.value);
|
||||
|
||||
historyVehicle.brand.name.normalized = baseVehicle.brand.name.normalized;
|
||||
historyVehicle.model = baseVehicle.model;
|
||||
|
||||
for(let period of historyVehicle.ownershipPeriods) {
|
||||
let basePeriod = baseVehicle.ownershipPeriods.find(p => p.from == period.from);
|
||||
if(basePeriod) {
|
||||
period.region = basePeriod.region;
|
||||
period.registrationRegion = basePeriod.registrationRegion;
|
||||
period.locality = basePeriod.locality;
|
||||
|
||||
if(basePeriod.street) {
|
||||
period.street = basePeriod.street;
|
||||
}
|
||||
|
||||
if(basePeriod.building) {
|
||||
period.building = basePeriod.building;
|
||||
}
|
||||
|
||||
if(basePeriod.inn) {
|
||||
period.inn = basePeriod.inn;
|
||||
}
|
||||
|
||||
if(basePeriod.code) {
|
||||
period.code = basePeriod.code;
|
||||
try {
|
||||
let vin = await Vin01Provider.getVin(number, token);
|
||||
console.log('vin01 found VIN: ', vin);
|
||||
|
||||
let checks = [Vin01Provider.runCheck('base', vin, token), Vin01Provider.runCheck('history', vin, token)];
|
||||
let [base, history] = (await Promise.allSettled(checks)).map(Vin01Provider.checkErrors);
|
||||
if(base.status == 'rejected' && history.status == 'rejected') {
|
||||
console.log('Vin01 base error: ', base.reason);
|
||||
console.log('Vin01 history error: ', history.reason);
|
||||
let vehicle = new Vehicle();
|
||||
vehicle.vin1 = Utils.cyrillicToLatin(vin);
|
||||
vehicle.debugInfo = {
|
||||
vin01vin: { fields: 0n, error: null },
|
||||
vin01history: { fields: 0n, error: history.reason },
|
||||
vin01base: { fields: 0n, error: base.reason }
|
||||
}
|
||||
return vehicle;
|
||||
} else if(base.status == 'rejected') {
|
||||
console.log('vin01 found history');
|
||||
let vehicle = Vehicle.fromVin01History(history.value);
|
||||
vehicle.number = number;
|
||||
Object.assign(vehicle.debugInfo, {
|
||||
vin01vin: { fields: 0n, error: null },
|
||||
vin01base: { fields: 0n, error: base.reason }
|
||||
});
|
||||
return vehicle;
|
||||
} else if(history.status == 'rejected') {
|
||||
console.log('vin01 found base info');
|
||||
let vehicle = Vehicle.fromVin01Base(base.value);
|
||||
vehicle.number = number;
|
||||
Object.assign(vehicle.debugInfo, {
|
||||
vin01vin: { fields: 0n, error: null },
|
||||
vin01history: { fields: 0n, error: history.reason }
|
||||
});
|
||||
return vehicle;
|
||||
} else {
|
||||
// Both history and base reports were successfully received, merge them in one report
|
||||
let baseVehicle = Vehicle.fromVin01Base(base.value);
|
||||
let historyVehicle = Vehicle.fromVin01History(history.value);
|
||||
|
||||
historyVehicle.brand.name.normalized = baseVehicle.brand.name.normalized;
|
||||
historyVehicle.model = baseVehicle.model;
|
||||
|
||||
for(let period of historyVehicle.ownershipPeriods) {
|
||||
let basePeriod = baseVehicle.ownershipPeriods.find(p => p.from == period.from);
|
||||
if(basePeriod) {
|
||||
period.region = basePeriod.region;
|
||||
period.registrationRegion = basePeriod.registrationRegion;
|
||||
period.locality = basePeriod.locality;
|
||||
|
||||
if(basePeriod.street) {
|
||||
period.street = basePeriod.street;
|
||||
}
|
||||
|
||||
if(basePeriod.building) {
|
||||
period.building = basePeriod.building;
|
||||
}
|
||||
|
||||
if(basePeriod.inn) {
|
||||
period.inn = basePeriod.inn;
|
||||
}
|
||||
|
||||
if(basePeriod.code) {
|
||||
period.code = basePeriod.code;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Object.assign(historyVehicle.debugInfo, { vin01vin: { fields: 0n, error: null } });
|
||||
Object.assign(historyVehicle.debugInfo, baseVehicle.debugInfo);
|
||||
|
||||
historyVehicle.number = number;
|
||||
return historyVehicle;
|
||||
}
|
||||
|
||||
historyVehicle.number = number;
|
||||
return historyVehicle;
|
||||
} catch(ex) {
|
||||
ex.debugInfo = {
|
||||
vin01vin: { fields: 0n, error: ex.message },
|
||||
vin01history: { fields: 0n, error: 'Not applicable' },
|
||||
vin01base: { fields: 0n, error: 'Not applicable' }
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -61,6 +61,13 @@ class Vehicle {
|
||||
v.addedDate = Date.now();
|
||||
v.events = [];
|
||||
|
||||
v.debugInfo = {
|
||||
autocod: {
|
||||
fields: 0n,
|
||||
error: null
|
||||
}
|
||||
};
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
@ -97,6 +104,13 @@ class Vehicle {
|
||||
v.addedDate = Date.now();
|
||||
v.events = [];
|
||||
|
||||
v.debugInfo = {
|
||||
vin01history: {
|
||||
fields: 0n,
|
||||
error: null
|
||||
}
|
||||
};
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
@ -151,6 +165,13 @@ class Vehicle {
|
||||
v.addedDate = Date.now();
|
||||
v.events = [];
|
||||
|
||||
v.debugInfo = {
|
||||
vin01base: {
|
||||
fields: 0n,
|
||||
error: null
|
||||
}
|
||||
};
|
||||
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
@ -45,8 +45,10 @@ router.post('/check', async (req, res) => {
|
||||
throw Error(autocod.reason);
|
||||
} else if(vin01.status == 'rejected') {
|
||||
vehicle = autocod.value;
|
||||
Object.assign(vehicle.debugInfo, vin01.reason.debugInfo);
|
||||
} else if(autocod.status == 'rejected') {
|
||||
vehicle = vin01.value;
|
||||
Object.assign(vehicle.debugInfo, autocod.reason.debugInfo);
|
||||
if(!vehicle.brand.name.normalized) {
|
||||
throw Error(autocod.reason);
|
||||
}
|
||||
@ -58,11 +60,14 @@ router.post('/check', async (req, res) => {
|
||||
vehicle.color = vin01.value.color;
|
||||
vehicle.ownershipPeriods = vin01.value.ownershipPeriods;
|
||||
}
|
||||
Object.assign(vehicle.debugInfo, vin01.value.debugInfo);
|
||||
}
|
||||
|
||||
if(nomerogram.status == 'fulfilled') {
|
||||
vehicle.ads = nomerogram.value;
|
||||
Object.assign(vehicle.debugInfo, { fields: 0n, error: null });
|
||||
} else {
|
||||
Object.assign(vehicle.debugInfo, nomerogram.reason.debugInfo);
|
||||
console.error('nomerogram error: ', nomerogram.reason);
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user