/*-------------------------------------------------------------------------------------------------------------- * Copyright (c) insite-gmbh. All rights reserved. * Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------------------------*/ import { Component, OnInit, OnDestroy } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { InaxAlarmService, AlarmListChangeEvent, IHmiAlarmEntry } from '../../../../@inax/alarm'; import { InaxTranslateService } from '../../../../@inax/translate'; import { Subscription } from 'rxjs/Rx'; import { MotionUiResourceManager } from './../resources/resource.service'; import { InaxMotionService } from './../../../motion/src/motion'; import { Subject } from 'rxjs/Subject'; import { DataChangeEvent } from '../../../plc/src/domain/dataChangedEvent'; import { IPlcEventProxy } from '../../../plc/src/domain/plcEventProxy'; import { IMotionEx, ViewMode } from '../../../motion/src/domain'; import { InaxMotionTextsService } from '../../../motion/src/motionTexts'; import { Converter } from '../../../plc/src/converter'; @Component({ selector: 'motion', templateUrl: './@inax/motionUi/src/motion/motion.component.html', styleUrls: ['./@inax/motionUi/src/motion/motion.component.css'] }) export class MotionComponent implements OnInit, OnDestroy { constructor(private _inaxMotion: InaxMotionService, private _inaxTranslate: InaxTranslateService, private _route: ActivatedRoute, private _inaxMotionTexts: InaxMotionTextsService) { } private _connectionError: boolean = false; private _entriesPerPage: number = 8; private _neededHeaderVariables: Array = ["Header.States.CheckSumInvalid", "Header.States.UpdateRequested", "Header.Commands.UpdateAllowed", "Header.Commands.AllSlotsLocked", "Header.Commands.NavigationLocked", "Header.Commands.SafeVersionInvalid"]; private _viewMode: ViewMode = ViewMode.Symbolic; private _stateViewIsVisible: boolean; private _headerEventProxy: IPlcEventProxy; private _checksumInvalid: boolean; private _updateRequested: boolean; private _updateAllowed: boolean; private _allSlotsLocked: boolean; private _navigationLocked: boolean; private _safeVersionInvalid: boolean; private _plcId: string; private _unit: number; private _unitDescription: string = ""; private _motions: Array = []; private _currentMotions: Array = []; private _page: number = 1; private _selectedMotion: IMotionEx; private _numberOfPages:number; public firstPage:number = 1; public get connectionError():boolean{ return this._connectionError; } public get unit(): string { return String(this._unit); } public get unitDescription(): string { return this._unitDescription; } public get motions(): Array { return this._currentMotions; } public get selectedMotion(): IMotionEx { return this._selectedMotion; } public get checkSumInvalid(): boolean { return this._checksumInvalid; } public get updateRequested(): boolean { return this._updateRequested; } public get updateAllowed(): boolean { return this._updateAllowed; } public get allSlotsLocked(): boolean { return this._allSlotsLocked; } public get navigationLocked(): boolean { return this._navigationLocked; } public get safeVersionInvalid(): boolean { return this._safeVersionInvalid; } public get viewMode(): ViewMode { return this._viewMode; } public get stateViewIsVisible(): boolean { return this._stateViewIsVisible; } public get isSymbolicViewState():boolean{ return this.viewMode == ViewMode.Symbolic; } public get numberOfPages():number{ return this._numberOfPages; } ngOnInit() { //subscribe to the services this._route.params.subscribe((params) => { //get parameters from route let plc = params["plc"] || ""; let unit = params["unit"] || 0; let entries = params["entries"] || 8; let plcChanged = this._plcId != plc; let unitChanged = this._unit != unit; let entriesChanged = this._entriesPerPage != entries; // if parameters are changing, update page if (plcChanged || unitChanged || entriesChanged) { this._plcId = plc; this._unit = unit; this._entriesPerPage = entries; if (plcChanged) this.createHeaderProxy(); if (plcChanged || unitChanged) this.determineMotions(); else this.filterMotions(); } }); } ngOnDestroy() { //unsubscribe from the services if (this._headerEventProxy != null) this._headerEventProxy.dispose(); } public onToggleViewMode() { if (this._viewMode == ViewMode.Symbolic) this._viewMode = ViewMode.Absolute; else this._viewMode = ViewMode.Symbolic; } public onToggleStateView() { this._stateViewIsVisible = !this._stateViewIsVisible; } public onpageChanged(page: number) { if (this._page != page) { this._page = page; this.filterMotions(); } } public onActivateLine(motion: IMotionEx) { this._selectedMotion = motion; } public onDeactivateLine(motion: IMotionEx) { if(this._selectedMotion == motion) this._selectedMotion = null; } private createHeaderProxy() { try { this._connectionError = false; if (this._headerEventProxy != null) { this._headerEventProxy.dispose(); this._headerEventProxy = null; } this._headerEventProxy = this._inaxMotion.createSafeBlockHeaderProxy(this._plcId, this._neededHeaderVariables); this._headerEventProxy.ChangeEvent.subscribe( (dce: DataChangeEvent) => this.updateProperties(dce), (error: any) => { console.warn("Attempt to join channel failed!", error); } ); } catch(error){ this._connectionError = true; console.warn("Attempt to join channel failed!", error); } } private async determineMotions() { let motions = await this._inaxMotion.getMotionsForBlockAsync(this._plcId, this._unit); this._numberOfPages = Math.floor(motions.length / this._entriesPerPage) + 1; this._motions = motions != null ? motions : []; this.updateUnitDescription(); this.filterMotions(); } private filterMotions() { let start: number = (this._page - 1) * this._entriesPerPage; let end: number = start + this._entriesPerPage; this._currentMotions = this._motions.slice(start, end); } private updateProperties(dce: DataChangeEvent) { try { if(this._inaxMotion.MappingName == dce.Mapping){ for (let [key, value] of Converter.flatten(dce.Data)) { if (key == "Header.States.CheckSumInvalid") { this._checksumInvalid = value; } else if (key == "Header.States.UpdateRequested") { this._updateRequested = value; } else if (key == "Header.Commands.UpdateAllowed") { this._updateAllowed = value; } else if (key == "Header.Commands.AllSlotsLocked") { this._allSlotsLocked = value; } else if (key == "Header.Commands.NavigationLocked") { this._navigationLocked = value; } else if (key == "Header.Commands.SafeVersionInvalid") { this._safeVersionInvalid = value; } } } } catch (error) { console.warn(`Error updateing data! ${error}`); } } private updateUnitDescription() { if (this._motions.length > 0) { this._unitDescription = this._motions[0].MappingName.split(".")[0]; } else { this._unitDescription = ""; } } }