Example #1
0
 private void ProcessQuadratureMessage(QuadratureStateChangeMessage e)
 {
     if (e.DirectionOfRotation == Enums.DirectionOfRotation.Stopped)
     {
         Cleanup();
         _windowService.CloseModalWindow(WindowType.UnloadingAtGin);
     }
 }
Example #2
0
 private void ProcessQuadratureEvent(QuadratureStateChangeMessage action)
 {
     if (action.DirectionOfRotation == Enums.DirectionOfRotation.RotatingCounterClockwise)
     {
         LoadStopped = true;
         ExecuteUnloadCommand();
     }
     else if (action.DirectionOfRotation == Enums.DirectionOfRotation.Stopped)
     {
         LoadStopped = true;
     }
 }
Example #3
0
        private void ProcessQuadratureMessage(QuadratureStateChangeMessage e)
        {
            Task.Run(() =>
            {
                //if stopped after unload
                var prevEvent = QuadratureEncoderDataProvider.PreviousEvent;
                if (e.DirectionOfRotation == Enums.DirectionOfRotation.Stopped && prevEvent != null && prevEvent.DirectionOfRotation == Enums.DirectionOfRotation.RotatingCounterClockwise)
                {
                    t.Stop();             //stop refresh timer

                    HandleRefreshTimer(); //run refresh to see if bad serials are still on truck

                    if (BadSerials.Count() > 0)
                    {
                        //show correction window
                        System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                        {
                            UnloadCorrectionViewModel vm = SimpleIoc.Default.GetInstance <UnloadCorrectionViewModel>();
                            vm.ActiveListID = this.ActiveListID;
                            vm.Initialize();
                            _windowService.ShowModalWindow(WindowType.UnloadCorrectionWindow, vm);
                        }));
                    }
                }
                else if (e.DirectionOfRotation == Enums.DirectionOfRotation.RotatingClockwise) //loading
                {
                    if (!t.Enabled)
                    {
                        t.Start();
                    }

                    if (_windowService.IsWindowOpen(WindowType.UnloadCorrectionWindow))
                    {
                        _windowService.CloseModalWindow(WindowType.UnloadCorrectionWindow);
                    }
                }
                else //we started unloading again so monitor for unloads
                {
                    if (_windowService.IsWindowOpen(WindowType.UnloadCorrectionWindow))
                    {
                        _windowService.CloseModalWindow(WindowType.UnloadCorrectionWindow);
                    }
                    _windowService.FocusLast(WindowType.WaitingForUnloadWindow);
                    t.Start();
                }
            });
        }
        private void ProcessQuadratureStateChange(QuadratureStateChangeMessage eventData)
        {
            int delay = 1;

            using (var dp = SimpleIoc.Default.GetInstance <IUnitOfWorkFactory>().CreateUnitOfWork())
            {
                var readDelay = dp.SettingsRepository.FindSingle(x => x.Key == TruckClientSettingKeys.RFID_READ_DELAY);

                if (readDelay != null)
                {
                    delay = int.Parse(readDelay.Value);
                }
            }

            QuadratureStateChangeMessage lastEvent = QuadratureEncoderDataProvider.LastEvent;

            System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                if (eventData.DirectionOfRotation == DirectionOfRotation.RotatingClockwise)
                {
                    QuadratureEvents.Add(new QuadratureEvent {
                        Status = "Load started", Timestamp = DateTime.Now.ToUniversalTime()
                    });
                    rfidStartTime = DateTime.Now.AddSeconds(delay).ToUniversalTime();
                }
                else if (eventData.DirectionOfRotation == DirectionOfRotation.RotatingCounterClockwise)
                {
                    QuadratureEvents.Add(new QuadratureEvent {
                        Status = "Unload started", Timestamp = DateTime.Now.ToUniversalTime()
                    });
                    rfidStartTime = DateTime.Now.AddSeconds(delay).ToUniversalTime();
                }
                else
                {
                    QuadratureEvents.Add(new QuadratureEvent {
                        Status = "Stopped", Timestamp = DateTime.Now.ToUniversalTime()
                    });
                    rfidStartTime = DateTime.Now.AddDays(100).ToUniversalTime();
                }
            }));
        }
