Example usage for org.joda.time DateTime now

List of usage examples for org.joda.time DateTime now

Introduction

In this page you can find the example usage for org.joda.time DateTime now.

Prototype

public static DateTime now() 

Source Link

Document

Obtains a DateTime set to the current system millisecond time using ISOChronology in the default time zone.

Usage

From source file:com.almende.pi5.common.agents.AggregatingAgent.java

License:Apache License

/**
 * Repeat steer./*from  w w  w .  j  ava2s . c  o  m*/
 */
public void repeatSteer() {
    steer();
    sendLog();

    DateTime next = this.currentTimeslot.plusMinutes(TIMESTEP - 1).plusSeconds(25);
    DateTime prev = next.minusMinutes(TIMESTEP);
    if (DateTime.now().isBefore(prev)) {
        next = prev;
    }

    schedule(new JSONRequest("repeatSteer", null), next);
}

From source file:com.almende.pi5.common.agents.AggregatingAgent.java

License:Apache License

/**
 * Steer.//  www.ja  v  a2s .  c om
 * Run on each incoming report, incoming request.
 */
private void steer() {
    if (!ControlMode.CONTRACT.equals(modus) && !ControlMode.BALANCE.equals(modus)) {
        if (!getId().startsWith("bus_")) {
            sendReport();
        }
        return;
    }
    if (!steeringLock.tryLock()) {
        if (steeringLock.hasQueuedThreads()) {
            return;
        }
        steeringLock.lock();
    }
    final DateTime now = DateTime.now();
    if (now.isAfter(currentTimeslot)) {
        updateTime();
    }

    final PowerProfile aggregate = generateReport();

    Double current_diff = getDiff(currentTimeslot.minus(TIMESLOTLENGTH), currentTimeslot, now, aggregate);
    Double next_diff = getDiff(currentTimeslot, currentTimeslot.plus(TIMESLOTLENGTH), currentTimeslot,
            aggregate);

    if (current_diff.isNaN()) {
        current_diff = 0.0;
    }
    if (next_diff.isNaN()) {
        next_diff = 0.0;
    }

    if (Math.abs(current_diff) < steerlimit && Math.abs(next_diff) < steerlimit) {
        LOG.info(getId() + ": Below steerlimit of " + steerlimit + " Watts (" + current_diff + " - " + next_diff
                + "):" + now);
        steeringLock.unlock();
        return;
    }

    // Current timeslot:
    double current_percentage;
    if (current_diff > 0) {
        current_percentage = (current_diff / getFlexUp(now, aggregate)) * 2;
        LOG.log(Level.INFO,
                getId() + ": request: Currently more power usage needed:" + current_diff + " Watt ("
                        + (current_percentage * 100) + "%):" + currentTimeslot.minus(TIMESLOTLENGTH) + "("
                        + currentTimeslot.minus(TIMESLOTLENGTH).getMillis() + ")");
    } else if (current_diff == 0) {
        current_percentage = 0;
    } else {
        current_percentage = (current_diff / getFlexDown(now, aggregate)) * 2;
        LOG.log(Level.INFO,
                getId() + ": request: Currently less power usage needed:" + (-current_diff) + " Watt ("
                        + (current_percentage * 100) + "%):" + currentTimeslot.minus(TIMESLOTLENGTH) + "("
                        + currentTimeslot.minus(TIMESLOTLENGTH).getMillis() + ")");
    }
    if (current_percentage > 1) {
        current_percentage = 1;
    }
    if (current_percentage < 0.01) {
        current_percentage = 0.01;
    }

    // Next timeslot:
    double next_percentage;
    if (next_diff > 0) {
        next_percentage = (next_diff / getFlexUp(currentTimeslot, aggregate)) * 2;
        LOG.log(Level.INFO,
                getId() + ": request: Next timeslot more power usage needed:" + next_diff + " Watt ("
                        + (next_percentage * 100) + "%):" + currentTimeslot + "(" + currentTimeslot.getMillis()
                        + ")");
    } else if (next_diff == 0) {
        next_percentage = 0;
    } else {
        next_percentage = (next_diff / getFlexDown(currentTimeslot, aggregate)) * 2;
        LOG.log(Level.INFO,
                getId() + ": request: Next timeslot less power usage needed:" + (-next_diff) + " Watt ("
                        + (next_percentage * 100) + "%):" + currentTimeslot + "(" + currentTimeslot.getMillis()
                        + ")");
    }
    if (next_percentage > 1) {
        next_percentage = 1;
    }
    if (next_percentage < 0.01) {
        next_percentage = 0.01;
    }

    final List<ReportWrap> list = new ArrayList<ReportWrap>(reports.size());
    reportsLock.readLock().lock();
    list.addAll(reports);
    reportsLock.readLock().unlock();

    Collections.sort(list);
    for (ReportWrap wrap : list) {
        if (Math.abs(current_diff) < 0.01 * steerlimit && Math.abs(next_diff) < 0.01 * steerlimit) {
            LOG.info(getId() + ": done steering:" + current_diff + " - " + next_diff);
            break;
        }
        LOG.info(
                getId() + ": still to go:" + current_diff + " - " + next_diff + " checking " + wrap.getOwner());

        String agentUrl = wrap.getOwner();
        PowerProfile report = wrap.getReport();
        report.calcAll();

        // Timeline based:
        PowerTimeLine diffLine = new PowerTimeLine(currentReport.getTimestamp());

        CategoryProfile rep = report.getCategoryProfile(Categories.ALL.name());
        double influence = 0;
        if (Math.abs(current_diff) >= 0) {
            if (current_diff > 0) {
                final double val = Math.min(current_diff, getFlexUp(now, report) * current_percentage);
                current_diff -= val;
                diffLine.addValueAt(now, val);
                influence += val;
            } else {
                final double val = Math.max(current_diff, getFlexDown(now, report) * current_percentage);
                current_diff -= val;
                diffLine.addValueAt(now, val);
                influence -= val;
            }
        }
        diffLine.zeroFrom(currentTimeslot);
        if (Math.abs(next_diff) >= 0) {
            if (next_diff > 0) {
                final double val = Math.min(next_diff, getFlexUp(currentTimeslot, report) * next_percentage);
                next_diff -= val;
                diffLine.addValueAt(currentTimeslot, val);
                influence += val;
            } else {
                final double val = Math.max(next_diff, getFlexDown(currentTimeslot, report) * next_percentage);
                next_diff -= val;
                diffLine.addValueAt(currentTimeslot, val);
                influence -= val;
            }
        }
        diffLine.zeroFrom(currentTimeslot.plus(TIMESLOTLENGTH));

        if (influence <= 0) {
            LOG.warning(getId() + ": Not sending zero influence request to:" + agentUrl);
            continue;
        }

        LOG.fine(getId() + " diffLine:" + JOM.getInstance().valueToTree(diffLine));
        LOG.fine(getId() + " demand:" + JOM.getInstance().valueToTree(rep.getDemand()));

        RequestProfile subRequest = new RequestProfile();
        subRequest.setTimestamp(currentReport.getTimestamp());
        subRequest.getRequest().add(rep.getDemand()).add(diffLine);

        ObjectNode params = JOM.createObjectNode();
        params.set("request", JOM.getInstance().valueToTree(subRequest));
        try {
            LOG.info(getId() + ": Sending request to Child:" + agentUrl + " -> " + params);

            reportsLock.writeLock().lock();
            wrap.getReport()
                    .merge(createProposal(report, subRequest), currentTimeslot.minus(TIMESLOTLENGTH),
                            currentTimeslot.plus(TIMESLOTLENGTH))
                    .dropHistory(currentTimeslot.minus(TIMESLOTLENGTH));
            reportsLock.writeLock().unlock();
            getSender().get().call(URI.create(agentUrl), "request", params);
        } catch (IOException e) {
            LOG.log(Level.WARNING, getId() + ": Couldn't send profile onwards to:" + agentUrl, e);
        }
    }
    if (Math.abs(current_diff) > steerlimit || Math.abs(next_diff) > steerlimit) {
        LOG.warning(getId() + ": Reached steering limits, resetting goal and reporting max.");
        updateCurrentReport(false);
        sendReport();
    }
    steeringLock.unlock();
}

