Archived
2

spectrum field working again

This commit is contained in:
VLE2FE
2020-07-09 13:48:27 +02:00
parent 6a02f09e7f
commit 1ddc2b617a
13 changed files with 269 additions and 115 deletions

View File

@ -1,55 +1,82 @@
const csv = require('csv-parser');
const fs = require('fs');
const axios = require('axios');
const {Builder} = require('selenium-webdriver');
const {Builder} = require('selenium-webdriver'); // selenium and the chrome driver must be installed and configured separately
const chrome = require('selenium-webdriver/chrome');
const pdfReader = require('pdfreader');
const iconv = require('iconv-lite');
const metadata = 'C:\\Users\\vle2fe\\Documents\\Data\\Rng_200622\\VZ.csv'; // metadata file
const nmDocs = 'C:\\Users\\vle2fe\\Documents\\Data\\Rng_200622\\nmDocs'; // NormMaster Documents
const dptFiles = 'C:\\Users\\vle2fe\\Documents\\Data\\Rng_200622\\DPT'; // Spectrum files
const metaDoc = 'C:\\Users\\vle2fe\\Documents\\Data\\Rng_200707\\metadata.csv'; // metadata files
const kfDoc = 'C:\\Users\\vle2fe\\Documents\\Data\\Rng_200707\\kf.csv';
const vzDoc = 'C:\\Users\\vle2fe\\Documents\\Data\\Rng_200707\\vz.csv';
const nmDocs = 'C:\\Users\\vle2fe\\Documents\\Data\\Rng_200707\\nmDocs'; // NormMaster Documents
const dptFiles = 'C:\\Users\\vle2fe\\Documents\\Data\\Rng_200707\\DPT'; // Spectrum files
// const host = 'http://localhost:3000';
const host = 'https://definma-api.apps.de1.bosch-iot-cloud.com';
let data = []; // metadata contents
let materials = {};
let samples = [];
let normMaster = {};
let sampleDevices = {};
// TODO: integrate measurement device information from DPT names using different users
// TODO: supplier: other for supplierless samples
// TODO: BASF twice, BASF as color
// TODO: trim color names
// TODO: duplicate kf values
// TODO: conditions
// TODO: comment and reference handling
// TODO: check last color errors (filter out already taken) use location and device for user, upload to BIC
main();
async function main() {
if (0) { // materials
await getNormMaster();
await importCsv();
await importCsv(metaDoc);
await allMaterials();
await saveMaterials();
await importCsv(kfDoc);
await allMaterials();
await saveMaterials();
await importCsv(vzDoc);
await allMaterials();
fs.writeFileSync('./data_import/materials.json', JSON.stringify(materials));
await saveMaterials();
}
else if (0) { // samples
await importCsv();
await allSamples();
await saveSamples();
if (0) { // samples
sampleDeviceMap();
if (1) {
console.log('-------- META ----------');
await importCsv(metaDoc);
await allSamples();
await saveSamples();
}
if (1) {
console.log('-------- KF ----------');
await importCsv(kfDoc);
await allSamples();
await saveSamples();
await allKfVz();
}
if (1) {
console.log('-------- VZ ----------');
await importCsv(vzDoc);
await allSamples();
await saveSamples();
await allKfVz();
}
}
else if (1) { // DPT
if (1) { // DPT
await allDpts();
}
else if (0) { // KF/VZ
await importCsv();
await allKfVz();
}
else if (0) { // pdf test
console.log(await readPdf('N28_BN22-O010_2018-03-08.pdf'));
if (0) { // pdf test
console.log(await readPdf('N28_BN05-OX013_2016-03-11.pdf'));
}
}
async function importCsv() {
async function importCsv(doc) {
data = [];
await new Promise(resolve => {
fs.createReadStream(metadata)
fs.createReadStream(doc)
.pipe(iconv.decodeStream('win1252'))
.pipe(csv())
.on('data', (row) => {
@ -57,6 +84,9 @@ async function importCsv() {
})
.on('end', () => {
console.info('CSV file successfully processed');
if (data[0]['Farbe']) { // fix German column names
data.map(e => {e['Color'] = e['Farbe']; return e; });
}
resolve();
});
});
@ -65,7 +95,7 @@ async function importCsv() {
async function allDpts() {
let res = await axios({
method: 'get',
url: 'http://localhost:3000/template/measurements',
url: host + '/template/measurements',
auth: {
username: 'admin',
password: 'Abc123!#'
@ -74,7 +104,7 @@ async function allDpts() {
const measurement_template = res.data.find(e => e.name === 'spectrum')._id;
res = await axios({
method: 'get',
url: 'http://localhost:3000/samples?status=all',
url: host + '/samples?status=all',
auth: {
username: 'admin',
password: 'Abc123!#'
@ -84,10 +114,10 @@ async function allDpts() {
res.data.forEach(sample => {
sampleIds[sample.number] = sample._id;
});
const regex = /.*?_(.*?)_(\d+|\d+_\d+).DPT/;
const dptRegex = /.*?_(.*?)_(\d+|\d+_\d+).DPT/;
const dpts = fs.readdirSync(dptFiles);
for (let i in dpts) {
const regexRes = regex.exec(dpts[i])
const regexRes = dptRegex.exec(dpts[i])
if (regexRes && sampleIds[regexRes[1]]) { // found matching sample
console.log(dpts[i]);
const f = fs.readFileSync(dptFiles + '\\' + dpts[i], 'utf-8');
@ -99,7 +129,7 @@ async function allDpts() {
data.values.dpt = f.split('\r\n').map(e => e.split(','));
await axios({
method: 'post',
url: 'http://localhost:3000/measurement/new',
url: host + '/measurement/new',
auth: {
username: 'admin',
password: 'Abc123!#'
@ -110,13 +140,16 @@ async function allDpts() {
console.error(err.response.data);
});
}
else {
console.log(`Could not find sample for ${dpts[i]} !!!!!!`);
}
}
}
async function allKfVz() {
let res = await axios({
method: 'get',
url: 'http://localhost:3000/template/measurements',
url: host + '/template/measurements',
auth: {
username: 'admin',
password: 'Abc123!#'
@ -126,7 +159,7 @@ async function allKfVz() {
const vz_template = res.data.find(e => e.name === 'vz')._id;
res = await axios({
method: 'get',
url: 'http://localhost:3000/samples?status=all',
url: host + '/samples?status=all',
auth: {
username: 'admin',
password: 'Abc123!#'
@ -140,13 +173,17 @@ async function allKfVz() {
console.info(`${index}/${data.length}`);
let sample = data[index];
if (sample['Sample number'] !== '') {
let credentials = ['admin', 'Abc123!#'];
if (sampleDevices[sample['Sample number']]) {
credentials = [sampleDevices[sample['Sample number']], '2020DeFinMachen!']
}
if (sample['KF in Gew%']) {
await axios({
method: 'post',
url: 'http://localhost:3000/measurement/new',
url: host + '/measurement/new',
auth: {
username: 'admin',
password: 'Abc123!#'
username: credentials[0],
password: credentials[1]
},
data: {
sample_id: sampleIds[sample['Sample number']],
@ -164,10 +201,10 @@ async function allKfVz() {
if (sample['VZ (ml/g)']) {
await axios({
method: 'post',
url: 'http://localhost:3000/measurement/new',
url: host + '/measurement/new',
auth: {
username: 'admin',
password: 'Abc123!#'
username: credentials[0],
password: credentials[1]
},
data: {
sample_id: sampleIds[sample['Sample number']],
@ -186,9 +223,10 @@ async function allKfVz() {
}
async function allSamples() {
samples = [];
let res = await axios({
method: 'get',
url: 'http://localhost:3000/materials?status=all',
url: host + '/materials?status=all',
auth: {
username: 'admin',
password: 'Abc123!#'
@ -200,7 +238,7 @@ async function allSamples() {
})
res = await axios({
method: 'get',
url: 'http://localhost:3000/samples?status=all',
url: host + '/samples?status=all',
auth: {
username: 'admin',
password: 'Abc123!#'
@ -215,7 +253,13 @@ async function allSamples() {
for (let index in data) {
console.info(`${index}/${data.length}`);
let sample = data[index];
if (sample['Sample number'] !== '' && sample['Supplier'] !== '' && sample['Granulate/Part'] !== '') { // TODO: wait for decision about samples without suppliers/color/type
if (sample['Sample number'] !== '') { // TODO: what about samples without color
if (sample['Supplier'] === '') { // empty supplier fields
sample['Supplier'] = 'unknown';
}
if (sample['Granulate/Part'] === '') { // empty supplier fields
sample['Granulate/Part'] = 'unknown';
}
const material = dbMaterials[trim(sample['Material name'])];
if (!material) { // could not find material, skipping sample
continue;
@ -236,13 +280,20 @@ async function allSamples() {
samples[si].color = material.numbers.find(e => e.number === sample['Material number']).color;
}
else if (sample['Color'] && sample['Color'] !== '') {
samples[si].color = material.numbers.find(e => e.color.indexOf(sample['Color']) >= 0).color;
let number = material.numbers.find(e => e.color.indexOf(trim(sample['Color'])) >= 0);
if (!number && /black/.test(sample['Color'])) { // special case bk for black
number = material.numbers.find(e => e.color.toLowerCase().indexOf('bk') >= 0);
if (!number) { // try German word
number = material.numbers.find(e => e.color.toLowerCase().indexOf('schwarz') >= 0);
}
}
samples[si].color = number.color;
}
else if (sampleColors[sample['Sample number'].split('_')[0]]) { // derive color from main sample for kf/vz
samples[si].color = sampleColors[sample['Sample number'].split('_')[0]];
}
else { // TODO: no color information at all
samples.pop();
else {
samples[si].color = '';
}
}
}
@ -251,41 +302,57 @@ async function allSamples() {
async function saveSamples() {
for (let i in samples) {
console.info(`${i}/${samples.length}`);
let credentials = ['admin', 'Abc123!#'];
if (sampleDevices[samples[i].number]) {
credentials = [sampleDevices[samples[i].number], '2020DeFinMachen!']
}
await axios({
method: 'post',
url: 'http://localhost:3000/sample/new',
url: host + '/sample/new',
auth: {
username: 'admin',
password: 'Abc123!#'
username: credentials[0],
password: credentials[1]
},
data: samples[i]
}).catch(err => {
console.log(samples[i]);
console.error(err.response.data);
if (err.response.data.status && err.response.data.status !== 'Sample number already taken') {
console.log(samples[i]);
console.error(err.response.data);
}
});
}
console.info('saved all samples');
}
async function allMaterials() {
materials = {};
for (let index in data) {
let sample = data[index];
if (sample['Sample number'] !== '' && sample['Supplier'] !== '') { // TODO: wait for decision about supplierless samples
if (sample['Sample number'] && sample['Sample number'] !== '') {
if (sample['Supplier'] === '') { // empty supplier fields
sample['Supplier'] = 'unknown';
}
if (sample['Material name'] === '') { // empty name fields
sample['Material name'] = sample['Material'];
}
if (!sample['Material']) { // column Material is named Plastic in VZ metadata
sample['Material'] = sample['Plastic'];
}
sample['Material name'] = trim(sample['Material name']);
if (materials.hasOwnProperty(sample['Material name'])) { // material already found at least once
if (sample['Material number'] !== '') {
if (sample['Material number'] && sample['Material number'] !== '') {
if (materials[sample['Material name']].numbers.length === 0 || !materials[sample['Material name']].numbers.find(e => e.number === stripSpaces(sample['Material number']))) { // new material number
if (materials[sample['Material name']].numbers.find(e => e.color === sample['Color'] && e.number === '')) { // color already in list, only number missing
materials[sample['Material name']].numbers.find(e => e.color === sample['Color'] && e.number === '').number = stripSpaces(sample['Material number']);
}
else {
materials[sample['Material name']].numbers.push({color: sample['Color'], number: stripSpaces(sample['Material number'])});
materials[sample['Material name']].numbers.push({color: trim(sample['Color']), number: stripSpaces(sample['Material number'])});
}
}
}
else if (sample['Color'] !== '') {
else if (sample['Color'] && sample['Color'] !== '') {
if (!materials[sample['Material name']].numbers.find(e => e.color === stripSpaces(sample['Color']))) { // new material color
materials[sample['Material name']].numbers.push({color: sample['Color'], number: ''});
materials[sample['Material name']].numbers.push({color: trim(sample['Color']), number: ''});
}
}
}
@ -293,8 +360,8 @@ async function allMaterials() {
console.info(`${index}/${data.length} ${sample['Material name']}`);
materials[sample['Material name']] = {
name: sample['Material name'],
supplier: sample['Supplier'],
group: sample['Material']
supplier: trim(sample['Supplier']),
group: trim(sample['Material'])
};
let tmp = /M(\d+)/.exec(sample['Reinforcing material']);
materials[sample['Material name']].mineral = tmp ? tmp[1] : 0;
@ -312,17 +379,20 @@ async function allMaterials() {
async function saveMaterials() {
const mKeys = Object.keys(materials)
for (let i in mKeys) {
console.info(`${i}/${mKeys.length}`);
await axios({
method: 'post',
url: 'http://localhost:3000/material/new',
url: host + '/material/new',
auth: {
username: 'admin',
password: 'Abc123!#'
},
data: materials[mKeys[i]]
}).catch(err => {
console.log(materials[mKeys[i]]);
console.error(err.response.data);
if (err.response.data.status && err.response.data.status !== 'Material name already taken') {
console.info(materials[mKeys[i]]);
console.error(err.response.data);
}
});
}
console.info('saved all materials');
@ -362,16 +432,16 @@ async function numbersFetch(sample) {
}
}
if (res.length === 0) { // no results
if (sample['Color'] !== '' || sample['Material number'] !== '') {
return [{color: sample['Color'], number: sample['Material number']}];
if ((sample['Color'] && sample['Color'] !== '') || (sample['Material number'] &&sample['Material number'] !== '')) {
return [{color: trim(sample['Color']), number: sample['Material number']}];
}
else {
return [];
}
}
else {
if (!res.find(e => e.number === sample['Material number'])) { // sometimes norm master does not include sample number even if listed
res.push({color: sample['Color'], number: sample['Material number']});
if (sample['Material number'] && !res.find(e => e.number === sample['Material number'])) { // sometimes norm master does not include sample number even if listed
res.push({color: trim(sample['Color']), number: sample['Material number']});
}
return res;
}
@ -403,7 +473,7 @@ async function getNormMaster(fetchAgain = false) {
}
function getNormMasterDoc(url, timing = 1) {
console.log(url);
console.info(url);
return new Promise(async resolve => {
const options = new chrome.Options();
options.setUserPreferences({
@ -453,7 +523,7 @@ function readPdf(file) {
rows.push(item.text);
}
else { // still the same row row
rows[rows.length - 1] += (item.x - lastX > 1.1 ? '$' : '') + item.text; // push to row, detect if still same cell
rows[rows.length - 1] += (item.x - lastX > 1.09 ? '$' : '') + item.text; // push to row, detect if still same cell
}
lastX = (item.w * 0.055) + item.x;
@ -465,7 +535,7 @@ function readPdf(file) {
table = -1;
// console.log(rows);
rows = rows.filter(e => /^\d{10}/m.test(stripSpaces(e))); // filter non-table rows
resolve(rows.map(e => {return {color: e.split('$')[3], number: stripSpaces(e.split('$')[0])}; }));
resolve(rows.map(e => {return {color: trim(e.split('$')[3]), number: stripSpaces(e.split('$')[0])}; }));
}
}
lastLastText = lastText;
@ -473,12 +543,23 @@ function readPdf(file) {
}
if (!item && table !== -1) { // document ended
rows = rows.filter(e => /^\d{10}/m.test(stripSpaces(e))); // filter non-table rows
resolve(rows.map(e => {return {color: e.split('$')[3], number: stripSpaces(e.split('$')[0])}; }));
resolve(rows.map(e => {return {color: trim(e.split('$')[3]), number: stripSpaces(e.split('$')[0])}; }));
}
});
});
}
function sampleDeviceMap() {
const dpts = fs.readdirSync(dptFiles);
const regex = /(.*?)_(.*?)_(\d+|[^_]+_\d+).DPT/;
for (let i in dpts) {
const regexRes = regex.exec(dpts[i])
if (regexRes) { // found matching sample
sampleDevices[regexRes[2]] = regexRes[1] === 'plastics' ? 'rng01' : regexRes[1].toLowerCase();
}
}
}
function stripSpaces(s) {
return s ? s.replace(/ /g,'') : '';
}