Example #5
0
        private void ProcessQuadratureStateChange2(QuadratureStateChangeMessage eventData)
        {
            try
            {
                if (!initialized)
                {
                    return;
                }

                Logging.Logger.Log("AGG", "Processing quadrature state change.");
                lock (instance.locker)
                {
                    using (var uow = SimpleIoc.Default.GetInstance <IUnitOfWorkFactory>().CreateUnitOfWork())
                    {
                        //if quadrature is turning then start the reader
                        if (eventData.DirectionOfRotation == DirectionOfRotation.RotatingClockwise || eventData.DirectionOfRotation == DirectionOfRotation.RotatingCounterClockwise)
                        {
                            if (eventData.DirectionOfRotation == DirectionOfRotation.RotatingClockwise)
                            {
                                GPSDataProvider.SetReverse(true);
                            }

                            readStartTime = DateTime.Now.AddSeconds(delay).ToUniversalTime();

                            Logging.Logger.Log("AGG", "Ignore tags before " + readStartTime.ToString());

                            isLoading   = false;
                            isUnloading = false;

                            tagBuffer.Clear();
                            Logging.Logger.Log("AGG", "Starting reader for read cycle");
                            TagDataProvider.Start(delay);

                            if (eventData.DirectionOfRotation == DirectionOfRotation.RotatingClockwise)
                            {
                                isLoading = true;
                                GPSDataProvider.SetReverse(true);
                                Logging.Logger.Log("AGG", "Loading started");
                            }
                            else
                            {
                                isUnloading = true;
                                GPSDataProvider.SetReverse(false);
                                Logging.Logger.Log("AGG", "Unloading started");
                            }
                        }
                        else
                        {
                            if (DateTime.UtcNow < readStartTime)
                            {
                                Logging.Logger.Log("AGG", "Cancelling read cycle");
                                TagDataProvider.Stop();
                                return;
                            }

                            Logging.Logger.Log("AGG", "Processing stop");
                            GPSDataProvider.SetReverse(false);
                            var      isLoadEvent = isLoading;
                            DateTime stopTime    = DateTime.Now.ToUniversalTime();

                            isLoading   = false; //stops adding tags to the buffer of distinct items scanned
                            isUnloading = false;

                            Logging.Logger.Log("AGG", "Checking for modules still in view");

                            if (!isLoadEvent)
                            {
                                System.Threading.Thread.Sleep(100);               //changed from 200 to 100
                            }
                            DateTime correctionStart = DateTime.UtcNow;
                            if (!isLoadEvent)
                            {
                                System.Threading.Thread.Sleep(200);               //changed from 2000 to 500
                            }
                            TagDataProvider.Stop();
                            DateTime correctionEnd = DateTime.UtcNow;

                            List <string> tagsStillOnTruck = new List <string>();
                            var           tagsRead         = TagDataProvider.GetTagsFirstSeenInTimeRange(readStartTime, stopTime);

                            if (!isLoadEvent)
                            {
                                tagsStillOnTruck = TagDataProvider.GetTagsLastSeenInTimeRange(correctionStart, correctionEnd).Select(t => t.SerialNumber).Distinct().ToList();
                                StringBuilder sb = new StringBuilder();
                                foreach (var s in tagsStillOnTruck)
                                {
                                    sb.Append(s + ", ");
                                }
                                Logging.Logger.Log("AGG", "Tags still on truck (detected in view after chains stopped): " + sb.ToString().TrimEnd(','));
                            }



                            var      truck      = uow.SettingsRepository.GetCurrentTruck();
                            var      driver     = uow.SettingsRepository.GetCurrentDriver();
                            string[] snsOnTruck = SerialNumbersOnTruck.ToArray();

                            TagItem[] tagsInBuffer = null;

                            lock (tagBuffer)
                            {
                                tagsInBuffer = tagBuffer.ToArray();
                            }

                            Logging.Logger.Log("AGG", "Processing unique tags seen.");
                            int agEventCount = 0;
                            foreach (var tag in tagsInBuffer)
                            {
                                var tagRead = tagsRead.Where(r => r.SerialNumber == tag.SerialNumber).FirstOrDefault();
                                //var lastTagLastTagRead = tagsRead.Where(r => r.SerialNumber == tag.SerialNumber).LastOrDefault();

                                DateTime startTime = readStartTime;
                                DateTime endTime   = stopTime;

                                var firstTag = tagsRead.Where(r => r.SerialNumber == tag.SerialNumber).FirstOrDefault();
                                var lastTag  = tagsRead.Where(r => r.SerialNumber == tag.SerialNumber).LastOrDefault();

                                if (tagRead != null)
                                {
                                    startTime = tagRead.Firstseen;
                                }
                                if (tagRead != null)
                                {
                                    endTime = tagRead.Lastseen;
                                }

                                var firstCoords = GPSDataProvider.GetFirstCoords(startTime, endTime);
                                var lastCoords  = GPSDataProvider.GetLastCoords(startTime, endTime);

                                AggregateEvent agEvent = new AggregateEvent();

                                if (isLoadEvent)
                                {
                                    agEvent.Timestamp = startTime;
                                }
                                else
                                {
                                    agEvent.Timestamp = endTime;
                                }

                                agEvent.SerialNumber = tag.SerialNumber;
                                agEvent.Epc          = tag.Epc;
                                agEvent.AverageLat   = GPSDataProvider.GetAverageLatitude(startTime, endTime);
                                agEvent.AverageLong  = GPSDataProvider.GetAverageLongitude(startTime, endTime);
                                agEvent.MedianLat    = GPSDataProvider.GetMedianLatitude(startTime, endTime);
                                agEvent.MedianLong   = GPSDataProvider.GetMedianLongitude(startTime, endTime);
                                agEvent.FirstLat     = GPSHelper.SafeLat(firstCoords);
                                agEvent.FirstLong    = GPSHelper.SafeLong(firstCoords);
                                agEvent.LastLat      = GPSHelper.SafeLat(lastCoords);
                                agEvent.LastLong     = GPSHelper.SafeLong(lastCoords);
                                agEvent.TruckID      = (truck != null) ? truck.Id : "";
                                agEvent.DriverID     = (driver != null) ? driver.Id : "";
                                agEvent.EventType    = (isLoadEvent) ? EventType.LOADED : EventType.UNLOADED;

                                lock (_aggregateEvents)
                                {
                                    //if we are on the gin yard leave load number empty
                                    if (!isLoadEvent)
                                    {
                                        agEvent.LoadNumber = "";
                                    }
                                    else
                                    {
                                        if (uow.SettingsRepository.EventOnGinYard(agEvent))
                                        {
                                            agEvent.LoadNumber = "";
                                        }
                                        else if (uow.SettingsRepository.EventAtFeeder(agEvent))
                                        {
                                            agEvent.LoadNumber = "";
                                        }
                                        else if (SerialNumbersOnTruck == null || SerialNumbersOnTruck.Count() == 0)
                                        {
                                            //NO MODULES ON TRUCK SO GENERATE NEW NUMBER
                                            agEvent.LoadNumber = uow.TruckRepository.GetNextLoadNumber();
                                        }
                                        else
                                        {
                                            //THERE ARE OTHER MODULES ON TRUCK SO GET LAST NUMBER USED
                                            agEvent.LoadNumber = uow.TruckRepository.GetLastLoadNumber();
                                        }
                                    }

                                    //only log event if it is a load of a module not previously scanned in a load cycle
                                    //or if an unload for which the serial wasn't picked up still on the truck - added check to only record unload event for modules that were on the truck
                                    if ((isLoadEvent && !snsOnTruck.Contains(agEvent.SerialNumber)) || (!isLoadEvent && snsOnTruck.Contains(agEvent.SerialNumber) /*&& !tagsStillOnTruck.Contains(agEvent.SerialNumber)*/))
                                    {
                                        agEventCount++;
                                        _aggregateEvents.Add(agEvent);
                                        agEvent.Created = DateTime.UtcNow;
                                        uow.AggregateEventRepository.Add(agEvent);
                                        uow.SaveChanges();

                                        //if unloading in the field clear last load number on events with this serial number
                                        //that were generated when loading stopped - this prevents skipping
                                        //load numbers when all modules are unloaded in the field
                                        CorrectLoadNumber(uow, isLoadEvent, agEvent);

                                        UpdateModuleStatus(uow, isLoadEvent, agEvent);

                                        Messenger.Default.Send <AggregateEvent>(agEvent);
                                    }
                                }
                            }
                            if (agEventCount > 0)
                            {
                                Messenger.Default.Send <AllAggEventsProcessComplete>(new AllAggEventsProcessComplete {
                                    IsLoading = isLoadEvent
                                });
                            }
                            tagBuffer.Clear();
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                Logging.Logger.Log(exc);
            }
        }