UNPKG

@c8y/client

Version:

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

172 lines 5.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AlarmService = void 0; const index_js_1 = require("../core/index.js"); /** * This class allows for managing alarms. */ class AlarmService extends index_js_1.Service { constructor() { super(...arguments); this.baseUrl = 'alarm'; this.listUrl = 'alarms'; this.propertyName = 'alarms'; this.channel = '/alarms/*'; } /** * Gets the details of selected alarms. * * @param {string|number|IIdentified} entityOrId Entity or Id of the entity. * * @returns Response wrapped in [[IResult]] * * **Example** * ```typescript * * const alarmId: number = 1; * * (async () => { * const {data, res} = await alarmService.detail(alarmId); * })(); * ``` */ async detail(entityOrId) { return super.detail(entityOrId); } /** * Creates a new alarm. * * @param {IAlarm} entity Alarm object with mandantory fragments. * * @returns Response wrapped in [[IResult]] * * **Example** * ```typescript * * const mandantoryObject: IAlarm = { * severity: Severity.CRITICAL, * source: device, * text: 'I am an Alarm!', * time: '2018-05-02T10:08:00Z', * type: 'device-type-here', * }; * * (async () => { * const {data, res} = await alarmService.create(mandantoryObject); * })(); * ``` */ async create(entity) { return super.create(entity); } /** * Updates alarm data. * * @param {Partial<IAlarm>} entity Partial alarm to update, must have `id`. * * @returns Response wrapped in [[IResult]] * * **Example** * ```typescript * * const partialUpdateObject: Partial<IAlarm> = { * id: 123, * severity: Severity.MINOR, * source: device, * text: 'Changed Alarm!' * }; * * (async () => { * const {data, res} = await alarmService.update(partialUpdateObject); * })(); * ``` */ async update(entity) { return super.update(entity); } /** * Updates alarm data in bulk and with additional query filters. * * @param {Partial<IAlarm>} entity Partial alarm object. * @param {Record<string, string | number | boolean>} filters Additional query filters. * * @returns Response in form of { IFetchResponse } * * **Example** * ```typescript * * const partialUpdateObject: Partial<IAlarm> = { * status: AlarmStatus.CLEARED * }; * * const additionalFilters: Record<string, string | number | boolean> = { * resolved: false, * severity: Severity.MINOR * }; * * (async () => { * const response = await alarmService.updateBulk(partialUpdateObject, additionalFilters); * })(); * ``` * In this example, every unresolved alarm that has a severity of `Severity.MINOR` will be updated with a status of `AlarmStatus.CLEARED`. */ async updateBulk(entity, filters) { const params = new URLSearchParams(Object.entries(filters).map(([key, val]) => [key, String(val)])); const url = this.getDetailUrl(entity).replace(/\/$/, '') + `?${params.toString()}`; const method = 'PUT'; const body = JSON.stringify(this.onBeforeUpdate(entity)); const headers = { 'content-type': 'application/json', accept: 'application/json' }; return await this.fetch(url, this.changeFetchOptions({ method, body, headers }, url)); } /** * Gets the list of alarms filtered by parameters. * * @returns Response wrapped in [[IResultList]] * * @param {AlarmQueryFilter} filter Filters to query alarms. * * **Example** * ```typescript * * const filter: AlarmQueryFilter = { * severity: Severity.MAJOR, * pageSize: 100, * withTotalPages: true * }; * * (async () => { * const {data, res, paging} = await alarmService.list(filter); * })(); * ``` */ async list(filter = {}) { return super.list(filter); } /** * Gets the number of alarms based on the filter criteria. * * @returns Response wrapped in [[IResultList]] * * @param {AlarmQueryFilter} filter Filters to query alarms. * * **Example** * ```typescript * * const filter: AlarmQueryFilter = { * severity: Severity.MAJOR, * }; * * (async () => { * const {data, res} = await alarmService.count(filter); * })(); * ``` */ async count(filter = {}) { const url = `${this.listUrl}/count`; const res = await this.fetch(url, this.changeFetchOptions({ params: filter }, url)); const data = await res.json(); return { res, data }; } } exports.AlarmService = AlarmService; //# sourceMappingURL=AlarmService.js.map