import {Component, OnInit, Input} from "@angular/core"; import {Http, Response} from "@angular/http"; import { InaxPlcService, DataChangeEvent, IPlcEventProxy } from '../../../@inax/plc'; import { InaxLoggerService } from '../../../@inax/logger'; export class PlcItem { active: boolean; name: string; value: any; isSubscribed: boolean; constructor(name: string, value: any) { this.name = name; this.value = value; } } @Component({ selector: 'plc-viewer', templateUrl: 'app/components/plcviewer/plcviewer.component.html', styleUrls: ['app/components/plcviewer/plcviewer.component.css'] }) export class PlcViewerComponent implements OnInit { private _proxy: IPlcEventProxy; public state: string = "not connected"; public area: string = "DB1112"; public address: string = "W0"; private groupName: string = "XXX"; public plcId: string = ""; public mapping: string = "DB_BST1_Organisation"; public varname: string = "This"; public selectedSymbol :string = "?"; public reads: Array = []; public subscribed: Array = []; constructor(private _inaxPlc: InaxPlcService, private _logger: InaxLoggerService) { } public ngOnInit() { // this._webPacService.subscribeConnectionChanged().subscribe((state) => { // this.state = state.Connected ? "connected" : "not connected"; // }); } public subscribe(item: PlcItem) { this.subscribed.push(item); item.isSubscribed = true; this.updateWatcher(); } public unsubscribe(item: PlcItem) { let idx = this.subscribed.indexOf(item); if(idx != -1){ this.subscribed.splice(idx,1); item.isSubscribed = false; this.updateWatcher(); } } private updateWatcher(){ if(this._proxy != null) this._proxy.dispose(); this._proxy = this._inaxPlc.createEventProxy(this.groupName, this.plcId, this.mapping, this.subscribed.map(x => x.name)); this._proxy.ChangeEvent.subscribe( (dce: DataChangeEvent) => this.dataUpdated(dce), (error: any) => { this.subscribed.forEach(item => item.isSubscribed = false ); console.warn("Attempt to join channel failed!", error); } ); } public onRead() { try { this.selectedSymbol = "?"; this._inaxPlc.read(this.plcId, this.mapping, [this.varname]).subscribe((data: any) => { try { if (data != null && data.This != null) { this.selectedSymbol = this.mapping; this.reads = new Array(); this._logger.logInfo(`successfully red data from ${this.mapping}`,true); this.resolveObject(this.reads, data.This, null, false); } else { console.warn("no data red!"); } } catch (error) { console.warn("no data red!"); } }); } catch (error) { console.error(error); } } public onWrite(item: PlcItem) { try { this._inaxPlc.write(this.plcId, this.mapping, item.name, item.value).subscribe((data: any) => { console.warn(data); }); } catch (error) { console.error(error); } } private dataUpdated(ev: DataChangeEvent): void { this.subscribed.forEach(updated => { try { if (updated != null) { console.warn(`onEvent - ${ev.Mapping}.${updated.name}`); eval("updated.value = ev.Data." + updated.name); } } catch (error) { console.warn(`Error updateing data! ${error}`); } }); } private resolveObject(result: Array, object: any, prefix: string, onlyVarNames: boolean) { let isArray = Array.isArray(object); //no prefix -> set it to null to handle only one value in the loop if (prefix == "") prefix = null; //onlyVarNames is true -> if this object isn't an array, clear prefix if (prefix != null && (!isArray && onlyVarNames)) prefix = null; //loop through the object for (var property in object) { if (object.hasOwnProperty(property)) { //get name and data var value = object[property]; var name = prefix != null ? isArray ? prefix + "[" + (Number(property) + 1) + "]" : prefix + "." + property : isArray ? "[" + (Number(property) + 1) + "]" : property; if (value === Object(value)) { //property is an object, so step up to handle these properties this.resolveObject(result, value, name, onlyVarNames); } else { //normal property, so save it to resultSet result.push(new PlcItem(name, String(value))); } } } } }