Search in sources :

Example 11 with SteveException

use of de.rwth.idsg.steve.SteveException in project steve by RWTH-i5-IDSG.

the class Deserializer method handleResult.

/**
 * Do NOT catch and handle exceptions for incoming RESPONSEs. Let the processing fail.
 * There is no mechanism in OCPP to report back such erroneous messages.
 */
private void handleResult(CommunicationContext context, String messageId, JsonParser parser) {
    FutureResponseContext responseContext = futureResponseContextStore.get(context.getSession(), messageId);
    if (responseContext == null) {
        throw new SteveException("A result message was received as response to a not-sent call. The message was: %s", context.getIncomingString());
    }
    ResponseType res;
    try {
        parser.nextToken();
        JsonNode responsePayload = parser.readValueAsTree();
        res = mapper.treeToValue(responsePayload, responseContext.getResponseClass());
    } catch (IOException e) {
        throw new SteveException("Deserialization of incoming response payload failed", e);
    }
    OcppJsonResult result = new OcppJsonResult();
    result.setMessageId(messageId);
    result.setPayload(res);
    context.setIncomingMessage(result);
    context.createResultHandler(responseContext.getTask());
}
Also used : OcppJsonResult(de.rwth.idsg.steve.ocpp.ws.data.OcppJsonResult) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) FutureResponseContext(de.rwth.idsg.steve.ocpp.ws.data.FutureResponseContext) SteveException(de.rwth.idsg.steve.SteveException) ResponseType(de.rwth.idsg.steve.ocpp.ResponseType)

Example 12 with SteveException

use of de.rwth.idsg.steve.SteveException in project steve by RWTH-i5-IDSG.

the class FutureResponseContextStoreImpl method get.

@Override
public FutureResponseContext get(WebSocketSession session, String messageId) {
    Map<String, FutureResponseContext> map = lookupTable.get(session);
    if (map == null) {
        throw new SteveException("sessionId '%s' is not in store", session.getId());
    } else {
        FutureResponseContext context = map.remove(messageId);
        log.debug("Store size for sessionId '{}': {}", session.getId(), map.size());
        return context;
    }
}
Also used : FutureResponseContext(de.rwth.idsg.steve.ocpp.ws.data.FutureResponseContext) SteveException(de.rwth.idsg.steve.SteveException)

Example 13 with SteveException

use of de.rwth.idsg.steve.SteveException in project steve by RWTH-i5-IDSG.

the class SettingsRepositoryImpl method update.

@Override
public void update(SettingsForm form) {
    String eMails = join(form.getRecipients());
    String features = join(form.getEnabledFeatures());
    try {
        ctx.update(SETTINGS).set(SETTINGS.HEARTBEAT_INTERVAL_IN_SECONDS, toSec(form.getHeartbeat())).set(SETTINGS.HOURS_TO_EXPIRE, form.getExpiration()).set(SETTINGS.MAIL_ENABLED, form.getEnabled()).set(SETTINGS.MAIL_HOST, form.getHost()).set(SETTINGS.MAIL_USERNAME, form.getUsername()).set(SETTINGS.MAIL_PASSWORD, form.getPassword()).set(SETTINGS.MAIL_FROM, form.getFrom()).set(SETTINGS.MAIL_PROTOCOL, form.getProtocol()).set(SETTINGS.MAIL_PORT, form.getPort()).set(SETTINGS.MAIL_RECIPIENTS, eMails).set(SETTINGS.NOTIFICATION_FEATURES, features).where(SETTINGS.APP_ID.eq(APP_ID)).execute();
    } catch (DataAccessException e) {
        throw new SteveException("FAILED to save the settings", e);
    }
}
Also used : DataAccessException(org.jooq.exception.DataAccessException) SteveException(de.rwth.idsg.steve.SteveException)

Example 14 with SteveException

use of de.rwth.idsg.steve.SteveException in project steve by RWTH-i5-IDSG.

the class TransactionRepositoryImpl method getDetails.

