Exemplo n.º 1
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
        }
        /// <summary>
        ///     Gets a response for a cancel reservation request by confirmation number from ESS reservation manager service.
        /// </summary>
        /// <param name="cancelReservationRequest">Cancel Reservation Request</param>
        /// <returns>CancelReservationResponse</returns>
        public CancelReservationResponse CancelReservation(CancelReservationRequest cancelReservationRequest)
        {
            using (Profiler.Step("ReservationServiceClient.CancelReservation"))
            {
                var activityId = String.Empty;
                try
                {
                    var header = ContextListBuilder.New().WithBusinessContext(ContextListAppContextSourceBusinessContext.VA)
                        .WithUserId(cancelReservationRequest.UserUniqueId.ToString())
                        .Build();
                    activityId = header.DiagnosticContext.ActivityId;

                    using (var itineraryManagerClient = new ItineraryManagerClient(header, ServiceRegistry.AddressPool.OfType<IItineraryManager>()))
                    {
                        var cancelReservationRQ = new CancelReservationRQ
                                                      {
                                                          Reservation = new CancelReservationRQReservation
                                                                            {
                                                                                CRS_confirmationNumber = cancelReservationRequest.CrsConfirmationNumber
                                                                            },
                                                          UserDetails = new CancelReservationRQUserDetails
                                                                            {
                                                                                Preferences = new CancelReservationRQUserDetailsPreferences
                                                                                                  {
                                                                                                      Language = new Language
                                                                                                                     {
                                                                                                                         Code = WebConstants.DefaultLanguage
                                                                                                                     }
                                                                                                  }
                                                                            }
                                                      };

                        var cancelReservationResponse = itineraryManagerClient.CancelReservation(cancelReservationRQ);

                        var cancelReservationResultResponse = new CancelReservationResponse
                                                                  {
                                                                      ApplicationResults = new ApplicationResultsModel(
                                                                          cancelReservationResponse.ApplicationResults.Success.IsNotNull(),
                                                                          cancelReservationResponse.ApplicationResults.Warning.IfNotNull(
                                                                              w => w.Select(
                                                                                  warning => warning.SystemSpecificResults.ShortText)),
                                                                          cancelReservationResponse.ApplicationResults.Error.IfNotNull(
                                                                              e => e.Select(error => error.SystemSpecificResults.ShortText)
                                                                          )),
                                                                      CrsConfirmationNumber = cancelReservationResponse.Reservation.CRS_confirmationNumber,
                                                                      CancellationNumber = cancelReservationResponse.Reservation.CRS_cancellationNumber
                                                                  };
                        return cancelReservationResultResponse;
                    }
                }
                catch (Exception exception)
                {
                    Logger.AppLogger.Error(
                        "CancelReservationUnhandledException",
                        exception,
                        "UserId".ToKvp(cancelReservationRequest.UserUniqueId),
                        "CrsConfirmationNumber".ToKvp(cancelReservationRequest.CrsConfirmationNumber));
                    throw HttpResponseExceptionHelper.CreateHttpResponseException(activityId, exception);
                }
            }
        }