public static Boolean TryParse(String Text, out WSRequestMessage RequestFrame) { RequestFrame = null; if (Text is null) { return(false); } // [ // 2, // MessageType: CALL (Client-to-Server) // "19223201", // RequestId // "BootNotification", // Action // { // "chargePointVendor": "VendorX", // "chargePointModel": "SingleSocketCharger" // } // ] try { var JSON = JArray.Parse(Text); if (JSON.Count != 4) { return(false); } if (!Byte.TryParse(JSON[0].Value <String>(), out Byte messageType)) { return(false); } var requestId = Request_Id.Parse(JSON[1].Value <String>()); var action = JSON[2].Value <String>(); var message = JSON[3] as JObject; if (message is null) { return(false); } RequestFrame = new WSRequestMessage(requestId, action, message, messageType); return(true); } catch { return(false); } }
public static Boolean TryParse(String Text, out WSResponseMessage ResponseFrame) { ResponseFrame = null; if (Text is null) { return(false); } // [ // 3, // MessageType: CALLRESULT (Server-to-Client) // "19223201", // RequestId copied from request // { // "status": "Accepted", // "currentTime": "2013-02-01T20:53:32.486Z", // "heartbeatInterval": 300 // } // ] try { var JSON = JArray.Parse(Text); if (JSON.Count != 3) { return(false); } if (!Byte.TryParse(JSON[0].Value <String>(), out Byte messageType)) { return(false); } var responseId = Request_Id.Parse(JSON[1].Value <String>()); var message = JSON[2] as JObject; if (message is null) { return(false); } ResponseFrame = new WSResponseMessage(responseId, message, messageType); return(true); } catch { return(false); } }
/// <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 }
public static Boolean TryParse(String Text, out WSErrorMessage RequestFrame) { RequestFrame = null; if (Text is null) { return(false); } // [ // 4, // "100007", // "InternalError", // "An internal error occurred and the receiver was not able to process the requested Action successfully", // {} // ] // [ // 2, // MessageType: CALL (Client-to-Server) // "19223201", // RequestId // "BootNotification", // Action // { // "chargePointVendor": "VendorX", // "chargePointModel": "SingleSocketCharger" // } // ] try { var JSON = JArray.Parse(Text); if (JSON.Count != 5) { return(false); } if (!Byte.TryParse(JSON[0].Value <String>(), out Byte messageType)) { return(false); } var requestId = Request_Id.Parse(JSON[1].Value <String>()); var error = Enum.TryParse(JSON[2].Value <String>(), out WSErrorCodes wsErrorCodes); var description = JSON[3].Value <String>(); var details = JSON[4] as JObject; if (details is null) { return(false); } RequestFrame = new WSErrorMessage(requestId, error ? WSErrorCodes.GenericError : wsErrorCodes, description, details); return(true); } catch { return(false); } }