Examples
Example 1: Subscribe file -> publish event
Requirement
- FTP Server
1.1 FTP Datasource
If not already done so, then create a FTP data source on your device. For that:
- navigate to "devices"
- select your device
- press on "+ Data Source"
- fill out the configuration to your FTP server
1.2 Edge Function
VL.subscribeFile = (dataString) => {
VL.publishMeasurements([{ name: 'Datatype_1', value: dataString[0] }]);
};
VL.reportDataPointMetadata = () => {
return [{ name: 'Datatype_1', variable_type: 'FLOAT', unit: '°C' }];
};
Example 2: Subscribe measurement -> publish measurement event
This example shows the conversion of Celsius to Fahrenheit
2.1 Datasource
If not already done so, then create a data source on your device. For that:
- navigate to "devices"
- select your device
- press on "+ Data Source"
- fill out the configuration for your data source
2.2 Edge Function
function celsiusToFahrenheit(celsius) {
return (celsius * 9) / 5 + 32;
}
const myFilterOptions = {
filter: { dataSource: { protocol: 's7' }, data: ['sensors_qaTemperature'] }
};
VL.subscribeMeasurements = (message) => {
let filteredMessage = VL.filter(message, myFilterOptions);
if (Object.values(filteredMessage).length === 0) {
return;
}
let sensorTemperaturData = filteredMessage.data.find(
(d) => d.name === 'sensors_qaTemperature'
);
let tmpF = celsiusToFahrenheit(sensorTemperaturData?.value);
VL.publishMeasurements([{ name: 'temperatureInF', value: tmpF }]);
};
VL.reportDataPointMetadata = () => {
return [{ name: 'temperatureInF', variable_type: 'FLOAT', unit: '°F' }];
};
Example 3: Subscribe measurement -> publish measurement event
This example calculates the active power
3.1 Datasource
If not already done so, then create a data source on your device. For that:
- navigate to "devices"
- select your device
- press on "+ Data Source"
- fill out the configuration for your data source
3.2 Edge Function
function calculateActivePowerThreePhase(voltage, current, phaseAngle) {
const phaseAngleInRadians = phaseAngle * (Math.PI / 180);
const activePower =
Math.sqrt(3) * voltage * current * Math.cos(phaseAngleInRadians);
return activePower;
}
const myFilterOptions = {
filter: { dataSource: { protocol: 's7' }, data: ['VoltageL1N, CurrentL1'] }
};
VL.subscribeMeasurements = (message) => {
let filteredMessage = VL.filter(message, myFilterOptions);
if (Object.values(filteredMessage).length === 0) {
return;
}
let sensorTemperaturData = filteredMessage.data.find(
(d) => d.name === 'sensors_qaTemperature'
);
let voltage = 230; // in Volts
let current = 10; // in Amperes
let phaseAngle = 30; // in Degrees
let activePower = calculateActivePowerThreePhase(
voltage,
current,
phaseAngle
);
VL.publishMeasurements([{ name: 'activePower', value: tmpF }]);
};
VL.reportDataPointMetadata = () => {
return [{ name: 'activePower', variable_type: 'FLOAT', unit: '°F' }];
};