async function postMeasurementData(deviceAlternateId, sensorAlternateId, capabilityAlternateId, measurements) {
        if (!Array.isArray(measurements)) {
            measurements = [measurements];
        }
        for (const measurement of measurements) {
            if (typeof measurement !== 'object') {
                throw new TypeError('measurements must be an object or an array of objects');
            }
        }
        if (typeof deviceAlternateId !== 'string') {
            throw new TypeError('deviceAlternateId must be a string');
        }
        if (typeof sensorAlternateId !== 'string') {
            throw new TypeError('sensorAlternateId must be a string');
        }
        if (typeof capabilityAlternateId !== 'string') {
            throw new TypeError('capabilityAlternateId must be a string');
        }

        const payload = {
            'capabilityAlternateId': capabilityAlternateId,
            'sensorAlternateId': sensorAlternateId,
            'measures': measurements
        };

        const body = JSON.stringify(payload);

        let promise = new Promise(function(resolve, reject) {
            const httpsOptions = {
                hostname: params.hostname,
                port: 443,
                path: '/iot/gateway/rest/measures/' + deviceAlternateId,
                method: 'POST',
                headers: {'Content-Type': 'application/json'},
                key: params.clientPrivateKeyPEM,
                passphrase: params.passphrase,
                cert: params.clientCertPEM,
                agent: httpsAgent
            };

            const request = https.request(httpsOptions, (response) => {
                console.debug('REST gateway response code: %d', response.statusCode);
                response.setEncoding('utf8');
                let responseBody = '';
                response.on('data', (chunk) => {
                    responseBody += chunk;
                    console.debug('Got chunk of HTTP response:', chunk);
                });
                response.on('end', () => {
                    console.debug('End of HTTP response.');
                    if (response.statusCode === 200 || response.statusCode === 202) {
                        resolve(response.statusCode);
                    } else {
                        // POSTing the measurement data failed. Try to recover the error messages, and throw an exception.
                        let errorMessages = '';
                        try {
                            const resp = JSON.parse(responseBody);
                            console.debug(resp);
                            errorMessages = resp[0].messages.join('\n');
                        } catch (e) {
                            // Ignore JSON parse errors and any other problems since we are handling an error already.
                        }
                        reject(Error(`Posting measurement data for device '${deviceAlternateId}', sensor '${sensorAlternateId}', capability '${capabilityAlternateId}' failed with status code ${response.statusCode}. The SAP error messages: "${errorMessages}"`));
                    }
                });
            });
            request.on('error', (e) => {
                console.error('Error sending HTTPS request:', e.message);
                reject(Error(e.message));
            });
            request.setHeader('Content-Length', Buffer.byteLength(body));
            request.write(body);
            request.end();
        });

        return promise;
    }
/**
     * Post an array of measurement data to IoT Services via a REST gateway.
     * @param {!string} deviceAlternateId - the alternate ID of the device on whose behalf we post the measurement data.
     *     The device must be previously created in IoT Service via the Web GUI (Cockpit) or the REST API.
     *     The certificate provided in the PEM form to the factory function exported by the module must have a matching
     *     CN: either including the deviceAlternateId, or belonging to a router device.
     * @param {!string} sensorAlternateId - the alternate ID of the sensor, which must be previously created.
     * @param {!string} capabilityAlternateId - the alternate ID of the Capability, a record-like type supported
     *     by the sensor. The Capability must be previously created.
     * @param {!Array} measurements - the measurement data. The elements of the array must be either arrays or objects.
     *     If they are arrays, the length of each array, the order of elements and their data types must match
     *     the properties defined by the Capability. If they are objects, the keys must be strings that match
     *     the names of the properties, and the values must have the types specified for the properies
     *     defined by the Capability.
     */
