async function createSensorTypeUnlessExists(typeName, capabilityId, isCommand=false) {
    const existingTypes = await SAP_DeviceManagement.apis.SensorTypes.getSensorTypesUsingGET(
        {
            tenantId: config.SAP_tenantID,
            filter: `name eq '${typeName}'`
        }
    );
    if (!(existingTypes.ok && Array.isArray(existingTypes.body))) {
        throw new Error('Lookup of existing sensor types failed!');
    }

    // Names are not guaranteed to be unique.
    for (const type of existingTypes.body) {
        for (const cap of type.capabilities) {
            // TODO: check for command
            if (cap.id === capabilityId) {
                console.debug(`Sensor type with name '${typeName}' found, ID='${type.id}'`);
                return type.id;
            }
        }
    }

    // The lookup was OK, but the sensor type with this name was not found.
    console.info(`Creating a new sensor type '${typeName}'`);
    const res = await SAP_DeviceManagement.apis.SensorTypes.createSensorTypeUsingPOST(
        {
            tenantId: config.SAP_tenantID,
            request: {
                name: typeName,
                capabilities: [{id: capabilityId, type: isCommand ? 'command' : 'measure'}]
            }
        }
    );
    if (!res.ok) {
        throw new Error('Sensor type creation failed!');
    }

    console.info(`Sensor type '${typeName}' created with ID '${res.body.id}'.`);
    return res.body.id;
}
/**
 * Create the sensor type in SAP IoT Services if it does not exist yet.
 * @param {string} gatewayId  the ID of the gateway to which the device sends the data
 * @param {string} alternateId  the alternate ID of the created device
 * @param {boolean} isRouter  true if the created device can post measurements on behalf of other devices
 * @return {string}  the ID of the device used by SAP IoT Services
 * @throws {Error} Raises an exception when SAP IoT Service returns an error.
 */
Create the sensor type in SAP IoT Services if it does not exist yet.
Create the sensor type in SAP IoT Services if it does not exist yet .
async function createSensorTypeUnlessExists(typeName, capabilityId, isCommand=false) {
    const existingTypes = await SAP_DeviceManagement.apis.SensorTypes.getSensorTypesUsingGET(
        {
            tenantId: config.SAP_tenantID,
            filter: `name eq '${typeName}'`
        }
    );
    if (!(existingTypes.ok && Array.isArray(existingTypes.body))) {
        throw new Error('Lookup of existing sensor types failed!');
    }
    for (const type of existingTypes.body) {
        for (const cap of type.capabilities) {
            if (cap.id === capabilityId) {
                console.debug(`Sensor type with name '${typeName}' found, ID='${type.id}'`);
                return type.id;
            }
        }
    }
    console.info(`Creating a new sensor type '${typeName}'`);
    const res = await SAP_DeviceManagement.apis.SensorTypes.createSensorTypeUsingPOST(
        {
            tenantId: config.SAP_tenantID,
            request: {
                name: typeName,
                capabilities: [{id: capabilityId, type: isCommand ? 'command' : 'measure'}]
            }
        }
    );
    if (!res.ok) {
        throw new Error('Sensor type creation failed!');
    }
    console.info(`Sensor type '${typeName}' created with ID '${res.body.id}'.`);
    return res.body.id;
}
async function createSensorTypeUnlessExists ( typeName , capabilityId , isCommand = false ) { const existingTypes = await SAP_DeviceManagement . apis . SensorTypes . getSensorTypesUsingGET ( { tenantId : config . SAP_tenantID , filter : ` ${ typeName } ` } ) ; if ( ! ( existingTypes . ok && Array . isArray ( existingTypes . body ) ) ) { throw new Error ( 'Lookup of existing sensor types failed!' ) ; } for ( const type of existingTypes . body ) { for ( const cap of type . capabilities ) { if ( cap . id === capabilityId ) { console . debug ( ` ${ typeName } ${ type . id } ` ) ; return type . id ; } } } console . info ( ` ${ typeName } ` ) ; const res = await SAP_DeviceManagement . apis . SensorTypes . createSensorTypeUsingPOST ( { tenantId : config . SAP_tenantID , request : { name : typeName , capabilities : [ { id : capabilityId , type : isCommand ? 'command' : 'measure' } ] } } ) ; if ( ! res . ok ) { throw new Error ( 'Sensor type creation failed!' ) ; } console . info ( ` ${ typeName } ${ res . body . id } ` ) ; return res . body . id ; }
Create the sensor type in SAP IoT Services if it does not exist yet.
Create the sensor type in SAP IoT Services if it does not exist yet .
// Names are not guaranteed to be unique. // TODO: check for command // The lookup was OK, but the sensor type with this name was not found.