Repository navigation
SlopeDetector
roosit edited this page Oct 16, 2026
·
4 revisions
Up | Previous | Next | Русский
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;
}
}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.
- The detector is streaming: the calling code passes atoms one at a time in
timeRaworder. - The first atom opens the first group.
- Each following atom is compared by
slopeTypewith the current group: if it matches, it is appended to it (addreturnsfalse); if not, a new group is opened (addreturnstrue). -
getGroups()returns all groups in creation order,getCurrentGroup()returns the last opened one. - Usage:
VelitaskPlagin.onSourceImportedreads all atoms of the slope sensor intimeRaworder, runs them through the detector, then callsSlopeGroup.buildAtomfor each group and writes the result to the plugin database (see VelitaskPlagin). The return value ofaddis not used there.
- The detector does not smooth the data: every short-lived change of
slopeTypecreates 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 inSlopeGroup.buildAtom. - It is not thread-safe: one instance per import pass.