UNPKG

@c8y/client

Version:

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

827 lines • 28.8 kB
import { __awaiter } from "tslib"; import { Service, QueriesUtil } from '../core/index.js'; import { InventoryBinaryService } from './InventoryBinaryService.js'; /** * Possible types of a child. */ export var ChildType; (function (ChildType) { ChildType["ASSETS"] = "childAssets"; ChildType["DEVICES"] = "childDevices"; ChildType["ADDITIONS"] = "childAdditions"; })(ChildType || (ChildType = {})); /** * This class allows for managing managed objects and different child types, see [[ChildType]]. */ export class InventoryService extends Service { constructor(client, realtime) { super(client, realtime); this.baseUrl = 'inventory'; this.listUrl = 'managedObjects'; this.propertyName = 'managedObjects'; this.channel = '/managedobjects/*'; this.inventoriesQueryParamName = 'query'; this.devicesQueryParamName = 'q'; this.queriesUtil = new QueriesUtil(); this.binary = new InventoryBinaryService(client); } /** * Gets the details of managed object * * @param {IdReference} managedObjectOrId ManagedObject or Id of the ManagedObject. * @param {object} filter Filter object. * * @returns Response wrapped in [[IResult]] * * **Example** * ```typescript * * const managedObjId: number = 1; * const filter = { withChildren: false }; * * (async () => { * const {data, res} = await inventoryService.detail(managedObjId, filter); * })(); * ``` */ detail(managedObjectOrId_1) { const _super = Object.create(null, { detail: { get: () => super.detail } }); return __awaiter(this, arguments, void 0, function* (managedObjectOrId, filter = {}) { return _super.detail.call(this, managedObjectOrId, filter); }); } /** * Creates a new managed object. * * @param {Partial<IManagedObject>} managedObject * * @returns Response wrapped in [[IResult]] * * **Example** * ```typescript * * const partialManagedObj: Partial<IManagedObject> = { * customFragment: 'yourData' * }; * * (async () => { * const {data, res} = await inventoryService.create(partialManagedObj); * })(); * ``` */ create(managedObject) { const _super = Object.create(null, { create: { get: () => super.create } }); return __awaiter(this, void 0, void 0, function* () { return _super.create.call(this, managedObject); }); } /** * Updates managed object data. * * @param {Partial<IManagedObject>} managedObject Managed object is partially updatable. * * @returns Response wrapped in [[IResult]] * * **Example** * ```typescript * * const partialUpdateObject: Partial<IManagedObject> = { * customFragment: 'Changed data', * name: 'Name' * }; * * (async () => { * const {data, res} = await inventoryService.update(partialUpdateObject); * })(); * ``` */ update(managedObject) { const _super = Object.create(null, { update: { get: () => super.update } }); return __awaiter(this, void 0, void 0, function* () { return _super.update.call(this, managedObject); }); } /** * Gets the list of managed objects filtered by parameters. * * @returns Response wrapped in [[IResultList]] * * @param {object} filter Object containing filters for querying managed objects. * * **Example** * ```typescript * * const filter: object = { * pageSize: 100, * withTotalPages: true * }; * * (async () => { * const {data, res, paging} = await inventoryService.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 total count of managed objects filtered by parameters. * * @returns Response wrapped in [[IResultList]] * * @param {object} filter Object containing filters for querying managed objects. * * **Example** * ```typescript * * const filter: object = { * type: 'c8y_MQTTDevice' * }; * * (async () => { * const {data, res} = await inventoryService.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 }; }); } /** * Gets the list of all managed objects filtered and sorted by given query. * * @returns Response wrapped in [[IResultList]] * * @param {object} filter Object containing filters for querying managed objects. * * **Example** * ```typescript * * const filter: object = { * pageSize: 100, * withTotalPages: true * }; * * const query = { * name: 'MY-NAM*' * } * * (async () => { * const {data, res, paging} = await inventoryService.listQuery(query, filter); * })(); * ``` */ listQuery(query_1) { const _super = Object.create(null, { list: { get: () => super.list } }); return __awaiter(this, arguments, void 0, function* (query, filter = {}) { filter[this.inventoriesQueryParamName] = this.queriesUtil.buildQuery(query); return _super.list.call(this, filter); }); } /** * Gets the list of all devices filtered and sorted by given query. * * @returns Response wrapped in [[IResultList]] * * @param {object} filter Object containing filters for querying devices. * * **Example** * ```typescript * * const filter: object = { * pageSize: 100, * withTotalPages: true * }; * * const query = { * name: 'MY-NAM*' * } * * (async () => { * const {data, res, paging} = await inventoryService.listQueryDevices(query, filter); * })(); * ``` */ listQueryDevices(query_1) { const _super = Object.create(null, { list: { get: () => super.list } }); return __awaiter(this, arguments, void 0, function* (query, filter = {}) { filter[this.devicesQueryParamName] = this.queriesUtil.buildQuery(query); return _super.list.call(this, filter); }); } /** * Removes managed object with given id. * * @returns Response wrapped in [[IResult]] * * @param {IdReference} managedObjectOrId ManagedObject or Id of the ManagedObject. * @param {object} params Additional query params. * * **Example** * ```typescript * * const managedObjectId: number = 1; * const params: any = { * cascade: true * } * * (async () => { * const {data, res} = await inventoryService.delete(managedObjectId, params); * })(); * ``` */ delete(managedObjectOrId_1) { const _super = Object.create(null, { delete: { get: () => super.delete } }); return __awaiter(this, arguments, void 0, function* (managedObjectOrId, params = {}) { return _super.delete.call(this, managedObjectOrId, params); }); } /** * Gets a list of child additions from a given managed object (parent) * * @returns Response wrapped in [[IResultList]] * * @param {IdReference} parentReference * @param {object} filter * * **Example** * ```typescript * const parentReferenceId: IdReference = 1; * * const filter: object = { * pageSize: 100, * withTotalPages: true * }; * * (async () => { * const {data, res, paging} = await inventoryService.childAdditionsList(parentReferenceId, filter); * })(); * ``` */ childAdditionsList(parentReference_1) { return __awaiter(this, arguments, void 0, function* (parentReference, filter = {}) { return this.listChildren(ChildType.ADDITIONS, parentReference, filter); }); } /** * Creates a new managed object as child addition to another managed object (parent) * * @returns Response wrapped in [[IResult]] * * @param {Partial<IManagedObject>} managedObject * @param {IdReference} parentReference * * **Example** * ```typescript * * const mOAsChildAddition: Partial<IManagedObject> = { * name: 'Child addition MO', * type: 'new type', * ... * }; * * // This is the identifier of the managed object which should be the parent of * // mOAsChildAddition, see above. * const parentReferenceId: number = 1; * * (async () => { * const {data, res} = await inventoryService.childAdditionsCreate(mOAsChildAddition, parentReferenceId); * })(); * ``` */ childAdditionsCreate(managedObject, parentReference) { return __awaiter(this, void 0, void 0, function* () { return this.createChild(ChildType.ADDITIONS, managedObject, parentReference); }); } /** * Adds an existing managed object as child addition to another managed object (parent) * * @returns Response wrapped in [[IResult]] * * @param {IdReference} childReference * @param {IdReference} parentReference * * **Example** * ```typescript * * const childRef: number = 2; * const parentReferenceId: number = 1; * * (async () => { * const {data, res} = await inventoryService.childAdditionsCreate(childRef, parentReferenceId); * })(); * ``` */ childAdditionsAdd(childReference, parentReference) { return __awaiter(this, void 0, void 0, function* () { return this.addChild(ChildType.ADDITIONS, childReference, parentReference); }); } /** * Adds bulk of existing managed objects as child addition to another managed object (parent). * * @returns Response wrapped in array of [[IResult]] * * @param {IdReference[]} childReference List of existing managed objects IDs that should be added to another managed object (parent). * @param {IdReference} parentReference * * **Example** * ```typescript * * const childAdditionsRefIds: string[] = ['2', '3']; * const parentReferenceId: number = 1; * * (async () => { * const {data, res} = await inventoryService.childAdditionsBulkAdd(childAdditionsRefIds, parentReferenceId); * })(); * ``` */ childAdditionsBulkAdd(childReference, parentReference) { return __awaiter(this, void 0, void 0, function* () { return this.addChildBulk(ChildType.ADDITIONS, childReference, parentReference); }); } /** * Removes an existing managed object as child addition from another managed object (parent) * * @returns Response wrapped in [[IResult]] * * @param {IdReference} childReference * @param {IdReference} parentReference * * **Example** * ```typescript * * const childRef: number = 2; * const parentReferenceId: number = 1; * * (async () => { * const {data, res} = await inventoryService.childAdditionsRemove(childRef, parentReferenceId); * })(); * ``` */ childAdditionsRemove(childReference, parentReference) { return __awaiter(this, void 0, void 0, function* () { return this.removeChild(ChildType.ADDITIONS, childReference, parentReference); }); } /** * Gets a list of child assets from a given managed object (parent) * * @returns Response wrapped in [[IResultList]] * * @param {IdReference} parentReference * @param {object} filter * * **Example** * ```typescript * * const parentReferenceId: IdReference = 1; * * const filter: object = { * pageSize: 100, * withTotalPages: true * }; * * (async () => { * const {data, res, paging} = await inventoryService.childAssetsList(parentReferenceId, filter); * })(); * ``` */ childAssetsList(parentReference_1) { return __awaiter(this, arguments, void 0, function* (parentReference, filter = {}) { return this.listChildren(ChildType.ASSETS, parentReference, filter); }); } /** * Creates a new managed object as child asset to another managed object (parent) * * @returns Response wrapped in [[IResult]] * * @param {Partial<IManagedObject>} managedObject * @param {IdReference} parentReference * * **Example** * ```typescript * * const mOAsChildAsset: Partial<IManagedObject> = { * name: 'Child asset MO', * type: 'new type', * ... * }; * * // This is the identifier of the managed object which should be the parent of * // mOAsChildAsset, see above. * const parentReferenceId: number = 1; * * (async () => { * const {data, res} = await inventoryService.childAdditionsCreate(mOAsChildAddition, parentReferenceId); * })(); * ``` */ childAssetsCreate(managedObject, parentReference) { return __awaiter(this, void 0, void 0, function* () { return this.createChild(ChildType.ASSETS, managedObject, parentReference); }); } /** * Adds an existing managed object as child asset to another managed object (parent) * * @returns Response wrapped in [[IResult]] * * @param {IdReference} childReference * @param {IdReference} parentReference * * **Example** * ```typescript * * const childRef: number = 2; * const parentReferenceId: number = 1; * * (async () => { * const {data, res} = await inventoryService.childAssetsAdd(childRef, parentReferenceId); * })(); * ``` */ childAssetsAdd(childReference, parentReference) { return __awaiter(this, void 0, void 0, function* () { return this.addChild(ChildType.ASSETS, childReference, parentReference); }); } /** * Adds bulk of existing managed objects as child assets to another managed object (parent). * * @returns Response wrapped in array of [[IResult]] * * @param {IdReference[]} childReference List of existing managed objects IDs that should be added to another managed object (parent). * @param {IdReference} parentReference * * **Example** * ```typescript * * const childAssetsRefIds: string[] = ['2', '3']; * const parentReferenceId: number = 1; * * (async () => { * const {data, res} = await inventoryService.childAssetsBulkAdd(childAssetsRefIds, parentReferenceId); * })(); * ``` */ childAssetsBulkAdd(childReference, parentReference) { return __awaiter(this, void 0, void 0, function* () { return this.addChildBulk(ChildType.ASSETS, childReference, parentReference); }); } /** * Removes an existing managed object as child asset from another managed object (parent) * * @returns Response wrapped in [[IResult]] * * @param {IdReference} childReference * @param {IdReference} parentReference * * **Example** * ```typescript * * const childRef: number = 2; * const parentReferenceId: number = 1; * * (async () => { * const {data, res} = await inventoryService.childAssetsRemove(childRef, parentReferenceId); * })(); * ``` */ childAssetsRemove(childReference, parentReference) { return __awaiter(this, void 0, void 0, function* () { return this.removeChild(ChildType.ASSETS, childReference, parentReference); }); } /** * Gets a list of child devices from a given managed object (parent) * * @returns Response wrapped in [[IResultList]] * * @param {IdReference} parentReference * @param {object} filter * * **Example** * ```typescript * * const parentReferenceId: IdReference = 1; * * const filter: object = { * pageSize: 100, * withTotalPages: true * }; * * (async () => { * const {data, res, paging} = await inventoryService.childDevicesList(parentReferenceId, filter); * })(); * ``` */ childDevicesList(parentReference_1) { return __awaiter(this, arguments, void 0, function* (parentReference, filter = {}) { return this.listChildren(ChildType.DEVICES, parentReference, filter); }); } /** * Creates a new managed object as child device to another managed object (parent) * * @returns Response wrapped in [[IResult]] * * @param {Partial<IManagedObject>} managedObject * @param {IdReference} parentReference * * **Example** * ```typescript * * const mOAsChildDevice: Partial<IManagedObject> = { * name: 'Child device MO', * type: 'new type', * ... * }; * * // This is the identifier of the managed object which should be the parent of * // mOAsChildDevice, see above. * const parentReferenceId: number = 1; * * (async () => { * const {data, res} = await inventoryService.childDevicesCreate(mOAsChildDevice, parentReferenceId); * })(); * ``` */ childDevicesCreate(managedObject, parentReference) { return __awaiter(this, void 0, void 0, function* () { return this.createChild(ChildType.DEVICES, managedObject, parentReference); }); } /** * Adds an existing managed object as child device to another managed object (parent) * * @returns Response wrapped in [[IResult]] * * @param {IdReference} childReference * @param {IdReference} parentReference * * **Example** * ```typescript * * const childRef: number = 2; * const parentReferenceId: number = 1; * * (async () => { * const {data, res} = await inventoryService.childDevicesAdd(childRef, parentReferenceId); * })(); * ``` */ childDevicesAdd(childReference, parentReference) { return __awaiter(this, void 0, void 0, function* () { return this.addChild(ChildType.DEVICES, childReference, parentReference); }); } /** * Adds bulk of existing managed objects as child devices to another managed object (parent). * * @returns Response wrapped in array of [[IResult]] * * @param {IdReference[]} childReference List of existing managed objects IDs that should be added to another managed object (parent). * @param {IdReference} parentReference * * **Example** * ```typescript * * const childDevicesRefIds: string[] = ['2', '3']; * const parentReferenceId: number = 1; * * (async () => { * const {data, res} = await inventoryService.childDevicesBulkAdd(childDevicesRefIds, parentReferenceId); * })(); * ``` */ childDevicesBulkAdd(childReference, parentReference) { return __awaiter(this, void 0, void 0, function* () { return this.addChildBulk(ChildType.DEVICES, childReference, parentReference); }); } /** * Removes an existing managed object as child device from another managed object (parent) * * @returns Response wrapped in [[IResult]] * * @param {IdReference} childReference * @param {IdReference} parentReference * * **Example** * ```typescript * * const childRef: number = 2; * const parentReferenceId: number = 1; * * (async () => { * const {data, res} = await inventoryService.childDevicesRemove(childRef, parentReferenceId); * })(); * ``` */ childDevicesRemove(childReference, parentReference) { return __awaiter(this, void 0, void 0, function* () { return this.removeChild(ChildType.DEVICES, childReference, parentReference); }); } /** * Gets an array of measurement fragments supported by the specified managedObject. * e.g. ["c8y_Temperature", "c8y_Humidity"] * * @returns array of supported measurement fragments * * @param {IdReference} managedObjectOrId */ getSupportedMeasurements(managedObjectOrId) { return __awaiter(this, void 0, void 0, function* () { return this.getSupportedMeasurementDetails(managedObjectOrId, 'supportedMeasurements'); }); } /** * Gets an array of measurement series supported by the specified managedObject. * e.g. ["c8y_Temperature.T", "c8y_Humidity.H"] * * @returns array of supported measurement series * * @param {IdReference} managedObjectOrId */ getSupportedSeries(managedObjectOrId) { return __awaiter(this, void 0, void 0, function* () { return this.getSupportedMeasurementDetails(managedObjectOrId, 'supportedSeries'); }); } /** * Gets an array of measurement series and fragments supported by the specified managedObject. * * @returns array of supported measurement series and fragments * * @param {IdReference} managedObjectOrId */ getMeasurementsAndSeries(managedObjectOrId) { return __awaiter(this, void 0, void 0, function* () { const [supportedMeasurements, supportedSeries] = yield Promise.all([ this.getSupportedMeasurements(managedObjectOrId), this.getSupportedSeries(managedObjectOrId) ]); // sort by longest measurement fragment first const sortedSupportedMeasurements = supportedMeasurements.sort((a, b) => b.length - a.length); return supportedSeries .map(s => { const fragment = sortedSupportedMeasurements.find(m => s.indexOf(`${m}.`) === 0); const series = s.replace(`${fragment}.`, ''); return { fragment, series }; }) .filter(obj => !!obj.fragment); }); } /** * Gets a list of KPIs (data point library entries) matching a given managed object * * @returns Response wrapped in [[IResultList]] * * @param {IdReference} parentReference * @param {object} filter */ assetKPIsList(parentReference_1) { return __awaiter(this, arguments, void 0, function* (parentReference, filter = {}) { return this.listKPIs(parentReference, filter); }); } listKPIs(parentReference_1) { return __awaiter(this, arguments, void 0, function* (parentReference, filter = {}) { const headers = { accept: 'application/json' }; const url = this.getChildrenUrl('kpis', parentReference); const res = yield this.fetch(url, this.changeFetchOptions({ headers, params: filter }, url)); const json = yield res.json(); const data = json[this.propertyName]; const paging = this.getPaging(json, filter); return { res, data, paging }; }); } onBeforeUpdate(objWithId) { delete objWithId.lastUpdated; return objWithId; } onBeforeCreate(managedObject) { delete managedObject.id; delete managedObject.lastUpdated; return managedObject; } getChildrenUrl(type, parentReference) { return `${this.getDetailUrl(parentReference)}/${type}`; } getChildUrl(type, childReference, parentReference) { const childId = this.getIdString(childReference); return `${this.getChildrenUrl(type, parentReference)}/${childId}`; } listChildren(type_1, parentReference_1) { return __awaiter(this, arguments, void 0, function* (type, parentReference, filter = {}) { const headers = { 'content-type': 'application/json' }; const url = this.getChildrenUrl(type, parentReference); const res = yield this.fetch(url, { headers, params: filter }); const json = yield res.json(); const data = json.references.map(ref => ref.managedObject); const paging = this.getPaging(json, filter); paging.list = pagingFilter => this.listChildren(type, parentReference, pagingFilter); return { res, data, paging }; }); } createChild(type, managedObject, parentReference) { return __awaiter(this, void 0, void 0, function* () { const url = this.getChildrenUrl(type, parentReference); const method = 'POST'; const body = JSON.stringify(this.onBeforeCreate(managedObject)); const headers = { 'content-type': this.mimeType('managedObject'), accept: 'application/json' }; const res = yield this.fetch(url, { method, body, headers }); const data = yield res.json(); return { res, data }; }); } addChild(type, childReference, parentReference) { return __awaiter(this, void 0, void 0, function* () { const url = this.getChildrenUrl(type, parentReference); const method = 'POST'; const childId = this.getIdString(childReference); const body = JSON.stringify({ managedObject: { id: String(childId) } }); const headers = { accept: 'application/json', 'content-type': this.mimeType('managedObjectReference') }; const res = yield this.fetch(url, { method, body, headers }); let data = yield res.json(); data = data.managedObject; return { res, data }; }); } addChildBulk(type, childReferenceArray, parentReference) { return __awaiter(this, void 0, void 0, function* () { const url = this.getChildrenUrl(type, parentReference); const method = 'POST'; const references = childReferenceArray.map(childId => ({ managedObject: { id: this.getIdString(childId) } })); const body = JSON.stringify({ references }); const headers = { accept: 'application/json', 'content-type': this.mimeType('managedObjectReferenceCollection') }; const res = yield this.fetch(url, { method, body, headers }); const data = (yield res.json()).references; return { res, data: data.map(obj => obj.managedObject) }; }); } removeChild(type, childReference, parentReference) { return __awaiter(this, void 0, void 0, function* () { const childId = this.getIdString(childReference); const url = `${this.getChildrenUrl(type, parentReference)}/${childId}`; const method = 'DELETE'; const headers = { accept: 'application/json' }; const res = yield this.fetch(url, { method, headers }); const data = null; return { res, data }; }); } getSupportedMeasurementDetails(managedObjectOrId_1) { return __awaiter(this, arguments, void 0, function* (managedObjectOrId, type = 'supportedSeries') { const url = `${this.getDetailUrl(managedObjectOrId)}/${type}`; this.getIdString(managedObjectOrId); const headers = { accept: 'application/json' }; const res = yield this.fetch(url, { headers }); const data = yield res.json(); return data.c8y_SupportedMeasurements || data.c8y_SupportedSeries; }); } } //# sourceMappingURL=InventoryService.js.map