/*-------------------------------------------------------------------------------------------------------------- * 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, Input, OnInit, Output, EventEmitter } from '@angular/core'; import { InaxPlcService, DataChangeEvent, IPlcEventProxy, PlcValidator } from '../../../plc'; import { InaxSignalR, Guid } from '../../../common'; import { PlcItem } from '../../../../app/components/plcviewer/plcviewer.component'; import { InaxLoggerService } from '../../../logger/src/logger'; import { PlcInput, PlcInputType, PlcAddressType } from '../plcInput'; @Component(Object.assign({ selector: 'plc-state', templateUrl: './@inax/plcUi/src/plcState/plc-state.component.html', styleUrls: ['./@inax/plcUi/src/plcState/plc-state.component.css'] }, PlcInput.metaData)) export class PlcStateComponent extends PlcInput { public intervalState: boolean = false; private _timer: NodeJS.Timer; private _interval: number = 500; constructor(inaxPlc: InaxPlcService) { super(inaxPlc); } /* SPECIFIC INPUTS */ @Input() trueValue: string = 'true'; @Input() falseValue: string = 'false'; @Input() trueValueMessage: string = 'Start'; @Input() falseValueMessage: string = 'Stop'; @Input() interval: string = '500'; public get Value(): string { return this._value; } public set Value(v: string) { } ngOnInit(): void { this.initializeDefaultProperties(); this.initialize(); this.subscribe(); } ngOnDestroy(): void { this.unsubscribe(); clearTimeout(this._timer); } /** * set the number specified by the string @Input() interval * if interval is not 0, the state-display will begin to blink */ public initialize(): void { this._interval = Number(this.interval); if (this._interval > 0) { this.blink(); } } /** * while the current value equals trueValue, this function causes the intervalState * to alternate (resulting in the display blinking) * otherwise, the intervalState will always be set to false, so the display does not * blink and is turned off in case the value changed while it was on * * the function sets a timeout with the delay in milliseconds specified by this._interval * which will then call this function again */ public blink(): void { // while value equals trueValue, blink if (this._value === this.trueValue) { this.intervalState = !this.intervalState; } else { this.intervalState = false; } this._timer = setTimeout(() => this.blink(), this._interval); // console.log('BLINK'); } /** * updates the current value * if interval is 0 (=> blinking is disabled), the state of the display is changed * to resemble the value (so it is 'on' if value === this.trueValue) */ public updateValue(value: string): void { this._value = value; // if blinking is disabled, update state of display if (this._interval === 0) { this.intervalState = (value === this.trueValue); } } }