Ejemplo n.º 1
0
        /// <summary>
        /// Register all URI templates for this SOAP API.
        /// </summary>
        protected void RegisterURITemplates()
        {
            #region ~/ - GetServiceAuthorisation

            SOAPServer.RegisterSOAPDelegate(HTTPHostname.Any,
                                            URLPrefix + AuthorisationURL,
                                            "GetServiceAuthorisationRequest",
                                            XML => XML.Descendants(eMIPNS.Authorisation + "eMIP_FromIOP_GetServiceAuthorisationRequest").FirstOrDefault(),
                                            async(HTTPRequest, GetServiceAuthorisationXML) => {
                GetServiceAuthorisationResponse Response = null;

                #region Send OnGetServiceAuthorisationSOAPRequest event

                var StartTime = DateTime.UtcNow;

                try
                {
                    if (OnGetServiceAuthorisationSOAPRequest != null)
                    {
                        await Task.WhenAll(OnGetServiceAuthorisationSOAPRequest.GetInvocationList().
                                           Cast <RequestLogHandler>().
                                           Select(e => e(StartTime,
                                                         SOAPServer.HTTPServer,
                                                         HTTPRequest))).
                        ConfigureAwait(false);
                    }
                }
                catch (Exception e)
                {
                    DebugX.LogException(e, nameof(EMPServer) + "." + nameof(OnGetServiceAuthorisationSOAPRequest));
                }

                #endregion


                if (GetServiceAuthorisationRequest.TryParse(GetServiceAuthorisationXML,
                                                            CustomGetServiceAuthorisationRequestParser,
                                                            out GetServiceAuthorisationRequest _GetServiceAuthorisationRequest,
                                                            OnException,

                                                            HTTPRequest,
                                                            HTTPRequest.Timestamp,
                                                            HTTPRequest.CancellationToken,
                                                            HTTPRequest.EventTrackingId,
                                                            HTTPRequest.Timeout ?? DefaultRequestTimeout))
                {
                    #region Send OnGetServiceAuthorisationRequest event

                    try
                    {
                        if (OnGetServiceAuthorisationRequest != null)
                        {
                            await Task.WhenAll(OnGetServiceAuthorisationRequest.GetInvocationList().
                                               Cast <OnGetServiceAuthorisationRequestDelegate>().
                                               Select(e => e(StartTime,
                                                             _GetServiceAuthorisationRequest.Timestamp.Value,
                                                             this,
                                                             ServiceName,
                                                             _GetServiceAuthorisationRequest.EventTrackingId,
                                                             _GetServiceAuthorisationRequest.TransactionId.Value,
                                                             _GetServiceAuthorisationRequest.PartnerId,
                                                             _GetServiceAuthorisationRequest.OperatorId,
                                                             _GetServiceAuthorisationRequest.TargetOperatorId,
                                                             _GetServiceAuthorisationRequest.EVSEId,
                                                             _GetServiceAuthorisationRequest.UserId,
                                                             _GetServiceAuthorisationRequest.RequestedServiceId,
                                                             _GetServiceAuthorisationRequest.ServiceSessionId,
                                                             _GetServiceAuthorisationRequest.BookingId,

                                                             _GetServiceAuthorisationRequest.RequestTimeout ?? DefaultRequestTimeout))).
                            ConfigureAwait(false);
                        }
                    }
                    catch (Exception e)
                    {
                        DebugX.LogException(e, nameof(EMPServer) + "." + nameof(OnGetServiceAuthorisationRequest));
                    }

                    #endregion

                    #region Call async subscribers

                    if (OnGetServiceAuthorisation != null)
                    {
                        var results = await Task.WhenAll(OnGetServiceAuthorisation.GetInvocationList().
                                                         Cast <OnGetServiceAuthorisationDelegate>().
                                                         Select(e => e(DateTime.UtcNow,
                                                                       this,
                                                                       _GetServiceAuthorisationRequest))).
                                      ConfigureAwait(false);

                        Response = results.FirstOrDefault();
                    }

                    //if (Response == null)
                    //    Response = Response<EMP.GetServiceAuthorisationRequest>.SystemError(
                    //                         _GetServiceAuthorisationRequest,
                    //                         "Could not process the incoming GetServiceAuthorisation request!",
                    //                         null,
                    //                         _GetServiceAuthorisationRequest.SessionId,
                    //                         _GetServiceAuthorisationRequest.PartnerSessionId
                    //                     );

                    #endregion

                    #region Send OnGetServiceAuthorisationResponse event

                    var EndTime = DateTime.UtcNow;

                    try
                    {
                        if (OnGetServiceAuthorisationResponse != null)
                        {
                            await Task.WhenAll(OnGetServiceAuthorisationResponse.GetInvocationList().
                                               Cast <OnGetServiceAuthorisationResponseDelegate>().
                                               Select(e => e(EndTime,
                                                             this,
                                                             ServiceName,
                                                             _GetServiceAuthorisationRequest.EventTrackingId,
                                                             _GetServiceAuthorisationRequest.TransactionId.Value,
                                                             _GetServiceAuthorisationRequest.PartnerId,
                                                             _GetServiceAuthorisationRequest.OperatorId,
                                                             _GetServiceAuthorisationRequest.TargetOperatorId,
                                                             _GetServiceAuthorisationRequest.EVSEId,
                                                             _GetServiceAuthorisationRequest.UserId,
                                                             _GetServiceAuthorisationRequest.RequestedServiceId,
                                                             _GetServiceAuthorisationRequest.ServiceSessionId,
                                                             _GetServiceAuthorisationRequest.BookingId,
                                                             _GetServiceAuthorisationRequest.RequestTimeout ?? DefaultRequestTimeout,
                                                             Response,
                                                             EndTime - StartTime))).
                            ConfigureAwait(false);
                        }
                    }
                    catch (Exception e)
                    {
                        DebugX.LogException(e, nameof(EMPServer) + "." + nameof(OnGetServiceAuthorisationResponse));
                    }

                    #endregion
                }

                //else
                //    Response = Response<EMP.GetServiceAuthorisationRequest>.DataError(
                //                          _GetServiceAuthorisationRequest,
                //                          "Could not process the incoming GetServiceAuthorisation request!"
                //                      );


                #region Create SOAPResponse

                var HTTPResponse = new HTTPResponse.Builder(HTTPRequest)
                {
                    HTTPStatusCode = HTTPStatusCode.OK,
                    Server         = SOAPServer.HTTPServer.DefaultServerName,
                    Date           = DateTime.UtcNow,
                    ContentType    = HTTPContentType.XMLTEXT_UTF8,
                    Content        = SOAP.Encapsulation(Response.ToXML(CustomGetServiceAuthorisationResponseSerializer)).ToUTF8Bytes(),
                    Connection     = "close"
                };

                #endregion

                #region Send OnGetServiceAuthorisationSOAPResponse event

                try
                {
                    if (OnGetServiceAuthorisationSOAPResponse != null)
                    {
                        await Task.WhenAll(OnGetServiceAuthorisationSOAPResponse.GetInvocationList().
                                           Cast <AccessLogHandler>().
                                           Select(e => e(HTTPResponse.Timestamp,
                                                         SOAPServer.HTTPServer,
                                                         HTTPRequest,
                                                         HTTPResponse))).
                        ConfigureAwait(false);
                    }
                }
                catch (Exception e)
                {
                    DebugX.LogException(e, nameof(EMPServer) + "." + nameof(OnGetServiceAuthorisationSOAPResponse));
                }

                #endregion

                return(HTTPResponse);
            });

            #endregion

            #region ~/ - SetSessionEventReport

            SOAPServer.RegisterSOAPDelegate(HTTPHostname.Any,
                                            URLPrefix + AuthorisationURL,
                                            "SetSessionEventReportRequest",
                                            XML => XML.Descendants(eMIPNS.Authorisation + "eMIP_FromIOP_SetSessionEventReportRequest").FirstOrDefault(),
                                            async(HTTPRequest, SetSessionEventReportXML) => {
                SetSessionEventReportResponse Response = null;

                #region Send OnSetSessionEventReportSOAPRequest event

                var StartTime = DateTime.UtcNow;

                try
                {
                    if (OnSetSessionEventReportSOAPRequest != null)
                    {
                        await Task.WhenAll(OnSetSessionEventReportSOAPRequest.GetInvocationList().
                                           Cast <RequestLogHandler>().
                                           Select(e => e(StartTime,
                                                         SOAPServer.HTTPServer,
                                                         HTTPRequest))).
                        ConfigureAwait(false);
                    }
                }
                catch (Exception e)
                {
                    DebugX.LogException(e, nameof(EMPServer) + "." + nameof(OnSetSessionEventReportSOAPRequest));
                }

                #endregion


                if (SetSessionEventReportRequest.TryParse(SetSessionEventReportXML,
                                                          out SetSessionEventReportRequest _SetSessionEventReportRequest,
                                                          CustomSetSessionEventReportRequestParser,
                                                          CustomSessionEventParser,
                                                          OnException,

                                                          HTTPRequest,
                                                          HTTPRequest.Timestamp,
                                                          HTTPRequest.CancellationToken,
                                                          HTTPRequest.EventTrackingId,
                                                          HTTPRequest.Timeout ?? DefaultRequestTimeout))
                {
                    #region Send OnSetSessionEventReportRequest event

                    try
                    {
                        if (OnSetSessionEventReportRequest != null)
                        {
                            await Task.WhenAll(OnSetSessionEventReportRequest.GetInvocationList().
                                               Cast <OnSetSessionEventReportRequestDelegate>().
                                               Select(e => e(StartTime,
                                                             _SetSessionEventReportRequest.Timestamp.Value,
                                                             this,
                                                             ServiceName,
                                                             _SetSessionEventReportRequest.EventTrackingId,

                                                             _SetSessionEventReportRequest.PartnerId,
                                                             _SetSessionEventReportRequest.OperatorId,
                                                             _SetSessionEventReportRequest.TargetOperatorId,
                                                             _SetSessionEventReportRequest.ServiceSessionId,
                                                             _SetSessionEventReportRequest.SessionEvent,

                                                             _SetSessionEventReportRequest.TransactionId,
                                                             _SetSessionEventReportRequest.SalePartnerSessionId,

                                                             _SetSessionEventReportRequest.RequestTimeout ?? DefaultRequestTimeout))).
                            ConfigureAwait(false);
                        }
                    }
                    catch (Exception e)
                    {
                        DebugX.LogException(e, nameof(EMPServer) + "." + nameof(OnSetSessionEventReportRequest));
                    }

                    #endregion

                    #region Call async subscribers

                    if (OnSetSessionEventReport != null)
                    {
                        var results = await Task.WhenAll(OnSetSessionEventReport.GetInvocationList().
                                                         Cast <OnSetSessionEventReportDelegate>().
                                                         Select(e => e(DateTime.UtcNow,
                                                                       this,
                                                                       _SetSessionEventReportRequest))).
                                      ConfigureAwait(false);

                        Response = results.FirstOrDefault();
                    }

                    //if (Response == null)
                    //    Response = Response<EMP.SetSessionEventReportRequest>.SystemError(
                    //                         _SetSessionEventReportRequest,
                    //                         "Could not process the incoming SetSessionEventReport request!",
                    //                         null,
                    //                         _SetSessionEventReportRequest.SessionId,
                    //                         _SetSessionEventReportRequest.PartnerSessionId
                    //                     );

                    #endregion

                    #region Send OnSetSessionEventReportResponse event

                    var EndTime = DateTime.UtcNow;

                    try
                    {
                        if (OnSetSessionEventReportResponse != null)
                        {
                            await Task.WhenAll(OnSetSessionEventReportResponse.GetInvocationList().
                                               Cast <OnSetSessionEventReportResponseDelegate>().
                                               Select(e => e(EndTime,
                                                             this,
                                                             ServiceName,
                                                             _SetSessionEventReportRequest.EventTrackingId,

                                                             _SetSessionEventReportRequest.PartnerId,
                                                             _SetSessionEventReportRequest.OperatorId,
                                                             _SetSessionEventReportRequest.TargetOperatorId,
                                                             _SetSessionEventReportRequest.ServiceSessionId,
                                                             _SetSessionEventReportRequest.SessionEvent,

                                                             _SetSessionEventReportRequest.TransactionId,
                                                             _SetSessionEventReportRequest.SalePartnerSessionId,

                                                             _SetSessionEventReportRequest.RequestTimeout ?? DefaultRequestTimeout,
                                                             Response,
                                                             EndTime - StartTime))).
                            ConfigureAwait(false);
                        }
                    }
                    catch (Exception e)
                    {
                        DebugX.LogException(e, nameof(EMPServer) + "." + nameof(OnSetSessionEventReportResponse));
                    }

                    #endregion
                }

                //else
                //    Response = Response<EMP.SetSessionEventReportRequest>.DataError(
                //                          _SetSessionEventReportRequest,
                //                          "Could not process the incoming SetSessionEventReport request!"
                //                      );


                #region Create SOAPResponse

                var HTTPResponse = new HTTPResponse.Builder(HTTPRequest)
                {
                    HTTPStatusCode = HTTPStatusCode.OK,
                    Server         = SOAPServer.HTTPServer.DefaultServerName,
                    Date           = DateTime.UtcNow,
                    ContentType    = HTTPContentType.XMLTEXT_UTF8,
                    Content        = SOAP.Encapsulation(Response.ToXML(CustomSetSessionEventReportResponseSerializer)).ToUTF8Bytes(),
                    Connection     = "close"
                };

                #endregion

                #region Send OnSetSessionEventReportSOAPResponse event

                try
                {
                    if (OnSetSessionEventReportSOAPResponse != null)
                    {
                        await Task.WhenAll(OnSetSessionEventReportSOAPResponse.GetInvocationList().
                                           Cast <AccessLogHandler>().
                                           Select(e => e(HTTPResponse.Timestamp,
                                                         SOAPServer.HTTPServer,
                                                         HTTPRequest,
                                                         HTTPResponse))).
                        ConfigureAwait(false);
                    }
                }
                catch (Exception e)
                {
                    DebugX.LogException(e, nameof(EMPServer) + "." + nameof(OnSetSessionEventReportSOAPResponse));
                }

                #endregion

                return(HTTPResponse);
            });

            #endregion

            #region ~/ - SetChargeDetailRecord

            SOAPServer.RegisterSOAPDelegate(HTTPHostname.Any,
                                            URLPrefix + AuthorisationURL,
                                            "SetChargeDetailRecordRequest",
                                            XML => XML.Descendants(eMIPNS.Authorisation + "eMIP_FromIOP_SetChargeDetailRecordRequest").FirstOrDefault(),
                                            async(HTTPRequest, SetChargeDetailRecordXML) => {
                SetChargeDetailRecordResponse Response = null;

                #region Send OnSetChargeDetailRecordSOAPRequest event

                var StartTime = DateTime.UtcNow;

                try
                {
                    if (OnSetChargeDetailRecordSOAPRequest != null)
                    {
                        await Task.WhenAll(OnSetChargeDetailRecordSOAPRequest.GetInvocationList().
                                           Cast <RequestLogHandler>().
                                           Select(e => e(StartTime,
                                                         SOAPServer.HTTPServer,
                                                         HTTPRequest))).
                        ConfigureAwait(false);
                    }
                }
                catch (Exception e)
                {
                    DebugX.LogException(e, nameof(EMPServer) + "." + nameof(OnSetChargeDetailRecordSOAPRequest));
                }

                #endregion


                if (SetChargeDetailRecordRequest.TryParse(SetChargeDetailRecordXML,
                                                          out SetChargeDetailRecordRequest _SetChargeDetailRecordRequest,
                                                          CustomSetChargeDetailRecordRequestParser,
                                                          CustomChargeDetailRecordParser,
                                                          CustomMeterReportParser,
                                                          OnException,

                                                          HTTPRequest,
                                                          HTTPRequest.Timestamp,
                                                          HTTPRequest.CancellationToken,
                                                          HTTPRequest.EventTrackingId,
                                                          HTTPRequest.Timeout ?? DefaultRequestTimeout))
                {
                    #region Send OnSetChargeDetailRecordRequest event

                    try
                    {
                        if (OnSetChargeDetailRecordRequest != null)
                        {
                            await Task.WhenAll(OnSetChargeDetailRecordRequest.GetInvocationList().
                                               Cast <OnSetChargeDetailRecordRequestDelegate>().
                                               Select(e => e(StartTime,
                                                             _SetChargeDetailRecordRequest.Timestamp.Value,
                                                             this,
                                                             ServiceName,
                                                             _SetChargeDetailRecordRequest.EventTrackingId,

                                                             _SetChargeDetailRecordRequest.PartnerId,
                                                             _SetChargeDetailRecordRequest.OperatorId,
                                                             _SetChargeDetailRecordRequest.ChargeDetailRecord,
                                                             _SetChargeDetailRecordRequest.TransactionId,

                                                             _SetChargeDetailRecordRequest.RequestTimeout ?? DefaultRequestTimeout))).
                            ConfigureAwait(false);
                        }
                    }
                    catch (Exception e)
                    {
                        DebugX.LogException(e, nameof(EMPServer) + "." + nameof(OnSetChargeDetailRecordRequest));
                    }

                    #endregion

                    #region Call async subscribers

                    if (OnSetChargeDetailRecord != null)
                    {
                        var results = await Task.WhenAll(OnSetChargeDetailRecord.GetInvocationList().
                                                         Cast <OnSetChargeDetailRecordDelegate>().
                                                         Select(e => e(DateTime.UtcNow,
                                                                       this,
                                                                       _SetChargeDetailRecordRequest))).
                                      ConfigureAwait(false);

                        Response = results.FirstOrDefault();
                    }

                    //if (Response == null)
                    //    Response = Response<EMP.SetChargeDetailRecordRequest>.SystemError(
                    //                         _SetChargeDetailRecordRequest,
                    //                         "Could not process the incoming SetChargeDetailRecord request!",
                    //                         null,
                    //                         _SetChargeDetailRecordRequest.SessionId,
                    //                         _SetChargeDetailRecordRequest.PartnerSessionId
                    //                     );

                    #endregion

                    #region Send OnSetChargeDetailRecordResponse event

                    var EndTime = DateTime.UtcNow;

                    try
                    {
                        if (OnSetChargeDetailRecordResponse != null)
                        {
                            await Task.WhenAll(OnSetChargeDetailRecordResponse.GetInvocationList().
                                               Cast <OnSetChargeDetailRecordResponseDelegate>().
                                               Select(e => e(EndTime,
                                                             this,
                                                             ServiceName,
                                                             _SetChargeDetailRecordRequest.EventTrackingId,

                                                             _SetChargeDetailRecordRequest.PartnerId,
                                                             _SetChargeDetailRecordRequest.OperatorId,
                                                             _SetChargeDetailRecordRequest.ChargeDetailRecord,
                                                             _SetChargeDetailRecordRequest.TransactionId,

                                                             _SetChargeDetailRecordRequest.RequestTimeout ?? DefaultRequestTimeout,
                                                             Response,
                                                             EndTime - StartTime))).
                            ConfigureAwait(false);
                        }
                    }
                    catch (Exception e)
                    {
                        DebugX.LogException(e, nameof(EMPServer) + "." + nameof(OnSetChargeDetailRecordResponse));
                    }

                    #endregion
                }

                //else
                //    Response = Response<EMP.SetChargeDetailRecordRequest>.DataError(
                //                          _SetChargeDetailRecordRequest,
                //                          "Could not process the incoming SetChargeDetailRecord request!"
                //                      );


                #region Create SOAPResponse

                var HTTPResponse = new HTTPResponse.Builder(HTTPRequest)
                {
                    HTTPStatusCode = HTTPStatusCode.OK,
                    Server         = SOAPServer.HTTPServer.DefaultServerName,
                    Date           = DateTime.UtcNow,
                    ContentType    = HTTPContentType.XMLTEXT_UTF8,
                    Content        = SOAP.Encapsulation(Response.ToXML(CustomSetChargeDetailRecordResponseSerializer)).ToUTF8Bytes(),
                    Connection     = "close"
                };

                #endregion

                #region Send OnSetChargeDetailRecordSOAPResponse event

                try
                {
                    if (OnSetChargeDetailRecordSOAPResponse != null)
                    {
                        await Task.WhenAll(OnSetChargeDetailRecordSOAPResponse.GetInvocationList().
                                           Cast <AccessLogHandler>().
                                           Select(e => e(HTTPResponse.Timestamp,
                                                         SOAPServer.HTTPServer,
                                                         HTTPRequest,
                                                         HTTPResponse))).
                        ConfigureAwait(false);
                    }
                }
                catch (Exception e)
                {
                    DebugX.LogException(e, nameof(EMPServer) + "." + nameof(OnSetChargeDetailRecordSOAPResponse));
                }

                #endregion

                return(HTTPResponse);
            });

            #endregion
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Register all URL templates for this SOAP API.
        /// </summary>
        protected void RegisterURLTemplates()
        {
            #region /Authorization - AuthorizeStart

            SOAPServer.RegisterSOAPDelegate(HTTPHostname.Any,
                                            URLPrefix + AuthorizationURL,
                                            "AuthorizeStart",
                                            XML => XML.Descendants(OICPNS.Authorization + "eRoamingAuthorizeStart").FirstOrDefault(),
                                            async(HTTPRequest, AuthorizeStartXML) => {
                CPO.AuthorizeStartRequest AuthorizeStartRequest = null;
                CPO.AuthorizationStart AuthorizationStart       = null;


                #region Send OnAuthorizeStartSOAPRequest event

                var StartTime = DateTime.UtcNow;

                try
                {
                    if (OnAuthorizeStartSOAPRequest != null)
                    {
                        await Task.WhenAll(OnAuthorizeStartSOAPRequest.GetInvocationList().
                                           Cast <RequestLogHandler>().
                                           Select(e => e(StartTime,
                                                         SOAPServer.HTTPServer,
                                                         HTTPRequest))).
                        ConfigureAwait(false);
                    }
                }
                catch (Exception e)
                {
                    e.Log(nameof(EMPSOAPServer) + "." + nameof(OnAuthorizeStartSOAPRequest));
                }

                #endregion

                try
                {
                    if (CPO.AuthorizeStartRequest.TryParse(AuthorizeStartXML,
                                                           out AuthorizeStartRequest,
                                                           CustomAuthorizeStartRequestParser,
                                                           CustomIdentificationParser,
                                                           CustomRFIDIdentificationParser,
                                                           OnException,

                                                           HTTPRequest.Timestamp,
                                                           HTTPRequest.CancellationToken,
                                                           HTTPRequest.EventTrackingId,
                                                           HTTPRequest.Timeout ?? DefaultRequestTimeout))
                    {
                        #region Send OnAuthorizeStartRequest event

                        try
                        {
                            if (OnAuthorizeStartRequest != null)
                            {
                                await Task.WhenAll(OnAuthorizeStartRequest.GetInvocationList().
                                                   Cast <OnAuthorizeStartRequestDelegate>().
                                                   Select(e => e(StartTime,
                                                                 AuthorizeStartRequest.Timestamp.Value,
                                                                 this,
                                                                 ServiceName,
                                                                 AuthorizeStartRequest.EventTrackingId,
                                                                 AuthorizeStartRequest.OperatorId,
                                                                 AuthorizeStartRequest.Identification,
                                                                 AuthorizeStartRequest.EVSEId,
                                                                 AuthorizeStartRequest.SessionId,
                                                                 AuthorizeStartRequest.PartnerProductId,
                                                                 AuthorizeStartRequest.CPOPartnerSessionId,
                                                                 AuthorizeStartRequest.EMPPartnerSessionId,
                                                                 AuthorizeStartRequest.RequestTimeout ?? DefaultRequestTimeout))).
                                ConfigureAwait(false);
                            }
                        }
                        catch (Exception e)
                        {
                            e.Log(nameof(EMPSOAPServer) + "." + nameof(OnAuthorizeStartRequest));
                        }

                        #endregion

                        #region Call async subscribers

                        if (OnAuthorizeStart != null)
                        {
                            var results = await Task.WhenAll(OnAuthorizeStart.GetInvocationList().
                                                             Cast <OnAuthorizeStartDelegate>().
                                                             Select(e => e(DateTime.UtcNow,
                                                                           this,
                                                                           AuthorizeStartRequest))).
                                          ConfigureAwait(false);

                            AuthorizationStart = results.FirstOrDefault();
                        }

                        if (AuthorizationStart == null)
                        {
                            AuthorizationStart = CPO.AuthorizationStart.SystemError(
                                AuthorizeStartRequest,
                                "Could not process the incoming AuthorizationStart request!",
                                null,
                                AuthorizeStartRequest.SessionId,
                                AuthorizeStartRequest.CPOPartnerSessionId,
                                AuthorizeStartRequest.EMPPartnerSessionId
                                );
                        }

                        #endregion

                        #region Send OnAuthorizeStartResponse event

                        var EndTime = DateTime.UtcNow;

                        try
                        {
                            if (OnAuthorizeStartResponse != null)
                            {
                                await Task.WhenAll(OnAuthorizeStartResponse.GetInvocationList().
                                                   Cast <OnAuthorizeStartResponseDelegate>().
                                                   Select(e => e(EndTime,
                                                                 this,
                                                                 ServiceName,
                                                                 AuthorizeStartRequest.EventTrackingId,
                                                                 AuthorizeStartRequest.OperatorId,
                                                                 AuthorizeStartRequest.Identification,
                                                                 AuthorizeStartRequest.EVSEId,
                                                                 AuthorizeStartRequest.SessionId,
                                                                 AuthorizeStartRequest.PartnerProductId,
                                                                 AuthorizeStartRequest.CPOPartnerSessionId,
                                                                 AuthorizeStartRequest.EMPPartnerSessionId,
                                                                 AuthorizeStartRequest.RequestTimeout ?? DefaultRequestTimeout,
                                                                 AuthorizationStart,
                                                                 EndTime - StartTime))).
                                ConfigureAwait(false);
                            }
                        }
                        catch (Exception e)
                        {
                            e.Log(nameof(EMPSOAPServer) + "." + nameof(OnAuthorizeStartResponse));
                        }

                        #endregion
                    }

                    else
                    {
                        AuthorizationStart = CPO.AuthorizationStart.DataError(
                            AuthorizeStartRequest,
                            "Could not process the incoming AuthorizeStart request!"
                            );
                    }
                }
                catch (Exception e)
                {
                    AuthorizationStart = CPO.AuthorizationStart.DataError(
                        AuthorizeStartRequest,
                        e.Message,
                        e.StackTrace
                        );
                }


                #region Create SOAP response

                var HTTPResponse = new HTTPResponse.Builder(HTTPRequest)
                {
                    HTTPStatusCode = HTTPStatusCode.OK,
                    Server         = SOAPServer.HTTPServer.DefaultServerName,
                    Date           = DateTime.UtcNow,
                    ContentType    = HTTPContentType.XMLTEXT_UTF8,
                    Content        = SOAP.Encapsulation(AuthorizationStart.ToXML()).ToUTF8Bytes(),
                    Connection     = "close"
                };

                #endregion

                #region Send OnAuthorizeStartSOAPResponse event

                try
                {
                    if (OnAuthorizeStartSOAPResponse != null)
                    {
                        await Task.WhenAll(OnAuthorizeStartSOAPResponse.GetInvocationList().
                                           Cast <AccessLogHandler>().
                                           Select(e => e(HTTPResponse.Timestamp,
                                                         SOAPServer.HTTPServer,
                                                         HTTPRequest,
                                                         HTTPResponse))).
                        ConfigureAwait(false);
                    }
                }
                catch (Exception e)
                {
                    e.Log(nameof(EMPSOAPServer) + "." + nameof(OnAuthorizeStartSOAPResponse));
                }

                #endregion

                return(HTTPResponse);
            });

            #endregion

            #region /Authorization - AuthorizeStop

            SOAPServer.RegisterSOAPDelegate(HTTPHostname.Any,
                                            URLPrefix + AuthorizationURL,
                                            "AuthorizeStop",
                                            XML => XML.Descendants(OICPNS.Authorization + "eRoamingAuthorizeStop").FirstOrDefault(),
                                            async(HTTPRequest, AuthorizeStopXML) => {
                CPO.AuthorizeStopRequest AuthorizeStopRequest = null;
                CPO.AuthorizationStop AuthorizationStop       = null;

                #region Send OnAuthorizeStopSOAPRequest event

                var StartTime = DateTime.UtcNow;

                try
                {
                    if (OnAuthorizeStopSOAPRequest != null)
                    {
                        await Task.WhenAll(OnAuthorizeStopSOAPRequest.GetInvocationList().
                                           Cast <RequestLogHandler>().
                                           Select(e => e(StartTime,
                                                         SOAPServer.HTTPServer,
                                                         HTTPRequest))).
                        ConfigureAwait(false);
                    }
                }
                catch (Exception e)
                {
                    e.Log(nameof(EMPSOAPServer) + "." + nameof(OnAuthorizeStartSOAPRequest));
                }

                #endregion


                try
                {
                    if (CPO.AuthorizeStopRequest.TryParse(AuthorizeStopXML,
                                                          out AuthorizeStopRequest,
                                                          CustomAuthorizeStopRequestParser,
                                                          CustomIdentificationParser,
                                                          CustomRFIDIdentificationParser,
                                                          OnException,

                                                          HTTPRequest.Timestamp,
                                                          HTTPRequest.CancellationToken,
                                                          HTTPRequest.EventTrackingId,
                                                          HTTPRequest.Timeout ?? DefaultRequestTimeout))
                    {
                        #region Send OnAuthorizeStopRequest event

                        try
                        {
                            if (OnAuthorizeStopRequest != null)
                            {
                                await Task.WhenAll(OnAuthorizeStopRequest.GetInvocationList().
                                                   Cast <OnAuthorizeStopRequestHandler>().
                                                   Select(e => e(StartTime,
                                                                 AuthorizeStopRequest.Timestamp.Value,
                                                                 this,
                                                                 ServiceName,
                                                                 AuthorizeStopRequest.EventTrackingId,
                                                                 AuthorizeStopRequest.SessionId,
                                                                 AuthorizeStopRequest.CPOPartnerSessionId,
                                                                 AuthorizeStopRequest.EMPPartnerSessionId,
                                                                 AuthorizeStopRequest.OperatorId,
                                                                 AuthorizeStopRequest.EVSEId,
                                                                 AuthorizeStopRequest.Identification,
                                                                 AuthorizeStopRequest.RequestTimeout ?? DefaultRequestTimeout))).
                                ConfigureAwait(false);
                            }
                        }
                        catch (Exception e)
                        {
                            e.Log(nameof(EMPSOAPServer) + "." + nameof(OnAuthorizeStopRequest));
                        }

                        #endregion

                        #region Call async subscribers

                        if (OnAuthorizeStop != null)
                        {
                            var results = await Task.WhenAll(OnAuthorizeStop.GetInvocationList().
                                                             Cast <OnAuthorizeStopDelegate>().
                                                             Select(e => e(DateTime.UtcNow,
                                                                           this,
                                                                           AuthorizeStopRequest))).
                                          ConfigureAwait(false);

                            AuthorizationStop = results.FirstOrDefault();
                        }

                        if (AuthorizationStop == null)
                        {
                            AuthorizationStop = CPO.AuthorizationStop.SystemError(
                                null,
                                "Could not process the incoming AuthorizeStop request!",
                                null,
                                AuthorizeStopRequest.SessionId,
                                AuthorizeStopRequest.CPOPartnerSessionId,
                                AuthorizeStopRequest.EMPPartnerSessionId
                                );
                        }

                        #endregion

                        #region Send OnAuthorizeStopResponse event

                        var EndTime = DateTime.UtcNow;

                        try
                        {
                            if (OnAuthorizeStopResponse != null)
                            {
                                await Task.WhenAll(OnAuthorizeStopResponse.GetInvocationList().
                                                   Cast <OnAuthorizeStopResponseHandler>().
                                                   Select(e => e(EndTime,
                                                                 this,
                                                                 ServiceName,
                                                                 AuthorizeStopRequest.EventTrackingId,
                                                                 AuthorizeStopRequest.SessionId,
                                                                 AuthorizeStopRequest.CPOPartnerSessionId,
                                                                 AuthorizeStopRequest.EMPPartnerSessionId,
                                                                 AuthorizeStopRequest.OperatorId,
                                                                 AuthorizeStopRequest.EVSEId,
                                                                 AuthorizeStopRequest.Identification,
                                                                 AuthorizeStopRequest.RequestTimeout ?? DefaultRequestTimeout,
                                                                 AuthorizationStop,
                                                                 EndTime - StartTime))).
                                ConfigureAwait(false);
                            }
                        }
                        catch (Exception e)
                        {
                            e.Log(nameof(EMPSOAPServer) + "." + nameof(OnAuthorizeStopResponse));
                        }

                        #endregion
                    }

                    else
                    {
                        AuthorizationStop = CPO.AuthorizationStop.DataError(
                            AuthorizeStopRequest,
                            "Could not process the incoming AuthorizeStop request!"
                            );
                    }
                }
                catch (Exception e)
                {
                    AuthorizationStop = CPO.AuthorizationStop.DataError(
                        AuthorizeStopRequest,
                        e.Message,
                        e.StackTrace
                        );
                }


                #region Create SOAP response

                var HTTPResponse = new HTTPResponse.Builder(HTTPRequest)
                {
                    HTTPStatusCode = HTTPStatusCode.OK,
                    Server         = SOAPServer.HTTPServer.DefaultServerName,
                    Date           = DateTime.UtcNow,
                    ContentType    = HTTPContentType.XMLTEXT_UTF8,
                    Content        = SOAP.Encapsulation(AuthorizationStop.ToXML()).ToUTF8Bytes(),
                    Connection     = "close"
                };

                #endregion

                #region Send OnAuthorizeStopSOAPResponse event

                try
                {
                    if (OnAuthorizeStopSOAPResponse != null)
                    {
                        await Task.WhenAll(OnAuthorizeStopSOAPResponse.GetInvocationList().
                                           Cast <AccessLogHandler>().
                                           Select(e => e(HTTPResponse.Timestamp,
                                                         SOAPServer.HTTPServer,
                                                         HTTPRequest,
                                                         HTTPResponse))).
                        ConfigureAwait(false);
                    }
                }
                catch (Exception e)
                {
                    e.Log(nameof(EMPSOAPServer) + "." + nameof(OnAuthorizeStopSOAPResponse));
                }

                #endregion

                return(HTTPResponse);
            });

            #endregion

            #region /Authorization - ChargeDetailRecord

            // curl -v -X POST --data "@../Testdata-UID-01.xml" -H "Content-Type: text/xml" -H "Accept: text/xml" http://127.0.0.1:3114/RNs/PROD/Authorization
            SOAPServer.RegisterSOAPDelegate(HTTPHostname.Any,
                                            URLPrefix + AuthorizationURL,
                                            "ChargeDetailRecord",
                                            XML => XML.Descendants(OICPNS.Authorization + "eRoamingChargeDetailRecord").FirstOrDefault(),
                                            async(HTTPRequest, ChargeDetailRecordXML) => {
                CPO.SendChargeDetailRecordRequest SendChargeDetailRecordRequest     = null;
                Acknowledgement <CPO.SendChargeDetailRecordRequest> Acknowledgement = null;

                #region Send OnChargeDetailRecordSOAPRequest event

                var StartTime = DateTime.UtcNow;

                try
                {
                    if (OnChargeDetailRecordSOAPRequest != null)
                    {
                        await Task.WhenAll(OnChargeDetailRecordSOAPRequest.GetInvocationList().
                                           Cast <RequestLogHandler>().
                                           Select(e => e(StartTime,
                                                         SOAPServer.HTTPServer,
                                                         HTTPRequest))).
                        ConfigureAwait(false);
                    }
                }
                catch (Exception e)
                {
                    e.Log(nameof(EMPSOAPServer) + "." + nameof(OnChargeDetailRecordSOAPRequest));
                }

                #endregion

                try
                {
                    if (CPO.SendChargeDetailRecordRequest.TryParse(ChargeDetailRecordXML,
                                                                   out SendChargeDetailRecordRequest,
                                                                   CustomChargeDetailRecordParser,
                                                                   CustomIdentificationParser,
                                                                   CustomRFIDIdentificationParser,
                                                                   OnException,

                                                                   HTTPRequest.Timestamp,
                                                                   HTTPRequest.CancellationToken,
                                                                   HTTPRequest.EventTrackingId,
                                                                   HTTPRequest.Timeout ?? DefaultRequestTimeout))
                    {
                        #region Send OnChargeDetailRecordRequest event

                        try
                        {
                            if (OnChargeDetailRecordRequest != null)
                            {
                                await Task.WhenAll(OnChargeDetailRecordRequest.GetInvocationList().
                                                   Cast <OnChargeDetailRecordRequestHandler>().
                                                   Select(e => e(StartTime,
                                                                 SendChargeDetailRecordRequest.Timestamp.Value,
                                                                 this,
                                                                 ServiceName,
                                                                 SendChargeDetailRecordRequest.EventTrackingId,
                                                                 SendChargeDetailRecordRequest.ChargeDetailRecord,
                                                                 SendChargeDetailRecordRequest.RequestTimeout ?? DefaultRequestTimeout))).
                                ConfigureAwait(false);
                            }
                        }
                        catch (Exception e)
                        {
                            e.Log(nameof(EMPSOAPServer) + "." + nameof(OnChargeDetailRecordRequest));
                        }

                        #endregion

                        #region Call async subscribers

                        if (OnChargeDetailRecord != null)
                        {
                            var results = await Task.WhenAll(OnChargeDetailRecord.GetInvocationList().
                                                             Cast <OnChargeDetailRecordDelegate>().
                                                             Select(e => e(DateTime.UtcNow,
                                                                           this,
                                                                           SendChargeDetailRecordRequest))).
                                          ConfigureAwait(false);

                            Acknowledgement = results.FirstOrDefault();
                        }

                        if (Acknowledgement == null)
                        {
                            Acknowledgement = Acknowledgement <CPO.SendChargeDetailRecordRequest> .SystemError(
                                null,
                                "Could not process the incoming SendChargeDetailRecordRequest request!",
                                null
                                );
                        }

                        #endregion

                        #region Send OnChargeDetailRecordResponse event

                        var EndTime = DateTime.UtcNow;

                        try
                        {
                            if (OnChargeDetailRecordResponse != null)
                            {
                                await Task.WhenAll(OnChargeDetailRecordResponse.GetInvocationList().
                                                   Cast <OnChargeDetailRecordResponseHandler>().
                                                   Select(e => e(EndTime,
                                                                 this,
                                                                 ServiceName,
                                                                 SendChargeDetailRecordRequest.EventTrackingId,
                                                                 SendChargeDetailRecordRequest.ChargeDetailRecord,
                                                                 SendChargeDetailRecordRequest.RequestTimeout ?? DefaultRequestTimeout,
                                                                 Acknowledgement,
                                                                 EndTime - StartTime))).
                                ConfigureAwait(false);
                            }
                        }
                        catch (Exception e)
                        {
                            e.Log(nameof(EMPSOAPServer) + "." + nameof(OnChargeDetailRecordResponse));
                        }

                        #endregion
                    }

                    else
                    {
                        Acknowledgement = Acknowledgement <CPO.SendChargeDetailRecordRequest> .DataError(
                            SendChargeDetailRecordRequest,
                            "Could not process the incoming SendChargeDetailRecord request!"
                            );
                    }
                }
                catch (Exception e)
                {
                    Acknowledgement = Acknowledgement <CPO.SendChargeDetailRecordRequest> .DataError(
                        SendChargeDetailRecordRequest,
                        e.Message,
                        e.StackTrace
                        );
                }


                #region Create SOAP response

                var HTTPResponse = new HTTPResponse.Builder(HTTPRequest)
                {
                    HTTPStatusCode = HTTPStatusCode.OK,
                    Server         = SOAPServer.HTTPServer.DefaultServerName,
                    Date           = DateTime.UtcNow,
                    ContentType    = HTTPContentType.XMLTEXT_UTF8,
                    Content        = SOAP.Encapsulation(Acknowledgement.ToXML()).ToUTF8Bytes(),
                    Connection     = "close"
                };

                #endregion

                #region Send OnChargeDetailRecordSOAPResponse event

                try
                {
                    if (OnChargeDetailRecordSOAPResponse != null)
                    {
                        await Task.WhenAll(OnChargeDetailRecordSOAPResponse.GetInvocationList().
                                           Cast <AccessLogHandler>().
                                           Select(e => e(HTTPResponse.Timestamp,
                                                         SOAPServer.HTTPServer,
                                                         HTTPRequest,
                                                         HTTPResponse))).
                        ConfigureAwait(false);
                    }
                }
                catch (Exception e)
                {
                    e.Log(nameof(EMPSOAPServer) + "." + nameof(OnChargeDetailRecordSOAPResponse));
                }

                #endregion

                return(HTTPResponse);
            });

            #endregion
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Register all URL templates for this SOAP API.
        /// </summary>
        protected void RegisterURLTemplates()
        {
            #region / - ReserveNow

            SOAPServer.RegisterSOAPDelegate(HTTPHostname.Any,
                                            URLPrefix,
                                            "ReserveNow",
                                            XML => XML.Descendants(OCPPNS.OCPPv1_6_CP + "reserveNowRequest").FirstOrDefault(),
                                            async(Request, HeaderXML, ReserveNowXML) => {
                #region Send OnReserveNowSOAPRequest event

                try
                {
                    OnReserveNowSOAPRequest?.Invoke(DateTime.UtcNow,
                                                    this.SOAPServer.HTTPServer,
                                                    Request);
                }
                catch (Exception e)
                {
                    e.Log(nameof(ChargePointSOAPServer) + "." + nameof(OnReserveNowSOAPRequest));
                }

                #endregion


                var _OCPPHeader        = SOAPHeader.Parse(HeaderXML);
                var _ReserveNowRequest = ReserveNowRequest.Parse(ReserveNowXML,
                                                                 Request_Id.Parse(_OCPPHeader.MessageId),
                                                                 _OCPPHeader.ChargeBoxIdentity);

                ReserveNowResponse response = null;



                #region Call async subscribers

                if (response == null)
                {
                    var results = OnReserveNowRequest?.
                                  GetInvocationList()?.
                                  SafeSelect(subscriber => (subscriber as OnReserveNowDelegate)
                                                 (DateTime.UtcNow,
                                                 this,
                                                 Request.CancellationToken,
                                                 Request.EventTrackingId,
                                                 _OCPPHeader.ChargeBoxIdentity,
                                                 _ReserveNowRequest.ConnectorId,
                                                 _ReserveNowRequest.ReservationId,
                                                 _ReserveNowRequest.ExpiryDate,
                                                 _ReserveNowRequest.IdTag,
                                                 _ReserveNowRequest.ParentIdTag,
                                                 DefaultRequestTimeout)).
                                  ToArray();

                    if (results.Length > 0)
                    {
                        await Task.WhenAll(results);

                        response = results.FirstOrDefault()?.Result;
                    }

                    if (results.Length == 0 || response == null)
                    {
                        response = ReserveNowResponse.Failed(_ReserveNowRequest);
                    }
                }

                #endregion



                #region Create SOAPResponse

                var HTTPResponse = new HTTPResponse.Builder(Request)
                {
                    HTTPStatusCode = HTTPStatusCode.OK,
                    Server         = SOAPServer.HTTPServer.DefaultServerName,
                    Date           = DateTime.UtcNow,
                    ContentType    = HTTPContentType.XMLTEXT_UTF8,
                    Content        = SOAP.Encapsulation(_OCPPHeader.ChargeBoxIdentity,
                                                        "/ReserveNowResponse",
                                                        null,
                                                        _OCPPHeader.MessageId,   // MessageId from the request as RelatesTo Id
                                                        _OCPPHeader.To,          // Fake it!
                                                        _OCPPHeader.From,        // Fake it!
                                                        response.ToXML()).ToUTF8Bytes()
                };

                #endregion


                #region Send OnReserveNowSOAPResponse event

                try
                {
                    OnReserveNowSOAPResponse?.Invoke(HTTPResponse.Timestamp,
                                                     this.SOAPServer.HTTPServer,
                                                     Request,
                                                     HTTPResponse);
                }
                catch (Exception e)
                {
                    e.Log(nameof(ChargePointSOAPServer) + "." + nameof(OnReserveNowSOAPResponse));
                }

                #endregion

                return(HTTPResponse);
            });

            #endregion

            #region / - CancelReservation

            SOAPServer.RegisterSOAPDelegate(HTTPHostname.Any,
                                            URLPrefix,
                                            "CancelReservation",
                                            XML => XML.Descendants(OCPPNS.OCPPv1_6_CP + "cancelReservationRequest").FirstOrDefault(),
                                            async(Request, HeaderXML, CancelReservationXML) => {
                #region Send OnCancelReservationSOAPRequest event

                try
                {
                    OnCancelReservationSOAPRequest?.Invoke(DateTime.UtcNow,
                                                           this.SOAPServer.HTTPServer,
                                                           Request);
                }
                catch (Exception e)
                {
                    e.Log(nameof(ChargePointSOAPServer) + "." + nameof(OnCancelReservationSOAPRequest));
                }

                #endregion


                var _OCPPHeader = SOAPHeader.Parse(HeaderXML);
                var _CancelReservationRequest = CancelReservationRequest.Parse(CancelReservationXML,
                                                                               Request_Id.Parse(_OCPPHeader.MessageId),
                                                                               _OCPPHeader.ChargeBoxIdentity);

                CancelReservationResponse response = null;



                #region Call async subscribers

                if (response == null)
                {
                    var results = OnCancelReservationRequest?.
                                  GetInvocationList()?.
                                  SafeSelect(subscriber => (subscriber as OnCancelReservationDelegate)
                                                 (DateTime.UtcNow,
                                                 this,
                                                 Request.CancellationToken,
                                                 Request.EventTrackingId,
                                                 _OCPPHeader.ChargeBoxIdentity,
                                                 _CancelReservationRequest.ReservationId,
                                                 DefaultRequestTimeout)).
                                  ToArray();

                    if (results.Length > 0)
                    {
                        await Task.WhenAll(results);

                        response = results.FirstOrDefault()?.Result;
                    }

                    if (results.Length == 0 || response == null)
                    {
                        response = CancelReservationResponse.Failed(_CancelReservationRequest);
                    }
                }

                #endregion



                #region Create SOAPResponse

                var HTTPResponse = new HTTPResponse.Builder(Request)
                {
                    HTTPStatusCode = HTTPStatusCode.OK,
                    Server         = SOAPServer.HTTPServer.DefaultServerName,
                    Date           = DateTime.UtcNow,
                    ContentType    = HTTPContentType.XMLTEXT_UTF8,
                    Content        = SOAP.Encapsulation(_OCPPHeader.ChargeBoxIdentity,
                                                        "/CancelReservationResponse",
                                                        null,
                                                        _OCPPHeader.MessageId,   // MessageId from the request as RelatesTo Id
                                                        _OCPPHeader.To,          // Fake it!
                                                        _OCPPHeader.From,        // Fake it!
                                                        response.ToXML()).ToUTF8Bytes()
                };

                #endregion


                #region Send OnCancelReservationSOAPResponse event

                try
                {
                    OnCancelReservationSOAPResponse?.Invoke(HTTPResponse.Timestamp,
                                                            this.SOAPServer.HTTPServer,
                                                            Request,
                                                            HTTPResponse);
                }
                catch (Exception e)
                {
                    e.Log(nameof(ChargePointSOAPServer) + "." + nameof(OnCancelReservationSOAPResponse));
                }

                #endregion

                return(HTTPResponse);
            });

            #endregion

            #region / - RemoteStartTransaction

            SOAPServer.RegisterSOAPDelegate(HTTPHostname.Any,
                                            URLPrefix,
                                            "RemoteStartTransaction",
                                            XML => XML.Descendants(OCPPNS.OCPPv1_6_CP + "remoteStartTransactionRequest").FirstOrDefault(),
                                            async(Request, HeaderXML, RemoteStartTransactionXML) => {
                #region Send OnRemoteStartTransactionSOAPRequest event

                try
                {
                    OnRemoteStartTransactionSOAPRequest?.Invoke(DateTime.UtcNow,
                                                                this.SOAPServer.HTTPServer,
                                                                Request);
                }
                catch (Exception e)
                {
                    e.Log(nameof(ChargePointSOAPServer) + "." + nameof(OnRemoteStartTransactionSOAPRequest));
                }

                #endregion


                var _OCPPHeader = SOAPHeader.Parse(HeaderXML);
                var _RemoteStartTransactionRequest = RemoteStartTransactionRequest.Parse(RemoteStartTransactionXML,
                                                                                         Request_Id.Parse(_OCPPHeader.MessageId),
                                                                                         _OCPPHeader.ChargeBoxIdentity);

                RemoteStartTransactionResponse response = null;



                #region Call async subscribers

                if (response == null)
                {
                    var results = OnRemoteStartTransaction?.
                                  GetInvocationList()?.
                                  SafeSelect(subscriber => (subscriber as OnRemoteStartTransactionDelegate)
                                                 (DateTime.UtcNow,
                                                 this,
                                                 Request.CancellationToken,
                                                 Request.EventTrackingId,
                                                 //_OCPPHeader.ChargeBoxIdentity,
                                                 _RemoteStartTransactionRequest)).
                                  ToArray();

                    if (results.Length > 0)
                    {
                        await Task.WhenAll(results);

                        response = results.FirstOrDefault()?.Result;
                    }

                    if (results.Length == 0 || response == null)
                    {
                        response = RemoteStartTransactionResponse.Failed(_RemoteStartTransactionRequest);
                    }
                }

                #endregion



                #region Create SOAPResponse

                var HTTPResponse = new HTTPResponse.Builder(Request)
                {
                    HTTPStatusCode = HTTPStatusCode.OK,
                    Server         = SOAPServer.HTTPServer.DefaultServerName,
                    Date           = DateTime.UtcNow,
                    ContentType    = HTTPContentType.XMLTEXT_UTF8,
                    Content        = SOAP.Encapsulation(_OCPPHeader.ChargeBoxIdentity,
                                                        "/RemoteStartTransactionResponse",
                                                        NextMessageId(),
                                                        _OCPPHeader.MessageId,   // MessageId from the request as RelatesTo Id
                                                        _OCPPHeader.To,          // Fake it!
                                                        _OCPPHeader.From,        // Fake it!
                                                        response.ToXML()).ToUTF8Bytes()
                };

                #endregion


                #region Send OnRemoteStartTransactionSOAPResponse event

                try
                {
                    OnRemoteStartTransactionSOAPResponse?.Invoke(HTTPResponse.Timestamp,
                                                                 this.SOAPServer.HTTPServer,
                                                                 Request,
                                                                 HTTPResponse);
                }
                catch (Exception e)
                {
                    e.Log(nameof(ChargePointSOAPServer) + "." + nameof(OnRemoteStartTransactionSOAPResponse));
                }

                #endregion

                return(HTTPResponse);
            });

            #endregion

            #region / - RemoteStopTransaction

            SOAPServer.RegisterSOAPDelegate(HTTPHostname.Any,
                                            URLPrefix,
                                            "RemoteStopTransaction",
                                            XML => XML.Descendants(OCPPNS.OCPPv1_6_CP + "remoteStopTransactionRequest").FirstOrDefault(),
                                            async(Request, HeaderXML, RemoteStopTransactionXML) => {
                #region Send OnRemoteStopTransactionSOAPRequest event

                try
                {
                    OnRemoteStopTransactionSOAPRequest?.Invoke(DateTime.UtcNow,
                                                               this.SOAPServer.HTTPServer,
                                                               Request);
                }
                catch (Exception e)
                {
                    e.Log(nameof(ChargePointSOAPServer) + "." + nameof(OnRemoteStopTransactionSOAPRequest));
                }

                #endregion


                var _OCPPHeader = SOAPHeader.Parse(HeaderXML);
                var _RemoteStopTransactionRequest = RemoteStopTransactionRequest.Parse(RemoteStopTransactionXML,
                                                                                       Request_Id.Parse(_OCPPHeader.MessageId),
                                                                                       _OCPPHeader.ChargeBoxIdentity);

                RemoteStopTransactionResponse response = null;



                #region Call async subscribers

                if (response == null)
                {
                    var results = OnRemoteStopTransaction?.
                                  GetInvocationList()?.
                                  SafeSelect(subscriber => (subscriber as OnRemoteStopTransactionDelegate)
                                                 (DateTime.UtcNow,
                                                 this,
                                                 Request.CancellationToken,
                                                 Request.EventTrackingId,
                                                 //_OCPPHeader.ChargeBoxIdentity,
                                                 _RemoteStopTransactionRequest)).
                                  ToArray();

                    if (results.Length > 0)
                    {
                        await Task.WhenAll(results);

                        response = results.FirstOrDefault()?.Result;
                    }

                    if (results.Length == 0 || response == null)
                    {
                        response = RemoteStopTransactionResponse.Failed(_RemoteStopTransactionRequest);
                    }
                }

                #endregion



                #region Create SOAPResponse

                var HTTPResponse = new HTTPResponse.Builder(Request)
                {
                    HTTPStatusCode = HTTPStatusCode.OK,
                    Server         = SOAPServer.HTTPServer.DefaultServerName,
                    Date           = DateTime.UtcNow,
                    ContentType    = HTTPContentType.XMLTEXT_UTF8,
                    Content        = SOAP.Encapsulation(_OCPPHeader.ChargeBoxIdentity,
                                                        "/RemoteStopTransactionResponse",
                                                        null,
                                                        _OCPPHeader.MessageId,   // MessageId from the request as RelatesTo Id
                                                        _OCPPHeader.To,          // Fake it!
                                                        _OCPPHeader.From,        // Fake it!
                                                        response.ToXML()).ToUTF8Bytes()
                };

                #endregion


                #region Send OnRemoteStopTransactionSOAPResponse event

                try
                {
                    OnRemoteStopTransactionSOAPResponse?.Invoke(HTTPResponse.Timestamp,
                                                                this.SOAPServer.HTTPServer,
                                                                Request,
                                                                HTTPResponse);
                }
                catch (Exception e)
                {
                    e.Log(nameof(ChargePointSOAPServer) + "." + nameof(OnRemoteStopTransactionSOAPResponse));
                }

                #endregion

                return(HTTPResponse);
            });

            #endregion


            #region / - DataTransfer

            SOAPServer.RegisterSOAPDelegate(HTTPHostname.Any,
                                            URLPrefix,
                                            "DataTransfer",
                                            XML => XML.Descendants(OCPPNS.OCPPv1_6_CP + "dataTransferRequest").FirstOrDefault(),
                                            async(Request, HeaderXML, DataTransferXML) => {
                #region Send OnDataTransferSOAPRequest event

                try
                {
                    OnDataTransferSOAPRequest?.Invoke(DateTime.UtcNow,
                                                      this.SOAPServer.HTTPServer,
                                                      Request);
                }
                catch (Exception e)
                {
                    e.Log(nameof(ChargePointSOAPServer) + "." + nameof(OnDataTransferSOAPRequest));
                }

                #endregion


                var _OCPPHeader          = SOAPHeader.Parse(HeaderXML);
                var _DataTransferRequest = CS.DataTransferRequest.Parse(DataTransferXML,
                                                                        Request_Id.Parse(_OCPPHeader.MessageId),
                                                                        _OCPPHeader.ChargeBoxIdentity);

                CP.DataTransferResponse response = null;



                #region Call async subscribers

                if (response == null)
                {
                    var results = OnDataTransferRequest?.
                                  GetInvocationList()?.
                                  SafeSelect(subscriber => (subscriber as OnDataTransferDelegate)
                                                 (DateTime.UtcNow,
                                                 this,
                                                 Request.CancellationToken,
                                                 Request.EventTrackingId,
                                                 _OCPPHeader.ChargeBoxIdentity,
                                                 _DataTransferRequest.VendorId,
                                                 _DataTransferRequest.MessageId,
                                                 _DataTransferRequest.Data,
                                                 DefaultRequestTimeout)).
                                  ToArray();

                    if (results.Length > 0)
                    {
                        await Task.WhenAll(results);

                        response = results.FirstOrDefault()?.Result;
                    }

                    if (results.Length == 0 || response == null)
                    {
                        response = DataTransferResponse.Failed(_DataTransferRequest);
                    }
                }

                #endregion



                #region Create SOAPResponse

                var HTTPResponse = new HTTPResponse.Builder(Request)
                {
                    HTTPStatusCode = HTTPStatusCode.OK,
                    Server         = SOAPServer.HTTPServer.DefaultServerName,
                    Date           = DateTime.UtcNow,
                    ContentType    = HTTPContentType.XMLTEXT_UTF8,
                    Content        = SOAP.Encapsulation(_OCPPHeader.ChargeBoxIdentity,
                                                        "/DataTransferResponse",
                                                        null,
                                                        _OCPPHeader.MessageId,   // MessageId from the request as RelatesTo Id
                                                        _OCPPHeader.To,          // Fake it!
                                                        _OCPPHeader.From,        // Fake it!
                                                        response.ToXML()).ToUTF8Bytes()
                };

                #endregion


                #region Send OnDataTransferSOAPResponse event

                try
                {
                    OnDataTransferSOAPResponse?.Invoke(HTTPResponse.Timestamp,
                                                       this.SOAPServer.HTTPServer,
                                                       Request,
                                                       HTTPResponse);
                }
                catch (Exception e)
                {
                    e.Log(nameof(ChargePointSOAPServer) + "." + nameof(OnDataTransferSOAPResponse));
                }

                #endregion

                return(HTTPResponse);
            });

            #endregion
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Parse the given HTTP request and return the roaming network
        /// for the given HTTP hostname and HTTP query parameter
        /// or an HTTP error response.
        /// </summary>
        /// <param name="HTTPRequest">A HTTP request.</param>
        /// <param name="HTTPServer">A HTTP server.</param>
        /// <param name="RoamingNetwork">The roaming network.</param>
        /// <param name="HTTPResponse">A HTTP error response.</param>
        /// <returns>True, when roaming network was found; false else.</returns>
        public static Boolean ParseRoamingNetwork(this HTTPRequest HTTPRequest,
                                                  HTTPServer <RoamingNetworks, RoamingNetwork> HTTPServer,
                                                  out RoamingNetwork RoamingNetwork,
                                                  out HTTPResponse HTTPResponse)
        {
            if (HTTPServer == null)
            {
                Console.WriteLine("HTTPServer == null!");
            }

            #region Initial checks

            if (HTTPRequest == null)
            {
                throw new ArgumentNullException("HTTPRequest", "The given HTTP request must not be null!");
            }

            if (HTTPServer == null)
            {
                throw new ArgumentNullException("HTTPServer", "The given HTTP server must not be null!");
            }

            #endregion

            RoamingNetwork_Id RoamingNetworkId;
            RoamingNetwork = null;
            HTTPResponse   = null;

            if (HTTPRequest.ParsedURLParameters.Length < 1)
            {
                HTTPResponse = new HTTPResponse.Builder(HTTPRequest)
                {
                    HTTPStatusCode = HTTPStatusCode.BadRequest,
                    Server         = HTTPServer.DefaultServerName,
                    Date           = DateTime.Now,
                };

                return(false);
            }

            if (!RoamingNetwork_Id.TryParse(HTTPRequest.ParsedURLParameters[0], out RoamingNetworkId))
            {
                HTTPResponse = new HTTPResponse.Builder(HTTPRequest)
                {
                    HTTPStatusCode = HTTPStatusCode.BadRequest,
                    Server         = HTTPServer.DefaultServerName,
                    Date           = DateTime.Now,
                    ContentType    = HTTPContentType.JSON_UTF8,
                    Content        = @"{ ""description"": ""Invalid RoamingNetworkId!"" }".ToUTF8Bytes()
                };

                return(false);
            }

            RoamingNetwork = HTTPServer.
                             GetAllTenants(HTTPRequest.Host).
                             FirstOrDefault(roamingnetwork => roamingnetwork.Id == RoamingNetworkId);

            if (RoamingNetwork == null)
            {
                HTTPResponse = new HTTPResponse.Builder(HTTPRequest)
                {
                    HTTPStatusCode = HTTPStatusCode.NotFound,
                    Server         = HTTPServer.DefaultServerName,
                    Date           = DateTime.Now,
                    ContentType    = HTTPContentType.JSON_UTF8,
                    Content        = @"{ ""description"": ""Unknown RoamingNetworkId!"" }".ToUTF8Bytes()
                };

                return(false);
            }

            return(true);
        }