UNPKG

@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

160 lines (159 loc) 9.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FITCreatorMapper = void 0; const creator_1 = require("../creators/creator"); const helpers_1 = require("../events/utilities/helpers"); const garmin_profile_mapper_1 = require("./garmin-profile.mapper"); const importer_fit_ant_plus_device_names_1 = require("./device-names/importer.fit.ant-plus.device.names"); const importer_fit_coros_device_names_1 = require("./device-names/importer.fit.coros.device.names"); const importer_fit_development_device_names_1 = require("./device-names/importer.fit.development.device.names"); const importer_fit_hammerhead_device_names_1 = require("./device-names/importer.fit.hammerhead.device.names"); const importer_fit_lezyne_device_names_1 = require("./device-names/importer.fit.lezyne.device.names"); const importer_fit_magellan_device_names_1 = require("./device-names/importer.fit.magellan.device.names"); const importer_fit_saris_device_names_1 = require("./device-names/importer.fit.saris.device.names"); const importer_fit_srm_device_names_1 = require("./device-names/importer.fit.srm.device.names"); const importer_fit_suunto_device_names_1 = require("./device-names/importer.fit.suunto.device.names"); const importer_fit_wahoo_device_names_1 = require("./device-names/importer.fit.wahoo.device.names"); class FITCreatorMapper { static getCreatorFromFitDataObject(fitDataObject) { var _a; const fileId = ((_a = fitDataObject === null || fitDataObject === void 0 ? void 0 : fitDataObject.file_ids) === null || _a === void 0 ? void 0 : _a[0]) || {}; const fileCreator = (fitDataObject === null || fitDataObject === void 0 ? void 0 : fitDataObject.file_creator) || {}; const deviceInfo = (fitDataObject === null || fitDataObject === void 0 ? void 0 : fitDataObject.device_info) || {}; const manufacturer = fileId.manufacturer; const productId = fileId.product || null; let productName = fileId.product_name || null; let recognizedName = null; let creator; switch (manufacturer) { case 'antplus': { recognizedName = importer_fit_ant_plus_device_names_1.ImporterFitAntPlusDeviceNames[productId]; creator = new creator_1.Creator(this.formatDeviceName(manufacturer, productName, recognizedName, 'ANT+'), productId); break; } case 'suunto': { recognizedName = importer_fit_suunto_device_names_1.ImporterFitSuuntoDeviceNames[productId]; creator = new creator_1.Creator(this.formatDeviceName(manufacturer, productName, recognizedName, 'Suunto'), productId); break; } case 'coros': { recognizedName = importer_fit_coros_device_names_1.ImporterFitCorosDeviceNames[productId]; creator = new creator_1.Creator(this.formatDeviceName(manufacturer, productName, recognizedName, 'Coros'), productId); break; } case 'garmin': { recognizedName = garmin_profile_mapper_1.GarminProfileMapper.getDeviceName(productId); creator = new creator_1.Creator(this.formatDeviceName(manufacturer, productName, recognizedName, 'Garmin'), productId); break; } case 'wahoo_fitness': { recognizedName = importer_fit_wahoo_device_names_1.ImporterFitWahooDeviceNames[productId]; creator = new creator_1.Creator(this.formatDeviceName(manufacturer, productName, recognizedName, 'Wahoo'), productId); break; } case 'hammerhead': { recognizedName = importer_fit_hammerhead_device_names_1.ImporterFitHammerheadDeviceNames[productId]; creator = new creator_1.Creator(this.formatDeviceName(manufacturer, productName, recognizedName, 'Hammerhead'), productId); break; } case 'lezyne': { recognizedName = importer_fit_lezyne_device_names_1.ImporterFitLezyneDeviceNames[productId]; creator = new creator_1.Creator(this.formatDeviceName(manufacturer, productName, recognizedName, 'Lezyne'), productId); break; } case 'magellan': { recognizedName = importer_fit_magellan_device_names_1.ImporterFitMagellanDeviceNames[productId]; creator = new creator_1.Creator(this.formatDeviceName(manufacturer, productName, recognizedName, 'Magellan'), productId); break; } case 'saris': { recognizedName = importer_fit_saris_device_names_1.ImporterFitSarisDeviceNames[productId]; creator = new creator_1.Creator(this.formatDeviceName(manufacturer, productName, recognizedName, 'Saris'), productId); break; } case 'srm': { recognizedName = importer_fit_srm_device_names_1.ImporterFitSrmDeviceNames[productId]; creator = new creator_1.Creator(this.formatDeviceName(manufacturer, productName, recognizedName, 'SRM'), productId); break; } case 'zwift': { recognizedName = 'Zwift'; creator = new creator_1.Creator(recognizedName); break; } case 'virtualtraining': { recognizedName = 'Rouvy'; creator = new creator_1.Creator(recognizedName); break; } case 'the_sufferfest': { recognizedName = `Wahoo SYSTM`; creator = new creator_1.Creator(recognizedName, productId); break; } case 'stryd': { recognizedName = `Stryd`; creator = new creator_1.Creator(recognizedName, productId, fileCreator.software_version, fileCreator.hardware_version, fileId.serial_number); break; } case 'development': { recognizedName = importer_fit_development_device_names_1.ImporterFitDevelopmentDeviceNames[productId]; creator = new creator_1.Creator(this.formatDeviceName(manufacturer, productName, recognizedName, null, true), productId); creator.isRecognized = typeof recognizedName === 'string' || recognizedName === null; break; } default: { const manufacturerName = typeof manufacturer === 'number' ? garmin_profile_mapper_1.GarminProfileMapper.getManufacturerName(manufacturer) : manufacturer; if (manufacturerName === 'garmin') { recognizedName = garmin_profile_mapper_1.GarminProfileMapper.getDeviceName(productId); } productName = fileId.product_name || null; creator = new creator_1.Creator(this.formatDeviceName(manufacturerName, productName, recognizedName, manufacturerName === 'garmin' ? 'Garmin' : null), productId); } } creator.manufacturer = manufacturer; creator.isRecognized = creator.isRecognized || !!recognizedName; if ((0, helpers_1.isNumberOrString)(fileCreator.hardware_version)) { creator.hwInfo = String(fileCreator.hardware_version); } if ((0, helpers_1.isNumberOrString)(fileCreator.software_version)) { creator.swInfo = String(fileCreator.software_version); } else if ((0, helpers_1.isNumberOrString)(deviceInfo.software_version)) { creator.swInfo = String(deviceInfo.software_version); } if ((0, helpers_1.isNumberOrString)(fileId.serial_number)) { creator.serialNumber = fileId.serial_number; } if (Number.isFinite(creator.name) || creator.name.match(/^\d+$/)) { creator.name = `Unknown`; } return creator; } static formatDeviceName(manufacturer, productName, recognizedName, recognizedBrand, isDevelopment = false) { const manufacturerString = (0, helpers_1.isNumberOrString)(manufacturer) ? String(manufacturer) : null; if (recognizedBrand && recognizedName) { return `${this.toStartCase(recognizedBrand)} ${recognizedName}`; } if (recognizedBrand && !recognizedName && productName) { if (productName.match(new RegExp(`${recognizedBrand}`, 'gi'))) { productName = productName.replace(new RegExp(`${recognizedBrand}`, 'gi'), '').trim(); } return `${this.toStartCase(recognizedBrand)} ${productName}`; } if (recognizedBrand && !recognizedName && !productName) { return `${this.toStartCase(recognizedBrand)}`; } if (manufacturerString && !recognizedBrand && !recognizedName && !productName && !isDevelopment) { return `${this.toStartCase(manufacturerString.replace(new RegExp('[-_]', 'gi'), ' ').trim())}`; } if (!recognizedBrand && recognizedName) { return `${recognizedName}`; } return 'Unknown'; } static toStartCase(str) { return str.replace(/\w\S*/g, txt => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()); } } exports.FITCreatorMapper = FITCreatorMapper;