Skip to content

SlopeDetector

roosit edited this page Oct 16, 2026 · 4 revisions

SlopeDetector

Up | Previous | Next | Русский

Full source with short explanations

Below is the full source of the class in documentation form. The comments inside the block explain the purpose of the parts of the class, but they are not stored in the working file SlopeDetector.java: the production code stays clean, and the explanations live in the wiki.

package com.velitask.plugin.official.slope;

import com.velitask.sdk.data.SlopeSensorAtom;
import java.util.ArrayList;
import java.util.List;

/*
 * Streaming detector of slope groups. Accepts atoms one at a time in time order
 * and collects consecutive atoms with the same slopeType into groups.
 */
public class SlopeDetector {

    /* The group that atoms are currently accumulated into. */
    private SlopeGroup mCurrentGroup;
    /* All collected groups in the order they were formed. */
    private final List<SlopeGroup> mGroups = new ArrayList<>();

    public List<SlopeGroup> getGroups() {
        return mGroups;
    }

    public SlopeGroup getCurrentGroup() {
        return mCurrentGroup;
    }

    /*
     * Adds an atom. Returns true if a new group has started
     * (the first atom or a change of slopeType), and false if the atom was appended to the current one.
     */
    public boolean add(SlopeSensorAtom a) {
        if (mCurrentGroup == null) {
            mGroups.add(mCurrentGroup = new SlopeGroup(a));
            return true;
        }
        if (mCurrentGroup.getType() == a.slopeType) {
            mCurrentGroup.add(a);
            return false;
        }
        mGroups.add(mCurrentGroup = new SlopeGroup(a));
        return true;
    }
}

Purpose

SlopeDetector merges a sequence of slope atoms (SlopeSensorAtom) into groups — continuous sections of one type: uphill, downhill or flat. The groups are then used to build the rows of the slope_groups table, which are used by SlopeTextIndicator and SlopeChartIndicator.

How it works

  • The detector is streaming: the calling code passes atoms one at a time in timeRaw order.
  • The first atom opens the first group.
  • Each following atom is compared by slopeType with the current group: if it matches, it is appended to it (add returns false); if not, a new group is opened (add returns true).
  • getGroups() returns all groups in creation order, getCurrentGroup() returns the last opened one.
  • Usage: VelitaskPlagin.onSourceImported reads all atoms of the slope sensor in timeRaw order, runs them through the detector, then calls SlopeGroup.buildAtom for each group and writes the result to the plugin database (see VelitaskPlagin). The return value of add is not used there.

Features and limitations

  • The detector does not smooth the data: every short-lived change of slopeType creates a separate group, including one made of a single atom.
  • The order of atoms is not checked — with unsorted input the groups will be wrong.
  • The detector knows nothing about the database or the sensor; the sensor identifier and the group number (measureIndex) are filled in later in SlopeGroup.buildAtom.
  • It is not thread-safe: one instance per import pass.

Clone this wiki locally