Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lf 3281 handle and convert as needed incoming temperature readings from ensemble #2692

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions packages/api/src/controllers/sensorController.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ import {
registerOrganizationWebhook,
bulkSensorClaim,
unclaimSensor,
ENSEMBLE_UNITS_MAPPING,
} from '../util/ensemble.js';

import { databaseUnit } from '../util/unit.js';
import { sensorErrors, parseSensorCsv } from '../../../shared/validation/sensorCSV.js';
import syncAsyncResponse from '../util/syncAsyncResponse.js';
import knex from '../util/knex.js';
Expand Down Expand Up @@ -446,11 +447,18 @@ const sensorController = {
);
continue;
}
const unit = sensorInfo.unit;
// Reconcile incoming units with stored as units and conversion function keys
const system = ENSEMBLE_UNITS_MAPPING[sensorInfo.unit]?.system;
const unit = ENSEMBLE_UNITS_MAPPING[sensorInfo.unit]?.conversionKey;
const readingTypeStoredAsUnit = databaseUnit[readingType] ?? undefined;
const isStoredAsUnit = unit == readingTypeStoredAsUnit;

if (sensorInfo.values.length < sensorInfo.timestamps.length)
if (sensorInfo.values.length < sensorInfo.timestamps.length) {
return res.status(400).send('sensor values and timestamps are not in sync');

}
if (!system || !unit || !readingTypeStoredAsUnit || !isStoredAsUnit) {
return res.status(400).send('provided units are not supported');
}
for (let k = 0; k < sensorInfo.values.length; ++k) {
infoBody.push({
read_time: sensorInfo.timestamps[k] || '',
Expand All @@ -463,6 +471,7 @@ const sensorController = {
}
}
}

if (infoBody.length === 0) {
return res.status(200).json({
error: 'No records of sensor readings added to the Litefarm.',
Expand Down
24 changes: 23 additions & 1 deletion packages/api/src/util/ensemble.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ if (process.env.NODE_ENV === 'integration') {
baseUrl = 'http://localhost:' + process.env.PORT;
}

//Known aliases for units from ensemble mapping to convert-units representation
const ENSEMBLE_UNITS_MAPPING = {
Celsius: {
conversionKey: 'C',
system: 'metric',
},
kPa: {
conversionKey: 'kPa',
system: 'metric',
},
Percent: {
conversionKey: '%',
system: 'all',
},
};

/**
* Sends a request to the Ensemble API for an organization to claim sensors
* @param {String} accessToken - a JWT token for accessing the Ensemble API
Expand Down Expand Up @@ -313,4 +329,10 @@ async function unclaimSensor(org_id, external_id, access_token) {
}
}

export { bulkSensorClaim, registerOrganizationWebhook, createOrganization, unclaimSensor };
export {
bulkSensorClaim,
registerOrganizationWebhook,
createOrganization,
unclaimSensor,
ENSEMBLE_UNITS_MAPPING,
};
9 changes: 9 additions & 0 deletions packages/api/src/util/unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// DO NOT CHANGE!
// Any change to this will lead to need for database backfilling.
// This is how we are storing INCOMING EXTERNAL data
// Please see sister file on frontend for internal database units
export const databaseUnit = {
temperature: 'C',
soil_water_potential: 'kPa',
soil_water_content: '%',
};