/// <summary>
        /// Attach the eMIP+ WebAPI to the given HTTP server.
        /// </summary>
        /// <param name="HTTPServer">A HTTP server.</param>
        /// <param name="URLPathPrefix">An optional prefix for the HTTP URIs.</param>
        /// <param name="HTTPRealm">The HTTP realm, if HTTP Basic Authentication is used.</param>
        /// <param name="HTTPLogins">An enumeration of logins for an optional HTTP Basic Authentication.</param>
        ///
        /// <param name="XMLNamespaces">An optional delegate to process the XML namespaces.</param>
        /// <param name="XMLPostProcessing">An optional delegate to process the XML after its final creation.</param>
        public WebAPI(HTTPServer <RoamingNetworks, RoamingNetwork> HTTPServer,
                      HTTPPath?URLPathPrefix = null,
                      String HTTPRealm       = DefaultHTTPRealm,
                      IEnumerable <KeyValuePair <String, String> > HTTPLogins = null,

                      XMLNamespacesDelegate XMLNamespaces         = null,
                      XMLPostProcessingDelegate XMLPostProcessing = null,

                      CustomOperatorIdMapperDelegate CustomOperatorIdMapper = null,
                      CustomEVSEIdMapperDelegate CustomEVSEIdMapper         = null)

        {
            this.HTTPServer    = HTTPServer ?? throw new ArgumentNullException(nameof(HTTPServer), "The given HTTP server must not be null!");
            this.URLPathPrefix = URLPathPrefix ?? DefaultURLPathPrefix;
            this.HTTPRealm     = HTTPRealm.IsNotNullOrEmpty() ? HTTPRealm : DefaultHTTPRealm;
            this.HTTPLogins    = HTTPLogins ?? new KeyValuePair <String, String> [0];
            this.DNSClient     = HTTPServer.DNSClient;

            this.XMLNamespaces     = XMLNamespaces;
            this.XMLPostProcessing = XMLPostProcessing;

            this.CustomOperatorIdMapper = CustomOperatorIdMapper;
            this.CustomEVSEIdMapper     = CustomEVSEIdMapper;

            this._CPOAdapters = new List <WWCPCPOAdapter>();

            // Link HTTP events...
            HTTPServer.RequestLog  += (HTTPProcessor, ServerTimestamp, Request) => RequestLog.WhenAll(HTTPProcessor, ServerTimestamp, Request);
            HTTPServer.ResponseLog += (HTTPProcessor, ServerTimestamp, Request, Response) => ResponseLog.WhenAll(HTTPProcessor, ServerTimestamp, Request, Response);
            HTTPServer.ErrorLog    += (HTTPProcessor, ServerTimestamp, Request, Response, Error, LastException) => ErrorLog.WhenAll(HTTPProcessor, ServerTimestamp, Request, Response, Error, LastException);

            var LogfilePrefix = "HTTPSSEs" + Path.DirectorySeparatorChar;

            this.DebugLog = HTTPServer.AddJSONEventSource(EventIdentification:      DebugLogId,
                                                          URLTemplate:              this.URLPathPrefix + "/DebugLog",
                                                          MaxNumberOfCachedEvents:  10000,
                                                          RetryIntervall:           TimeSpan.FromSeconds(5),
                                                          EnableLogging:            true,
                                                          LogfilePrefix:            LogfilePrefix);

            RegisterURITemplates();
        }
Example #2
0
        /// <summary>
        /// Convert the given enumeration of EVSE data records to XML.
        /// </summary>
        /// <param name="EVSEDataRecords">An enumeration of EVSE data records.</param>
        /// <param name="RoamingNetwork">The WWCP roaming network.</param>
        /// <param name="XMLNamespaces">An optional delegate to process the XML namespaces.</param>
        /// <param name="EVSEDataRecord2XML">An optional delegate to process an EVSE data record XML before sending it somewhere.</param>
        /// <param name="XMLPostProcessing">An optional delegate to process the XML after its final creation.</param>
        public static XElement ToXML(this IEnumerable <EVSEDataRecord> EVSEDataRecords,
                                     RoamingNetwork RoamingNetwork,
                                     XMLNamespacesDelegate XMLNamespaces = null,
                                     //EVSEDataRecord2XMLDelegate        EVSEDataRecord2XML  = null,
                                     XMLPostProcessingDelegate XMLPostProcessing = null)
        {
            #region Initial checks

            if (EVSEDataRecords == null)
            {
                throw new ArgumentNullException(nameof(EVSEDataRecords), "The given enumeration of EVSE data records must not be null!");
            }

            var _EVSEDataRecords = EVSEDataRecords.ToArray();

            //if (EVSEDataRecord2XML == null)
            //    EVSEDataRecord2XML = (evsedatarecord, xml) => xml;

            if (XMLPostProcessing == null)
            {
                XMLPostProcessing = xml => xml;
            }

            #endregion

            return(XMLPostProcessing(
                       SOAP.Encapsulation(new XElement(OICPNS.EVSEData + "eRoamingEvseData",
                                                       new XElement(OICPNS.EVSEData + "EvseData",

                                                                    _EVSEDataRecords.Any()

                                                      ? _EVSEDataRecords.
                                                                    ToLookup(evsedatarecord => evsedatarecord.Id.OperatorId).
                                                                    Select(group => group.Any(evsedatarecord => evsedatarecord != null)

                                                                       ? new XElement(OICPNS.EVSEData + "OperatorEvseData",

                                                                                      new XElement(OICPNS.EVSEData + "OperatorID", group.Key.ToString()),

                                                                                      RoamingNetwork.GetChargingStationOperatorById(group.Key.ToWWCP().Value).Name.Any()
                                                                                 ? new XElement(OICPNS.EVSEData + "OperatorName", RoamingNetwork.GetChargingStationOperatorById(group.Key.ToWWCP().Value).Name.FirstText())
                                                                                 : null,

                                                                                      new XElement(OICPPlusNS.EVSEOperator + "DataLicenses",
                                                                                                   RoamingNetwork.GetChargingStationOperatorById(group.Key.ToWWCP().Value).DataLicenses.
                                                                                                   SafeSelect(license => new XElement(OICPPlusNS.EVSEOperator + "DataLicense",
                                                                                                                                      new XElement(OICPPlusNS.EVSEOperator + "Id", license.Id),
                                                                                                                                      new XElement(OICPPlusNS.EVSEOperator + "Description", license.Description),
                                                                                                                                      license.URIs.Any()
                                                                                                                   ? new XElement(OICPPlusNS.EVSEOperator + "DataLicenseURIs",
                                                                                                                                  license.URIs.SafeSelect(uri => new XElement(OICPPlusNS.EVSEOperator + "DataLicenseURI", uri)))
                                                                                                                   : null
                                                                                                                                      ))
                                                                                                   ),

                                                                                      // <EvseDataRecord> ... </EvseDataRecord>
                                                                                      group.Where(evsedatarecord => evsedatarecord != null).
                                                                                      //Select(evsedatarecord => EVSEDataRecord2XML(evsedatarecord, evsedatarecord.ToXML())).
                                                                                      ToArray()

                                                                                      )

                                                                       : null

                                                                           ).ToArray()

                                                        : null

                                                                    )
                                                       ),
                                          XMLNamespaces)));
        }
