UNPKG

@p2olab/pimad-core

Version:

PiMAd (Process-industry-Modular-Automation-description) High level access to automation of modular plants.

162 lines (161 loc) 6.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PEAPoolVendor = void 0; const Importer_1 = require("../Converter/Importer/Importer"); const Utils_1 = require("../Utils"); const uuid_1 = require("uuid"); const Backbone_1 = require("../Backbone"); var PiMAdResponseVendor = Backbone_1.Backbone.PiMAdResponseVendor; var PiMAdResponseHandler = Backbone_1.Backbone.PiMAdResponseHandler; var PiMAdResponseTypes = Backbone_1.Backbone.PiMAdResponseTypes; class APEAPool { constructor() { this.initialized = false; this.peas = []; this.importerChainFirstElement = {}; this.responseVendor = new PiMAdResponseVendor(); this.responseHandler = new PiMAdResponseHandler(); } generateUniqueIdentifier(callback) { const identifier = uuid_1.v4(); this.getPEA(identifier, response => { if (response.constructor.name === this.responseVendor.buyErrorResponse().constructor.name) { callback(identifier); } else { // identifier is already used, so try again // TODO: how to test that? this.generateUniqueIdentifier(callback); } }); } initialize(firstChainElement) { if (!this.initialized) { this.initialized = true; this.importerChainFirstElement = firstChainElement; return (JSON.stringify(this.importerChainFirstElement) == JSON.stringify(firstChainElement)); } else { return false; } } initializeMTPFreeze202001Importer() { if (!this.initialized) { this.initialized = true; const fImporter = new Importer_1.LastChainElementImporterFactory(); const mtpFreeze202001Importer = new Importer_1.MTPFreeze202001ImporterFactory().create(); mtpFreeze202001Importer.initialize(fImporter.create()); this.importerChainFirstElement = mtpFreeze202001Importer; return true; } else { return false; } } /** * Add PEAModel to Pool * @param instructions - parsing instructions including the filepath of uploaded file * @param callback */ addPEA(instructions, callback) { if (this.initialized) { this.generateUniqueIdentifier(identifier => { this.importerChainFirstElement.convertFrom({ source: instructions.source, identifier: identifier }, (response) => { if (response.constructor.name === this.responseVendor.buySuccessResponse().constructor.name) { this.peas.push(response.getContent()); callback(response); } else { callback(response); } }); }); } else { this.responseHandler.handleCallbackWithResponse(PiMAdResponseTypes.ERROR, 'PEAPool is not initialized!', {}, callback); } } /** * Delete PEAModel from PiMad-Pool by given Identifier * @param identifier - individual identifier of PEAModel * @param callback - contains Success/Failure message with Reason of Failure */ deletePEA(identifier, callback) { if (this.initialized) { // find pea by id const localPEA = this.peas.find(pea => identifier === pea.getPiMAdIdentifier()); // check if PEA exists if (!localPEA) { this.responseHandler.handleCallbackWithResponse(PiMAdResponseTypes.ERROR, 'PEA not found', {}, callback); } else { // get index of pea const index = this.peas.indexOf(localPEA, 0); // delete PEA this.peas.splice(index, 1); // check if deletion was successful if (this.peas.find(pea => identifier === pea.getPiMAdIdentifier()) == undefined) { this.responseHandler.handleCallbackWithResponse(PiMAdResponseTypes.SUCCESS, 'Success!', {}, callback); } else { this.responseHandler.handleCallbackWithResponse(PiMAdResponseTypes.ERROR, 'Deletion was unsuccessful!', {}, callback); } } } else { this.responseHandler.handleCallbackWithResponse(PiMAdResponseTypes.ERROR, 'PEAPool is not initialized!', {}, callback); } } /** * Get PEAModel by given Identifier * @param identifier - individual identifier of PEAModel * @param callback - contains Success/Failure message with Reason of Failure */ getPEA(identifier, callback) { if (this.initialized) { const localPEA = this.peas.find(pea => identifier === pea.getPiMAdIdentifier()); if (localPEA === undefined) { this.responseHandler.handleCallbackWithResponse(PiMAdResponseTypes.ERROR, 'PEAModel <' + identifier + '> is not part of the pool party!', {}, callback); } else { this.responseHandler.handleCallbackWithResponse(PiMAdResponseTypes.SUCCESS, 'Success!', localPEA, callback); } } else { this.responseHandler.handleCallbackWithResponse(PiMAdResponseTypes.ERROR, 'PEAPool is not initialized!', {}, callback); } } /** * get All PEAs from PiMad-Pool * @param callback */ getAllPEAs(callback) { if (this.initialized) { callback(this.responseHandler.handleResponse(PiMAdResponseTypes.SUCCESS, 'Success!', this.peas)); } else { callback(this.responseHandler.handleResponse(PiMAdResponseTypes.ERROR, 'This PEAPool is not initialized', {})); } } } class BasePEAPool extends APEAPool { } /* Factory */ class APEAPoolFactory { } class BasePEAPoolFactory extends APEAPoolFactory { create() { Utils_1.logger.debug('BasePEAPool generated via Factory'); return new BasePEAPool(); } } /* Vendor */ class PEAPoolVendor { constructor() { this.dependencyPEAPoolFactory = new BasePEAPoolFactory(); } buyDependencyPEAPool() { return this.dependencyPEAPoolFactory.create(); } } exports.PEAPoolVendor = PEAPoolVendor;