List of usage examples for org.joda.time Duration Duration
public Duration(ReadableInstant start, ReadableInstant end)
From source file:com.almende.bridge.resources.SimulatedResource.java
License:Apache License
/** * Gets the geo json description of this Resource. * * @param incTrack// w w w. ja v a 2 s. co m * Should the track data be included? * @param incTarget * the inc target * @return the geo json */ public FeatureCollection getGeoJson(@Optional @Name("includeTrack") Boolean incTrack, @Optional @Name("includeTarget") Boolean incTarget) { getCurrentLocation(); final FeatureCollection fc = new FeatureCollection(); fc.setProperty("id", getId()); final Feature origin = new Feature(); origin.setId(getId()); final Point originPoint = new Point(); originPoint.setCoordinates(new LngLatAlt(geoJsonPos[0], geoJsonPos[1])); origin.setGeometry(originPoint); origin.setProperty("type", "currentLocation"); addProperties(origin); addTaskProperties(origin); // TODO: add resource icon fc.add(origin); if (route != null) { if (incTrack != null && incTrack) { final Feature track = new Feature(); track.setId(getId()); final LineString tracksteps = new LineString(); tracksteps.add(new LngLatAlt(geoJsonPos[0], geoJsonPos[1])); final long millis = new Duration(route.routeBase, DateTime.now()).getMillis(); for (double[] step : route.route) { if (step[3] > millis) { tracksteps.add(new LngLatAlt(step[0], step[1])); } } track.setGeometry(tracksteps); track.setProperty("type", "route"); addProperties(track); addTaskProperties(track); fc.add(track); } if (incTarget != null && incTarget) { final Feature goal = new Feature(); goal.setId(getId()); final Point goalPoint = new Point(); goalPoint.setCoordinates(new LngLatAlt(geoJsonGoal[0], geoJsonGoal[1])); goal.setGeometry(goalPoint); goal.setProperty("type", "targetLocation"); addRouteProperties(goal); addProperties(goal); addTaskProperties(goal); fc.add(goal); } addRouteProperties(origin); } return fc; }
From source file:com.almende.eve.scheduling.SyncScheduler.java
License:Apache License
/** * Sync with peer./*from w w w .jav a 2 s .c o m*/ * * @param peer * the peer * @return the long */ @Access(AccessType.PUBLIC) public SyncTupple syncWithPeer(final URI peer) { if (caller == null) { LOG.warning("Sync requested, but caller is still null, invalid!"); return null; } LOG.info("Starting sync with: " + peer + "!"); final DateTime start = DateTime.now(); try { final Long result = caller.callSync(peer, "syncScheduler.ping", JOM.createObjectNode(), Long.class); final long roundtrip = new Duration(start, DateTime.now()).getMillis(); final long offset = result - now() + (roundtrip / 2); LOG.info("Sync resulted in offset:" + offset + " ( " + roundtrip + ":" + start + ":" + result + ")"); return new SyncTupple(offset, roundtrip); } catch (IOException e) { LOG.log(Level.WARNING, "failed to send ping", e); } return null; }
From source file:com.almende.pi5.common.agents.GraphAgent.java
License:Apache License
/** * Do compare./* ww w.j a v a2s .co m*/ * * @param start * the start * @param end * the end * @param now * the now * @param aggregate * the aggregate * @return the diff */ public Double getDiff(final DateTime start, final DateTime end, final DateTime now, final PowerProfile aggregate) { if (currentReport == null) { return 0.0; } currentRepLock.lock(); final Double goal = currentReport.getCategoryProfile(Categories.ALL.name()).getDemand().getIntegral(start, end); final Double actual = aggregate.getCategoryProfile(Categories.ALL.name()).getDemand().getIntegral(start, end); currentRepLock.unlock(); long seconds = new Duration(now, end).getStandardSeconds(); if (seconds <= 0) { seconds = 1; } return (goal - actual) / seconds; }
From source file:com.almende.pi5.common.PowerTimeLine.java
License:Apache License
/** * Gets the value at./*from w ww . j a v a 2s . c o m*/ * * @param timestamp * the timestamp * @return the value at */ @JsonIgnore public Double getValueAt(DateTime timestamp) { long offset = new Duration(this.timestamp, timestamp).getMillis(); PowerTime oldVal = null; for (PowerTime pt : series) { if (pt.getOffset() > offset) { return (oldVal == null) ? 0 : oldVal.getValue(); } oldVal = pt; } return (oldVal == null) ? 0 : oldVal.getValue(); }
From source file:com.almende.pi5.common.PowerTimeLine.java
License:Apache License
/** * Adds the value at./*from w w w.ja va 2 s. co m*/ * * @param timestamp * the timestamp * @param value * the value * @return the power time line */ public PowerTimeLine addValueAt(final DateTime timestamp, final double value) { long offset = new Duration(this.timestamp, timestamp).getMillis(); if (series.isEmpty()) { series.add(new PowerTime(offset, value)); } else { PowerTime last = series.get(series.size() - 1); if (last.getOffset() < offset) { series.add(new PowerTime(offset, value)); } else { for (int i = 0; i < series.size(); i++) { PowerTime pt = series.get(i); if (pt.getOffset() < offset) { continue; } if (pt.getOffset() == offset) { pt.setValue(value); } else { series.add(i, new PowerTime(offset, value)); } break; } } } return this; }
From source file:com.almende.pi5.common.PowerTimeLine.java
License:Apache License
/** * Make this TL discrete, returning the TL; * As a side effect, removes values outside the start and end times. * * @param start/*from w w w . j a v a2 s . com*/ * the start * @param end * the end * @param stepSize * the step size * @return this */ @JsonIgnore public PowerTimeLine discrete(final DateTime start, final DateTime end, final Duration stepSize) { final PowerTimeLine newTL = new PowerTimeLine(); newTL.timestamp = this.timestamp; final ArrayList<PowerTime> steps = new ArrayList<PowerTime>(); long offset = new Duration(timestamp, start).getMillis(); Interval interval = stepSize.toIntervalFrom(start); while (interval.getEnd().isBefore(end) || interval.getEnd().isEqual(end)) { steps.add(new PowerTime(offset, 0)); offset += interval.toDurationMillis(); interval = stepSize.toIntervalFrom(timestamp.plusMillis((int) offset)); } newTL.setSeries(steps); this.add(newTL).zeroBefore(start).zeroFrom(end); final Duration diff = new Duration(start, end); if (series.size() > (diff.getMillis() / stepSize.getMillis())) { int index = 0; long expectedOffset = new Duration(timestamp, start).getMillis() + stepSize.getMillis(); while (index < series.size() - 1) { PowerTime pt = series.get(index); ArrayList<PowerTime> temp = new ArrayList<PowerTime>(); int nextIndex = index + 1; PowerTime next = series.get(nextIndex); while (next.getOffset() < expectedOffset) { temp.add(next); series.remove(nextIndex); if (nextIndex == series.size()) { break; } next = series.get(nextIndex); } if (temp.size() > 0) { temp.add(0, pt); double integral = getIntegral(pt.getOffset(), pt.getOffset() + stepSize.getMillis(), temp); series.set(index, new PowerTime(pt.getOffset(), integral / stepSize.getMillis())); } index++; expectedOffset += stepSize.getMillis(); } } return this; }
From source file:com.almende.pi5.common.PowerTimeLine.java
License:Apache License
/** * Gets the average watts./* www. j a v a 2 s. c om*/ * * @param fromDateTime * the from date time * @param untilDateTime * the until date time * @return the average watts */ @JsonIgnore public double getAverageWatts(final DateTime fromDateTime, final DateTime untilDateTime) { final double difference = new Duration(fromDateTime, untilDateTime).getMillis() / 1000.0; if (difference > 0) { return getIntegral(fromDateTime, untilDateTime) / difference; } else { return 0; } }
From source file:com.almende.pi5.common.PowerTimeLine.java
License:Apache License
/** * Gets the integral./* w ww. j a va2s.com*/ * * @param fromDateTime * the from date time * @param untilDateTime * the until date time * @return the integral */ @JsonIgnore public double getIntegral(final DateTime fromDateTime, final DateTime untilDateTime) { final long from = new Duration(timestamp, fromDateTime).getMillis(); final long until = new Duration(timestamp, untilDateTime).getMillis(); return getIntegral(from, until, this.series) / 1000.0; }
From source file:com.almende.pi5.common.PowerTimeLine.java
License:Apache License
private PowerTimeLine operation(operator op, PowerTimeLine other) { if (other.series.size() == 0) { return this; }/*from w w w .j ava2s .co m*/ final long offset = new Duration(this.timestamp, other.timestamp).getMillis(); final ArrayList<PowerTime> result = new ArrayList<PowerTime>(); if (this.series.size() == 0) { for (PowerTime pt : other.series) { result.add(new PowerTime(pt.getOffset() + offset, op.doOp(0, pt.getValue()))); } this.series = result; return this; } PowerTime val_mine = null; PowerTime val_other = null; int index_mine = 0; int index_other = 0; double value_mine = 0; double value_other = 0; while (index_mine < this.series.size() && index_other < other.series.size()) { val_mine = this.series.get(index_mine); val_other = other.series.get(index_other); switch ((int) Math.signum(val_other.getOffset() + offset - val_mine.getOffset())) { case 1: value_mine = val_mine.getValue(); result.add(new PowerTime(val_mine.getOffset(), op.doOp(value_mine, value_other))); index_mine++; break; case 0: value_mine = val_mine.getValue(); value_other = val_other.getValue(); result.add(new PowerTime(val_mine.getOffset(), op.doOp(value_mine, value_other))); index_mine++; index_other++; break; case -1: value_other = val_other.getValue(); result.add(new PowerTime(val_other.getOffset() + offset, op.doOp(value_mine, value_other))); index_other++; break; } } for (int p = index_mine; p < this.series.size(); p++) { PowerTime pt = this.series.get(p); result.add(new PowerTime(pt.getOffset(), op.doOp(pt.getValue(), value_other))); } for (int p = index_other; p < other.series.size(); p++) { PowerTime pt = other.series.get(p); result.add(new PowerTime(pt.getOffset() + offset, op.doOp(value_mine, pt.getValue()))); } this.series = result; return this; }
From source file:com.almende.pi5.common.PowerTimeLine.java
License:Apache License
/** * Clear to zero between start (inclusive) and end (exclusive). * * @param start//from w ww. j av a 2 s . c o m * the start * @param end * the end * @return this for chaining */ public PowerTimeLine zeroBetween(final DateTime start, final DateTime end) { if (this.series.size() == 0) { return this; } final long startOffset = new Duration(this.timestamp, start).getMillis(); final long endOffset = new Duration(this.timestamp, end).getMillis(); // get current value at end final double endVal = getValueAt(end); // remove all values with index between start incl and end incl. final Iterator<PowerTime> iter = this.series.iterator(); int index = 0; while (iter.hasNext()) { final PowerTime item = iter.next(); final long offset = item.getOffset(); if (offset < startOffset) { index++; } else if (offset >= startOffset && offset <= endOffset) { iter.remove(); } else if (offset > endOffset) { break; } } // Add zero at start this.series.add(index, new PowerTime(startOffset, 0)); // add current value at end this.series.add(index + 1, new PowerTime(endOffset, endVal)); return this; }