Example #3
0
        /// <summary>
        /// Convert the given enumeration of EVSEs into an EVSE status records XML.
        /// </summary>
        /// <param name="EVSEs">An enumeration of EVSEs.</param>
        /// <param name="RoamingNetwork">The WWCP roaming network.</param>
        /// <param name="XMLNamespaces">An optional delegate to process the XML namespaces.</param>
        /// <param name="EVSEStatusRecord2XML">An optional delegate to process an EVSE status record XML before sending it somewhere.</param>
        /// <param name="XMLPostProcessing">An optional delegate to process the XML after its final creation.</param>
        public static XElement ToXML(this IEnumerable <EVSE> EVSEs,
                                     RoamingNetwork RoamingNetwork,
                                     XMLNamespacesDelegate XMLNamespaces = null,
                                     //EVSEStatusRecord2XMLDelegate  EVSEStatusRecord2XML  = null,
                                     XMLPostProcessingDelegate XMLPostProcessing = null)
        {
            #region Initial checks

            if (EVSEs == null)
            {
                throw new ArgumentNullException(nameof(EVSEs), "The given enumeration of EVSEs must not be null!");
            }

            //if (EVSEStatusRecord2XML == null)
            //    EVSEStatusRecord2XML = (evsestatusrecord, xml) => xml;

            if (XMLPostProcessing == null)
            {
                XMLPostProcessing = xml => xml;
            }

            #endregion

            return(XMLPostProcessing(
                       SOAP.Encapsulation(new XElement(OICPNS.EVSEStatus + "eRoamingEvseStatus",
                                                       new XElement(OICPNS.EVSEStatus + "EvseStatuses",

                                                                    EVSEs.ToLookup(evse => evse.Operator,
                                                                                   evse => {
                try
                {
                    return WWCP.EVSEStatus.Snapshot(evse).AsOICPEVSEStatus();
                }
#pragma warning disable RCS1075  // Avoid empty catch clause that catches System.Exception.
#pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
                catch (Exception)
#pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
#pragma warning restore RCS1075  // Avoid empty catch clause that catches System.Exception.
                { }

                return null;
            }).

                                                                    Select(group => group.Any(evsestatusrecord => evsestatusrecord != null)

                                                            ? new XElement(OICPNS.EVSEStatus + "OperatorEvseStatus",

                                                                           new XElement(OICPNS.EVSEStatus + "OperatorID", group.Key.Id.ToString()),

                                                                           group.Key.Name.Any()
                                                                    ? new XElement(OICPNS.EVSEStatus + "OperatorName", group.Key.Name.FirstText())
                                                                    : null,

                                                                           new XElement(OICPPlusNS.EVSEOperator + "DataLicenses",
                                                                                        group.Key.DataLicenses.SafeSelect(license => new XElement(OICPPlusNS.EVSEOperator + "DataLicense",
                                                                                                                                                  new XElement(OICPPlusNS.EVSEOperator + "Id", license.Id),
                                                                                                                                                  new XElement(OICPPlusNS.EVSEOperator + "Description", license.Description),
                                                                                                                                                  license.URIs.Any()
                                                                                                                         ? new XElement(OICPPlusNS.EVSEOperator + "DataLicenseURIs",
                                                                                                                                        license.URIs.SafeSelect(uri => new XElement(OICPPlusNS.EVSEOperator + "DataLicenseURI", uri)))
                                                                                                                         : null
                                                                                                                                                  ))
                                                                                        ),

                                                                           // <EvseStatusRecord> ... </EvseStatusRecord>
                                                                           group.Where(evsestatusrecord => evsestatusrecord != null).
                                                                           //Select(evsestatusrecord => EVSEStatusRecord2XML(evsestatusrecord, evsestatusrecord.ToXML())).
                                                                           ToArray()

                                                                           )
                                                          : null

                                                                           ).ToArray()

                                                                    )
                                                       ),
                                          XMLNamespaces)));
        }