@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
233 lines (232 loc) • 10.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventImporterJSON = void 0;
const event_1 = require("../../../event");
const activity_1 = require("../../../../activities/activity");
const lap_1 = require("../../../../laps/lap");
const creator_1 = require("../../../../creators/creator");
const intensity_zones_1 = require("../../../../intensity-zones/intensity-zones");
const data_store_1 = require("../../../../data/data.store");
const lap_types_1 = require("../../../../laps/lap.types");
const activity_types_1 = require("../../../../activities/activity.types");
const stream_1 = require("../../../../streams/stream");
const data_ibi_1 = require("../../../../data/data.ibi");
const ibi_stream_1 = require("../../../../streams/ibi-stream");
const device_1 = require("../../../../activities/devices/device");
const data_time_1 = require("../../../../data/data.time");
const data_power_curve_1 = require("../../../../data/data.power-curve");
const swim_length_1 = require("../../../../swim-lengths/swim-length");
class EventImporterJSON {
static getEventFromJSON(json) {
const event = new event_1.Event(json.name, new Date(json.startDate), new Date(json.endDate), json.srcFileType, json.privacy, json.description || undefined, json.isMerge || false);
this.addStatsFromJSON(event, json.stats || {});
if (json.powerCurve && json.powerCurve[data_power_curve_1.DataPowerCurve.type] !== undefined) {
event.powerCurve = new data_power_curve_1.DataPowerCurve(json.powerCurve[data_power_curve_1.DataPowerCurve.type]);
event.addStat(event.powerCurve);
}
(json.activities || []).forEach(activityJSON => {
event.addActivity(this.getActivityFromJSON(activityJSON));
});
return event;
}
static getCreatorFromJSON(json) {
const creator = new creator_1.Creator(json.name || 'Unknown Device');
if (json.hwInfo) {
creator.hwInfo = json.hwInfo;
}
if (json.swInfo) {
creator.swInfo = json.swInfo;
}
if (json.serialNumber) {
creator.serialNumber = json.serialNumber;
}
if (json.manufacturer) {
creator.manufacturer = json.manufacturer;
}
if (json.isRecognized) {
creator.isRecognized = json.isRecognized;
}
if (json.productId) {
creator.productId = json.productId;
}
if (json.devices && json.devices.length) {
json.devices.forEach(jsonDevice => creator.devices.push(this.getDeviceFromJSON(jsonDevice)));
}
return creator;
}
static getDeviceFromJSON(json) {
const device = new device_1.Device(json.type);
if (json.index !== null && json.index !== undefined) {
device.index = json.index;
}
if (json.name !== null && json.name !== undefined) {
device.name = json.name;
}
if (json.batteryStatus !== null && json.batteryStatus !== undefined) {
device.batteryStatus = json.batteryStatus;
}
if (json.batteryLevel !== null && json.batteryLevel !== undefined) {
device.batteryLevel = json.batteryLevel;
}
if (json.batteryVoltage !== null && json.batteryVoltage !== undefined) {
device.batteryVoltage = json.batteryVoltage;
}
if (json.manufacturer !== null && json.manufacturer !== undefined) {
device.manufacturer = json.manufacturer;
}
if (json.serialNumber !== null && json.serialNumber !== undefined) {
device.serialNumber = json.serialNumber;
}
if (json.product !== null && json.product !== undefined) {
device.product = json.product;
}
if (json.swInfo !== null && json.swInfo !== undefined) {
device.swInfo = json.swInfo;
}
if (json.hwInfo !== null && json.hwInfo !== undefined) {
device.hwInfo = json.hwInfo;
}
if (json.antDeviceNumber !== null && json.antDeviceNumber !== undefined) {
device.antDeviceNumber = json.antDeviceNumber;
}
if (json.antTransmissionType !== null && json.antTransmissionType !== undefined) {
device.antTransmissionType = json.antTransmissionType;
}
if (json.antNetwork !== null && json.antNetwork !== undefined) {
device.antNetwork = json.antNetwork;
}
if (json.sourceType !== null && json.sourceType !== undefined) {
device.sourceType = json.sourceType;
}
if (json.antId !== null && json.antId !== undefined) {
device.antId = json.antId;
}
if (json.cumOperatingTime !== null && json.cumOperatingTime !== undefined) {
device.cumOperatingTime = json.cumOperatingTime;
}
if (json.timestamp) {
device.timestamp = new Date(json.timestamp);
}
return device;
}
static getLapFromJSON(json, lapIndex) {
const lap = new lap_1.Lap(new Date(json.startDate), new Date(json.endDate), lapIndex + 1, lap_types_1.LapTypes[json.type]);
this.addStatsFromJSON(lap, json.stats);
return lap;
}
static getCanonicalDataType(dataType) {
try {
return data_store_1.DynamicDataLoader.getDataClassFromDataType(dataType).type;
}
catch (_error) {
return dataType;
}
}
static shouldReplaceCanonicalEntry(existing, sourceType, canonicalType) {
if (!existing) {
return true;
}
return existing.sourceType !== canonicalType && sourceType === canonicalType;
}
static getCanonicalJSONMap(json) {
const canonicalMap = new Map();
Object.keys(json || {}).forEach(sourceType => {
const canonicalType = this.getCanonicalDataType(sourceType);
const existing = canonicalMap.get(canonicalType);
if (this.shouldReplaceCanonicalEntry(existing, sourceType, canonicalType)) {
canonicalMap.set(canonicalType, { sourceType, value: json[sourceType] });
}
});
return canonicalMap;
}
static addStatsFromJSON(target, stats = {}) {
this.getCanonicalJSONMap(stats).forEach((entry, canonicalType) => {
target.addStat(data_store_1.DynamicDataLoader.getDataInstanceFromDataType(canonicalType, entry.value));
});
}
static getStreamFromJSON(json) {
const streamType = this.getCanonicalDataType(json.type);
if (streamType === data_ibi_1.DataIBI.type) {
return new ibi_stream_1.IBIStream(json.data);
}
return new stream_1.Stream(streamType, json.data);
}
static getCanonicalStreamsFromJSON(streams) {
const canonicalMap = new Map();
const addStream = (sourceType, data) => {
const canonicalType = this.getCanonicalDataType(sourceType);
if (canonicalType === data_time_1.DataTime.type) {
return;
}
const existing = canonicalMap.get(canonicalType);
if (this.shouldReplaceCanonicalEntry(existing, sourceType, canonicalType)) {
canonicalMap.set(canonicalType, {
sourceType,
value: {
type: canonicalType,
data
}
});
}
};
if (Array.isArray(streams)) {
streams.forEach(streamJson => addStream(streamJson.type, streamJson.data));
}
else {
Object.keys(streams || {}).forEach(streamKey => {
addStream(streamKey, streams[streamKey]);
});
}
return Array.from(canonicalMap.values()).map(entry => entry.value);
}
static getIntensityZonesFromJSON(json) {
const zones = new intensity_zones_1.IntensityZones(json.type);
zones.zone1Duration = json.zone1Duration;
zones.zone1LowerLimit = json.zone1LowerLimit;
zones.zone2Duration = json.zone2Duration;
zones.zone2LowerLimit = json.zone2LowerLimit;
zones.zone3Duration = json.zone3Duration;
zones.zone3LowerLimit = json.zone3LowerLimit;
zones.zone4Duration = json.zone4Duration;
zones.zone4LowerLimit = json.zone4LowerLimit;
zones.zone5Duration = json.zone5Duration;
zones.zone5LowerLimit = json.zone5LowerLimit;
zones.zone6Duration = json.zone6Duration;
zones.zone6LowerLimit = json.zone6LowerLimit;
zones.zone7Duration = json.zone7Duration;
zones.zone7LowerLimit = json.zone7LowerLimit;
return zones;
}
static getActivityEventFromJSON(json) {
return (data_store_1.DynamicDataLoader.getDataInstanceFromDataType(Object.keys(json)[0], Object.values(json)[0]));
}
static getActivityFromJSON(json) {
const activity = new activity_1.Activity(new Date(json.startDate), new Date(json.endDate), activity_types_1.ActivityTypes[json.type], EventImporterJSON.getCreatorFromJSON(json.creator));
this.addStatsFromJSON(activity, json.stats);
if (json.powerCurve && json.powerCurve[data_power_curve_1.DataPowerCurve.type] !== undefined) {
activity.powerCurve = new data_power_curve_1.DataPowerCurve(json.powerCurve[data_power_curve_1.DataPowerCurve.type]);
activity.addStat(activity.powerCurve);
}
json.laps.forEach((lapJSON, index) => {
activity.addLap(EventImporterJSON.getLapFromJSON(lapJSON, index));
});
if (Array.isArray(json.swimLengths)) {
json.swimLengths.forEach(swimLengthJSON => {
activity.addSwimLength(swim_length_1.SwimLength.fromJSON(swimLengthJSON));
});
}
this.getCanonicalStreamsFromJSON(json.streams).forEach(streamJson => {
activity.addStream(EventImporterJSON.getStreamFromJSON(streamJson));
});
json.intensityZones.forEach(intensityZonesJSON => {
activity.intensityZones.push(EventImporterJSON.getIntensityZonesFromJSON(intensityZonesJSON));
});
if (json.events) {
json.events.forEach(activityEvent => {
activity.addEvent(this.getActivityEventFromJSON(activityEvent));
});
}
return activity;
}
}
exports.EventImporterJSON = EventImporterJSON;