From source file:com.almende.pi5.common.agents.AggregatingAgent.java

License:Apache License

/**
 * Describe status./* w  w  w  .j  a va  2 s .c om*/
 *
 * @return the status
 */
@JsonIgnore
@Access(AccessType.PUBLIC)
public String describe() {
    final StringBuilder sb = new StringBuilder();
    final PowerProfile aggregate = generateReport();
    final DateTime now = DateTime.now();
    sb.append(getId());
    sb.append(" ");
    sb.append(DateTime.now());
    sb.append("|");
    sb.append(currentTimeslot);
    sb.append(" ");
    sb.append(getModus());
    sb.append('\n');
    sb.append("T0 c:");
    sb.append(aggregate.getCategoryProfile(Categories.ALL.name()).getDemand().getValueAt(now));
    sb.append(" a:");
    sb.append(aggregate.getCategoryProfile(Categories.ALL.name()).getDemand()
            .getIntegral(currentTimeslot.minus(TIMESLOTLENGTH), currentTimeslot) / (TIMESLOTLENGTH / 1000));
    sb.append(" g:");
    sb.append(getGoal(now));
    sb.append(" d:(");
    sb.append(getDiff(currentTimeslot.minus(TIMESLOTLENGTH), currentTimeslot, now, aggregate));
    sb.append(")");
    sb.append('\n');
    sb.append("T1 c:");
    sb.append(aggregate.getCategoryProfile(Categories.ALL.name()).getDemand().getValueAt(currentTimeslot));
    sb.append(" a:");
    sb.append(aggregate.getCategoryProfile(Categories.ALL.name()).getDemand().getIntegral(currentTimeslot,
            currentTimeslot.plus(TIMESLOTLENGTH)) / (TIMESLOTLENGTH / 1000));
    sb.append(" g:");
    sb.append(getGoal(currentTimeslot));
    sb.append(" d:(");
    sb.append(getDiff(currentTimeslot, currentTimeslot.plus(TIMESLOTLENGTH), currentTimeslot, aggregate));
    sb.append(")");
    sb.append('\n');
    sb.append(getLogLine(aggregate));
    sb.append('\n');
    sb.append("(nof reports:" + reports.size() + ")");
    return sb.toString();
}

