@sports-alliance/sports-lib
Version:
A Library to for importing / exporting and processing GPX, TCX, FIT and JSON files from services such as Strava, Movescount, Garmin, Polar etc
130 lines (129 loc) • 6.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventImporterSuuntoSML = void 0;
const importer_suunto_json_1 = require("./importer.suunto.json");
const helpers_1 = require("../../../utilities/helpers");
const activity_parsing_options_1 = require("../../../../activities/activity-parsing-options");
const fast_xml_parser_1 = require("fast-xml-parser");
class EventImporterSuuntoSML {
static getFromXML(contents, options = activity_parsing_options_1.ActivityParsingOptions.DEFAULT, _name = 'New Event') {
const parser = new fast_xml_parser_1.XMLParser();
const json = parser.parse(contents).sml;
// debugger;
// A few mods here to convert it to compatible json suunto string
json.DeviceLog.Samples = json.DeviceLog.Samples.Sample;
const samplesWithUTC = json.DeviceLog.Samples.filter((sample) => !!sample.UTC);
// Find the first UTC timestamped sample and use it later for start date
const startDate = samplesWithUTC.length
? new Date(samplesWithUTC[0].UTC)
: new Date(json.DeviceLog.Header.DateTime);
// Determine the end date
const endDate = samplesWithUTC.length > 1
? samplesWithUTC[samplesWithUTC.length - 1].UTC
: new Date(startDate.getTime() + json.DeviceLog.Header.Duration * 1000);
// Filter out the old activity type
json.DeviceLog.Samples = json.DeviceLog.Samples.filter((sample) => {
return !(sample.Events && sample.Events.Activity);
});
// Convert the events
json.DeviceLog.Samples.filter((sample) => !!sample.Events).forEach((sample) => {
sample.Events = [sample.Events];
});
// Inject start and end sample with event
json.DeviceLog.Samples.unshift({
Events: [
{
Activity: { ActivityType: json.DeviceLog.Header.ActivityType }
}
],
TimeISO8601: startDate.toISOString()
});
// Add the end time and adjust the start time
json.DeviceLog.Header.TimeISO8601 = json.DeviceLog.Header.TimeISO8601 || endDate;
// Add the time on the samples
json.DeviceLog.Samples.forEach((sample) => {
if (sample.TimeISO8601) {
return;
}
if (sample.UTC) {
sample.TimeISO8601 = new Date(sample.UTC).toISOString();
return;
}
if ((0, helpers_1.isNumber)(sample.Time)) {
sample.TimeISO8601 = new Date(startDate.getTime() + sample.Time * 1000).toISOString();
return;
}
});
// Convert the RR
if (json.DeviceLog['R-R']) {
json.DeviceLog['R-R'].Data = json.DeviceLog['R-R'].Data.split(' ').map((dataString) => Number(dataString));
}
json.DeviceLog.Header.Altitude = json.DeviceLog.Header.Altitude ? [json.DeviceLog.Header.Altitude] : null;
json.DeviceLog.Header.HR = json.DeviceLog.Header.HR ? [json.DeviceLog.Header.HR] : null;
json.DeviceLog.Header.Cadence = json.DeviceLog.Header.Cadence ? [json.DeviceLog.Header.Cadence] : null;
json.DeviceLog.Header.Speed = json.DeviceLog.Header.Speed ? [json.DeviceLog.Header.Speed] : null;
json.DeviceLog.Header.Power = json.DeviceLog.Header.Power ? [json.DeviceLog.Header.Power] : null;
json.DeviceLog.Header.Temperature = json.DeviceLog.Header.Temperature ? [json.DeviceLog.Header.Temperature] : null;
json.DeviceLog.Windows = [{ Window: Object.assign({ Type: 'Activity' }, json.DeviceLog.Header) }];
// debugger;
return importer_suunto_json_1.EventImporterSuuntoJSON.getFromJSONString(JSON.stringify(json), options);
}
static getFromJSONString(jsonString, options = activity_parsing_options_1.ActivityParsingOptions.DEFAULT) {
const json = JSON.parse(jsonString);
let samples;
// Try to be a hero here
try {
samples = json.Samples.filter((sample) => !!JSON.parse(sample.Attributes)['suunto/sml'].Sample).map((sample) => {
return Object.assign({ TimeISO8601: sample.TimeISO8601 }, JSON.parse(sample.Attributes)['suunto/sml'].Sample);
});
}
catch (_e) {
samples = json.Samples.filter((sample) => !!sample.Attributes['suunto/sml'].Sample).map((sample) => {
return Object.assign({ TimeISO8601: sample.TimeISO8601 }, sample.Attributes['suunto/sml'].Sample);
});
}
let rr;
try {
rr = {
Data: json.Samples.filter((sample) => !!JSON.parse(sample.Attributes)['suunto/sml']['R-R'])
.map((sample) => {
return JSON.parse(sample.Attributes)['suunto/sml']['R-R'];
})
.reduce((accu, rrSample) => {
return accu.concat(rrSample.Data.split(',').map((dataString) => Number(dataString)));
}, [])
};
}
catch (_e) {
rr = {
Data: json.Samples.filter((sample) => !!sample.Attributes['suunto/sml']['R-R'])
.map((sample) => {
return sample.Attributes['suunto/sml']['R-R'];
})
.reduce((accu, rrSample) => {
return accu.concat(rrSample.IBI);
}, [])
};
}
// debugger;
const suuntoJSON = {
DeviceLog: {
Header: {},
Device: {
Name: 'Suunto unknown',
Info: {
HW: 0,
SW: 0,
SerialNumber: 0
}
},
Windows: [],
Samples: samples,
['R-R']: rr
}
};
// debugger;
return importer_suunto_json_1.EventImporterSuuntoJSON.getFromJSONString(JSON.stringify(suuntoJSON), options);
}
}
exports.EventImporterSuuntoSML = EventImporterSuuntoSML;