From 675b8ceda77b6b50f372ab82fe2858047780feda Mon Sep 17 00:00:00 2001 From: Martin Karkowski Date: Mon, 30 Aug 2021 12:41:27 +0200 Subject: [PATCH] Fixing wamo manager --- modules/wamo/src/wamo.basemodule.module.ts | 26 +++++-- modules/wamo/src/wamo.lineManager.module.ts | 4 +- .../src/xetics.functions.ts | 68 ++++++++++++++++--- .../src/xetics.module.ts | 33 ++++----- 4 files changed, 98 insertions(+), 33 deletions(-) diff --git a/modules/wamo/src/wamo.basemodule.module.ts b/modules/wamo/src/wamo.basemodule.module.ts index 2cb6ca0..a249cac 100644 --- a/modules/wamo/src/wamo.basemodule.module.ts +++ b/modules/wamo/src/wamo.basemodule.module.ts @@ -28,6 +28,7 @@ import { IMqttSettings, validFormats } from "../../mqtt-connector/types/interfaces"; +import { JobDTO } from "../../xetics-lean-connector/generated"; import { IXeticsInterfaceClient, MESTask @@ -197,6 +198,8 @@ export class WaMOBaseModule taskFinished: (options: { checkedInWorkpieceCarrier: string; productId: number; + numOfOpenJobs: number + }) => Promise; resetOnInit?: boolean, autoStart?: boolean, @@ -380,6 +383,8 @@ export class WaMOBaseModule // present. if (tasks.length === 0) { tasks = await _this._xetics.updateTasks(); + // Wait until the update arrived here + // await _this._xetics.availableTasks.waitForUpdate(); } // Only if there are tasks we try to get the product id for @@ -404,12 +409,13 @@ export class WaMOBaseModule if (productIdToStartWith) { // We are not running in the demo-mode and we have // a task to start, which we are able to select. - await _this.performCurrentTask(productIdToStartWith); + const numOfOpenJobs = await _this.performCurrentTask(productIdToStartWith); // Now we have finished the task. await settings.taskFinished({ checkedInWorkpieceCarrier: carrierId, - productId: productIdToStartWith + productId: productIdToStartWith, + numOfOpenJobs: numOfOpenJobs as number }); } } @@ -754,7 +760,7 @@ export class WaMOBaseModule @exportMethod({ paramsHasNoCallback: true }) - public async performCurrentTask(productIdToStartWith: number): Promise { + public async performCurrentTask(productIdToStartWith: number): Promise { if (this.connected.getContent()) { const _this = this; @@ -768,6 +774,10 @@ export class WaMOBaseModule } let finished = false; + let taskFinished: false | { + jobsOnStation: JobDTO[]; + jobsOfProduct: number; + } = false; while (!finished) { finished = true; @@ -804,11 +814,12 @@ export class WaMOBaseModule // If we have waited to finish a Task, we are able // to proceed with the task. if (value.active == false && first == false) { - // Unsubscribe the Observer - clearObservers(); + // Unsubscribe the Observer if (value.result) { + clearObservers(); resolve(); } else if (value.error) { + clearObservers(); reject(new Error("An Error is returned from the Processmodul")); } } @@ -872,7 +883,7 @@ export class WaMOBaseModule _this.connector.forcePublish(); // Mark the Task as Finished inside of the MES - const taskFinished = await _this._xetics.finishCurrentTask(); + taskFinished = await _this._xetics.finishCurrentTask(); // Only if the Task has been finished proceed if (taskFinished) { @@ -885,6 +896,7 @@ export class WaMOBaseModule // We have set finished = true to end the // loop finished = (await this._xetics.getCurrentTask())?.lotId != productIdToStartWith; + finished = taskFinished.jobsOnStation.length === 0; } else { _this._logger.error("Failed to finish the Task."); _this.baseInformation.getContent().logical.currentState = "error"; @@ -904,6 +916,8 @@ export class WaMOBaseModule } } + const numOfTask = (taskFinished as any).jobsOfProduct as number; + return numOfTask; } } diff --git a/modules/wamo/src/wamo.lineManager.module.ts b/modules/wamo/src/wamo.lineManager.module.ts index 0bad6df..3b83a76 100644 --- a/modules/wamo/src/wamo.lineManager.module.ts +++ b/modules/wamo/src/wamo.lineManager.module.ts @@ -331,10 +331,12 @@ export class WaMOLineManager extends InjectableNopeBaseModule { taskFinished: async (options: { checkedInWorkpieceCarrier: string; productId: number; + numOfOpenJobs: number; }) => { // Now we need to test, if the product has open tasks, after we have finished // that one - if (options.productId && await this._amountOfTasksForProduct(options.productId) === 1) { + _this._logger.info("Task on Station finished. options =", options); + if (options.numOfOpenJobs <= 0) { await this.carrierMapper.removeProductFromCarrier(options.checkedInWorkpieceCarrier); } }, diff --git a/modules/xetics-lean-connector/src/xetics.functions.ts b/modules/xetics-lean-connector/src/xetics.functions.ts index 5dc8d52..38562a0 100644 --- a/modules/xetics-lean-connector/src/xetics.functions.ts +++ b/modules/xetics-lean-connector/src/xetics.functions.ts @@ -34,7 +34,7 @@ function _cacheFunction(func: (...args) => Promise, id: string) { cache.get(id).set(argsAsKey, result); } - return cache.get(id).get(argsAsKey); + return cache.get(id).get(argsAsKey) as T; }; return exportFunctionToDispatcher(wrapped, { @@ -110,6 +110,12 @@ export const finishTask = exportFunctionToDispatcher(_finishTask, { */ export const amountOfTasksForProduct = _cacheFunction(_amountOfTasksForProduct, "amountOfTasksForProduct"); +export const getCapabilitiesOfStation = _cacheFunction(_getCapabilitiesOfStation, "getCapabilitiesOfStation"); + +export const getAllJobs = _cacheFunction(_getAllJobs, "getAllJobs"); + +export const getNextJobsIfFinished = exportFunctionToDispatcher(_getNextJobsIfFinished, { "id": "getNextJobsIfFinished" }); + const _adaptJobs = _cacheFunction(__adaptJobs, "_adaptJobs"); /** @@ -324,6 +330,48 @@ async function _startTask( return true; } +async function _getCapabilitiesOfStation(station: number | string) { + const id = await getStationId(station); + const result: { + "id": number, + "processTarget": { + "name": string, + "id": number + } + }[] = (await StationControllerService.getEquipment({ id })).capabilities; + + const processIds = result.map(item => item.processTarget.id); + const capabilities = result.map(item => item.processTarget.name); + + return { + processIds, + capabilities + }; +} + +async function _getAllJobs(size = MAX_QUERY_LIST_SIZE) { + return await JobControllerService.getTrackingJobs2({ size }); +} + +/** + * Function to receive the next Jobs of this entity + * + * @author M.Karkowski + * @param {(number | string)} station + * @param {number} jobId + * @return {*} + */ +async function _getNextJobsIfFinished(station: number | string, jobId: number, size = MAX_QUERY_LIST_SIZE) { + const stationId = await getStationId(station); + const processIds = (await getCapabilitiesOfStation(stationId)).processIds; + const nextJobs: JobDTO[] = await JobControllerService.getNextJob({ jobId }); + + return { + jobsOnStation: nextJobs.filter(job => processIds.includes(job.processTarget.id)), + jobsOfProduct: nextJobs.length + }; +} + /** * Function to Mark a Task as finished. * @param station The Station (Name or ID) @@ -342,8 +390,10 @@ async function _finishTask( const stationId = await getStationId(station); - // Get the Active Jobs for the Equipment - const job = await JobControllerService.stopSingleTracking({ + const nextJobs = await getNextJobsIfFinished(stationId, jobId); + + // Finish the next job + await JobControllerService.stopSingleTracking({ requestBody: { jobId, qualityParameters, @@ -358,10 +408,11 @@ async function _finishTask( resetCache([ "getTasksFromMES", "getActiveTasksFromMES", - "amountOfTasksForProduct" + "amountOfTasksForProduct", + "getAllJobs" ]); - return true; + return nextJobs; } /** @@ -371,10 +422,7 @@ async function _finishTask( * @param {(string | number)} product * @return {*} */ -async function _amountOfTasksForProduct(product: string | number,) { - const jobs = await JobControllerService.getTrackingJobs2({ - size: MAX_QUERY_LIST_SIZE - }); - +async function _amountOfTasksForProduct(product: string | number, size = MAX_QUERY_LIST_SIZE) { + const jobs = await getAllJobs(size); return jobs.filter(job => job.lotId == product).length > 0; } \ No newline at end of file diff --git a/modules/xetics-lean-connector/src/xetics.module.ts b/modules/xetics-lean-connector/src/xetics.module.ts index fe9cdad..ccf4358 100644 --- a/modules/xetics-lean-connector/src/xetics.module.ts +++ b/modules/xetics-lean-connector/src/xetics.module.ts @@ -75,7 +75,10 @@ export interface IXeticsInterfaceClient { finishCurrentTask( qualityParameters?: QualityParameterDTO[], traceabilityParameters?: TraceabilityParameterDTO[] - ): Promise; + ): Promise; /** * Start the current Task. @@ -230,6 +233,7 @@ export class XeticsInterfaceClient this.availableTasks.setContent( await getTasksFromMES(this._station) ); + this._logger.info("Updated tasks, because we wont find a matching solution"); _possibleTasks = this.availableTasks .getContent() .filter((task) => task.lotId === id || task.lotAutoId === id); @@ -305,7 +309,9 @@ export class XeticsInterfaceClient // If the task has been sucessfully finished => assign a new Task. if (finished) { - await this.updateTasks(); + // We dont need to check for open task. It is only one Task currently + // active. Multiple Tasks can be active at a time. + await this.updateTasks([]); } // Return the Result. @@ -325,20 +331,18 @@ export class XeticsInterfaceClient @exportMethod({ paramsHasNoCallback: true }) - async updateTasks() { + async updateTasks(activeTasks = null) { let _taskActive = false; - console.time(this.identifier + "-getActiveTasksFromMES"); - - // Check if there exists a currently active Task - // if so => assign this task - const activeTasks = await getActiveTasksFromMES( - this._station - ); - - console.timeEnd(this.identifier + "-getActiveTasksFromMES"); + if (activeTasks == null) { + // Check if there exists a currently active Task + // if so => assign this task + activeTasks = await getActiveTasksFromMES( + this._station + ); + } if (activeTasks.length > 0) { @@ -372,9 +376,6 @@ export class XeticsInterfaceClient this.taskActive.setContent(_taskActive); // Return the whether a task has been assigned. - return ( - this.currentTask.getContent() !== undefined || - this.currentTask.getContent() !== null - ); + return this.availableTasks.getContent(); } }