From source file:com.almende.pi5.common.agents.AggregatingAgent.java

License:Apache License

/**
 * Send log.//from   w  ww .  ja va  2  s.c  o m
 */
@JsonIgnore
@Access(AccessType.PUBLIC)
public void sendLog() {
    final DateTime now = DateTime.now();
    if (now.isAfter(currentTimeslot)) {
        updateTime();
    }
    final CategoryProfile current = generateReport().getCategoryProfile(Categories.ALL.name());
    CategoryProfile expected = current;
    if (getModus().equals(ControlMode.CONTRACT)) {
        expected = currentReport.getCategoryProfile(Categories.ALL.name());
    }
    final LogLine ll = LogLine.fromProfiles(getId(), current, expected, now, currentTimeslot,
            getModus().equals(ControlMode.CONTRACT));
    final Params params = new Params();
    params.add("logline", ll);
    try {
        call(loggerUrl, "log", params);
    } catch (IOException e) {
        LOG.log(Level.WARNING, "Couldn't log!", e);
    }
}

From source file:com.almende.pi5.common.agents.AggregatingAgent.java

License:Apache License

/**
 * Gets the log line.//ww  w .  j  av  a 2  s .co  m
 *
 * @param aggregate
 *            the aggregate
 * @return the log line
 */
@JsonIgnore
@Access(AccessType.PUBLIC)
public String getLogLine(final PowerProfile aggregate) {
    final DateTime now = DateTime.now();
    final CategoryProfile current = aggregate.getCategoryProfile(Categories.ALL.name());
    CategoryProfile expected = current;
    if (getModus().equals(ControlMode.CONTRACT)) {
        expected = currentReport.getCategoryProfile(Categories.ALL.name());
    }
    final LogLine ll = LogLine.fromProfiles(getId(), current, expected, now, currentTimeslot,
            getModus().equals(ControlMode.CONTRACT));
    return ll.getCsv();
}

