async function post_StatusData(records) {
    const measurements = {}; // Key: vehicle identification number (VIN), value: an array of measurements
    for (const record of records) {
        const geotabDeviceId = record.device.id;
        // The device alternate IDs used by SAP IoT Services are generally supposed to have some
        // real-world meaning, so that they can be derived from real-world data. We use VIN numbers
        // to identify vehicles.
        let vin = geotabDeviceCache[geotabDeviceId];
        if (!vin) {
            try {
                // Cache lookup failed.
                const device = await lookupDevice(geotabDeviceId);
                vin = device.vehicleIdentificationNumber;
                if (!vin) {
                    console.error(`Geotab device with ID ${geotabDeviceId} has no vehicleIdentificationNumber, skipped!`);
                    continue;
                }
                console.info(`Caching the SAP device for the vehicle with VIN='${vin}'`);
                const sapDeviceId = await createDeviceUnlessExists(gatewayId, vin);
                await createSensorUnlessExists(sapDeviceId, 'StatusData', sensorTypeId_StatusData);
                // Add the VIN to the cache.
                geotabDeviceCache[geotabDeviceId] = vin;
            } catch (err) {
                console.error(`Could not look up device with ID ${geotabDeviceId}!`);
                continue;
            }
        }

        const diagnosticId = record.diagnostic.id;
        // Here we merge the data from two Geotab records: the first of typeName StatusData
        // and the second with typeName Diagnostic, referenced by the first one.
        // The Diagnostic data for the diagnostic ID are cached in memory.
        let diagnostic = geotabDiagnosticCache[diagnosticId];
        if (!diagnostic) {
            // Cache miss
            try {
                diagnostic = await lookupDiagnostic(diagnosticId);
                console.info(`Caching the Geotab Diagnostic with ID='${diagnosticId}'`);
                // Add the Diagnostic to the cache
                geotabDiagnosticCache[diagnosticId] = diagnostic;
                // TODO: purge the oldest/least recently used records from the in-memory cache.
            } catch (err) {
                console.error(`Could not look up Diagnostic with ID ${diagnosticId}!`);
                continue;
            }
        }

        let value = parseFloat(record.data);
        if (isNaN(value)) {
            console.error('Numeric value expected. Skipping!');
            continue;
        }
        // Construct the SAP IoT measurement from the Geotab record and the cached Diagnostics
        (measurements[vin] = measurements[vin] || []).push({
            value,
            name: diagnostic.name,
            code: diagnostic.code,
            diagnosticType: diagnostic.diagnosticType,
            faultResetMode: diagnostic.faultResetMode,
            dateTime: record.dateTime
        });
    }

    const promises = [];
    for (const vin of Object.keys(measurements)) {
        promises.push(routerDevice.postMeasurementData(vin, 'StatusData', 'StatusData', measurements[vin]));
    }
    // Measurements for different vehicles (devices) are POSTed concurrently.
    // The maximum number of parallel connections is defined in SAP_IoT_device_REST.
    return Promise.all(promises);
}
/**
 * Post an array of StatusData objects to SAP IoT Service.
 *
 * Measurement data from different vehicles (devices in the SAP IoT Service data model) are POSTed concurrently.
 * The maximum number of parallel connections is defined in SAP_IoT_device_REST.
 * @param {!Object[]} records  the StatusData records to post
 * @throws {Error} Raises an exception when SAP IoT Service returns an error.
 */
