| /******************************************************************************** |
| * Copyright (c) 2015-2018 Contributors to the Eclipse Foundation |
| * |
| * See the NOTICE file(s) distributed with this work for additional |
| * information regarding copyright ownership. |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| ********************************************************************************/ |
| |
| import { HttpClient, HttpHeaders } from '@angular/common/http'; |
| import { Injectable } from '@angular/core'; |
| |
| import { plainToClass } from 'class-transformer'; |
| import { map, tap, catchError } from 'rxjs/operators'; |
| |
| import { HttpErrorHandler } from '../../core/http-error-handler'; |
| import { PropertyService } from '../../core/property.service'; |
| import { Node } from '../../navigator/node'; |
| import { MeasuredValuesResponse, MeasuredValues, PreviewValueList } from '../model/chartviewer.model'; |
| |
| export function getDataArray(m: MeasuredValues) { |
| if (m.scalarType === 'INTEGER') { |
| return m.integerArray; |
| } else if (m.scalarType === 'FLOAT') { |
| return m.floatArray; |
| } else if (m.scalarType === 'DOUBLE') { |
| return m.doubleArray; |
| } else if (m.scalarType === 'DATE') { |
| return m.dateArray; |
| } else if (m.scalarType === 'SHORT') { |
| return m.shortArray; |
| } else if (m.scalarType === 'BOOLEAN') { |
| return m.booleanArray; |
| } |
| } |
| |
| @Injectable({ |
| providedIn: 'root' |
| }) |
| export class ChartViewerDataService { |
| private _contextUrl: string; |
| |
| private httpOptions = { |
| headers: new HttpHeaders({ |
| 'Content-Type': 'application/json', |
| 'Accept': 'application/json' |
| }) |
| }; |
| |
| constructor(private http: HttpClient, |
| private httpErrorHandler: HttpErrorHandler, |
| private _prop: PropertyService) { |
| this._contextUrl = _prop.getUrl('mdm/environments'); |
| } |
| |
| loadMeasuredValues(channelGroup: Node, channels: Node[], startIndex = 0, requestSize = 0) { |
| let readRequest = { |
| 'channelGroupId': channelGroup.id, |
| 'channelIds': channels.map(channel => channel.id), |
| 'start_index': startIndex, |
| 'request_size': requestSize, |
| 'valuesMode': 'CALCULATED' |
| }; |
| |
| return this.http.post<MeasuredValuesResponse>(this._contextUrl + '/' + channelGroup.sourceName + '/values/read', |
| readRequest, this.httpOptions) |
| .pipe( |
| map(res => plainToClass(MeasuredValues, res.values)), |
| map(measurementValues => measurementValues.sort((a, b) => a.name.localeCompare(b.name))), |
| // tap(r => console.log(r)), |
| catchError(this.httpErrorHandler.handleError) |
| ); |
| } |
| |
| loadPreviewValues(channelGroup: Node, channels: Node[], chunks: number, startIndex = 0, requestSize = 0) { |
| let readRequest = { |
| 'channelGroupId': channelGroup.id, |
| 'channelIds': channels.map(channel => channel.id), |
| 'start_index': startIndex, |
| 'request_size': requestSize, |
| 'valuesMode': 'CALCULATED' |
| }; |
| let previewRequest = { |
| 'numberOfChunks': chunks, |
| 'readRequest': readRequest |
| }; |
| |
| return this.http.post<PreviewValueList>(this._contextUrl + '/' + channelGroup.sourceName + '/values/preview', |
| previewRequest, this.httpOptions) |
| .pipe( |
| map(res => plainToClass(MeasuredValues, res.avg)), |
| // tap(r => console.log(r)), |
| catchError(this.httpErrorHandler.handleError) |
| ); |
| } |
| |
| // readMeasuredValuesMetadata(channelGroup: Node, channels: Node[]) { |
| // let readRequest = { |
| // 'channelGroupId': channelGroup.id, |
| // 'channelIds': channels.map(channel => channel.id), |
| // 'valuesMode': 'CALCULATED' |
| // }; |
| |
| // return this.http.post<MeasuredValuesResponse>(this._contextUrl + '/' + channelGroup.sourceName + '/values/read', |
| // readRequest, this.httpOptions) |
| // .pipe( |
| // map(res => plainToClass(MeasuredValues, res.values)), |
| // map(measurementValues => measurementValues.sort((a, b) => a.name.localeCompare(b.name))), |
| // tap(r => console.log(r))); |
| // } |
| |
| /** |
| * @Todo workarround to get axis type. This schould be possible without accessign data. Requires RESTAPI changes. |
| */ |
| readAxisType(channelGroup: Node) { |
| let readRequest = { |
| 'channelGroupId': channelGroup.id, |
| 'start_index': 0, |
| 'request_size': 1, |
| 'valuesMode': 'CALCULATED' |
| }; |
| |
| return this.http.post<MeasuredValuesResponse>(this._contextUrl + '/' + channelGroup.sourceName + '/values/read', |
| readRequest, this.httpOptions) |
| .pipe( |
| map(res => plainToClass(MeasuredValues, res.values)), |
| map(measurementValues => measurementValues.sort((a, b) => a.name.localeCompare(b.name))), |
| // tap(r => console.log(r)) |
| ); |
| } |
| |
| getNumberOfRows(channelGroup: Node) { |
| let attr = channelGroup.attributes.find(a => a.name === 'SubMatrixNoRows'); |
| if (attr === undefined) { |
| // TODO |
| console.log('Could not find attribute SubMatrixNoRows on ChannelGroup with id ' + channelGroup.id); |
| return; |
| } |
| return parseInt(<string> attr.value, 10); |
| } |
| } |