Repository navigation
SlopeDetector
roosit edited this page Nov 15, 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 kept 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. Takes atoms one by one in time order
* and gathers consecutive atoms with the same slopeType into groups; recording pauses split 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 started (the first atom,
* a slopeType change or a recording pause between atoms) 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;
}
/* A group does not cross a recording pause: the atom must continue the last atom of the group. */
List<SlopeSensorAtom> list = mCurrentGroup.getList();
SlopeSensorAtom last = list.get(list.size() - 1);
if (mCurrentGroup.getType() == a.slopeType && last.continuesWith(a)) {
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, which are climb, descent or flat. The groups are then used to build the rows of the slope_groups table that SlopeTextIndicator and SlopeChartIndicator use.
- The detector is streaming: the calling code passes atoms one by one in
timeRaworder. - The first atom opens the first group.
- Each next atom is compared with the current group: if
slopeTypematches and the atom continues the last atom of the group (last.continuesWith(a): it is later in time and belongs to the same recording segment, that is, there is no pause between them), it is appended to the group (addreturnsfalse); otherwise 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 into 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. - A recording pause between atoms (including when
slopeTypeis the same) always starts a new group: a climb section interrupted by a stop is split into two groups. Belonging to a recording segment is determined by thesegmentStartfield of the atom, whichVelitaskPlaginreads fromsensors_slope_data. - The order of atoms is not checked: with unsorted input the groups come out 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. - There is no thread safety: one instance per one import pass.