public static void WithCharging(ChargingData chargingData)
        {
            transaction.charging = new ChargingData();
            transaction.charging.clientBirthday    = chargingData.clientBirthday;
            transaction.charging.clientCode        = chargingData.clientCode;
            transaction.charging.clientDocument    = chargingData.clientDocument;
            transaction.charging.clientDocumentTwo = chargingData.clientDocumentTwo;
            transaction.charging.clientEmail       = chargingData.clientEmail;
            transaction.charging.clientName        = chargingData.clientName;
            transaction.charging.clientSex         = chargingData.clientSex;
            transaction.charging.clientType        = chargingData.clientType;

            if (chargingData.clientChargingAdress != null)
            {
                transaction.charging.clientChargingAdress            = new AddressData();
                transaction.charging.clientChargingAdress.city       = chargingData.clientChargingAdress.city;
                transaction.charging.clientChargingAdress.complement = chargingData.clientChargingAdress.complement;
                transaction.charging.clientChargingAdress.country    = chargingData.clientChargingAdress.country;
                transaction.charging.clientChargingAdress.district   = chargingData.clientChargingAdress.district;
                transaction.charging.clientChargingAdress.number     = chargingData.clientChargingAdress.number;
                transaction.charging.clientChargingAdress.state      = chargingData.clientChargingAdress.state;
                transaction.charging.clientChargingAdress.street     = chargingData.clientChargingAdress.street;
                transaction.charging.clientChargingAdress.zipCode    = chargingData.clientChargingAdress.zipCode;
            }

            if (chargingData.clientChargingPhone != null && chargingData.clientChargingPhone.Count > 0)
            {
                List <PhoneData> newPhones = new List <PhoneData>();

                foreach (var phone in chargingData.clientChargingPhone)
                {
                    PhoneData newPhone = new PhoneData();
                    newPhone.ddd       = phone.ddd;
                    newPhone.ddi       = phone.ddi;
                    newPhone.phone     = phone.phone;
                    newPhone.phoneType = phone.phoneType;
                    newPhones.Add(newPhone);
                }

                transaction.charging.clientChargingPhone = newPhones;
            }
        }
        public async Task <IActionResult> Index()
        {
            Logger.LogTrace("Index: Loading charge points with latest transactions...");

            OverviewViewModel overviewModel = new OverviewViewModel();

            overviewModel.ChargePoints = new List <ChargePointsOverviewViewModel>();
            try
            {
                using (OCPPCoreContext dbContext = new OCPPCoreContext(this.Config))
                {
                    var chargeData = from cp in dbContext.ChargePoints
                                     from t in cp.Transactions
                                     .OrderByDescending(t => t.TransactionId)
                                     .DefaultIfEmpty()
                                     .Take(1)
                                     select new { cp, t };

                    ChargePointStatus[] statusList = null;

                    string serverApiUrl = base.Config.GetValue <string>("ServerApiUrl");
                    string apiKeyConfig = base.Config.GetValue <string>("ApiKey");
                    if (!string.IsNullOrEmpty(serverApiUrl))
                    {
                        bool serverError = false;
                        try
                        {
                            using (var httpClient = new HttpClient())
                            {
                                if (!serverApiUrl.EndsWith('/'))
                                {
                                    serverApiUrl += "/";
                                }
                                Uri uri = new Uri(serverApiUrl);
                                uri = new Uri(uri, "Status");
                                httpClient.Timeout = new TimeSpan(0, 0, 4); // use short timeout

                                // API-Key authentication?
                                if (!string.IsNullOrWhiteSpace(apiKeyConfig))
                                {
                                    httpClient.DefaultRequestHeaders.Add("X-API-Key", apiKeyConfig);
                                }
                                else
                                {
                                    Logger.LogWarning("Index: No API-Key configured!");
                                }

                                HttpResponseMessage response = await httpClient.GetAsync(uri);

                                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                                {
                                    string jsonData = await response.Content.ReadAsStringAsync();

                                    if (!string.IsNullOrEmpty(jsonData))
                                    {
                                        statusList = JsonConvert.DeserializeObject <ChargePointStatus[]>(jsonData);
                                        overviewModel.ServerConnection = true;
                                    }
                                    else
                                    {
                                        Logger.LogError("Index: Result of status web request is empty");
                                        serverError = true;
                                    }
                                }
                                else
                                {
                                    Logger.LogError("Index: Result of status web request => httpStatus={0}", response.StatusCode);
                                    serverError = true;
                                }
                            }

                            Logger.LogInformation("Index: Result of status web request => Length={0}", statusList?.Length);
                        }
                        catch (Exception exp)
                        {
                            Logger.LogError(exp, "Index: Error in status web request => {0}", exp.Message);
                            serverError = true;
                        }

                        if (serverError)
                        {
                            ViewBag.ErrorMsg = _localizer["ErrorOCPPServer"];
                        }
                    }

                    int i = 0;
                    foreach (var cp in chargeData)
                    {
                        // count number of charge points
                        i++;

                        // Copy data in view model
                        ChargePointsOverviewViewModel cpovm = new ChargePointsOverviewViewModel();
                        cpovm.ChargePointId = cp.cp.ChargePointId;
                        cpovm.Name          = cp.cp.Name;
                        cpovm.Comment       = cp.cp.Comment;
                        if (cp.t != null)
                        {
                            cpovm.MeterStart = cp.t.MeterStart;
                            cpovm.MeterStop  = cp.t.MeterStop;
                            cpovm.StartTime  = cp.t.StartTime;
                            cpovm.StopTime   = cp.t.StopTime;

                            // default status: active transaction or not?
                            cpovm.ConnectorStatus = (cpovm.StopTime.HasValue) ? ConnectorStatus.Available : ConnectorStatus.Occupied;
                        }
                        else
                        {
                            cpovm.MeterStart = -1;
                            cpovm.MeterStop  = -1;
                            cpovm.StartTime  = null;
                            cpovm.StopTime   = null;

                            // default status: Available
                            cpovm.ConnectorStatus = ConnectorStatus.Available;
                        }
                        overviewModel.ChargePoints.Add(cpovm);

                        if (statusList != null)
                        {
                            foreach (ChargePointStatus cpStatus in statusList)
                            {
                                if (cpStatus.Id.Equals(cpovm.ChargePointId, StringComparison.InvariantCultureIgnoreCase))
                                {
                                    // ChargePoint in statuslist => online
                                    cpovm.Online = true;

                                    if (cpStatus.EVSE1Status != ConnectorStatus.Undefined)
                                    {
                                        // TODO: display multiple connectors
                                        Logger.LogTrace("Index: Charge point '{0}' => Override status '{1}' to '{2}'", cpovm.ChargePointId, cpovm.ConnectorStatus, cpStatus.EVSE1Status);
                                        cpovm.ConnectorStatus = cpStatus.EVSE1Status;
                                    }

                                    if (cpovm.ConnectorStatus == ConnectorStatus.Occupied)
                                    {
                                        ChargingData chargingData = cpStatus.ChargingDataEVSE1;
                                        if (chargingData == null)
                                        {
                                            chargingData = cpStatus.ChargingDataEVSE2;
                                        }
                                        if (chargingData != null)
                                        {
                                            string currentCharge = string.Empty;
                                            if (chargingData.ChargeRateKW != null)
                                            {
                                                currentCharge = string.Format("{0:0.0}kW", chargingData.ChargeRateKW.Value);
                                            }
                                            if (chargingData.SoC != null)
                                            {
                                                if (!string.IsNullOrWhiteSpace(currentCharge))
                                                {
                                                    currentCharge += " | ";
                                                }
                                                currentCharge += string.Format("{0:0}%", chargingData.SoC.Value);
                                            }
                                            if (!string.IsNullOrWhiteSpace(currentCharge))
                                            {
                                                cpovm.CurrentChargeData = currentCharge;
                                            }
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                    }

                    Logger.LogInformation("Index: Found {0} charge points", i);
                }
            }
            catch (Exception exp)
            {
                Logger.LogError(exp, "Index: Error loading charge points from database");
                TempData["ErrMessage"] = exp.Message;
                return(RedirectToAction("Error", new { Id = "" }));
            }

            return(View(overviewModel));
        }