From source file:com.almende.pi5.common.agents.GraphAgent.java

License:Apache License

@Override
protected void onReady() {
    final ObjectNode config = getConfig();

    this.myParentUrl = config.hasNonNull(PARENT_URL_KEY) ? URI.create(config.get(PARENT_URL_KEY).asText())
            : null;/*from   ww w. java 2  s . c om*/
    this.loggerUrl = config.hasNonNull(LOGGER_URL_KEY) ? URI.create(config.get(LOGGER_URL_KEY).asText()) : null;
    this.sendOffset = config.hasNonNull(SEND_OFFSET_KEY)
            ? config.get(SEND_OFFSET_KEY).asInt(SEND_OFFSET_DEFAULT)
            : 0;
    this.sendInterval = config.hasNonNull(SEND_INTERVAL_KEY)
            ? config.get(SEND_INTERVAL_KEY).asInt(SEND_INTERVAL_DEFAULT)
            : 0;

    repeatUpdateTime();
    repeatSendReportOn15();
    repeatSendReportOnInterval();

    schedule(sendReport, null, DateTime.now().plusMinutes(1));
    super.onReady();
}

From source file:com.almende.pi5.common.agents.GraphAgent.java

License:Apache License

/**
 * updates the current time./*from   www .  ja  va 2  s .  c  o m*/
 */
@Access(AccessType.PUBLIC)
public void repeatUpdateTime() {
    schedule("updateTime", null, DateTime.now());
    schedule(repeatUpdateTime, null, 60000);
}

From source file:com.almende.pi5.common.agents.GraphAgent.java

License:Apache License

/**
 * Update time.//from w w  w  .j  av a2  s  . c o m
 */
@Access(AccessType.PUBLIC)
public void updateTime() {
    DateTime now = DateTime.now();
    now = now.plus((TIMESTEP - (now.getMinuteOfHour() % TIMESTEP)) * 60000 - (now.getSecondOfMinute() * 1000)
            - now.getMillisOfSecond());
    if (!this.currentTimeslot.equals(now)) {
        this.currentTimeslot = now;
        LOG.fine(getId() + ": updateTime to: " + now);
    }
}

From source file:com.almende.pi5.common.agents.GraphAgent.java

License:Apache License

/**
 * Schedule send report on 15 minutes./* ww  w.  j  a  v  a2s  .  c  o  m*/
 */
@Access(AccessType.PUBLIC)
public void repeatSendReportOn15() {
    LOG.fine(getId() + ": repeatSendReportOn15");
    if (this.sendOffset <= 0) {
        LOG.info("repeatSendReportOn15 stopped");
        return;
    }

    updateTime();

    DateTime next = this.currentTimeslot.plus(this.sendOffset);
    DateTime prev = this.currentTimeslot.minusMinutes(TIMESTEP).plus(this.sendOffset);
    if (DateTime.now().isBefore(prev)) {
        next = prev;
    }

    LOG.fine(getId() + ": repeatSendReportOn15, next run at:" + next);
    schedule(sendReport, null, DateTime.now());
    schedule(repeatSendReportOn15, null, next);
}

From source file:com.almende.pi5.common.agents.GraphAgent.java

License:Apache License

/**
 * Schedule report on interval.//  ww  w . j ava2  s  . c o  m
 */
@Access(AccessType.PUBLIC)
public void repeatSendReportOnInterval() {
    if (this.sendInterval <= 0)
        return;

    LOG.info(getId() + ": repeatSendReportOnInterval, next run at:" + DateTime.now().plus(this.sendInterval));

    updateTime();
    schedule(sendReport, null, DateTime.now());
    schedule(repeatSendReportOnInterval, null, this.sendInterval);
}