Repository navigation
SlopeGroup
roosit edited this page Oct 16, 2026
·
2 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 SlopeGroup.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;
/* A series of consecutive slope atoms of one type; a database row (SlopeGroupAtom) is built from it. */
public class SlopeGroup {
/* Row identifier in the groups table; assigned after insertion into the database. */
private long mId;
/* Slope type of the group: -1 downhill, 0 flat, 1 uphill. */
private final int mType;
/* Atoms of the group in time order. */
private final List<SlopeSensorAtom> mList = new ArrayList<>();
/* The group is created by its first atom and takes its type. */
public SlopeGroup(SlopeSensorAtom first) {
mType = first.slopeType;
mList.add(first);
}
public int getType() {
return mType;
}
public List<SlopeSensorAtom> getList() {
return mList;
}
public long getId() {
return mId;
}
public void setId(long id) {
mId = id;
}
public void add(SlopeSensorAtom atom) {
mList.add(atom);
}
/*
* Builds a row for the groups table: boundaries from the first and last atom,
* sums of elevation gain and loss, slope percentage (x100).
*/
public SlopeGroupAtom buildAtom(long sensorId, int measureIndex) {
SlopeGroupAtom g = new SlopeGroupAtom();
g.id = mId;
g.sensor_id = sensorId;
g.measureIndex = measureIndex;
g.slopeType = mType;
if (mList.isEmpty()) {
return g;
}
/* The group starts at the first atom and ends at the window of the last atom. */
SlopeSensorAtom first = mList.get(0);
SlopeSensorAtom last = mList.get(mList.size() - 1);
g.timeRaw = first.timeRaw;
g.duration = (last.timeRaw + last.duration) - first.timeRaw;
g.distance = first.distance;
g.elevation = first.elevation;
long endDistance = last.distance + last.distanceDelta;
int endElevation = last.elevation + last.elevationDelta;
g.distanceDelta = endDistance - first.distance;
g.elevationDelta = endElevation - first.elevation;
/* Elevation gain and loss: sums of positive and negative elevation increments of the atoms (loss is negative). */
int eleUp = 0;
int eleDown = 0;
for (SlopeSensorAtom a : mList) {
if (a.elevationDelta > 0) {
eleUp += a.elevationDelta;
} else if (a.elevationDelta < 0) {
eleDown += a.elevationDelta;
}
}
g.eleUp = eleUp;
g.eleDown = eleDown;
/* Percentage x100: elevation increment * 10000 / length. With zero length it stays 0. */
if (g.distanceDelta > 0) {
g.slopePercent = (int) (g.elevationDelta * 10000L / g.distanceDelta);
}
return g;
}
}SlopeGroup is a group of consecutive slope atoms of one type (slopeType). It is created by SlopeDetector and can be turned into a row of the slope_groups table — SlopeGroupAtom.
- The group type (
-1downhill,0flat,1uphill) is fixed by the first atom and does not change. -
buildAtom(sensorId, measureIndex)assembles the row:-
timeRaw,distanceandelevationcome from the first atom;durationruns from the start of the first atom to the end of the last one (last.timeRaw + last.duration - first.timeRaw); -
distanceDeltaandelevationDeltaare the differences between the end of the last atom (distance + distanceDelta,elevation + elevationDelta) and the start of the first one; -
eleUpis the sum of the positiveelevationDeltavalues of the atoms,eleDownis the sum of the negative ones (the value is negative); -
slopePercentiselevationDelta * 10000 / distanceDelta, that is, the slope percentage multiplied by 100, integer part; whendistanceDelta <= 0it stays 0.
-
- The row
idis 0 by default; the real identifier is assigned after insertion into the database throughsetId. - All distances and elevations are in millimeters, as in the sensor atoms.
- An empty atom list is impossible in the constructor (the first atom is passed immediately); the emptiness check in
buildAtomis a safeguard. - The slope percentage is calculated for the whole group: it averages all atoms of the group and may differ from the percentages of individual atoms.
- Integer division truncates the fractional part toward zero: a slope below 0.01% becomes 0.
-
eleUpandeleDownare calculated in the group, but the plugin indicators show the accumulated values of the atom, not these.