@c8y/client
Version:
Client application programming interface to access the Cumulocity IoT-Platform REST services.
193 lines • 5.89 kB
JavaScript
import { __awaiter } from "tslib";
import { Service } from '../core/index.js';
/**
* This class allows for managing alarms.
*/
export class AlarmService extends 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);
* })();
* ```
*/
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 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);
* })();
* ```
*/
create(entity) {
const _super = Object.create(null, {
create: { get: () => super.create }
});
return __awaiter(this, void 0, void 0, function* () {
return _super.create.call(this, 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);
* })();
* ```
*/
update(entity) {
const _super = Object.create(null, {
update: { get: () => super.update }
});
return __awaiter(this, void 0, void 0, function* () {
return _super.update.call(this, 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`.
*/
updateBulk(entity, filters) {
return __awaiter(this, void 0, void 0, function* () {
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 yield 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);
* })();
* ```
*/
list() {
const _super = Object.create(null, {
list: { get: () => super.list }
});
return __awaiter(this, arguments, void 0, function* (filter = {}) {
return _super.list.call(this, 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);
* })();
* ```
*/
count() {
return __awaiter(this, arguments, void 0, function* (filter = {}) {
const url = `${this.listUrl}/count`;
const res = yield this.fetch(url, this.changeFetchOptions({ params: filter }, url));
const data = yield res.json();
return { res, data };
});
}
}
//# sourceMappingURL=AlarmService.js.map