/*-------------------------------------------------------------------------------------------------------------- * 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, ElementRef, Directive, OnInit, OnDestroy } from '@angular/core'; import { InaxPlcService } from '../../../plc'; import { PlcInputType, PlcAddressType } from '../plcInput'; import { Guid } from '../../../common/src/guid'; import { PlcTextToken, PlcButtonToken, PlcCheckboxToken, PlcRadioToken, PlcStateToken } from '../plcToken'; export enum PlcInputOrder { default = 1, asc = 2, desc = 3 } export class PlcRadioValue { public guid: string = Guid.newGuid(); constructor(public value: string) { } } @Component({ selector: 'plc', templateUrl: './@inax/plcUi/src/plc/plc.component.html', styleUrls: ['./@inax/plcUi/src/plc/plc.component.css'] }) export class PlcComponent implements OnInit, OnDestroy { private _defaultPlcOrder: Array = []; private _sortedBy: string = 'default'; private _sortDirection: PlcInputOrder = PlcInputOrder.default; public plcs: Array = []; public default: string = '(empty)'; public radioValues: Array = [new PlcRadioValue('true'), new PlcRadioValue('false')]; constructor(private _inaxPlc: InaxPlcService) { } ngOnInit() { // this.load(); } ngOnDestroy() { // this.save(); } public addTextbox(plcGroup: string, plcAddress: string, plcId: string, plcWriteperm: string, addressType: string, defaultText: string, minval: any, maxval: any): void { let plc = new PlcTextToken(plcGroup, plcAddress, plcId, plcWriteperm, addressType, defaultText, minval, maxval); this._defaultPlcOrder.push(plc) this.sort(); // console.log('added ' + plc.toString()); } public addButton(plcGroup: string, plcAddress: string, plcId: string, plcWriteperm: string, addressType: string, toggle: string, trueVal: string, falseVal: string, trueValMes: string, falseValMes: string): void { let plc = new PlcButtonToken(plcGroup, plcAddress, plcId, plcWriteperm, addressType, toggle, trueVal, falseVal, trueValMes, falseValMes); this._defaultPlcOrder.push(plc); this.sort(); // console.log('added ' + plc.toString()); } public addCheckbox(plcGroup: string, plcAddress: string, plcId: string, plcWriteperm: string, addressType: string, trueVal: string, falseVal: string, trueValMes: string, falseValMes: string): void { let plc = new PlcCheckboxToken(plcGroup, plcAddress, plcId, plcWriteperm, addressType, trueVal, falseVal, trueValMes, falseValMes); this._defaultPlcOrder.push(plc); this.sort(); // console.log('added ' + plc.toString()); } public addRadio(plcGroup: string, plcAddress: string, plcId: string, plcWriteperm: string, addressType: string, values: Array) { let valueStrings: Array = []; values.forEach((val) => { valueStrings.push(val.value); }); let plc = new PlcRadioToken(plcGroup, plcAddress, plcId, plcWriteperm, addressType, valueStrings); this._defaultPlcOrder.push(plc); this.sort(); // console.log('added ' + plc.toString()); // reset selection in control panel (or should we let it as it is?) this.radioValues = [new PlcRadioValue('true'), new PlcRadioValue('false')]; } public addState(plcGroup: string, plcAddress: string, plcId: string, plcWriteperm: string, addressType: string, trueVal: string, falseVal: string, trueValMes: string, falseValMes: string, interval: number) { let plc = new PlcStateToken(plcGroup, plcAddress, plcId, plcWriteperm, addressType, trueVal, falseVal, trueValMes, falseValMes, interval); this._defaultPlcOrder.push(plc); this.sort(); // console.log('added ' + plc.toString()); } public removePlc(plc: PlcTextToken | PlcButtonToken | PlcCheckboxToken | PlcRadioToken | PlcStateToken): void { this._defaultPlcOrder.splice(this._defaultPlcOrder.indexOf(plc), 1); this.sort(); // console.log('removed ' + plc); } public addRadioValue(): void { this.radioValues.push(new PlcRadioValue('')); } public changeRadioValue(item: PlcRadioValue, value: string): void { this.radioValues[this.radioValues.indexOf(item)].value = value; } public removeRadioValue(prv: PlcRadioValue): void { this.radioValues.splice(this.radioValues.indexOf(prv),1); } public sortBy(column: string): void { console.log('sort by ' + column); if (this._sortedBy === column) { if (this._sortDirection === PlcInputOrder.asc) { // if sorted ascending already, sort descending this._sortDirection = PlcInputOrder.desc; this.sort(); } else { // if sorted descending already, sort default this._sortedBy = 'default'; this._sortDirection = PlcInputOrder.default; this.plcs = this._defaultPlcOrder; } } else { // if sorted default or by different column, sort ascending this._sortDirection = PlcInputOrder.asc; this._sortedBy = column; this.sort(); } } public sort(): void { // sorts by current order let c = this._sortedBy; let d = this._sortDirection; this.plcs = []; this._defaultPlcOrder.forEach(elem => { // create copy of defaultPlcOrder without reference this.plcs.push(elem); });; if (d === PlcInputOrder.asc) { this.plcs.sort((a, b) => { return (a[c] > b[c]) ? 1 : -1 }); // sort asc } else if (d === PlcInputOrder.desc) { this.plcs.sort((a, b) => { return (a[c] < b[c]) ? 1 : -1 }); // sort desc } } private str2Plc(plcString: string): Array { let plcs = new Array(); /* // implement after Tokens let plcStringArray = plcString.split('~~~SEPARATOR~~~'); console.log(plcStringArray); for (let ps in plcStringArray) { let props = ps.split('~p~l~c~'); // double check length if (props.length === 8) { let textInput = (props[5] === 'true'); let plc = new PlcTextToken(props[0],props[1],props[2],props[3],props[4],textInput,props[6]); plcs.push(plc); } else { console.warn('invalid plcs' + props); } }*/ return plcs; } public save(): void { console.log('SAVING'); let StringArray = new Array(); this._defaultPlcOrder.forEach((plc) => { StringArray.push(plc.toString()); }); window.localStorage.setItem('plc-address-storage',StringArray.join('~~~SEPARATOR~~~')); } public load(): void { console.log('LOADING'); let plcs = window.localStorage.getItem('plc-address-storage'); console.log(plcs); if (plcs !== null) { this._defaultPlcOrder = this.str2Plc(plcs); this.sort(); } } public logg(x: string) { console.log(x); } }