Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 69 additions & 1 deletion OCPP.Core.Server/OCPPMiddleware.OCPP16.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private async Task Receive16(ChargePointStatus chargePointStatus, HttpContext co
}

/// <summary>
/// Waits for new OCPP V1.6 messages on the open websocket connection and delegates processing to a controller
/// Sends a (Soft-)Reset to the chargepoint
/// </summary>
private async Task Reset16(ChargePointStatus chargePointStatus, HttpContext apiCallerContext)
{
Expand Down Expand Up @@ -147,6 +147,74 @@ private async Task Reset16(ChargePointStatus chargePointStatus, HttpContext apiC
await apiCallerContext.Response.WriteAsync(apiResult);
}

/// <summary>
/// Sends a Remote Start Transaction to the chargepoint
/// </summary>
private async Task RemoteStartTransaction16(ChargePointStatus chargePointStatus, HttpContext apiCallerContext)
{
ILogger logger = _logFactory.CreateLogger("OCPPMiddleware.OCPP16");
ControllerOCPP16 controller16 = new ControllerOCPP16(_configuration, _logFactory, chargePointStatus);

Messages_OCPP16.ResetRequest resetRequest = new Messages_OCPP16.ResetRequest();
resetRequest.Type = Messages_OCPP16.ResetRequestType.Soft;
string jsonResetRequest = JsonConvert.SerializeObject(resetRequest);

OCPPMessage msgOut = new OCPPMessage();
msgOut.MessageType = "2";
msgOut.Action = "RemoteStartTransaction";
msgOut.UniqueId = Guid.NewGuid().ToString("N");
msgOut.JsonPayload = jsonResetRequest;
msgOut.TaskCompletionSource = new TaskCompletionSource<string>();

// store HttpContext with MsgId for later answer processing (=> send anwer to API caller)
_requestQueue.Add(msgOut.UniqueId, msgOut);

// Send OCPP message with optional logging/dump
await SendOcpp16Message(msgOut, logger, chargePointStatus.WebSocket);

// Wait for asynchronous chargepoint response and processing
string apiResult = await msgOut.TaskCompletionSource.Task;

//
apiCallerContext.Response.StatusCode = 200;
apiCallerContext.Response.ContentType = "application/json";
await apiCallerContext.Response.WriteAsync(apiResult);
}

/// <summary>
/// Sends a Remote Start Transaction to the chargepoint
/// </summary>
private async Task RemoteStopTransaction16(ChargePointStatus chargePointStatus, HttpContext apiCallerContext)
{
ILogger logger = _logFactory.CreateLogger("OCPPMiddleware.OCPP16");
ControllerOCPP16 controller16 = new ControllerOCPP16(_configuration, _logFactory, chargePointStatus);

Messages_OCPP16.ResetRequest resetRequest = new Messages_OCPP16.ResetRequest();
resetRequest.Type = Messages_OCPP16.ResetRequestType.Soft;
string jsonResetRequest = JsonConvert.SerializeObject(resetRequest);

OCPPMessage msgOut = new OCPPMessage();
msgOut.MessageType = "2";
msgOut.Action = "RemoteStopTransaction";
msgOut.UniqueId = Guid.NewGuid().ToString("N");
msgOut.JsonPayload = jsonResetRequest;
msgOut.TaskCompletionSource = new TaskCompletionSource<string>();

// store HttpContext with MsgId for later answer processing (=> send anwer to API caller)
_requestQueue.Add(msgOut.UniqueId, msgOut);

// Send OCPP message with optional logging/dump
await SendOcpp16Message(msgOut, logger, chargePointStatus.WebSocket);

// Wait for asynchronous chargepoint response and processing
string apiResult = await msgOut.TaskCompletionSource.Task;

//
apiCallerContext.Response.StatusCode = 200;
apiCallerContext.Response.ContentType = "application/json";
await apiCallerContext.Response.WriteAsync(apiResult);
}

/// <summary>
/// Sends a Unlock-Request to the chargepoint
/// </summary>
Expand Down
74 changes: 74 additions & 0 deletions OCPP.Core.Server/OCPPMiddleware.OCPP20.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,80 @@ private async Task Reset20(ChargePointStatus chargePointStatus, HttpContext apiC
await apiCallerContext.Response.WriteAsync(apiResult);
}

/// <summary>
/// Sends a Remote Start Transaction to the chargepoint
/// </summary>
private async Task RemoteStartTransaction20(ChargePointStatus chargePointStatus, HttpContext apiCallerContext)
{
ILogger logger = _logFactory.CreateLogger("OCPPMiddleware.OCPP20");
ControllerOCPP20 controller20 = new ControllerOCPP20(_configuration, _logFactory, chargePointStatus);

Messages_OCPP20.ResetRequest resetRequest = new Messages_OCPP20.ResetRequest();
resetRequest.Type = Messages_OCPP20.ResetEnumType.OnIdle;
resetRequest.CustomData = new CustomDataType();
resetRequest.CustomData.VendorId = ControllerOCPP20.VendorId;

string jsonResetRequest = JsonConvert.SerializeObject(resetRequest);

OCPPMessage msgOut = new OCPPMessage();
msgOut.MessageType = "2";
msgOut.Action = "RemoteStartTransaction";
msgOut.UniqueId = Guid.NewGuid().ToString("N");
msgOut.JsonPayload = jsonResetRequest;
msgOut.TaskCompletionSource = new TaskCompletionSource<string>();

// store HttpContext with MsgId for later answer processing (=> send anwer to API caller)
_requestQueue.Add(msgOut.UniqueId, msgOut);

// Send OCPP message with optional logging/dump
await SendOcpp20Message(msgOut, logger, chargePointStatus.WebSocket);

// Wait for asynchronous chargepoint response and processing
string apiResult = await msgOut.TaskCompletionSource.Task;

//
apiCallerContext.Response.StatusCode = 200;
apiCallerContext.Response.ContentType = "application/json";
await apiCallerContext.Response.WriteAsync(apiResult);
}

/// <summary>
/// Sends a Remote Stop Transaction to the chargepoint
/// </summary>
private async Task RemoteStopTransaction20(ChargePointStatus chargePointStatus, HttpContext apiCallerContext)
{
ILogger logger = _logFactory.CreateLogger("OCPPMiddleware.OCPP20");
ControllerOCPP20 controller20 = new ControllerOCPP20(_configuration, _logFactory, chargePointStatus);

Messages_OCPP20.ResetRequest resetRequest = new Messages_OCPP20.ResetRequest();
resetRequest.Type = Messages_OCPP20.ResetEnumType.OnIdle;
resetRequest.CustomData = new CustomDataType();
resetRequest.CustomData.VendorId = ControllerOCPP20.VendorId;

string jsonResetRequest = JsonConvert.SerializeObject(resetRequest);

OCPPMessage msgOut = new OCPPMessage();
msgOut.MessageType = "2";
msgOut.Action = "RemoteStopTransaction";
msgOut.UniqueId = Guid.NewGuid().ToString("N");
msgOut.JsonPayload = jsonResetRequest;
msgOut.TaskCompletionSource = new TaskCompletionSource<string>();

// store HttpContext with MsgId for later answer processing (=> send anwer to API caller)
_requestQueue.Add(msgOut.UniqueId, msgOut);

// Send OCPP message with optional logging/dump
await SendOcpp20Message(msgOut, logger, chargePointStatus.WebSocket);

// Wait for asynchronous chargepoint response and processing
string apiResult = await msgOut.TaskCompletionSource.Task;

//
apiCallerContext.Response.StatusCode = 200;
apiCallerContext.Response.ContentType = "application/json";
await apiCallerContext.Response.WriteAsync(apiResult);
}

/// <summary>
/// Sends a Unlock-Request to the chargepoint
/// </summary>
Expand Down
80 changes: 80 additions & 0 deletions OCPP.Core.Server/OCPPMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,86 @@ public async Task Invoke(HttpContext context)
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
}
}
else if (cmd == "RemoteStartTransaction")
{
if (!string.IsNullOrEmpty(urlChargePointId))
{
try
{
ChargePointStatus status = null;
if (_chargePointStatusDict.TryGetValue(urlChargePointId, out status))
{
// Send message to chargepoint
if (status.Protocol == Protocol_OCPP20)
{
// OCPP V2.0
await RemoteStartTransaction20(status, context);
}
else
{
// OCPP V1.6
await RemoteStartTransaction16(status, context);
}
}
else
{
// Chargepoint offline
_logger.LogError("OCPPMiddleware SoftReset => Chargepoint offline: {0}", urlChargePointId);
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
}
}
catch (Exception exp)
{
_logger.LogError(exp, "OCPPMiddleware SoftReset => Error: {0}", exp.Message);
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
}
}
else
{
_logger.LogError("OCPPMiddleware SoftReset => Missing chargepoint ID");
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
}
}
else if (cmd == "RemoteStopTransaction")
{
if (!string.IsNullOrEmpty(urlChargePointId))
{
try
{
ChargePointStatus status = null;
if (_chargePointStatusDict.TryGetValue(urlChargePointId, out status))
{
// Send message to chargepoint
if (status.Protocol == Protocol_OCPP20)
{
// OCPP V2.0
await RemoteStopTransaction20(status, context);
}
else
{
// OCPP V1.6
await RemoteStopTransaction16(status, context);
}
}
else
{
// Chargepoint offline
_logger.LogError("OCPPMiddleware SoftReset => Chargepoint offline: {0}", urlChargePointId);
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
}
}
catch (Exception exp)
{
_logger.LogError(exp, "OCPPMiddleware SoftReset => Error: {0}", exp.Message);
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
}
}
else
{
_logger.LogError("OCPPMiddleware SoftReset => Missing chargepoint ID");
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
}
}
else if (cmd == "UnlockConnector")
{
if (!string.IsNullOrEmpty(urlChargePointId))
Expand Down