fabyr / ocpp-sharp

Implementation of the Open Charge Point Protocol in C# (.NET 8)
MIT License
1 stars 0 forks source link

Sending a request to the charging station #2

Closed DgnOnur closed 1 year ago

DgnOnur commented 1 year ago

Hello,

Thank you for your sample project. I was able to successfully establish a connection as shown in the screen visuals when I configured the settings of my charging station device according to your application. resim

I couldn't fulfill my requests such as starting, stopping, and restarting conditions on the Server-connected Condition Device through a different application where I will be doing development. Could you please share sample code that I can use to perform these operations?

fabyr commented 1 year ago

Hello,

I'm sorry I can't quite follow your goal, could you please elaborate? And please add any code you've already tried (if there is). Is the question about how to implement the Handler-Methods of StartTransaction and StopTransaction (or other requests from the station)? Or about how to send a request to the charging station outside of a Handler?

DgnOnur commented 1 year ago

Hello, I have established the connection between the server and the charging station.

To illustrate what I want to do: When I initiate charging from a different application I will write in C# .NET, I couldn't write the code to send this request to the charging station.

I have added all the handlers in the server application. However, even if I send a request from the client application to the server application, I cannot send this request to the actual device.

fabyr commented 1 year ago

You can send a Request to a charging station if you keep track of the clients which are connected. Which you have to do yourself currently:

// list to store the connecctions
List<OcppClientConnection> connectedClients = new();

OcppSharpServer server = new("/ocpp16", ProtocolVersion.OCPP16, port);
server.RegisterHandler<BootNotificationRequest>((server, sender, req) => {

    connectedClients.Add(sender);

    return /* ... */
});
/* ... */
/* ... */
/* ... */
// later in time
// send a request to all connected charging stations
foreach(OcppClientConnection client in connectedClients)
{
    // for example: Reset
    await client.SendRequestAsync(new ResetRequest() 
    {
        type = ResetType.Enum.Soft
    });
}

Note: It may not be threadsafe yet and lead to issues with many concurrent connections. And also when a client disconnects, this is not registered.

Admittedly this is kind-of a bad solution and i might add a way to keep track of connections internally in the server class. And also add some more useful events.

Since you metioned a client application and an actual device, there's the possibility that you want to use the server as some kind of proxy/relay. This is not the intended use of the server and probably won't work. But if that's not the goal it should work out :)

DgnOnur commented 1 year ago

Thank you. problem solved.