Esempio n. 1
0
        private void NotifyViaMail(RegistrationJournal regJournal)
        {
            if (mailing != null)
            {
                string message = $"<p><a href=\"https://60ghz.ctu.cz/en/station/{regJournal.RegistrationId}/3\" target=\"_blank\" rel=\"noopener\">{regJournal.RegistrationId}</a> is enterfering with stations listed below</p>";

                message += "<table><tbody>";
                message += "<tr><td> Id </td><td> name </td ><td> owned </td><td> type </td></tr> ";
                foreach (var item in regJournal.CollisionStations)
                {
                    message += $"<tr><td> {item.Id} </td><td><a href = \"https://60ghz.ctu.cz/en/station/{item.Id}/3\" target = \"_blank\" rel = \"noopener\" > {item.Name} </a></td><td> {item.Owned} </td><td> {item.Type} </td>";
                }
                message += "</tr></tbody></table> ";
                mailing.Send("collision detected", message);
            }
        }
Esempio n. 2
0
 public void AddRegistrationEntryAsync(RegistrationJournal entry)
 {
     _context.RegistrationJournal.Add(entry);
 }
Esempio n. 3
0
        private async Task DoWork(CancellationToken cancellationToken)
        {
            logger.LogInformation($"Launching CTU60G, current version: {Assembly.GetExecutingAssembly().GetName().Version}");
            SourceDataType sourceDataType = default;

            mailing = CheckEmailOptionValues();
            if (await CheckWorkerOptionValues(cancellationToken, sourceDataType))
            {
                logger.LogInformation($"Synchronization type set to: {workerOptions.Synchronization }");
                string msg = $"Run type set to: {workerOptions.Run}";
                msg += (workerOptions.Run == RunTypeEnum.InSpecifedTime.ToString()) ? $", app will run every day in {workerOptions.RunTimeSpecification.Hours}:{workerOptions.RunTimeSpecification.Minutes}":"";
                msg += (workerOptions.Run == RunTypeEnum.AfterSpecifedTime.ToString()) ? $", app will run always after {workerOptions.RunTimeSpecification}":"";
                logger.LogInformation(msg);
                do
                {
                    //Loading source data from file or url
                    logger.LogInformation("Loading data");
                    string loadedData = default;
                    try
                    {
                        loadedData = await LoadData(sourceDataType);
                    }
                    catch (Exception e)
                    {
                        logger.LogError("Could not obtain source data, exception message:\n {0}.", e.Message);
                        TaskCompletionSource.SetResult(false);
                        appLifetime.StopApplication();
                        return;
                    }

                    //parsing loaded source data
                    logger.LogInformation("parsing loaded data");
                    List <WirelessSite> parsedSites = await ParseData(cancellationToken, loadedData);

                    cancellationToken.ThrowIfCancellationRequested();
                    try
                    {
                        logger.LogInformation("LoggingIn");
                        using (CTUClient client = new CTUClient(workerOptions.CTULogin, workerOptions.CTUPass, workerOptions.SignalIsolationConsentIfMyOwn))
                        {
                            logger.LogInformation("LoggedIn");
                            OnSiteData ctuMetaData = new OnSiteData(await client.GetMyStationsAsync());

                            foreach (var site in parsedSites)
                            {
                                cancellationToken.ThrowIfCancellationRequested();
                                if ((DateTime.Now - ctuMetaData.LasTimeRefreshed).TotalSeconds > 10)
                                {
                                    ctuMetaData.Refresh(await client.GetMyStationsAsync());
                                }

                                if (site.Infos.SiteType == "ptp")
                                {
                                    List <P2PSite> pairs = (ProcessWUnitCreation(site) as List <P2PSite>);
                                    if (pairs != null)
                                    {
                                        foreach (var pair in pairs)
                                        {
                                            RegistrationJournal regJournal = default;
                                            if (pair.StationB.CTUId > 0) // update already existing site
                                            {
                                                ProcessRegistrationJournal(regJournal = await client.UpdatePTPConnectionAsync(pair), site);
                                            }
                                            else // create new site
                                            {
                                                ProcessRegistrationJournal(regJournal = await client.AddPTPSiteAsync(pair), site);
                                                if (regJournal.Phase == RegistrationJournalPhaseEnum.Published && !string.IsNullOrEmpty(workerOptions.ResponseWithCTUID))
                                                {
                                                    HttpClient          cl = new HttpClient();
                                                    HttpResponseMessage rm = await cl.GetAsync(workerOptions.ResponseWithCTUID + $"&id={pair.StationA.OwnerId}&ctuId={regJournal.RegistrationId}");

                                                    HttpResponseMessage rm2 = await cl.GetAsync(workerOptions.ResponseWithCTUID + $"&id={pair.StationB.OwnerId}&ctuId={regJournal.RegistrationId}");
                                                }
                                            }
                                        }
                                    }
                                }
                                else if (site.Infos.SiteType == "ptmp")
                                {
                                    WigigPTMPUnitInfo wigig = (ProcessWUnitCreation(site) as WigigPTMPUnitInfo);
                                    if (wigig != null)
                                    {
                                        ProcessRegistrationJournal(await client.AddWIGIG_PTP_PTMPConnectionAsync(wigig), site);
                                    }
                                }
                                else if (site.Infos.SiteType == "delete" && workerOptions.Synchronization == SynchronizationTypeEnum.Manual.ToString())
                                {
                                    ProcessDeletion(site, client);
                                }
                                else
                                {
                                    logger.LogWarning($"{site.Infos.Ssid} site type is missing or has unrecognizable value, dont know where to publish:\n" +
                                                      $"original source:\n" +
                                                      JsonConvert.SerializeObject(site, Formatting.Indented));
                                }
                            }
                        }
                    }
                    catch (InvalidMailOrPasswordException)
                    {
                        logger.LogError("Invalid login credentials");
                        TaskCompletionSource.SetResult(false);
                        appLifetime.StopApplication();
                        return;
                    }
                    catch (WebServerException e)
                    {
                        logger.LogError($"Web server exception occured during comunication with ctu\n{e.Message} {e.Status.ToString()}\n {e.StackTrace}");
                        TaskCompletionSource.SetResult(false);
                        appLifetime.StopApplication();
                        return;
                    }
                    catch (OperationCanceledException e)
                    {
                        logger.LogError($"Task was stopped as respond to system signal\n{e.Message}");
                        TaskCompletionSource.SetResult(false);
                        appLifetime.StopApplication();
                        return;
                    }
                    catch (Exception e)
                    {
                        logger.LogError($"unknown exception occured during login\n{e.Message}\n {e.StackTrace}");
                        logger.LogInformation(e.Message);
                        logger.LogWarning("Shuting service down");
                        TaskCompletionSource.SetResult(false);
                        appLifetime.StopApplication();
                        return;
                    }

                    if (workerOptions.Run == RunTypeEnum.AfterSpecifedTime.ToString())
                    {
                        Log.Information($"task goes to sleep, will resume at {DateTime.Now + workerOptions.RunTimeSpecification}");
                        await Task.Delay(workerOptions.RunTimeSpecification, cancellationToken);
                    }
                    if (workerOptions.Run == RunTypeEnum.InSpecifedTime.ToString())
                    {
                        TimeSpan timeToWait = DateTime.Today + workerOptions.RunTimeSpecification - DateTime.Now;
                        if (timeToWait < TimeSpan.Zero)
                        {
                            timeToWait += new TimeSpan(1, 0, 0, 0);
                        }
                        Log.Information($"task goes to sleep, will resume at {DateTime.Now + timeToWait}");
                        await Task.Delay(timeToWait, cancellationToken);
                    }
                }while (!cancellationToken.IsCancellationRequested && (RunTypeEnum)Enum.Parse(typeof(RunTypeEnum), workerOptions.Run) != RunTypeEnum.Once);
                logger.LogInformation("allDone");
            }



            void ProcessRegistrationJournal(RegistrationJournal regJournal, WirelessSite site)
            {
                using (LogContext.PushProperty("phase", regJournal.Phase.ToString()))
                    using (LogContext.PushProperty("type", regJournal.Type.ToString()))
                        using (LogContext.PushProperty("id", regJournal.RegistrationId))
                        {
                            switch (regJournal.Phase)
                            {
                            case RegistrationJournalPhaseEnum.InputValidation:
                            {
                                logger.LogWarning("{phase}\nid:{id}\n{type}\nwas not possible due to Invalid values\n" +
                                                  "No records were created/updated");
                            }
                            break;

                            case RegistrationJournalPhaseEnum.Localization:
                            {
                                if (regJournal.ThrownException.GetType() == typeof(WebServerException))
                                {
                                    logger.LogWarning("{phase}\nid:{id}\n{type}\nwas not possible due to unexpected web behaviour\n" +
                                                      $"No record were created");
                                }
                                else
                                {
                                    logger.LogWarning("{phase}\nid:{id}\n{type}\nwas not possible due to unexpected behaviour\n" +
                                                      $"No record were created");
                                }
                            }
                            break;

                            case RegistrationJournalPhaseEnum.TechnicalSpecification:
                            {
                                using (LogContext.PushProperty("record", "Draft"))
                                {
                                    if (regJournal.ThrownException.GetType() == typeof(WebServerException))
                                    {
                                        logger.LogWarning("{phase}\nid:{id}\n{type}\nwas not possible due to unexpected web behaviour\n" +
                                                          "Record is now in state {record}");
                                    }
                                    else
                                    {
                                        logger.LogWarning("{phase}\nid:{id}\n{type}\nwas not possible due to unexpected behaviour\n" +
                                                          "Record is now in state {record}");
                                    }
                                }
                            }
                            break;

                            case RegistrationJournalPhaseEnum.CollissionSummary:
                            {
                                using (LogContext.PushProperty("record", "WAITING"))
                                {
                                    if (regJournal.ThrownException.GetType() == typeof(WebServerException))
                                    {
                                        logger.LogWarning("{phase}\nid:{id}\n{type}\nwas not possible due to unexpected web behaviour\n" +
                                                          "Record is now in state {record}");
                                    }
                                    else if (regJournal.ThrownException.GetType() == typeof(CollisionDetectedException))
                                    {
                                        logger.LogWarning("{phase}\nid:{id}\n{type}\nwas not possible due to possible collision with another connection\n" +
                                                          "Record is now in state {record}");
                                        NotifyViaMail(regJournal);
                                    }
                                    else
                                    {
                                        logger.LogWarning("{phase}\nid:{id}\n{type}\nwas not possible due to unexpected behaviour\n" +
                                                          "Record is now in state {record}");
                                    }
                                }
                            }
                            break;

                            case RegistrationJournalPhaseEnum.Published:
                            {
                                using (LogContext.PushProperty("record", "PUBLISHED"))
                                {
                                    logger.LogInformation("{phase}\nid:{id}\n{type}\nwas successfully published\n" +
                                                          "Record is now in state {record}");
                                }
                            }
                            break;

                            default:
                                break;
                            }
                        }
            }

            async Task ZeroDatabase(List <WirelessSite> sites)
            {
                foreach (var site in sites)
                {
                    HttpClient cl = new HttpClient();
                    if (site.Ap != null)
                    {
                        foreach (var item in site?.Ap)
                        {
                            if (!string.IsNullOrEmpty(workerOptions.ResponseOnDelete))
                            {
                                HttpResponseMessage rm2 = await cl.GetAsync(workerOptions.ResponseOnDelete + $"&id={item.Id}");

                                if (await rm2.Content.ReadAsStringAsync() != "ok")
                                {
                                }
                            }
                        }
                    }
                    if (site.Stations != null)
                    {
                        foreach (var item in site?.Stations)
                        {
                            if (!string.IsNullOrEmpty(workerOptions.ResponseOnDelete))
                            {
                                HttpResponseMessage rm2 = await cl.GetAsync(workerOptions.ResponseOnDelete + $"&id={item.Id}");

                                if (await rm2.Content.ReadAsStringAsync() != "ok")
                                {
                                }
                            }
                        }
                    }
                }
            }

            object ProcessWUnitCreation(WirelessSite site)
            {
                try
                {
                    if (site.Infos.SiteType == "ptp")
                    {
                        return(WirellesUnitFactory.CreatePTP(site));
                    }
                    else if (site.Infos.SiteType == "ptmp")
                    {
                        return(WirellesUnitFactory.CreateWigigPTMP(site));
                    }
                }
                catch (MissingParameterException e)
                {
                    using (LogContext.PushProperty("ci", "missing critical information"))
                    {
                        logger.LogWarning($"{site.Infos.Ssid} will not be possible to publish, because of" + "{ci}:\n" +
                                          $"{e.Message}\n" +
                                          $"original source:\n" +
                                          JsonConvert.SerializeObject(site, Formatting.Indented));
                    }
                }
                catch (InvalidPropertyValueException e)
                {
                    using (LogContext.PushProperty("ci", "invalid critical information"))
                    {
                        logger.LogWarning($"{site.Infos.Ssid} will not be possible to publish, because of" + "{ci} :\n" +
                                          $"Expected value: {e.ExpectedVauleInfo}\n" +
                                          $"Current value: {e.CurrentValue}\n" +
                                          $"original source:\n" +
                                          JsonConvert.SerializeObject(site, Formatting.Indented));
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }

                return(null);
            }

            void ProcessDeletion(WirelessSite site, CTUClient client)
            {
                try
                {
                    if (site.Stations.Count > 0)
                    {
                        foreach (var item in site.Stations)
                        {
                            List <CtuWirelessUnit> stations = default;
                            stations = client.GetStationByIdAsync(item.CtuReported).Result;
                        }
                    }
                    else
                    {
                        List <CtuWirelessUnit> stations = default;
                        stations = client.GetStationByIdAsync(site.Ap.FirstOrDefault()?.CtuReported).Result;
                        if (stations.Count > 0)
                        {
                            client.DeleteConnectionAsync(site.Ap.FirstOrDefault()?.CtuReported);
                        }
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            TaskCompletionSource.SetResult(true);
            appLifetime.StopApplication();
            return;
        }