UNPKG

@c8y/client

Version:

Client application programming interface to access the Cumulocity IoT-Platform REST services.

190 lines 5.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MeasurementService = void 0; const index_js_1 = require("../core/index.js"); /** * This class allows for managing measurements. */ class MeasurementService extends index_js_1.Service { constructor() { super(...arguments); this.baseUrl = 'measurement'; this.listUrl = 'measurements'; this.propertyName = 'measurements'; this.channel = '/measurements/*'; } /** * Gets the details of selected measurement. * * @param {string|number|IIdentified} entityOrId Entity or Id of the entity. * * @returns Response wrapped in [[IResult]] * * @deprecated As of version 10.16.0.0 and the usage of the time series database, * reading a single measurement via id is not supported any more. * * **Example** * ```typescript * * const measurementId: number = 1; * * (async () => { * const {data, res} = await measurementService.detail(measurementId); * })(); * ``` */ async detail(entityOrId) { return super.detail(entityOrId); } /** * Creates a new measurement. * * @param {Partial<IMeasurementCreate>} entity At least sourceId is mandantory. * * @returns Response wrapped in [[IResult]] * * **Example** * ```typescript * * const mandantoryObject: Partial<IMeasurementCreate> = { * sourceId: device.id, * fragment: { series: { unit: '%', value: 51 } }, * }; * * (async () => { * const {data, res} = await measurementService.create(mandantoryObject); * })(); * ``` */ async create(entity) { return super.create(this.onBeforeCreate(entity)); } /** * Gets the list of measurements filtered by parameters. * * @returns Response wrapped in [[IResultList]] * * @param {object} filter Object containing filters for querying measurements. * * **Example** * ```typescript * * const filter: object = { * pageSize: 100, * withTotalPages: true * }; * * (async () => { * const {data, res, paging} = await measurementService.list(filter); * })(); * ``` */ async list(filter = {}) { return super.list(filter); } /** * Removes a measurement with given id. * * @returns Response wrapped in [[IResult]] * * @param {string | number | IIdentified} entityOrId * * @deprecated As of version 10.16.0.0 and the usage of the time series database, * deleting a single measurement via id is not supported any more. * * **Example** * ```typescript * * const id: number = 1; * * (async () => { * const {data, res} = await measurementService.delete(id); * })(); * ``` */ async delete(entityOrId) { return super.delete(entityOrId); } /** * Gets the list of series in a measurement filtered by parameters. * * @returns Response wrapped in [[IResult]] * * @param {object} filter Object containing filters for querying measurements. * * **Example** * ```typescript * * const filter: object = { * dateFrom: '2018-02-06T10:43:55.077Z', * dateTo: '2018-02-06T10:50:55.077Z', * source: device.id * }; * * (async () => { * const {data, res} = await measurementService.listSeries(filter); * })(); * ``` */ async listSeries(params) { const url = `${this.baseUrl}/${this.listUrl}/series`; const res = await this.client.fetch(url, { params }); const data = await res.json(); return { res, data }; } /** * Retrieves the measurement file based on the provided filter parameters and headers, but only if the response is 200. * If the response is 202, the file is processed in the background and the file is sent by email. * * **Example** * ```typescript * * const filter: IFetchResponse = { * dateFrom: "2024-08-11T12:13:00+02:00" * dateTo: "2024-08-12T12:15:00+02:00" * source: "32666427" * valueFragmentSeries: "accelerationX" * valueFragmentType: "c8y_Acceleration" * }; * * const headers = { * accept: 'text/csv' * } * * (async () => { * const response = await measurementService.getMeasurementsFile(filter, headers); * if (response.status === 200) { * const blob = await response.blob(); * } * })(); * ``` * * @param filter Object containing filters for querying measurements. * @param headers The headers for the request. * @returns A promise that resolves to the fetch response. */ async getMeasurementsFile(params, headers) { const url = `${this.baseUrl}/${this.listUrl}/`; return await this.client.fetch(url, { params, headers }); } onBeforeCreate(entity) { if (!entity.time) { entity.time = new Date().toISOString(); } if (!entity.type) { entity.type = 'c8y_Measurement'; } if (entity.sourceId) { const { sourceId } = entity; delete entity.sourceId; if (!entity.source) { entity.source = { id: String(sourceId) }; } } return entity; } } exports.MeasurementService = MeasurementService; //# sourceMappingURL=MeasurementService.js.map