async function createSensorUnlessExists(deviceId, alternateId, sensorTypeId) {
    const existingSensors = await SAP_DeviceManagement.apis.Sensors.getSensorsUsingGET(
        {
            tenantId: config.SAP_tenantID,
            filter: `(deviceId eq '${deviceId}') and (alternateId eq '${alternateId}')`
        }
    );
    if (!(existingSensors.ok && Array.isArray(existingSensors.body))) {
        throw new Error('Lookup of existing sensors failed!');
    }

    if (existingSensors.body.length > 0) {
        console.debug(`Sensor with alternateId '${alternateId}' found in device '${deviceId}', ID='${existingSensors.body[0].id}'`);
        return existingSensors.body[0].id;
    }

    // The lookup was OK, but the capability with this alternate ID was not found.
    console.info(`Creating a new sensor '${alternateId}'`);
    const res = await SAP_DeviceManagement.apis.Sensors.createSensorUsingPOST(
        {
            tenantId: config.SAP_tenantID,
            request: {
                deviceId,
                alternateId,
                sensorTypeId,
                name: alternateId
            }
        }
    );
    if (!res.ok) {
        throw new Error('Sensor creation failed!');
    }

    console.info(`Sensor '${alternateId}' for device '${deviceId}' created with ID='${res.body.id}'.`);
    return res.body.id;
}
/**
 * Create the sensor in SAP IoT Services if it does not exist yet.
 * @param {string} deviceId  the ID of the device to which the sensor is attached
 * @param {string} alternateId  the alternate ID of the created sensor
 * @param {string} sensorTypeId  the ID of the type of the created sensor
 * @return {string}  the ID of the sensor used by SAP IoT Services
 * @throws {Error} Raises an exception when SAP IoT Service returns an error.
 */
Create the sensor in SAP IoT Services if it does not exist yet.
Create the sensor in SAP IoT Services if it does not exist yet .
async function createSensorUnlessExists(deviceId, alternateId, sensorTypeId) {
    const existingSensors = await SAP_DeviceManagement.apis.Sensors.getSensorsUsingGET(
        {
            tenantId: config.SAP_tenantID,
            filter: `(deviceId eq '${deviceId}') and (alternateId eq '${alternateId}')`
        }
    );
    if (!(existingSensors.ok && Array.isArray(existingSensors.body))) {
        throw new Error('Lookup of existing sensors failed!');
    }
    if (existingSensors.body.length > 0) {
        console.debug(`Sensor with alternateId '${alternateId}' found in device '${deviceId}', ID='${existingSensors.body[0].id}'`);
        return existingSensors.body[0].id;
    }
    console.info(`Creating a new sensor '${alternateId}'`);
    const res = await SAP_DeviceManagement.apis.Sensors.createSensorUsingPOST(
        {
            tenantId: config.SAP_tenantID,
            request: {
                deviceId,
                alternateId,
                sensorTypeId,
                name: alternateId
            }
        }
    );
    if (!res.ok) {
        throw new Error('Sensor creation failed!');
    }
    console.info(`Sensor '${alternateId}' for device '${deviceId}' created with ID='${res.body.id}'.`);
    return res.body.id;
}
async function createSensorUnlessExists ( deviceId , alternateId , sensorTypeId ) { const existingSensors = await SAP_DeviceManagement . apis . Sensors . getSensorsUsingGET ( { tenantId : config . SAP_tenantID , filter : ` ${ deviceId } ${ alternateId } ` } ) ; if ( ! ( existingSensors . ok && Array . isArray ( existingSensors . body ) ) ) { throw new Error ( 'Lookup of existing sensors failed!' ) ; } if ( existingSensors . body . length > 0 ) { console . debug ( ` ${ alternateId } ${ deviceId } ${ existingSensors . body [ 0 ] . id } ` ) ; return existingSensors . body [ 0 ] . id ; } console . info ( ` ${ alternateId } ` ) ; const res = await SAP_DeviceManagement . apis . Sensors . createSensorUsingPOST ( { tenantId : config . SAP_tenantID , request : { deviceId , alternateId , sensorTypeId , name : alternateId } } ) ; if ( ! res . ok ) { throw new Error ( 'Sensor creation failed!' ) ; } console . info ( ` ${ alternateId } ${ deviceId } ${ res . body . id } ` ) ; return res . body . id ; }
Create the sensor in SAP IoT Services if it does not exist yet.
Create the sensor in SAP IoT Services if it does not exist yet .
// The lookup was OK, but the capability with this alternate ID was not found.