/** * Gets the textual representation of the transaction identifier. * <p/> * The textual representation is formed by concatenating the charging station identifier, protocol identifier, and * the transaction number. * * @return the textual representation of the transaction identifier. */ @Override public String getId() { return String.format("%s_%s_%s", chargingStationId.getId(), protocol, number); }
public void addWebSocket(String chargingStationIdentifier, WebSocket webSocket) { WebSocketWrapper existingWrapper = sockets.get(chargingStationIdentifier); if (existingWrapper != null && !webSocket.equals(existingWrapper.getWebSocket())) { LOG.info("Found existing websocket connection when adding new websocket for {}. Closing existing one", chargingStationIdentifier); closeAndRemoveWebSocket(chargingStationIdentifier, existingWrapper.getWebSocket()); } sockets.put(chargingStationIdentifier, new WebSocketWrapper(new ChargingStationId(chargingStationIdentifier), webSocket, wampMessageHandler, gson)); }
/** * Gets the textual representation of the identifier. * <p/> * The textual representation is formed by concatenating the charging station identifier, protocol identifier, and * the reservation number. * * @return the textual representation of the identifier. */ @Override public String getId() { return String.format("%s_%s_%s", chargingStationId.getId(), protocol, number); }
@Override public void onTextStream(WebSocket webSocket, Reader reader) throws IOException { String chargingStationId = determineIdentifier(webSocket); ocppJsonService.handleMessage(new ChargingStationId(chargingStationId), reader); }
private void writeToSocket(String message) throws IOException { LOG.info("Writing to charging station [{}]: {}", chargingStationId.getId(), message); webSocket.write(message); }
@Override public HeartbeatResponse heartbeat(HeartbeatRequest parameters, String chargeBoxIdentity) { domainService.heartbeat(new ChargingStationId(chargeBoxIdentity), addOnIdentity); HeartbeatResponse response = new HeartbeatResponse(); response.setCurrentTime(new Date()); return response; }
private void sendWampMessage(WampMessage wampMessage, ChargingStationId chargingStationId) { WebSocketWrapper webSocketWrapper = sockets.get(chargingStationId.getId()); if (webSocketWrapper != null) { try { webSocketWrapper.sendMessage(wampMessage); } catch (ChargePointCommunicationException e) { closeAndRemoveWebSocket(chargingStationId.getId(), e.getWebSocket()); LOG.warn("Removed web socket for chargepoint [{}] due to errors", chargingStationId); } } else { LOG.error("No web socket found for charging station id [{}]", chargingStationId.getId()); } }
@Override public HeartbeatResponse heartbeat(HeartbeatRequest parameters, String chargeBoxIdentity) { domainService.heartbeat(new ChargingStationId(chargeBoxIdentity), addOnIdentity); HeartbeatResponse response = new HeartbeatResponse(); response.setCurrentTime(new Date()); return response; }
/** * Retrieves the number from a transaction id string. ChargingStationId and protocol are passed to make a better * guess at the number. * * @param chargingStationId the charging station's identifier. * @param protocol the protocol identifier. * @param transactionId the transaction id containing the number. * @return the transaction number * @throws NumberFormatException if the number cannot be extracted from {@code transactionId}. */ private int numberFromTransactionIdString(ChargingStationId chargingStationId, String protocol, String transactionId) { String transactionIdPartBeforeNumber = String.format("%s_%s_", chargingStationId.getId(), protocol); try { return Integer.parseInt(transactionId.substring(transactionIdPartBeforeNumber.length())); } catch (NumberFormatException e) { throw new NumberFormatException(String.format("Cannot retrieve transaction number from string [%s]", transactionId)); } }
@Override public MeterValuesResponse meterValues(MeterValuesRequest request, String chargeBoxIdentity) { ChargingStationId chargingStationId = new ChargingStationId(chargeBoxIdentity); //OCPP 1.2 does not contain transactionId references in it's metervalue communication TransactionId transactionId = null; List<MeterValue> meterValues = new ArrayList<>(); for (io.motown.ocpp.v12.soap.centralsystem.schema.MeterValue mv : request.getValues()) { meterValues.add(new MeterValue(mv.getTimestamp(), Integer.toString(mv.getValue()))); } domainService.meterValues(chargingStationId, transactionId, new EvseId(request.getConnectorId()), meterValues, addOnIdentity); return new MeterValuesResponse(); }
/** * Tries to find a {@code ChargingStation} by the passed id, if not found an error will be logged and null will be * returned. * * @param chargingStationId charging station identifier. * @return charging station if found, null otherwise. */ private ChargingStation getChargingStation(ChargingStationId chargingStationId) { ChargingStation chargingStation = chargingStationRepository.findOne(chargingStationId.getId()); if (chargingStation == null) { LOG.error("Could not find charging station {}", chargingStationId); } return chargingStation; }
@Override public DiagnosticsStatusNotificationResponse diagnosticsStatusNotification(DiagnosticsStatusNotificationRequest request, String chargeBoxIdentity) { ChargingStationId chargingStationId = new ChargingStationId(chargeBoxIdentity); DiagnosticsStatus diagnosticsStatus = request.getStatus(); domainService.diagnosticsUploadStatusUpdate(chargingStationId, DiagnosticsStatus.UPLOADED.equals(diagnosticsStatus), addOnIdentity); return new DiagnosticsStatusNotificationResponse(); }
@Override public int getAuthorizationListVersion(ChargingStationId id) { ChargePointService chargePointService = this.createChargingStationService(id); GetLocalListVersionResponse response = chargePointService.getLocalListVersion(new GetLocalListVersionRequest(), id.getId()); int currentWhitelistVersion = response.getListVersion(); LOG.info("At the moment {} has authorizationlist version {}", id.getId(), currentWhitelistVersion); return currentWhitelistVersion; }
@Override public DiagnosticsStatusNotificationResponse diagnosticsStatusNotification(DiagnosticsStatusNotificationRequest request, String chargeBoxIdentity) { ChargingStationId chargingStationId = new ChargingStationId(chargeBoxIdentity); DiagnosticsStatus diagnosticsStatus = request.getStatus(); domainService.diagnosticsUploadStatusUpdate(chargingStationId, DiagnosticsStatus.UPLOADED.equals(diagnosticsStatus), addOnIdentity); return new DiagnosticsStatusNotificationResponse(); }
@Override public boolean clearCache(ChargingStationId id) { ChargePointService chargePointService = this.createChargingStationService(id); ClearCacheRequest request = new ClearCacheRequest(); boolean requestResult; ClearCacheResponse response = chargePointService.clearCache(request, id.getId()); if (ClearCacheStatus.ACCEPTED.equals(response.getStatus())) { LOG.info("Clear cache on {} has been accepted", id.getId()); requestResult = true; } else { LOG.warn("Clear cache on {} has been rejected", id.getId()); requestResult = false; } return requestResult; }
@Override public StopTransactionResponse stopTransaction(StopTransactionRequest request, String chargeBoxIdentity) { ChargingStationId chargingStationId = new ChargingStationId(chargeBoxIdentity); NumberedTransactionId transactionId = new NumberedTransactionId(chargingStationId, PROTOCOL_IDENTIFIER, request.getTransactionId()); IdentifyingToken identifyingToken = new TextualToken(request.getIdTag()); //OCPP 1.2 does not include the meter values in a stop transaction message List<MeterValue> meterValues = new ArrayList<>(); domainService.stopTransaction(chargingStationId, transactionId, identifyingToken, request.getMeterStop(), request.getTimestamp(), meterValues, addOnIdentity); return new StopTransactionResponse(); }
@Override public boolean clearCache(ChargingStationId id) { ChargePointService chargePointService = this.createChargingStationService(id); ClearCacheRequest request = new ClearCacheRequest(); boolean requestResult; ClearCacheResponse response = chargePointService.clearCache(request, id.getId()); if (ClearCacheStatus.ACCEPTED.equals(response.getStatus())) { LOG.info("Clear cache on {} has been accepted", id.getId()); requestResult = true; } else { LOG.warn("Clear cache on {} has been rejected", id.getId()); requestResult = false; } return requestResult; }
/** * {@inheritDoc} */ @Override public void handle(String chargingStationId, JsonObject commandObject, IdentityContext identityContext) throws UserIdentityUnauthorizedException { ChargingStationId csId = new ChargingStationId(chargingStationId); if (!commandAuthorizationService.isAuthorized(csId, identityContext.getUserIdentity(), RequestDataTransferCommand.class)) { throw new UserIdentityUnauthorizedException(chargingStationId, identityContext.getUserIdentity(), RequestDataTransferCommand.class); } try { ChargingStation chargingStation = repository.findOne(chargingStationId); if (chargingStation != null && chargingStation.communicationAllowed()) { DataTransferMessage dataTransferMessage = gson.fromJson(commandObject, DataTransferMessage.class); commandGateway.send(new RequestDataTransferCommand(csId, dataTransferMessage, identityContext), new CorrelationToken()); } } catch (JsonSyntaxException ex) { throw new IllegalArgumentException("Data transfer command not able to parse the payload, is your json correctly formatted?", ex); } } }
protected void checkAuthorization(ChargingStationId chargingStationId, UserIdentity userIdentity, Class commandClass) throws UserIdentityUnauthorizedException { if (!commandAuthorizationService.isAuthorized(chargingStationId, userIdentity, commandClass)) { throw new UserIdentityUnauthorizedException(chargingStationId.getId(), userIdentity, commandClass); } }
/** * {@inheritDoc} */ @Override public void handle(String chargingStationId, JsonObject commandObject, IdentityContext identityContext) throws UserIdentityUnauthorizedException { ChargingStationId csId = new ChargingStationId(chargingStationId); if (!commandAuthorizationService.isAuthorized(csId, identityContext.getUserIdentity(), AddChargingStationOpeningTimesCommand.class)) { throw new UserIdentityUnauthorizedException(chargingStationId, identityContext.getUserIdentity(), AddChargingStationOpeningTimesCommand.class); } try { ChargingStation chargingStation = repository.findOne(chargingStationId); if (chargingStation != null && chargingStation.isAccepted()) { AddChargingStationOpeningTimesApiCommand command = gson.fromJson(commandObject, AddChargingStationOpeningTimesApiCommand.class); commandGateway.send(new AddChargingStationOpeningTimesCommand(csId, command.getOpeningTimes(), identityContext)); } } catch (JsonSyntaxException e) { throw new IllegalArgumentException("Set charging station opening times command not able to parse the payload, is your JSON correctly formatted?", e); } } }