@Override
public TransactionDetails getDetails(int transactionPk) {
    // -------------------------------------------------------------------------
    // Step 1: Collect general data about transaction
    // -------------------------------------------------------------------------
    TransactionQueryForm form = new TransactionQueryForm();
    form.setTransactionPk(transactionPk);
    form.setType(TransactionQueryForm.QueryType.ALL);
    form.setPeriodType(TransactionQueryForm.QueryPeriodType.ALL);
    Record11<Integer, String, Integer, String, DateTime, String, DateTime, String, String, Integer, Integer> transaction = getInternal(form).fetchOne();
    if (transaction == null) {
        throw new SteveException("There is no transaction with id '%s'", transactionPk);
    }
    DateTime startTimestamp = transaction.value5();
    DateTime stopTimestamp = transaction.value7();
    String stopValue = transaction.value8();
    String chargeBoxId = transaction.value2();
    int connectorId = transaction.value3();
    // -------------------------------------------------------------------------
    // Step 2: Collect intermediate meter values
    // -------------------------------------------------------------------------
    Condition timestampCondition;
    if (stopTimestamp == null && stopValue == null) {
        // active transaction
        timestampCondition = CONNECTOR_METER_VALUE.VALUE_TIMESTAMP.greaterOrEqual(startTimestamp);
    } else {
        // finished transaction
        timestampCondition = CONNECTOR_METER_VALUE.VALUE_TIMESTAMP.between(startTimestamp, stopTimestamp);
    }
    // Case 1: Ideal and most accurate case. Station sends meter values with transaction id set.
    // 
    SelectQuery<ConnectorMeterValueRecord> transactionQuery = ctx.selectFrom(CONNECTOR_METER_VALUE).where(CONNECTOR_METER_VALUE.TRANSACTION_PK.eq(transactionPk)).getQuery();
    // Case 2: Fall back to filtering according to time windows
    // 
    SelectQuery<ConnectorMeterValueRecord> timestampQuery = ctx.selectFrom(CONNECTOR_METER_VALUE).where(CONNECTOR_METER_VALUE.CONNECTOR_PK.eq(ctx.select(CONNECTOR.CONNECTOR_PK).from(CONNECTOR).where(CONNECTOR.CHARGE_BOX_ID.eq(chargeBoxId)).and(CONNECTOR.CONNECTOR_ID.eq(connectorId)))).and(timestampCondition).getQuery();
    // Actually, either case 1 applies or 2. If we retrieved values using 1, case 2 is should not be
    // executed (best case). In worst case (1 returns empty list and we fall back to case 2) though,
    // we make two db calls. Alternatively, we can pass both queries in one go, and make the db work.
    // 
    // UNION removes all duplicate records
    // 
    Table<ConnectorMeterValueRecord> t1 = transactionQuery.union(timestampQuery).asTable("t1");
    // -------------------------------------------------------------------------
    // Step 3: Charging station might send meter vales at fixed intervals (e.g.
    // every 15 min) regardless of the fact that connector's meter value did not
    // change (e.g. vehicle is fully charged, but cable is still connected). This
    // yields multiple entries in db with the same value but different timestamp.
    // We are only interested in the first arriving entry.
    // -------------------------------------------------------------------------
    Field<DateTime> dateTimeField = DSL.min(t1.field(2, DateTime.class)).as("min");
    List<TransactionDetails.MeterValues> values = ctx.select(dateTimeField, t1.field(3, String.class), t1.field(4, String.class), t1.field(5, String.class), t1.field(6, String.class), t1.field(7, String.class), t1.field(8, String.class), t1.field(9, String.class)).from(t1).groupBy(t1.field(3), t1.field(4), t1.field(5), t1.field(6), t1.field(7), t1.field(8), t1.field(9)).orderBy(dateTimeField).fetch().map(r -> TransactionDetails.MeterValues.builder().valueTimestamp(r.value1()).value(r.value2()).readingContext(r.value3()).format(r.value4()).measurand(r.value5()).location(r.value6()).unit(r.value7()).phase(r.value8()).build());
    return new TransactionDetails(new TransactionMapper().map(transaction), values);
}
Also used : Condition(org.jooq.Condition) ConnectorMeterValueRecord(jooq.steve.db.tables.records.ConnectorMeterValueRecord) TransactionDetails(de.rwth.idsg.steve.repository.dto.TransactionDetails) DateTime(org.joda.time.DateTime) TransactionQueryForm(de.rwth.idsg.steve.web.dto.TransactionQueryForm) SteveException(de.rwth.idsg.steve.SteveException)

Example 15 with SteveException

use of de.rwth.idsg.steve.SteveException in project steve by RWTH-i5-IDSG.

the class TransactionRepositoryImpl method processType.

private void processType(SelectQuery selectQuery, TransactionQueryForm form) {
    switch(form.getPeriodType()) {
        case TODAY:
            selectQuery.addConditions(date(TRANSACTION.START_TIMESTAMP).eq(date(CustomDSL.utcTimestamp())));
            break;
        case LAST_10:
        case LAST_30:
        case LAST_90:
            DateTime now = DateTime.now();
            selectQuery.addConditions(date(TRANSACTION.START_TIMESTAMP).between(date(now.minusDays(form.getPeriodType().getInterval())), date(now)));
            break;
        case ALL:
            break;
        case FROM_TO:
            selectQuery.addConditions(TRANSACTION.START_TIMESTAMP.between(form.getFrom().toDateTime(), form.getTo().toDateTime()));
            break;
        default:
            throw new SteveException("Unknown enum type");
    }
}
Also used : DateTime(org.joda.time.DateTime) SteveException(de.rwth.idsg.steve.SteveException)

Aggregations

SteveException (de.rwth.idsg.steve.SteveException)17 DataAccessException (org.jooq.exception.DataAccessException)6 DSLContext (org.jooq.DSLContext)5 FutureResponseContext (de.rwth.idsg.steve.ocpp.ws.data.FutureResponseContext)4 IOException (java.io.IOException)3 AddressRecord (jooq.steve.db.tables.records.AddressRecord)2 DateTime (org.joda.time.DateTime)2 SelectQuery (org.jooq.SelectQuery)2 JsonParser (com.fasterxml.jackson.core.JsonParser)1 TreeNode (com.fasterxml.jackson.core.TreeNode)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 RequestType (de.rwth.idsg.steve.ocpp.RequestType)1 ResponseType (de.rwth.idsg.steve.ocpp.ResponseType)1 ActionResponsePair (de.rwth.idsg.steve.ocpp.ws.data.ActionResponsePair)1 CommunicationContext (de.rwth.idsg.steve.ocpp.ws.data.CommunicationContext)1 ErrorCode (de.rwth.idsg.steve.ocpp.ws.data.ErrorCode)1 MessageType (de.rwth.idsg.steve.ocpp.ws.data.MessageType)1 OcppJsonCall (de.rwth.idsg.steve.ocpp.ws.data.OcppJsonCall)1 OcppJsonError (de.rwth.idsg.steve.ocpp.ws.data.OcppJsonError)1 OcppJsonResult (de.rwth.idsg.steve.ocpp.ws.data.OcppJsonResult)1