Формирование файла dataset; Загрузка на файловый ресурс SCP; Передача данных фреймворку; Запрос на обучение модели.
155 lines
5.5 KiB
JavaScript
155 lines
5.5 KiB
JavaScript
/*
|
||
Сервис интеграции ПП Парус 8 с WEB API
|
||
Дополнительный модуль: Загрузки файла на сервер SCP
|
||
*/
|
||
|
||
//------------------------------
|
||
// Подключение внешних библиотек
|
||
//------------------------------
|
||
|
||
const { ServerError } = require("./../core/server_errors"); //Типовая ошибка
|
||
const { SERR_APP_SERVER_BEFORE, SERR_DB_SERVER } = require("./../core/constants"); //Общесистемные константы
|
||
const oracledb = require("oracledb"); //Работа с СУБД Oracle
|
||
const { Client } = require('node-scp')
|
||
|
||
//------------
|
||
// Тело модуля
|
||
//------------
|
||
|
||
|
||
//------------
|
||
// Чтение файла и запись в file_buffer
|
||
//------------
|
||
let myReadFiles = async(nRN,dbConn) => {
|
||
if (dbConn){
|
||
//Если есть подключение к БД
|
||
if (dbConn.bConnected) {
|
||
let pooledConnection;
|
||
try {
|
||
//Выполняем процедуру
|
||
console.log('!1 -',nRN);
|
||
pooledConnection = await dbConn.connection.getConnection();
|
||
console.log('!2 -',nRN);
|
||
let res = await pooledConnection.execute(
|
||
"select t.file_name, t.dataset from UDO_V_T_EQUIPDSCMFL1 t where t.prn = :nRN",
|
||
{nRN: nRN}
|
||
);
|
||
console.log('!!3 -',nRN);
|
||
return res.rows;
|
||
} catch (e) {
|
||
throw new ServerError(`Выполнение процедуры не удачно`, e.message);
|
||
} finally {
|
||
if (pooledConnection) {
|
||
try {
|
||
await pooledConnection.close();
|
||
} catch (e) {
|
||
throw new Error(`Чегото не то!`, e.message);
|
||
}
|
||
}
|
||
}
|
||
} else {
|
||
throw new ServerError(SERR_DB_SERVER, `Нет подключения к БД`);
|
||
}
|
||
} else{
|
||
throw new ServerError(SERR_DB_SERVER, `Что тот не то ${dbConn}`);
|
||
}
|
||
};
|
||
|
||
|
||
|
||
//------------
|
||
// Загрузка файлов на сервер SCP
|
||
//------------
|
||
async function uploadSCP(sHost, sPort, sUsername, sPassword, sSCPPatch, data ) {
|
||
try {
|
||
//console.log(`sHost ${sHost}, sPort ${sPort}, sUsername ${sUsername}, sPassword ${sPassword}, sSCPPatch ${sSCPPatch}`);
|
||
console.log("sHost",sHost);
|
||
console.log("sPort",sPort);
|
||
console.log("sUsername",sUsername);
|
||
console.log("sPassword",sPassword);
|
||
console.log("data",data);
|
||
const client = await Client({
|
||
host: sHost,
|
||
port: sPort,
|
||
username: sUsername,
|
||
password: sPassword,
|
||
// privateKey: fs.readFileSync('./key.pem'),
|
||
// passphrase: 'your key passphrase',
|
||
})
|
||
console.log("1",client);
|
||
await client.writeFile(sSCPPatch,
|
||
data,
|
||
)
|
||
//client.uploadFile(
|
||
// sLocalPatch,
|
||
// sSCPPatch,
|
||
// options?: TransferOptions
|
||
// )
|
||
//const result = await client.stat('/home/bilaboutsource/parus')
|
||
//console.log(result)
|
||
const result = await client.list('/home/sftp_user/parus')
|
||
console.log(result)
|
||
// you can perform upload multiple times
|
||
//await client.uploadFile('./test1.txt', '/workspace/test1.txt')
|
||
client.close() // remember to close connection after you finish
|
||
} catch (e) {
|
||
//console.log(`Ошибка загрузки файла ${e}`)
|
||
return e.message;
|
||
}
|
||
}
|
||
|
||
|
||
|
||
//Обработчик "До" для полученного сообщения
|
||
const uploadFileSCP = async prms => {
|
||
//Проверим, что есть подключение к БД
|
||
if (prms.dbConn.bConnected) {
|
||
try {
|
||
//Разбираем
|
||
//console.log("sHost:", prms.options["SHOST_SCP"]);
|
||
//console.log( "sPort:", prms.options["SPORT_SCP"]);
|
||
//console.log( "sUsername:", prms.options["SUSER_SCP"]);
|
||
//console.log( "sPassword:", prms.options["SPASS_SCP"]);
|
||
//console.log( "sLocalPatch:", prms.options["SPATCH_LOCAL"]);
|
||
//console.log( "sSCPPatch:", prms.options["SPATCH_FSERV"]);
|
||
//console.log("data",prms.queue.blMsg);
|
||
//let rows = await myReadFiles(prms.options["NEQUIPDSCMN"],prms.dbConn);
|
||
//console.log(rows.length);
|
||
//res = "urq";
|
||
//for ( let i= 0; i < rows.count; i++){
|
||
// console.log()
|
||
//}
|
||
//console.log(prms.options["SPATCH_FSERV"]);
|
||
//console.log(prms.queue.blMsg);
|
||
let res = await uploadSCP(prms.options["SHOST_SCP"],prms.options["SPORT_SCP"], prms.options["SUSER_SCP"], prms.options["SPASS_SCP"],prms.options["SPATCH_FSERV"], prms.queue.blMsg )
|
||
if (res){
|
||
stateres = 500;
|
||
}else{
|
||
stateres = 200;
|
||
res = "";
|
||
}
|
||
console.log(stateres);
|
||
console.log(res);
|
||
// Соберем результат в XML
|
||
let resXml = "";
|
||
resXml += "<root>";
|
||
resXml += `<state>${stateres}</state>`;
|
||
resXml += `<message>${res}</message>`;
|
||
resXml += "</root>";
|
||
return{
|
||
blMsg: Buffer.from(resXml),
|
||
bStopPropagation: true
|
||
}
|
||
} catch (e) {
|
||
throw new ServerError(SERR_APP_SERVER_BEFORE, `Ошибка обработки тела входящего сообщения: ${e.message}`);
|
||
}
|
||
} else throw new ServerError(SERR_DB_SERVER, "Нет подключения к БД");
|
||
|
||
|
||
};
|
||
|
||
//-----------------
|
||
// Интерфейс модуля
|
||
//-----------------
|
||
|
||
exports.uploadFileSCP = uploadFileSCP;
|