Post an array of measurement data to IoT Services via a REST gateway.
@param {!string} deviceAlternateId - the alternate ID of the device on whose behalf we post the measurement data.
The device must be previously created in IoT Service via the Web GUI (Cockpit) or the REST API.
The certificate provided in the PEM form to the factory function exported by the module must have a matching
CN: either including the deviceAlternateId, or belonging to a router device.
@param {!string} sensorAlternateId - the alternate ID of the sensor, which must be previously created.
@param {!string} capabilityAlternateId - the alternate ID of the Capability, a record-like type supported
by the sensor. The Capability must be previously created.
@param {!Array} measurements - the measurement data. The elements of the array must be either arrays or objects.
If they are arrays, the length of each array, the order of elements and their data types must match
the properties defined by the Capability. If they are objects, the keys must be strings that match
the names of the properties, and the values must have the types specified for the properies
defined by the Capability.
Post an array of measurement data to IoT Services via a REST gateway . @param { !string } deviceAlternateId - the alternate ID of the device on whose behalf we post the measurement data . The device must be previously created in IoT Service via the Web GUI ( Cockpit ) or the REST API . The certificate provided in the PEM form to the factory function exported by the module must have a matching CN : either including the deviceAlternateId or belonging to a router device . @param { !string } sensorAlternateId - the alternate ID of the sensor which must be previously created . @param { !string } capabilityAlternateId - the alternate ID of the Capability a record - like type supported by the sensor . The Capability must be previously created . @param { !Array } measurements - the measurement data . The elements of the array must be either arrays or objects . If they are arrays the length of each array the order of elements and their data types must match the properties defined by the Capability . If they are objects the keys must be strings that match the names of the properties and the values must have the types specified for the properies defined by the Capability .
async function postMeasurementData(deviceAlternateId, sensorAlternateId, capabilityAlternateId, measurements) {
        if (!Array.isArray(measurements)) {
            measurements = [measurements];
        }
        for (const measurement of measurements) {
            if (typeof measurement !== 'object') {
                throw new TypeError('measurements must be an object or an array of objects');
            }
        }
        if (typeof deviceAlternateId !== 'string') {
            throw new TypeError('deviceAlternateId must be a string');
        }
        if (typeof sensorAlternateId !== 'string') {
            throw new TypeError('sensorAlternateId must be a string');
        }
        if (typeof capabilityAlternateId !== 'string') {
            throw new TypeError('capabilityAlternateId must be a string');
        }
        const payload = {
            'capabilityAlternateId': capabilityAlternateId,
            'sensorAlternateId': sensorAlternateId,
            'measures': measurements
        };
        const body = JSON.stringify(payload);
        let promise = new Promise(function(resolve, reject) {
            const httpsOptions = {
                hostname: params.hostname,
                port: 443,
                path: '/iot/gateway/rest/measures/' + deviceAlternateId,
                method: 'POST',
                headers: {'Content-Type': 'application/json'},
                key: params.clientPrivateKeyPEM,
                passphrase: params.passphrase,
                cert: params.clientCertPEM,
                agent: httpsAgent
            };
            const request = https.request(httpsOptions, (response) => {
                console.debug('REST gateway response code: %d', response.statusCode);
                response.setEncoding('utf8');
                let responseBody = '';
                response.on('data', (chunk) => {
                    responseBody += chunk;
                    console.debug('Got chunk of HTTP response:', chunk);
                });
                response.on('end', () => {
                    console.debug('End of HTTP response.');
                    if (response.statusCode === 200 || response.statusCode === 202) {
                        resolve(response.statusCode);
                    } else {
                        let errorMessages = '';
                        try {
                            const resp = JSON.parse(responseBody);
                            console.debug(resp);
                            errorMessages = resp[0].messages.join('\n');
                        } catch (e) {
                        }
                        reject(Error(`Posting measurement data for device '${deviceAlternateId}', sensor '${sensorAlternateId}', capability '${capabilityAlternateId}' failed with status code ${response.statusCode}. The SAP error messages: "${errorMessages}"`));
                    }
                });
            });
            request.on('error', (e) => {
                console.error('Error sending HTTPS request:', e.message);
                reject(Error(e.message));
            });
            request.setHeader('Content-Length', Buffer.byteLength(body));
            request.write(body);
            request.end();
        });
        return promise;
    }
async function postMeasurementData ( deviceAlternateId , sensorAlternateId , capabilityAlternateId , measurements ) { if ( ! Array . isArray ( measurements ) ) { measurements = [ measurements ] ; } for ( const measurement of measurements ) { if ( typeof measurement !== 'object' ) { throw new TypeError ( 'measurements must be an object or an array of objects' ) ; } } if ( typeof deviceAlternateId !== 'string' ) { throw new TypeError ( 'deviceAlternateId must be a string' ) ; } if ( typeof sensorAlternateId !== 'string' ) { throw new TypeError ( 'sensorAlternateId must be a string' ) ; } if ( typeof capabilityAlternateId !== 'string' ) { throw new TypeError ( 'capabilityAlternateId must be a string' ) ; } const payload = { 'capabilityAlternateId' : capabilityAlternateId , 'sensorAlternateId' : sensorAlternateId , 'measures' : measurements } ; const body = JSON . stringify ( payload ) ; let promise = new Promise ( function ( resolve , reject ) { const httpsOptions = { hostname : params . hostname , port : 443 , path : '/iot/gateway/rest/measures/' + deviceAlternateId , method : 'POST' , headers : { 'Content-Type' : 'application/json' } , key : params . clientPrivateKeyPEM , passphrase : params . passphrase , cert : params . clientCertPEM , agent : httpsAgent } ; const request = https . request ( httpsOptions , ( response ) => { console . debug ( 'REST gateway response code: %d' , response . statusCode ) ; response . setEncoding ( 'utf8' ) ; let responseBody = '' ; response . on ( 'data' , ( chunk ) => { responseBody += chunk ; console . debug ( 'Got chunk of HTTP response:' , chunk ) ; } ) ; response . on ( 'end' , ( ) => { console . debug ( 'End of HTTP response.' ) ; if ( response . statusCode === 200 || response . statusCode === 202 ) { resolve ( response . statusCode ) ; } else { let errorMessages = '' ; try { const resp = JSON . parse ( responseBody ) ; console . debug ( resp ) ; errorMessages = resp [ 0 ] . messages . join ( '\n' ) ; } catch ( e ) { } reject ( Error ( ` ${ deviceAlternateId } ${ sensorAlternateId } ${ capabilityAlternateId } ${ response . statusCode } ${ errorMessages } ` ) ) ; } } ) ; } ) ; request . on ( 'error' , ( e ) => { console . error ( 'Error sending HTTPS request:' , e . message ) ; reject ( Error ( e . message ) ) ; } ) ; request . setHeader ( 'Content-Length' , Buffer . byteLength ( body ) ) ; request . write ( body ) ; request . end ( ) ; } ) ; return promise ; }
Post an array of measurement data to IoT Services via a REST gateway.
Post an array of measurement data to IoT Services via a REST gateway .
// POSTing the measurement data failed. Try to recover the error messages, and throw an exception. // Ignore JSON parse errors and any other problems since we are handling an error already.