private SectionConfigurationModel GetSectionByCode(string sectionCodeAtPointA, string sectionCodeAtPointB) { using (var dbContext = new Kapsch.RTE.Data.DataContext()) { var item = dbContext.DotSectionConfigurations.FirstOrDefault(c => c.SectionCodePointA == sectionCodeAtPointA && c.SectionCodePointB == sectionCodeAtPointB); if (item != null) { SectionConfigurationModel model = new SectionConfigurationModel { LevenshteinMatchDistance = item.LevenshteinMatchDistance, CreatePhysicalInfringement = bool.Parse(item.CreatePhysicalInfringement.ToString()), SectionCodePointA = item.SectionCodePointA, SectionCodePointB = item.SectionCodePointB, SectionCode = item.SectionCode, SectionDescription = item.SectionDescription, SectionDistanceInMeter = item.SectionDistance }; return(model); } } return(null); }
public IHttpActionResult GetSection(string sectionCodeAtPointA, string sectionCodeAtPointB) { using (var dbContext = new Kapsch.RTE.Data.DataContext()) { var item = dbContext.DotSectionConfigurations.FirstOrDefault(c => c.SectionCodePointA == sectionCodeAtPointA && c.SectionCodePointB == sectionCodeAtPointB); if (item == null) { return(this.BadRequestEx(Error.SectionConfigurationDoesNotExist)); } var model = new SectionConfigurationModel { LevenshteinMatchDistance = item.LevenshteinMatchDistance, CreatePhysicalInfringement = item.CreatePhysicalInfringement == 1, SectionCodePointA = item.SectionCodePointA, SectionCodePointB = item.SectionCodePointB, SectionCode = item.SectionCode, SectionDescription = item.SectionDescription, SectionDistanceInMeter = item.SectionDistance }; return(Ok(model)); } }
private SectionConfigurationModel GetSectionByIP(string iPAddressPointA, string iPAddressPointB, DateTime eventsAfter) { AtPointService cpds = new AtPointService(); FilterModel filterSourceA = new FilterModel { Operation = Operation.Equals, PropertyName = "ListenerSource", Value = iPAddressPointA }; FilterModel filterEvent = new FilterModel { Operation = Operation.GreaterThan, PropertyName = "EventDateTime", Value = eventsAfter }; IList <FilterModel> filters = new List <FilterModel> { filterEvent, filterSourceA }; PaginationListModel <AtPointModel> itemsPointA = cpds.GetPaginatedList(filters, FilterJoin.And, true, "ListenerSource", 0, 1); FilterModel filterSourceB = new FilterModel { Operation = Operation.Equals, PropertyName = "ListenerSource", Value = iPAddressPointB }; filters = new List <FilterModel> { filterEvent, filterSourceB }; PaginationListModel <AtPointModel> itemsPointB = cpds.GetPaginatedList(filters, FilterJoin.And, true, "ListenerSource", 0, 1); if (itemsPointA.TotalCount > 0 && itemsPointB.TotalCount > 0) { AtPointModel sectionCodeAtPointA = itemsPointA.Models.FirstOrDefault(); AtPointModel sectionCodeAtPointB = itemsPointB.Models.FirstOrDefault(); if (sectionCodeAtPointA != null && sectionCodeAtPointB != null) { SectionConfigurationModel section = GetSectionByCode(sectionCodeAtPointA.SectionPointCode, sectionCodeAtPointB.SectionPointCode); return(section); } } return(null); }
public IHttpActionResult RegisterMobileDevice([FromBody] iTicketConfigurationModel model, DateTime eventsAfter) { try { string pointA = model.PointAIpAddress + ":" + model.PointAPort; string pointB = model.PointBIpAddress + ":" + model.PointBPort; SectionConfigurationModel sectionModel = GetSectionByIP(pointA, pointB, eventsAfter); if (sectionModel == null) { string processToStart = ConfigurationManager.AppSettings["DistanceOverTimeAdapter"].ToString(); Process process = new Process(); if (!string.IsNullOrEmpty(model.PointAIpAddress) && model.PointAPort > 0 && !string.IsNullOrEmpty(model.PointBIpAddress) && model.PointBPort > 0) { process.StartInfo = new ProcessStartInfo { FileName = processToStart, Arguments = string.Format("-listenerType=S -listenMs={0} -ipA={1}:{2} -ipB={3}:{4}", model.PollMs, model.PointAIpAddress, model.PointAPort, model.PointBIpAddress, model.PointBPort) }; } process.Start(); } DateTime timeout = DateTime.Now.AddMinutes(2); while (true) { if (DateTime.Now >= timeout) { return(this.BadRequestEx(Error.RegisterMobileDeviceTimeout)); } sectionModel = GetSectionByIP(pointA, pointB, eventsAfter); if (sectionModel != null) { break; } Thread.Sleep(1000); } return(Ok(sectionModel)); } catch (Exception ex) { return(this.BadRequestEx(Error.PopulateUnexpectedException(ex))); } }
public static SectionConfigurationModel GetMockSectionConfiguration(string sectionCodePointA, string sectionCodePointB) { SectionConfigurationModel config = new SectionConfigurationModel { SectionCode = sectionCodePointA + "_" + sectionCodePointB, CreatePhysicalInfringement = false, LevenshteinMatchDistance = 1, SectionDescription = "Mock Section", SectionDistanceInMeter = 10000, }; return(config); }
public bool SendHeartbeatToAdapter(SectionConfigurationModel model, ListenerTypeEnum listener, long heartBeatSeconds) { var request = new RestRequest("api/Configuration/DOT/Heartbeat/Adapter", Method.POST); request.AddQueryParameter("listener", listener.ToString()); request.AddQueryParameter("heartBeatSeconds", heartBeatSeconds.ToString()); request.AddJsonBody(model); var response = RestClient.Execute(request); if (response.StatusCode != HttpStatusCode.OK) { throw CreateException(response); } return(JsonConvert.DeserializeObject <bool>(response.Content)); }
public IHttpActionResult RegisterAdapter([FromBody] SectionConfigurationModel section, string listener, long heartBeatSeconds) { var key = listener + "|" + section.SectionCode; var dictionary = Startup.RegisteredAdapters().Get(key, false); if (dictionary == null) { dictionary = new Dictionary <SectionConfigurationModel, long> { { section, heartBeatSeconds } }; Startup.RegisteredAdapters().Set(key, dictionary, (heartBeatSeconds / 60) + 10); //Grace of 10 seconds return(Ok(true)); } return(Ok(false)); }
private static void ThreadLoop(object callback) { Logger.Info("Reading config settings..."); Logger.Info("Starting New Distance Over Time Adapter for Service Type {0} - DO NOT CLOSE!!", _defaults.Listener); BaseCameraListener cameraListenerA = ListenerFactory.GetListener(_defaults, ListenerFactory.PointDefinition.PointA); cameraListenerA.Connect(); BaseCameraListener cameraListenerB = ListenerFactory.GetListener(_defaults, ListenerFactory.PointDefinition.PointB); cameraListenerB.Connect(); List <AtPointModel> pointsA = new List <AtPointModel>(); List <AtPointModel> pointsB = new List <AtPointModel>(); ConfigurationDotService configurationDotService = new ConfigurationDotService(); AtPointService atPointService = new AtPointService(); OverSectionService overSectionService = new OverSectionService(); DateTime timeout = DateTime.Now.AddMinutes(1); Logger.Info("Reading config settings.This can take some time as the service connects to the listener..."); SectionConfigurationModel sectionConfigurationModel = null; AtPointModel a = null; AtPointModel b = null; int retryCounter = 0; int maxRetryCounter = 3; while (true) { if (DateTime.Now > timeout) { Logger.Error("Cannot connect to the listener or no data received. Stopping adapter service after timeout of 1 minute."); return; } try { if (cameraListenerA.DataPoints.Count > 0 && cameraListenerB.DataPoints.Count > 0) { if (a == null) { cameraListenerA.DataPoints.TryTake(out a); atPointService.Create(a); } if (b == null) { cameraListenerB.DataPoints.TryTake(out b); atPointService.Create(b); } if (a != null && b != null) { sectionConfigurationModel = configurationDotService.GetSectionConfiguration(a.SectionPointCode, b.SectionPointCode); break; } } } catch (Exception ex) { retryCounter++; Logger.Error(ex); } if (retryCounter > maxRetryCounter) { Logger.Error("After trying {0} times, the service cannot register this adapter - the REST service might be down! Stopping adapter service!", maxRetryCounter); return; } } if (!configurationDotService.RegisterAdapter(sectionConfigurationModel, _defaults.Listener, Helper.HeartbeatSeconds)) { Logger.Error("Cannot register this adapter - there is a similar adapter configured already! Stopping adapter service!"); return; } int listenerCounterTimeoutThreshold = Helper.ListenerCounterTimeout; int listenerCounterTimeout = 0; retryCounter = 0; DateTime?nextHeartBeat = null; while (true) { nextHeartBeat = SendHeartBeat(nextHeartBeat, configurationDotService, sectionConfigurationModel); //if (listenerCounterTimeout > listenerCounterTimeoutThreshold) //{ // Logger.Error("Warning!! After trying {0} times, it seems like the listener is not reading data! Check the Adapters!", listenerCounterTimeoutThreshold); // listenerCounterTimeout = 0; //} if (retryCounter > maxRetryCounter) { Logger.Error("After trying {0} times, the service cannot read / post data - the REST service might be down! Check the Service!", maxRetryCounter); retryCounter = 0; } try { while (cameraListenerA.DataPoints.Count > 0) { AtPointModel m; if (cameraListenerA.DataPoints.TryTake(out m)) { atPointService.Create(m); pointsA.Add(m); } } while (cameraListenerB.DataPoints.Count > 0) { AtPointModel m; if (cameraListenerB.DataPoints.TryTake(out m)) { atPointService.Create(m); pointsB.Add(m); } } nextHeartBeat = SendHeartBeat(nextHeartBeat, configurationDotService, sectionConfigurationModel); pointsA = pointsA.OrderBy(c => c.EventDateTime).ToList(); pointsB = pointsB.OrderBy(c => c.EventDateTime).ToList(); if (pointsA.Count == 0 || pointsB.Count == 0) { listenerCounterTimeout++; Thread.Sleep(1000); continue; } listenerCounterTimeout = 0; SectionCalculator sectionCalculator = new SectionCalculator( sectionConfigurationModel.LevenshteinMatchDistance, sectionConfigurationModel.SectionDistanceInMeter, sectionConfigurationModel.SectionDescription, sectionConfigurationModel.SectionCode, pointsA, pointsB); List <SectionCalculationResult> offences = sectionCalculator.Calculate(); foreach (SectionCalculationResult sectionCalculationResult in offences) { nextHeartBeat = SendHeartBeat(nextHeartBeat, configurationDotService, sectionConfigurationModel); OverSectionModel model = new OverSectionModel { AtPointA = sectionCalculationResult.AtPointEnd, AtPointB = sectionCalculationResult.AtPointEnd, Zone = sectionCalculationResult.Zone, Vln = sectionCalculationResult.Vln, MachineId = sectionCalculationResult.MachineId, DateFormat = sectionCalculationResult.DateFormat, SectionDescription = sectionCalculationResult.SectionDescription, SectionCode = sectionCalculationResult.SectionCode, AverageAnprAccuracy = sectionCalculationResult.AverageAnprAccuracy, AverageSpeed = sectionCalculationResult.AverageSpeed, GraceSpeed = sectionCalculationResult.GraceSpeed, SectionDistanceInMeter = sectionCalculationResult.SectionDistanceInMeter, TravelDistance = sectionCalculationResult.TravelDistance, TripDuration = sectionCalculationResult.TripDuration, IsOffence = sectionCalculationResult.IsOffence, FileName = sectionCalculationResult.FileName, FrameNumber = sectionCalculationResult.FrameNumber }; if (sectionConfigurationModel.CreatePhysicalInfringement) { if (string.IsNullOrEmpty(Helper.PhysicalInfringementPath)) { Logger.Error("WARNING!!! The Configuration is setup to create physical infringments but the application config file does not contain a valid path. Please stop this process and update the path!!"); } PhysicalInfringement.Create(model, Helper.PhysicalInfringementPath); } string paths = Path.Combine(sectionCalculationResult.AtPointStart.ImagePhysicalFileAndPath, sectionCalculationResult.AtPointStart.ImageName); model.AtPointA.Image = File.ReadAllBytes(paths); string pathe = Path.Combine(sectionCalculationResult.AtPointEnd.ImagePhysicalFileAndPath, sectionCalculationResult.AtPointEnd.ImageName); model.AtPointB.Image = File.ReadAllBytes(pathe); overSectionService.PostData(model); Logger.Info("Added Offence"); } } catch (Exception ex) { retryCounter++; Logger.Error(ex); } Thread.Sleep(500); } }
private static DateTime SendHeartBeat(DateTime?nextHeartBeat, ConfigurationDotService configurationDotService, SectionConfigurationModel sectionConfigurationModel) { if (nextHeartBeat == null || DateTime.Now > nextHeartBeat.Value) { configurationDotService.SendHeartbeatToAdapter(sectionConfigurationModel, _defaults.Listener, Helper.HeartbeatSeconds); return(DateTime.Now.AddSeconds(Helper.HeartbeatSeconds)); } return(nextHeartBeat.Value); }