@c8y/client
Version:
Client application programming interface to access the Cumulocity IoT-Platform REST services.
741 lines • 25.2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InventoryService = exports.ChildType = void 0;
const index_js_1 = require("../core/index.js");
const InventoryBinaryService_js_1 = require("./InventoryBinaryService.js");
/**
* Possible types of a child.
*/
var ChildType;
(function (ChildType) {
ChildType["ASSETS"] = "childAssets";
ChildType["DEVICES"] = "childDevices";
ChildType["ADDITIONS"] = "childAdditions";
})(ChildType || (exports.ChildType = ChildType = {}));
/**
* This class allows for managing managed objects and different child types, see [[ChildType]].
*/
class InventoryService extends index_js_1.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 index_js_1.QueriesUtil();
this.binary = new InventoryBinaryService_js_1.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);
* })();
* ```
*/
async detail(managedObjectOrId, filter = {}) {
return super.detail(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);
* })();
* ```
*/
async create(managedObject) {
return super.create(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);
* })();
* ```
*/
async update(managedObject) {
return super.update(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);
* })();
* ```
*/
async list(filter = {}) {
return super.list(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);
* })();
* ```
*/
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 };
}
/**
* 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);
* })();
* ```
*/
async listQuery(query, filter = {}) {
filter[this.inventoriesQueryParamName] = this.queriesUtil.buildQuery(query);
return super.list(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);
* })();
* ```
*/
async listQueryDevices(query, filter = {}) {
filter[this.devicesQueryParamName] = this.queriesUtil.buildQuery(query);
return super.list(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);
* })();
* ```
*/
async delete(managedObjectOrId, params = {}) {
return super.delete(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);
* })();
* ```
*/
async childAdditionsList(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);
* })();
* ```
*/
async childAdditionsCreate(managedObject, parentReference) {
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);
* })();
* ```
*/
async childAdditionsAdd(childReference, parentReference) {
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);
* })();
* ```
*/
async childAdditionsBulkAdd(childReference, parentReference) {
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);
* })();
* ```
*/
async childAdditionsRemove(childReference, parentReference) {
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);
* })();
* ```
*/
async childAssetsList(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);
* })();
* ```
*/
async childAssetsCreate(managedObject, parentReference) {
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);
* })();
* ```
*/
async childAssetsAdd(childReference, parentReference) {
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);
* })();
* ```
*/
async childAssetsBulkAdd(childReference, parentReference) {
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);
* })();
* ```
*/
async childAssetsRemove(childReference, parentReference) {
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);
* })();
* ```
*/
async childDevicesList(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);
* })();
* ```
*/
async childDevicesCreate(managedObject, parentReference) {
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);
* })();
* ```
*/
async childDevicesAdd(childReference, parentReference) {
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);
* })();
* ```
*/
async childDevicesBulkAdd(childReference, parentReference) {
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);
* })();
* ```
*/
async childDevicesRemove(childReference, parentReference) {
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
*/
async getSupportedMeasurements(managedObjectOrId) {
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
*/
async getSupportedSeries(managedObjectOrId) {
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
*/
async getMeasurementsAndSeries(managedObjectOrId) {
const [supportedMeasurements, supportedSeries] = await 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
*/
async assetKPIsList(parentReference, filter = {}) {
return this.listKPIs(parentReference, filter);
}
async listKPIs(parentReference, filter = {}) {
const headers = { accept: 'application/json' };
const url = this.getChildrenUrl('kpis', parentReference);
const res = await this.fetch(url, this.changeFetchOptions({ headers, params: filter }, url));
const json = await 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}`;
}
async listChildren(type, parentReference, filter = {}) {
const headers = { 'content-type': 'application/json' };
const url = this.getChildrenUrl(type, parentReference);
const res = await this.fetch(url, { headers, params: filter });
const json = await 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 };
}
async createChild(type, managedObject, parentReference) {
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 = await this.fetch(url, { method, body, headers });
const data = await res.json();
return { res, data };
}
async addChild(type, childReference, parentReference) {
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 = await this.fetch(url, { method, body, headers });
let data = await res.json();
data = data.managedObject;
return { res, data };
}
async addChildBulk(type, childReferenceArray, parentReference) {
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 = await this.fetch(url, { method, body, headers });
const data = (await res.json()).references;
return { res, data: data.map(obj => obj.managedObject) };
}
async removeChild(type, childReference, parentReference) {
const childId = this.getIdString(childReference);
const url = `${this.getChildrenUrl(type, parentReference)}/${childId}`;
const method = 'DELETE';
const headers = { accept: 'application/json' };
const res = await this.fetch(url, { method, headers });
const data = null;
return { res, data };
}
async getSupportedMeasurementDetails(managedObjectOrId, type = 'supportedSeries') {
const url = `${this.getDetailUrl(managedObjectOrId)}/${type}`;
this.getIdString(managedObjectOrId);
const headers = { accept: 'application/json' };
const res = await this.fetch(url, { headers });
const data = await res.json();
return data.c8y_SupportedMeasurements || data.c8y_SupportedSeries;
}
}
exports.InventoryService = InventoryService;
//# sourceMappingURL=InventoryService.js.map