// -----------------------
// TypeScript [2.6.2]
// -----------------------
export default function (segments: Array<string>): Broadcast {
let broadcast = {
takenAt: (new Date()).getTime(),
ordinalId: 0,
buildMajor: 0,
buildMinor: 0,
interval: 0,
realTime: false,
cadence: 0,
heartRate: 0,
power: 0,
caloricBurn: 0,
duration: 0,
gear: 0,
distance: 0
} as Broadcast
broadcast.ordinalId = parseInt(segments[0], 10)
if (broadcast.ordinalId <= 0 || broadcast.ordinalId > 200) {
throw new Error('Invalid machine id')
}
if (segments[4] !== 'F') {
throw new Error('Duplicate machine ids detected [' + broadcast.ordinalId + ']')
}
const buildSegments = segments[6].split('.')
broadcast.buildMajor = parseInt(buildSegments[0], 10)
if (broadcast.buildMajor !== 6) {
throw new Error('Invalid build major')
}
broadcast.buildMinor = parseInt(buildSegments[1], 10)
if (broadcast.buildMinor < 0 || broadcast.buildMinor > 99) {
throw new Error('Invalid build minor')
}
const dataType = parseInt(segments[7], 10)
if (dataType === 0 || dataType === 255) {
broadcast.interval = 0
} else if (dataType > 128 && dataType < 255) {
broadcast.interval = dataType - 128
}
broadcast.realTime = dataType === 0 || (dataType > 128 && dataType < 255)
broadcast.cadence = Math.round(parseInt(segments[8], 10) / 10)
broadcast.heartRate = Math.round(parseInt(segments[9], 10) / 10) || null
broadcast.power = parseInt(segments[10], 10)
broadcast.caloricBurn = parseInt(segments[11], 10)
const timeSegments = segments[12].split(':')
broadcast.duration = parseInt(timeSegments[0], 10) * 60 + parseInt(timeSegments[1], 10)
broadcast.gear = parseInt(segments[13], 10) || null
if (segments[15] === 'Mi') {
// Imperial (to Metric)
broadcast.distance = (parseInt(segments[14], 10) / 10) * 1.60934
} else {
// Metric
broadcast.distance = parseInt(segments[14], 10) / 10
}
return broadcast
}