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

trinidad & tobago, tasmania #1066

Merged
merged 1 commit into from
Oct 30, 2023
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
18 changes: 10 additions & 8 deletions src/adapters/tasmania.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import got from 'got';

export const name = 'tasmania';

export function fetchData (source, cb) {
export function fetchData(source, cb) {
got(source.url, { timeout: { request: REQUEST_TIMEOUT } }).then(
(response) => {
try {
Expand All @@ -34,19 +34,21 @@ export function fetchData (source, cb) {

const formatData = function (data, source) {
const parseDate = function (string) {
const date = DateTime.fromFormat(
string.trim(),
'HHmmss',
{ zone: 'Australia/Hobart' }
);
const now = DateTime.now().setZone('Australia/Tasmania');

const hours = parseInt(string.substring(0, 2), 10);
const minutes = parseInt(string.substring(2, 4), 10);
const seconds = parseInt(string.substring(4, 6), 10);

const date = now.set({ hour: hours, minute: minutes, second: seconds });

if (!date.isValid) {
throw new Error('Invalid date format');
}

return {
utc: date.toUTC().toISO({ suppressMilliseconds: true }),
local: date.toISO({ suppressMilliseconds: true }),
utc: date.toUTC().toFormat("yyyy-MM-dd'T'HH:mm:ssZZ"),
local: date.toFormat("yyyy-MM-dd'T'HH:mm:ssZZ"),
};
};

Expand Down
58 changes: 36 additions & 22 deletions src/adapters/trinidadtobago.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@

import {
unifyParameters,
unifyMeasurementUnits
unifyMeasurementUnits,
} from '../lib/utils.js';
import log from '../lib/logger.js';
import { REQUEST_TIMEOUT } from '../lib/constants.js';
import _ from 'lodash';
import { DateTime } from 'luxon';
Expand All @@ -28,7 +29,7 @@ const timestamp = DateTime.now().toMillis();
* @param {function} cb A callback of the form cb(err, data)
*/

export async function fetchData (source, cb) {
export async function fetchData(source, cb) {
// Fetch both the measurements and meta-data about the locations
// List of keys for parameters used in url [3: 'CO', 1465: 'NO2', 2130: 'O3', 18: 'PM-10', 20: 'PM-2.5', 23: 'SO2']
const parameterIDs = ['3', '1465', '2130', '18', '20', '23'];
Expand All @@ -43,7 +44,10 @@ export async function fetchData (source, cb) {
.replace('$parameter', parameterIDs[i]) + timestamp;
const task = async function () {
try {
const { body } = await got(sourceURL, { cookieJar: new tough.CookieJar(), timeout: { request: REQUEST_TIMEOUT } });
const { body } = await got(sourceURL, {
cookieJar: new tough.CookieJar(),
timeout: { request: REQUEST_TIMEOUT },
});
return { meta: e, values: body };
} catch (err) {
throw new Error(err.response.body);
Expand All @@ -54,7 +58,7 @@ export async function fetchData (source, cb) {
});

try {
const results = await Promise.all(tasks.map(task => task()));
const results = await Promise.all(tasks.map((task) => task()));
// Format the data
const data = formatData(results);
if (data === undefined) {
Expand Down Expand Up @@ -90,15 +94,31 @@ const formatData = function (results) {
}
return data;
};
// Loops through all items
const validParameters = ['CO', 'NO2', 'O3', 'PM-10', 'PM-2.5', 'SO2'];

results.forEach((item) => {
item = parseToJSON(item);
// If values are empty or something fails, dont run

if (item !== undefined) {
let parameter = validParameters.find((p) =>
item.values.hasOwnProperty(p)
);

if (
!parameter ||
item.values[parameter].length !== item.values.xlabels.length
) {
log.info(
'Parameter mismatch or length mismatch between readings and labels.',
item
);
return;
}

const template = {
city: item.meta.city,
location: item.meta.location,
parameter: Object.keys(item.values)[0],
parameter: parameter.toLowerCase(),
coordinates: {
latitude: parseFloat(item.meta.latitude),
longitude: parseFloat(item.meta.longitude),
Expand All @@ -110,18 +130,13 @@ const formatData = function (results) {
},
],
averagingPeriod: { unit: 'hours', value: 1 },
unit: parameter === 'CO' ? 'mg/m3' : 'ug/m3',
};
// Units are mostly ug/m3, but CO is mg/m3, according to site
template.unit = template.parameter === 'CO' ? 'mg/m3' : 'ug/m3';
// Loops through the latest data for 24 hours, data is hourly
for (let i in item.values[template.parameter]) {
// Do not add values if values are Null
if (item.values[template.parameter][i] !== null) {
let m = Object.assign(
{ value: item.values[template.parameter][i] },
template
);
// Adds the formated date

item.values[parameter].forEach((value, i) => {
if (value !== null) {
let m = Object.assign({ value: value }, template);

const dateMoment = DateTime.fromFormat(
item.values.xlabels[i],
'yyyy-MM-dd HH',
Expand All @@ -133,17 +148,16 @@ const formatData = function (results) {
.toISO({ suppressMilliseconds: true }),
local: dateMoment.toISO({ suppressMilliseconds: true }),
};
// unifies parameters and measurement units

m = unifyParameters(m);
m = unifyMeasurementUnits(m);
measurements.push(m);
}
}
});
}
});
// corrects the parameter names

measurements = correctMeasurementParameter(measurements);
// filters out the measurements that are not the latest
measurements = getLatestMeasurements(measurements);
return {
name: 'unused',
Expand Down