Skip to content

Commit

Permalink
Added support for sensor status,versions and instances
Browse files Browse the repository at this point in the history
  • Loading branch information
caparker committed Sep 19, 2024
1 parent 8585b43 commit 7ec511c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 11 deletions.
18 changes: 15 additions & 3 deletions fetcher/providers/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ class Sensor {
this.sensor_id = data.sensor_id;
this.parameter = null;
this.interval_seconds = null;
this.metadata = {};
this.version_date = null;
this.instance = null;
this.status = null;
this.metadata = null;
classAssign(this, data, []);
}

Expand All @@ -206,6 +209,9 @@ class Sensor {
json() {
return stripNulls({
sensor_id: this.sensor_id,
version_date: this.version_date,
status: this.status,
instance: this.instance,
parameter: this.parameter,
interval_seconds: this.interval_seconds,
});
Expand Down Expand Up @@ -274,7 +280,7 @@ class Client {
const location_id = this.getLocationId(row);
let key = null;
if (manufacturer && model) {
key = `${manufacturer}::${model}`;
key = `${manufacturer}:${model}`;
} else if (!manufacturer & !model) {
key = 'default';
} else {
Expand All @@ -292,10 +298,15 @@ class Client {
getSensorId(row) {
const measurand = this.measurands[row.metric];
const location_id = this.getLocationId(row);
const version = cleanKey(row.version_date);
const instance = cleanKey(row.instance);
if (!measurand) {
throw new Error(`Could not find measurand for ${row.metric}`);
}
return `${location_id}-${measurand.parameter}`;
let key = [measurand.parameter];

Check failure on line 306 in fetcher/providers/generic.js

View workflow job for this annotation

GitHub Actions / build

'key' is never reassigned. Use 'const' instead
if (instance) key.push(instance);
if (version) key.push(version);
return `${location_id}-${key.join(':')}`;
}


Expand Down Expand Up @@ -440,6 +451,7 @@ class Client {
const sensor_id = this.getSensorId({
location: d[this.location_key],
metric: d[this.parameter_key],
...d,
});

const system_id = this.getSystemId(d);
Expand Down
62 changes: 59 additions & 3 deletions test/generic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ tape('simple simple sensors work', async (t) => {
t.equal(loc1.systems[0].sensors.length, 1, '1 sensor added to system');
t.equal(loc2.systems.length, 1, 'One systems were added to #2');
t.equal(loc2.systems[0].sensors.length, 2, '2 sensors added to system');
t.equal(loc2.systems[0].system_id, 'testing-test_site_2-metone::aio2', 'Has correct system name');
t.equal(loc2.systems[0].sensors[0].status, 'u', 'has correct status');
t.equal(loc2.systems[0].system_id, 'testing-test_site_2-metone:aio2', 'Has correct system name');
t.end();
});

Expand Down Expand Up @@ -181,7 +182,6 @@ tape('simple all files work', async (t) => {
}));

const data = client.data();
//console.dir(data, {depth:null})
const loc1 = data.locations[0];
const loc2 = data.locations[1];

Expand All @@ -193,6 +193,62 @@ tape('simple all files work', async (t) => {
t.equal(loc1.systems[0].sensors.length, 1, '1 sensor added to system');
t.equal(loc2.systems.length, 1, 'One systems were added to #2');
t.equal(loc2.systems[0].sensors.length, 2, '2 sensors added to system');
t.equal(loc2.systems[0].system_id, 'testing-test_site_2-metone::aio2', 'Has correct system name');
t.equal(loc2.systems[0].system_id, 'testing-test_site_2-metone:aio2', 'Has correct system name');
t.end();
});


tape('versioned sensors work', async (t) => {

const client = new generic.Client(config);
await client.fetchMeasurands();

const files = [
{
"type": "sensors",
"path": `${__dirname}/test_sensors_versioned.csv`
}
];

await Promise.all(files.map(async (file) => {
await client.processData(file);
}));

const data = client.data();
const loc1 = data.locations[0];

t.equal(data.locations.length, 1, 'locations were added');
t.equal(loc1.systems.length, 1, 'systems were added to #1');
t.equal(loc1.systems[0].sensors.length, 2, '2 sensor added to system');
t.end();
});


/**
* This would be the case when one node/location has more than one
* instance of a given parameter/measurand
*/
tape('sensors instances work', async (t) => {

const client = new generic.Client(config);
await client.fetchMeasurands();

const files = [
{
"type": "sensors",
"path": `${__dirname}/test_sensor_instances.csv`
}
];

await Promise.all(files.map(async (file) => {
await client.processData(file);
}));

const data = client.data();
const loc1 = data.locations[0];

t.equal(data.locations.length, 1, 'locations were added');
t.equal(loc1.systems.length, 1, 'systems were added to #1');
t.equal(loc1.systems[0].sensors.length, 2, '2 sensor added to system');
t.end();
});
3 changes: 3 additions & 0 deletions test/test_sensor_instances.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
location,parameter,units,manufacturer_name,model_name,interval_seconds,instance
Test Site #1,ws,m/s,MetOne,AIO2,3600,a
Test Site #1,ws,m/s,MetOne,AIO2,3600,b
10 changes: 5 additions & 5 deletions test/test_sensors_simple.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
location,parameter,units,manufacturer_name,model_name,interval_seconds
Test Site #1,ws,m/s,MetOne,AIO2,3600
Test Site #1,co,ppb,Ecotech,Serinus 30,3600
Test Site #2,ws,m/s,MetOne,AIO2,3600
Test Site #2,wd,deg,MetOne,AIO2,3600
location,parameter,units,manufacturer_name,model_name,interval_seconds,status
Test Site #1,ws,m/s,MetOne,AIO2,3600,u
Test Site #1,co,ppb,Ecotech,Serinus 30,3600,u
Test Site #2,ws,m/s,MetOne,AIO2,3600,u
Test Site #2,wd,deg,MetOne,AIO2,3600,u
3 changes: 3 additions & 0 deletions test/test_sensors_versioned.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
location,parameter,units,manufacturer_name,model_name,interval_seconds,status,version_date
Test Site #1,ws,m/s,MetOne,AIO2,3600,u,
Test Site #1,ws,m/s,MetOne,AIO2,3600,r,20240101

0 comments on commit 7ec511c

Please sign in to comment.