Basic support for debug info.

Returning status of each source as part of vehicle model.
This commit is contained in:
Selim Mustafaev 2021-01-02 19:33:51 +03:00
parent d1ebe4b2f4
commit 5de45af0e5
5 changed files with 149 additions and 74 deletions

13
.vscode/launch.json vendored
View File

@ -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",

View File

@ -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,6 +75,7 @@ function waitForReport(pubnubConfig, channel) {
class AvtocodProvider {
static async getReport(number) {
try {
let url = `${baseUrl}/auto/generate?number=${encodeURIComponent(number)}&device_token=${deviceToken}`;
let resp = await getJson(url);
@ -96,6 +98,15 @@ class AvtocodProvider {
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;
}
}
}

View File

@ -54,6 +54,7 @@ class Vin01Provider {
}
static async getReport(number, token) {
try {
let vin = await Vin01Provider.getVin(number, token);
console.log('vin01 found VIN: ', vin);
@ -64,16 +65,29 @@ class Vin01Provider {
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
@ -108,9 +122,20 @@ class Vin01Provider {
}
}
Object.assign(historyVehicle.debugInfo, { vin01vin: { fields: 0n, error: null } });
Object.assign(historyVehicle.debugInfo, baseVehicle.debugInfo);
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;
}
}
}

View File

@ -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;
}
}

View File

@ -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);
}