Custom activity streams with Javascript

Gave Di2/eTap/EPS streams a shot. I have something that works for me and in theory it should be universal, but if it doesn’t work for you, try and share a FIT file and I might give it a look. No promises though. Search for “Di2” in custom streams and they should all show up.

This only works if FIT is available, since it needs the FIT gear_change_event messages.

I have written/tested this with data from Wahoo+Di2, so it’s entirely possible there are nuances I missed with other products.

I also made a Chart that combines front and rear gear:

Here’s the code for one of them:

{
    function cmp(a, b) {
        return a.timestamp.value - b.timestamp.value;
    }

    let gear_event_names = ["FRONT_GEAR_CHANGE", "REAR_GEAR_CHANGE"];
    let change_events = [];
    let start_timestamp;
    for (let m = 0; m < icu.fit.length; m++) {
        let r = icu.fit[m];
        if (r.event == null) continue;
        if (gear_event_names.includes(r.event.valueName)) {
            change_events.push(r);
        } else if (r.event.valueName === "TIMER" && r.event_type.valueName === "START") {
            start_timestamp = start_timestamp ? Math.min(start_timestamp, r.timestamp.value) : r.timestamp.value;
        }
    }

    change_events.sort(cmp); // Probably unnecessary?

    let value = undefined;
    for (let x=0; x < icu.streams.time.length; x++) {
        let t = icu.streams.time[x];
        while (change_events.length > 0 && t >= change_events[0].timestamp.value - start_timestamp) {
            let next_event = change_events.shift();
            value = next_event.front_gear.value;
            if (Array.isArray(value)) {
                // I am not sure why, but sometimes the value is an array with the same value twice?
                value = value[0];
            }
        }
        data[x] = value;
    }
}

The only thing different between the charts is the line starting with value = , which either get front_gear, front_gear_num, rear_gear or rear_gear_num.

I imagine this could be used as the basis for some maybe-useful charts as well. I hope someone grabs the code and makes something cool with it :slight_smile:

4 Likes