2025-06-10 12:36:39 +02:00
|
|
|
const { MeasurementContainer } = require('./index');
|
|
|
|
|
|
2025-08-07 13:52:29 +02:00
|
|
|
console.log('=== MEASUREMENT CONTAINER EXAMPLES ===\n');
|
|
|
|
|
console.log('This guide shows how to use the MeasurementContainer for storing,');
|
|
|
|
|
console.log('retrieving, and converting measurement data with automatic unit handling.\n');
|
|
|
|
|
|
|
|
|
|
// ====================================
|
|
|
|
|
// BASIC SETUP EXAMPLES
|
|
|
|
|
// ====================================
|
|
|
|
|
console.log('--- Example 1: Basic Setup & Event Subscription ---');
|
|
|
|
|
|
|
|
|
|
// Create a basic container
|
|
|
|
|
const basicContainer = new MeasurementContainer({ windowSize: 20 });
|
|
|
|
|
|
|
|
|
|
// Subscribe to flow events to monitor changes
|
|
|
|
|
basicContainer.emitter.on('flow.predicted.upstream', (data) => {
|
|
|
|
|
console.log(`📡 Event: Flow predicted upstream update: ${data.value} at ${new Date(data.timestamp).toLocaleTimeString()}`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//show all flow values from variant measured
|
|
|
|
|
basicContainer.emitter.on('flow.measured.*', (data) => {
|
|
|
|
|
console.log(`📡 Event---------- I DID IT: Flow measured ${data.position} update: ${data.value}`)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Basic value setting with chaining
|
|
|
|
|
console.log('Setting basic pressure values...');
|
|
|
|
|
basicContainer.type('pressure').variant('measured').position('upstream').value(100).unit('psi');
|
|
|
|
|
basicContainer.type('pressure').variant('measured').position('downstream').value(95).unit('psi');
|
|
|
|
|
basicContainer.type('pressure').variant('measured').position('downstream').value(80); // Additional value
|
|
|
|
|
|
|
|
|
|
console.log('✅ Basic setup complete\n');
|
|
|
|
|
|
|
|
|
|
// ====================================
|
|
|
|
|
// AUTO-CONVERSION SETUP EXAMPLES
|
|
|
|
|
// ====================================
|
|
|
|
|
console.log('--- Example 2: Auto-Conversion Setup ---');
|
|
|
|
|
console.log('Setting up a container with automatic unit conversion...\n');
|
|
|
|
|
|
|
|
|
|
// Create container with auto-conversion enabled
|
|
|
|
|
const autoContainer = new MeasurementContainer({
|
|
|
|
|
autoConvert: true,
|
|
|
|
|
windowSize: 50,
|
|
|
|
|
defaultUnits: {
|
|
|
|
|
pressure: 'bar', // Default pressure unit
|
|
|
|
|
flow: 'l/min', // Default flow unit
|
|
|
|
|
power: 'kW', // Default power unit
|
|
|
|
|
temperature: 'C' // Default temperature unit
|
|
|
|
|
},
|
|
|
|
|
preferredUnits: {
|
|
|
|
|
pressure: 'psi' // Override: store pressure in PSI instead of bar
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Values are automatically converted to preferred units
|
|
|
|
|
console.log('Adding pressure data with auto-conversion:');
|
|
|
|
|
autoContainer.type('pressure').variant('measured').position('upstream')
|
|
|
|
|
.value(1.5, Date.now(), 'bar'); // Input: 1.5 bar → Auto-stored as ~21.76 psi
|
|
|
|
|
|
|
|
|
|
autoContainer.type('pressure').variant('measured').position('downstream')
|
|
|
|
|
.value(20, Date.now(), 'psi'); // Input: 20 psi → Stored as 20 psi (already in preferred unit)
|
|
|
|
|
|
|
|
|
|
// Check what was actually stored
|
|
|
|
|
const storedPressure = autoContainer.type('pressure').variant('measured').position('upstream').get();
|
|
|
|
|
console.log(` Stored upstream pressure: ${storedPressure.getCurrentValue()} ${storedPressure.unit}`);
|
|
|
|
|
console.log(' Auto-conversion setup complete\n');
|
|
|
|
|
|
|
|
|
|
// ====================================
|
|
|
|
|
// UNIT CONVERSION EXAMPLES
|
|
|
|
|
// ====================================
|
|
|
|
|
console.log('--- Example 3: Unit Conversion on Retrieval ---');
|
|
|
|
|
console.log('Getting values in different units without changing stored data...\n');
|
|
|
|
|
|
|
|
|
|
// Add flow data in different units
|
|
|
|
|
autoContainer.type('flow').variant('predicted').position('upstream')
|
|
|
|
|
.value(100, Date.now(), 'l/min'); // Stored in l/min (default)
|
|
|
|
|
|
|
|
|
|
autoContainer.type('flow').variant('predicted').position('downstream')
|
|
|
|
|
.value(6, Date.now(), 'm3/h'); // Auto-converted from m3/h to l/min
|
|
|
|
|
|
|
|
|
|
// Retrieve the same data in different units
|
|
|
|
|
const flowLPM = autoContainer.type('flow').variant('predicted').position('upstream').getCurrentValue('l/min');
|
|
|
|
|
const flowM3H = autoContainer.type('flow').variant('predicted').position('upstream').getCurrentValue('m3/h');
|
|
|
|
|
const flowGPM = autoContainer.type('flow').variant('predicted').position('upstream').getCurrentValue('gal/min');
|
|
|
|
|
|
|
|
|
|
console.log(`Flow in l/min: ${flowLPM}`);
|
|
|
|
|
console.log(`Flow in m³/h: ${flowM3H.toFixed(2)}`);
|
|
|
|
|
console.log(`Flow in gal/min: ${flowGPM.toFixed(2)}`);
|
|
|
|
|
console.log('Unit conversion examples complete\n');
|
|
|
|
|
|
|
|
|
|
// ====================================
|
|
|
|
|
// SMART UNIT SELECTION
|
|
|
|
|
// ====================================
|
|
|
|
|
console.log('--- Example 4: Smart Unit Selection ---');
|
|
|
|
|
console.log('Automatically finding the best unit for readability...\n');
|
|
|
|
|
|
|
|
|
|
// Add a very small pressure value
|
|
|
|
|
autoContainer.type('pressure').variant('test').position('sensor')
|
|
|
|
|
.value(0.001, Date.now(), 'bar');
|
|
|
|
|
|
|
|
|
|
// Get the best unit for this small value
|
|
|
|
|
const bestUnit = autoContainer.type('pressure').variant('test').position('sensor').getBestUnit();
|
|
|
|
|
if (bestUnit) {
|
|
|
|
|
console.log(`Best unit representation: ${bestUnit.val} ${bestUnit.unit}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get all available units for pressure
|
|
|
|
|
const availableUnits = autoContainer.getAvailableUnits('pressure');
|
|
|
|
|
console.log(`Available pressure units: ${availableUnits.slice(0, 8).join(', ')}... (${availableUnits.length} total)`);
|
|
|
|
|
console.log('Smart unit selection complete\n');
|
|
|
|
|
|
|
|
|
|
// ====================================
|
|
|
|
|
// BASIC RETRIEVAL AND CALCULATIONS
|
|
|
|
|
// ====================================
|
|
|
|
|
console.log('--- Example 5: Basic Value Retrieval ---');
|
|
|
|
|
console.log('Getting individual values and their units...\n');
|
|
|
|
|
|
|
|
|
|
// Using basic container for clear examples
|
|
|
|
|
const upstreamValue = basicContainer.type('pressure').variant('measured').position('upstream').getCurrentValue();
|
|
|
|
|
const upstreamUnit = basicContainer.type('pressure').variant('measured').position('upstream').get().unit;
|
2025-06-10 12:36:39 +02:00
|
|
|
console.log(`Upstream pressure: ${upstreamValue} ${upstreamUnit}`);
|
2025-08-07 13:52:29 +02:00
|
|
|
|
|
|
|
|
const downstreamValue = basicContainer.type('pressure').variant('measured').position('downstream').getCurrentValue();
|
|
|
|
|
const downstreamUnit = basicContainer.type('pressure').variant('measured').position('downstream').get().unit;
|
2025-06-10 12:36:39 +02:00
|
|
|
console.log(`Downstream pressure: ${downstreamValue} ${downstreamUnit}`);
|
2025-08-07 13:52:29 +02:00
|
|
|
console.log('Basic retrieval complete\n');
|
2025-06-10 12:36:39 +02:00
|
|
|
|
2025-08-07 13:52:29 +02:00
|
|
|
// ====================================
|
|
|
|
|
// CALCULATIONS AND STATISTICS
|
|
|
|
|
// ====================================
|
|
|
|
|
console.log('--- Example 6: Calculations & Statistics ---');
|
|
|
|
|
console.log('Using built-in calculation methods...\n');
|
2025-06-10 12:36:39 +02:00
|
|
|
|
2025-08-07 13:52:29 +02:00
|
|
|
// Add flow data for calculations
|
|
|
|
|
basicContainer.type('flow').variant('predicted').position('upstream').value(200).unit('gpm');
|
|
|
|
|
basicContainer.type('flow').variant('predicted').position('downstream').value(195).unit('gpm');
|
|
|
|
|
|
|
|
|
|
const flowAvg = basicContainer.type('flow').variant('predicted').position('upstream').getAverage();
|
2025-06-10 12:36:39 +02:00
|
|
|
console.log(`Average upstream flow: ${flowAvg} gpm`);
|
|
|
|
|
|
2025-08-07 13:52:29 +02:00
|
|
|
// Calculate pressure difference between upstream and downstream
|
|
|
|
|
const pressureDiff = basicContainer.type('pressure').variant('measured').difference();
|
2025-06-10 12:36:39 +02:00
|
|
|
console.log(`Pressure difference: ${pressureDiff.value} ${pressureDiff.unit}`);
|
2025-08-07 13:52:29 +02:00
|
|
|
console.log('Calculations complete\n');
|
|
|
|
|
|
|
|
|
|
// ====================================
|
|
|
|
|
// ADVANCED STATISTICS
|
|
|
|
|
// ====================================
|
|
|
|
|
console.log('--- Example 7: Advanced Statistics & History ---');
|
|
|
|
|
console.log('Adding multiple values and getting comprehensive statistics...\n');
|
|
|
|
|
|
|
|
|
|
// Add several flow measurements to build history
|
|
|
|
|
basicContainer.type('flow').variant('measured').position('upstream')
|
|
|
|
|
.value(210).value(215).value(205).value(220).value(200).unit('m3/h');
|
|
|
|
|
basicContainer.type('flow').variant('measured').position('downstream')
|
|
|
|
|
.value(190).value(195).value(185).value(200).value(180).unit('m3/h');
|
|
|
|
|
|
|
|
|
|
// Get comprehensive statistics
|
|
|
|
|
const measurement = basicContainer.type('flow').variant('measured').position('upstream');
|
|
|
|
|
console.log('Flow Statistics:');
|
|
|
|
|
console.log(`- Current value: ${measurement.getCurrentValue()} ${measurement.get().unit}`);
|
|
|
|
|
console.log(`- Average: ${measurement.getAverage().toFixed(1)} ${measurement.get().unit}`);
|
|
|
|
|
console.log(`- Minimum: ${measurement.getMin()} ${measurement.get().unit}`);
|
|
|
|
|
console.log(`- Maximum: ${measurement.getMax()} ${measurement.get().unit}`);
|
|
|
|
|
|
|
|
|
|
// Show all values with timestamps
|
|
|
|
|
const allValues = measurement.getAllValues();
|
|
|
|
|
console.log(`- Total samples: ${allValues.values.length}`);
|
|
|
|
|
console.log(`- Value history: [${allValues.values.join(', ')}]`);
|
|
|
|
|
console.log('Advanced statistics complete\n');
|
|
|
|
|
|
|
|
|
|
// ====================================
|
|
|
|
|
// DYNAMIC UNIT MANAGEMENT
|
|
|
|
|
// ====================================
|
|
|
|
|
console.log('--- Example 8: Dynamic Unit Management ---');
|
|
|
|
|
console.log('Changing preferred units at runtime...\n');
|
|
|
|
|
|
|
|
|
|
// Change preferred unit for flow measurements
|
|
|
|
|
autoContainer.setPreferredUnit('flow', 'm3/h');
|
|
|
|
|
console.log('Changed preferred flow unit to m³/h');
|
|
|
|
|
|
|
|
|
|
// Add new flow data - will auto-convert to new preferred unit
|
|
|
|
|
autoContainer.type('flow').variant('realtime').position('inlet')
|
|
|
|
|
.value(150, Date.now(), 'l/min'); // Input in l/min, stored as m³/h
|
2025-06-10 12:36:39 +02:00
|
|
|
|
2025-08-07 13:52:29 +02:00
|
|
|
const realtimeFlow = autoContainer.type('flow').variant('realtime').position('inlet');
|
|
|
|
|
console.log(`Stored as: ${realtimeFlow.getCurrentValue()} ${realtimeFlow.get().unit}`);
|
|
|
|
|
console.log(`Original unit: ${realtimeFlow.getCurrentValue('l/min')} l/min`);
|
|
|
|
|
console.log('Dynamic unit management complete\n');
|
2025-06-10 12:36:39 +02:00
|
|
|
|
2025-08-07 13:52:29 +02:00
|
|
|
// ====================================
|
|
|
|
|
// DATA EXPLORATION
|
|
|
|
|
// ====================================
|
|
|
|
|
console.log('--- Example 9: Data Exploration ---');
|
|
|
|
|
console.log('Discovering what data is available in the container...\n');
|
|
|
|
|
|
|
|
|
|
console.log('Available measurement types:', basicContainer.getTypes());
|
|
|
|
|
console.log('Pressure variants:', basicContainer.type('pressure').getVariants());
|
|
|
|
|
console.log('Measured pressure positions:', basicContainer.type('pressure').variant('measured').getPositions());
|
|
|
|
|
|
|
|
|
|
// Show data structure overview
|
|
|
|
|
console.log('\nData Structure Overview:');
|
|
|
|
|
basicContainer.getTypes().forEach(type => {
|
|
|
|
|
console.log(`${type.toUpperCase()}:`);
|
|
|
|
|
const variants = basicContainer.type(type).getVariants();
|
|
|
|
|
variants.forEach(variant => {
|
|
|
|
|
const positions = basicContainer.type(type).variant(variant).getPositions();
|
|
|
|
|
positions.forEach(position => {
|
|
|
|
|
const measurement = basicContainer.type(type).variant(variant).position(position).get();
|
|
|
|
|
if (measurement && measurement.values.length > 0) {
|
|
|
|
|
console.log(` └── ${variant}.${position}: ${measurement.values.length} values (${measurement.unit || 'no unit'})`);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
console.log('Data exploration complete\n');
|
|
|
|
|
|
|
|
|
|
// ====================================
|
|
|
|
|
// BEST PRACTICES SUMMARY
|
|
|
|
|
// ====================================
|
|
|
|
|
console.log('--- Best Practices Summary ---');
|
|
|
|
|
console.log('BEST PRACTICES FOR NEW USERS:\n');
|
|
|
|
|
|
|
|
|
|
console.log('1. SETUP:');
|
|
|
|
|
console.log(' • Enable auto-conversion for consistent units');
|
|
|
|
|
console.log(' • Define default units for your measurement types');
|
|
|
|
|
console.log(' • Set appropriate window size for your data needs\n');
|
|
|
|
|
|
|
|
|
|
console.log('2. STORING DATA:');
|
|
|
|
|
console.log(' • Always use the full chain: type().variant().position().value()');
|
|
|
|
|
console.log(' • Specify source unit when adding values: .value(100, timestamp, "psi")');
|
|
|
|
|
console.log(' • Set units immediately after first value: .value(100).unit("psi")\n');
|
|
|
|
|
|
|
|
|
|
console.log('3. RETRIEVING DATA:');
|
|
|
|
|
console.log(' • Use .getCurrentValue("unit") to get values in specific units');
|
|
|
|
|
console.log(' • Use .getBestUnit() for automatic unit selection');
|
|
|
|
|
console.log(' • Use .difference() for automatic upstream/downstream calculations\n');
|
|
|
|
|
|
|
|
|
|
console.log('4. MONITORING:');
|
|
|
|
|
console.log(' • Subscribe to events for real-time updates');
|
|
|
|
|
console.log(' • Use .emitter.on("type.variant.position", callback)');
|
|
|
|
|
console.log(' • Explore available data with .getTypes(), .getVariants(), .getPositions()\n');
|
|
|
|
|
|
|
|
|
|
console.log('All examples complete! Ready to use MeasurementContainer');
|
|
|
|
|
|
|
|
|
|
// Export for programmatic use
|
2025-06-10 12:36:39 +02:00
|
|
|
module.exports = {
|
|
|
|
|
runExamples: () => {
|
2025-08-07 13:52:29 +02:00
|
|
|
console.log('Measurement Container Examples - Complete Guide for New Users');
|
|
|
|
|
console.log('This file demonstrates all features with practical examples.');
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Export containers for testing
|
|
|
|
|
basicContainer,
|
|
|
|
|
autoContainer
|
|
|
|
|
};
|