UNPKG

@c8y/client

Version:

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

211 lines 6.48 kB
import { __awaiter } from "tslib"; import { Service } from '../core/index.js'; /** * This class allows for managing measurements. */ export class MeasurementService extends 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); * })(); * ``` */ detail(entityOrId) { const _super = Object.create(null, { detail: { get: () => super.detail } }); return __awaiter(this, void 0, void 0, function* () { return _super.detail.call(this, 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); * })(); * ``` */ create(entity) { const _super = Object.create(null, { create: { get: () => super.create } }); return __awaiter(this, void 0, void 0, function* () { return _super.create.call(this, 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); * })(); * ``` */ list() { const _super = Object.create(null, { list: { get: () => super.list } }); return __awaiter(this, arguments, void 0, function* (filter = {}) { return _super.list.call(this, 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); * })(); * ``` */ delete(entityOrId) { const _super = Object.create(null, { delete: { get: () => super.delete } }); return __awaiter(this, void 0, void 0, function* () { return _super.delete.call(this, 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); * })(); * ``` */ listSeries(params) { return __awaiter(this, void 0, void 0, function* () { const url = `${this.baseUrl}/${this.listUrl}/series`; const res = yield this.client.fetch(url, { params }); const data = yield 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. */ getMeasurementsFile(params, headers) { return __awaiter(this, void 0, void 0, function* () { const url = `${this.baseUrl}/${this.listUrl}/`; return yield 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; } } //# sourceMappingURL=MeasurementService.js.map