async function createDeviceUnlessExists(gatewayId, alternateId, isRouter=false) {
    const existingDevices = await SAP_DeviceManagement.apis.Devices.getDevicesUsingGET(
        {
            tenantId: config.SAP_tenantID,
            filter: `(alternateId eq '${alternateId}') and (gatewayId eq '${gatewayId}')`
        }
    );
    if (!(existingDevices.ok && Array.isArray(existingDevices.body))) {
        throw new Error('Lookup of existing devices failed!');
    }

    if (existingDevices.body.length > 0) {
        console.debug(`Device with alternateId '${alternateId}' found, ID='${existingDevices.body[0].id}'`);
        return existingDevices.body[0].id;
    }

    // The lookup was OK, but the capability with this alternate ID was not found.
    console.info(`Creating a new device '${alternateId}'`);
    const request = {
        gatewayId,
        alternateId,
        name: alternateId
    }
    if (isRouter) {
        request.authorizations = [{type: 'router'}];
    }
    const res = await SAP_DeviceManagement.apis.Devices.createDeviceUsingPOST(
        {
            request,
            tenantId: config.SAP_tenantID
        }
    );
    if (!res.ok) {
        throw new Error('Device creation failed!');
    }

    console.info(`Device '${alternateId}' created with ID='${res.body.id}'.`);
    return res.body.id;
}
/**
 * Create the device 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 device in SAP IoT Services if it does not exist yet.
Create the device in SAP IoT Services if it does not exist yet .
async function createDeviceUnlessExists(gatewayId, alternateId, isRouter=false) {
    const existingDevices = await SAP_DeviceManagement.apis.Devices.getDevicesUsingGET(
        {
            tenantId: config.SAP_tenantID,
            filter: `(alternateId eq '${alternateId}') and (gatewayId eq '${gatewayId}')`
        }
    );
    if (!(existingDevices.ok && Array.isArray(existingDevices.body))) {
        throw new Error('Lookup of existing devices failed!');
    }
    if (existingDevices.body.length > 0) {
        console.debug(`Device with alternateId '${alternateId}' found, ID='${existingDevices.body[0].id}'`);
        return existingDevices.body[0].id;
    }
    console.info(`Creating a new device '${alternateId}'`);
    const request = {
        gatewayId,
        alternateId,
        name: alternateId
    }
    if (isRouter) {
        request.authorizations = [{type: 'router'}];
    }
    const res = await SAP_DeviceManagement.apis.Devices.createDeviceUsingPOST(
        {
            request,
            tenantId: config.SAP_tenantID
        }
    );
    if (!res.ok) {
        throw new Error('Device creation failed!');
    }
    console.info(`Device '${alternateId}' created with ID='${res.body.id}'.`);
    return res.body.id;
}
async function createDeviceUnlessExists ( gatewayId , alternateId , isRouter = false ) { const existingDevices = await SAP_DeviceManagement . apis . Devices . getDevicesUsingGET ( { tenantId : config . SAP_tenantID , filter : ` ${ alternateId } ${ gatewayId } ` } ) ; if ( ! ( existingDevices . ok && Array . isArray ( existingDevices . body ) ) ) { throw new Error ( 'Lookup of existing devices failed!' ) ; } if ( existingDevices . body . length > 0 ) { console . debug ( ` ${ alternateId } ${ existingDevices . body [ 0 ] . id } ` ) ; return existingDevices . body [ 0 ] . id ; } console . info ( ` ${ alternateId } ` ) ; const request = { gatewayId , alternateId , name : alternateId } if ( isRouter ) { request . authorizations = [ { type : 'router' } ] ; } const res = await SAP_DeviceManagement . apis . Devices . createDeviceUsingPOST ( { request , tenantId : config . SAP_tenantID } ) ; if ( ! res . ok ) { throw new Error ( 'Device creation failed!' ) ; } console . info ( ` ${ alternateId } ${ res . body . id } ` ) ; return res . body . id ; }
Create the device in SAP IoT Services if it does not exist yet.
Create the device in SAP IoT Services if it does not exist yet .
// The lookup was OK, but the capability with this alternate ID was not found.