public void PostData(OverSectionModel model) { var request = new RestRequest("api/OverSection", Method.POST); request.AddJsonBody(model); var response = RestClient.Execute(request); if (response.StatusCode != HttpStatusCode.OK) { throw CreateException(response); } }
public IHttpActionResult Post([FromBody] OverSectionModel model) { try { PushSignalR(model); return(Ok(true)); } catch (Exception ex) { //return this.TMTBadRequest(TMTErrorBase.PopulateUnexpectedException(ex)); return(Ok(false)); } }
private static void PushSignalR(OverSectionModel model) { if (!model.IsOffence) { return; } string json = JsonConvert.SerializeObject(model); using (var hubConnection = new HubConnection(SignalRHubUrl)) { var hubProxy = hubConnection.CreateHubProxy("DotNotificationHub"); hubConnection.Start().Wait(); hubProxy.Invoke("SendOverSectionInfringement", model.SectionCode, json); } }
public static bool Create(OverSectionModel model, string destinationPath) { var fileName = Path.Combine(destinationPath, model.FileName); var dir = Path.GetDirectoryName(fileName); if (!Directory.Exists(dir)) { if (dir != null) { Directory.CreateDirectory(dir); } } try { using (var memoryStream = new MemoryStream()) { using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) { var meta = archive.CreateEntry("metadata.txt"); using (var entryStream = meta.Open()) using (var streamWriter = new StreamWriter(entryStream)) { streamWriter.WriteLine(model.SectionCode + "|" + model.SectionDescription.Replace("|", " ") + "|" + model.AverageSpeed + "|" + model.TravelDistance + "|" + model.Zone + "|" + model.AtPointA.EventDateTime.ToString(model.DateFormat, CultureInfo.InvariantCulture) + "|" + model.AtPointB.EventDateTime.ToString(model.DateFormat, CultureInfo.InvariantCulture) + "|" + model.Vln + "|" + model.DateFormat + "|" + model.MachineId + "|" + model.FrameNumber); } var data = archive.CreateEntry("data.txt"); using (var entryStream = data.Open()) using (var streamWriter = new StreamWriter(entryStream)) { streamWriter.WriteLine(model.AtPointA.SourceTextLine); streamWriter.WriteLine(model.AtPointB.SourceTextLine); } } using (var fileStream = new FileStream(fileName, FileMode.Create)) { memoryStream.Seek(0, SeekOrigin.Begin); memoryStream.CopyTo(fileStream); } } using (var newFile = ZipFile.Open(fileName, ZipArchiveMode.Update)) { newFile.CreateEntryFromFile(Path.Combine(model.AtPointA.ImagePhysicalFileAndPath, model.AtPointA.ImageName), model.AtPointA.ImageName, CompressionLevel.Optimal); newFile.CreateEntryFromFile(Path.Combine(model.AtPointB.ImagePhysicalFileAndPath, model.AtPointA.ImageName), model.AtPointB.ImageName, CompressionLevel.Optimal); newFile.CreateEntryFromFile(Path.Combine(model.AtPointA.PlateImagePhysicalFileAndPath, model.AtPointA.PlateImageName), model.AtPointA.PlateImageName.Replace(".jpg", "_PLATE_1.jpg"), CompressionLevel.Optimal); newFile.CreateEntryFromFile(Path.Combine(model.AtPointB.PlateImagePhysicalFileAndPath, model.AtPointA.PlateImageName), model.AtPointB.PlateImageName.Replace(".jpg", "_PLATE_2.jpg"), CompressionLevel.Optimal); } return(true); } catch { if (File.Exists(fileName)) { File.Delete(fileName); } return(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); } }