Post an array of StatusData objects to SAP IoT Service.
Measurement data from different vehicles (devices in the SAP IoT Service data model) are POSTed concurrently.
The maximum number of parallel connections is defined in SAP_IoT_device_REST.
@param {!Object[]} records  the StatusData records to post
@throws {Error} Raises an exception when SAP IoT Service returns an error.
Post an array of StatusData objects to SAP IoT Service . Measurement data from different vehicles ( devices in the SAP IoT Service data model ) are POSTed concurrently . The maximum number of parallel connections is defined in SAP_IoT_device_REST . @param { !Object [] } records the StatusData records to post @throws { Error } Raises an exception when SAP IoT Service returns an error .
async function post_StatusData(records) {
    const measurements = {}; 
    for (const record of records) {
        const geotabDeviceId = record.device.id;
        let vin = geotabDeviceCache[geotabDeviceId];
        if (!vin) {
            try {
                const device = await lookupDevice(geotabDeviceId);
                vin = device.vehicleIdentificationNumber;
                if (!vin) {
                    console.error(`Geotab device with ID ${geotabDeviceId} has no vehicleIdentificationNumber, skipped!`);
                    continue;
                }
                console.info(`Caching the SAP device for the vehicle with VIN='${vin}'`);
                const sapDeviceId = await createDeviceUnlessExists(gatewayId, vin);
                await createSensorUnlessExists(sapDeviceId, 'StatusData', sensorTypeId_StatusData);
                geotabDeviceCache[geotabDeviceId] = vin;
            } catch (err) {
                console.error(`Could not look up device with ID ${geotabDeviceId}!`);
                continue;
            }
        }
        const diagnosticId = record.diagnostic.id;
        let diagnostic = geotabDiagnosticCache[diagnosticId];
        if (!diagnostic) {
            try {
                diagnostic = await lookupDiagnostic(diagnosticId);
                console.info(`Caching the Geotab Diagnostic with ID='${diagnosticId}'`);
                geotabDiagnosticCache[diagnosticId] = diagnostic;
            } catch (err) {
                console.error(`Could not look up Diagnostic with ID ${diagnosticId}!`);
                continue;
            }
        }
        let value = parseFloat(record.data);
        if (isNaN(value)) {
            console.error('Numeric value expected. Skipping!');
            continue;
        }
        (measurements[vin] = measurements[vin] || []).push({
            value,
            name: diagnostic.name,
            code: diagnostic.code,
            diagnosticType: diagnostic.diagnosticType,
            faultResetMode: diagnostic.faultResetMode,
            dateTime: record.dateTime
        });
    }
    const promises = [];
    for (const vin of Object.keys(measurements)) {
        promises.push(routerDevice.postMeasurementData(vin, 'StatusData', 'StatusData', measurements[vin]));
    }
    return Promise.all(promises);
}
async function post_StatusData ( records ) { const measurements = { } ; for ( const record of records ) { const geotabDeviceId = record . device . id ; let vin = geotabDeviceCache [ geotabDeviceId ] ; if ( ! vin ) { try { const device = await lookupDevice ( geotabDeviceId ) ; vin = device . vehicleIdentificationNumber ; if ( ! vin ) { console . error ( ` ${ geotabDeviceId } ` ) ; continue ; } console . info ( ` ${ vin } ` ) ; const sapDeviceId = await createDeviceUnlessExists ( gatewayId , vin ) ; await createSensorUnlessExists ( sapDeviceId , 'StatusData' , sensorTypeId_StatusData ) ; geotabDeviceCache [ geotabDeviceId ] = vin ; } catch ( err ) { console . error ( ` ${ geotabDeviceId } ` ) ; continue ; } } const diagnosticId = record . diagnostic . id ; let diagnostic = geotabDiagnosticCache [ diagnosticId ] ; if ( ! diagnostic ) { try { diagnostic = await lookupDiagnostic ( diagnosticId ) ; console . info ( ` ${ diagnosticId } ` ) ; geotabDiagnosticCache [ diagnosticId ] = diagnostic ; } catch ( err ) { console . error ( ` ${ diagnosticId } ` ) ; continue ; } } let value = parseFloat ( record . data ) ; if ( isNaN ( value ) ) { console . error ( 'Numeric value expected. Skipping!' ) ; continue ; } ( measurements [ vin ] = measurements [ vin ] || [ ] ) . push ( { value , name : diagnostic . name , code : diagnostic . code , diagnosticType : diagnostic . diagnosticType , faultResetMode : diagnostic . faultResetMode , dateTime : record . dateTime } ) ; } const promises = [ ] ; for ( const vin of Object . keys ( measurements ) ) { promises . push ( routerDevice . postMeasurementData ( vin , 'StatusData' , 'StatusData' , measurements [ vin ] ) ) ; } return Promise . all ( promises ) ; }
Post an array of StatusData objects to SAP IoT Service.
Post an array of StatusData objects to SAP IoT Service .
// Key: vehicle identification number (VIN), value: an array of measurements // The device alternate IDs used by SAP IoT Services are generally supposed to have some // real-world meaning, so that they can be derived from real-world data. We use VIN numbers // to identify vehicles. // Cache lookup failed. // Add the VIN to the cache. // Here we merge the data from two Geotab records: the first of typeName StatusData // and the second with typeName Diagnostic, referenced by the first one. // The Diagnostic data for the diagnostic ID are cached in memory. // Cache miss // Add the Diagnostic to the cache // TODO: purge the oldest/least recently used records from the in-memory cache. // Construct the SAP IoT measurement from the Geotab record and the cached Diagnostics // Measurements for different vehicles (devices) are POSTed concurrently. // The maximum number of parallel connections is defined in SAP_IoT_device_REST.