private void ExecuteCancelCommand()
 {
     TagDataProvider.SetGPOState(1, false);
     TagDataProvider.SetGPOState(2, false);
     TagDataProvider.SetGPOState(3, false);
     ExitToIdlePage();
 }
        private void handleWeightAcquired(WeightAcquiredMessage msg)
        {
            try
            {
                if (!_initialized || _canceled || _loadCreated)
                {
                    return;
                }

                Logging.Logger.Log("INFO", "Entering handleWeightAcquired " + msg.Weight.ToString());
                //lock (_dataLocker)
                //{
                if (_canceled)
                {
                    return;
                }

                if (!_weight1Acquired)
                {
                    Weight1          = msg.Weight;
                    _weight1Acquired = true;

                    //if not semi signal weight acquired
                    if (!IsSplitWeight)
                    {
                        WelcomeMessage = "WEIGHT RECORDED";
                        Logging.Logger.Log("INFO", "WEIGHT RECORDED " + msg.Weight.ToString());
                        waitingForExit = true;
                        TagDataProvider.SetGPOState(2, true);
                        TagDataProvider.SetGPOState(3, false);
                    }
                    else
                    {
                        WelcomeMessage = "WEIGHT 1 OF 2 RECORDED";
                        Logging.Logger.Log("INFO", "WEIGHT 1 OF 2 RECORDED " + msg.Weight.ToString());
                        TagDataProvider.SetGPOState(2, true);
                        TagDataProvider.SetGPOState(3, false);
                        _waitingForMotion = true;
                    }
                }
                else if (_isSplitWeigh && _weight1Acquired && !_waitingForMotion && !_weight2Acquired)
                {
                    Weight2          = msg.Weight;
                    _weight2Acquired = true;
                    waitingForExit   = true;
                    //signal pull forward
                    WelcomeMessage = "WEIGHT 2 OF 2 RECORDED ";
                    Logging.Logger.Log("INFO", "WEIGHT 2 OF 2 RECORDED " + msg.Weight.ToString());
                    TagDataProvider.SetGPOState(3, false);
                    TagDataProvider.SetGPOState(2, true);
                }
                //}
            }
            catch (Exception exc)
            {
                Logging.Logger.Log(exc);
                Logging.Logger.WriteBuffer();
            }
        }
 private void handleWeightAcquired(WeightAcquiredMessage msg)
 {
     try
     {
         TagDataProvider.SetGPOState(3, false);
     }
     catch (Exception exc)
     {
         Logging.Logger.Log(exc);
         Logging.Logger.WriteBuffer();
     }
 }
 private void handleInMotionMessage(InMotionMessage msg)
 {
     try
     {
         TagDataProvider.SetGPOState(3, true);
     }
     catch (Exception exc)
     {
         Logging.Logger.Log(exc);
         Logging.Logger.WriteBuffer();
     }
 }
        private void handleInMotion(InMotionMessage msg)
        {
            try
            {
                if (!_initialized || _canceled || _loadCreated)
                {
                    return;
                }

                if (timer != null)
                {
                    timer.Enabled = false;
                }

                Logging.Logger.Log("INFO", "Entering handleInMotion");
                //lock (_dataLocker)
                //{

                if (_canceled)
                {
                    return;
                }

                TagDataProvider.SetGPOState(2, false);

                if (zeroWeightCount < 20)
                {
                    TagDataProvider.SetGPOState(3, true);
                }
                //TagDataProvider.SetGPOState(3, true);

                if (!_weight1Acquired)
                {
                    Weight1 = msg.Weight;
                }
                else if (_weight1Acquired && _waitingForMotion)
                {
                    _waitingForMotion = false;
                }
                else if (!_weight2Acquired && IsSplitWeight)
                {
                    Weight2 = msg.Weight;
                }
                //}
            }
            catch (Exception exc)
            {
                Logging.Logger.Log(exc);
                Logging.Logger.WriteBuffer();
            }
        }
        public override void Cleanup()
        {
            Messenger.Default.Unregister <ScaleWeightReportMessage>(this);
            Messenger.Default.Unregister <WeightAcquiredMessage>(this);
            Messenger.Default.Unregister <InMotionMessage>(this);
            Messenger.Default.Unregister <BarcodeScannedMessage>(this);
            Messenger.Default.Unregister <List <TagItem> >(this, handleTagsReported);

            TagDataProvider.SetGPOState(1, false);
            TagDataProvider.SetGPOState(2, false);
            TagDataProvider.SetGPOState(3, false);

            base.Cleanup();
        }
        private void ExecuteLookupLoad()
        {
            try
            {
                if (_loadCreated)
                {
                    return;
                }

                _canceled = true;
                timer.Stop();
                autoSaveTimer.Stop();

                Logging.Logger.Log("INFO", "Entering ExecuteLookupLoad");
                //lock (_dataLocker)
                //{
                bool newLoad = false;

                if (string.IsNullOrWhiteSpace(GinTicketLoadNumber))
                {
                    HasLookupError = true;
                    return;
                }
                else
                {
                    HasLookupError = false;
                }
                Logging.Logger.Log("INFO", "CREATE LOAD FROM LOAD LOOKUP");

                if (!_loadCreated)
                {
                    CreateLoad(ref newLoad);
                }

                TagDataProvider.SetGPOState(1, false);
                TagDataProvider.SetGPOState(2, false);
                TagDataProvider.SetGPOState(3, false);

                var vm = new LoadViewModel(NavService);
                NavService.ShowPage(PageType.LOAD_PAGE, false, (BasePageViewModel)vm);
                vm.Initialize(GinTicketLoadNumber, newLoad);
                //}
            }
            catch (Exception exc)
            {
                Logging.Logger.Log(exc);
                Logging.Logger.WriteBuffer();
            }
        }
        public void Initialize(TruckEntity selectedTruck)
        {
            Logging.Logger.Log("INFO", "Entering Weighin Initialize");
            //lock (_dataLocker)
            //{
            _activeTruck = selectedTruck;
            using (var dp = SimpleIoc.Default.GetInstance <IUnitOfWorkFactory>().CreateUnitOfWork())
            {
                isUnattendedMode = bool.Parse(dp.SettingsRepository.GetSettingWithDefault(BridgeSettingKeys.UNATTENDED_MODE, "FALSE"));

                GinName         = dp.SettingsRepository.GetSettingWithDefault(BridgeSettingKeys.GIN_NAME, "");
                _allTrucks      = dp.TruckRepository.GetAll().ToList();
                WelcomeMessage  = selectedTruck.Name + " PULLING ONTO SCALE";
                IsSplitWeight   = _activeTruck.IsSemi;
                IsSplitCanceled = !_activeTruck.IsSemi;
                _yellowLightOn  = false;
                zeroWeightCount = 0;
                if (TagDataProvider.TagsInBuffer() >= 1)
                {
                    TagDataProvider.SetGPOState(1, true);
                    _yellowLightOn = true;
                    TagDataProvider.SetGPOState(2, false);
                    TagDataProvider.SetGPOState(3, false);
                }

                timer           = new System.Timers.Timer();
                timer.Interval  = int.Parse(dp.SettingsRepository.GetSettingWithDefault(BridgeSettingKeys.WEIGH_IN_TIMEOUT, "30")) * 1000;
                timer.AutoReset = false;
                timer.Elapsed  += Timer_Elapsed;
                timer.Start();

                autoSaveTimer           = new System.Timers.Timer();
                autoSaveTimer.Interval  = int.Parse(dp.SettingsRepository.GetSettingWithDefault(BridgeSettingKeys.WEIGHT_AUTO_SAVE_TIMEOUT, "10")) * 60 * 1000;
                autoSaveTimer.AutoReset = false;
                autoSaveTimer.Elapsed  += AutoSaveTimer_Elapsed;;
                autoSaveTimer.Start();
            }

            _initialized = true;
            //}
        }
        private void handleTagsReported(List <TagItem> tags)
        {
            try
            {
                if (!_initialized || _canceled || _loadCreated)
                {
                    return;
                }

                if (!_yellowLightOn)
                {
                    if (tags.Any(t => !_allTrucks.Any(truck => truck.RFIDTagId == t.SerialNumber || truck.RFIDTagId == t.Epc)))
                    {
                        TagDataProvider.SetGPOState(1, true);
                        _yellowLightOn = true;
                    }
                }
            }
            catch (Exception exc)
            {
                Logging.Logger.Log(exc);
            }
        }
        private void AutoSaveTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                if (_canceled || _loadCreated)
                {
                    return;
                }

                autoSaveTimer.Enabled = false;
                autoSaveTimer.Stop();

                Logging.Logger.Log("INFO", "Entering AutoSave");
                //lock (_dataLocker)
                //{
                bool newLoad = true;

                if (!_loadCreated)
                {
                    setGinTicketToAuto();
                    Logging.Logger.Log("INFO", "CREATE LOAD FROM AUTO SAVE");
                    CreateLoad(ref newLoad);
                }

                TagDataProvider.SetGPOState(1, false);
                TagDataProvider.SetGPOState(2, false);
                TagDataProvider.SetGPOState(3, false);

                ExitToIdlePage();
                //}
            }
            catch (Exception exc)
            {
                Logging.Logger.Log(exc);
                Logging.Logger.WriteBuffer();
            }
        }
        public void Initialize()
        {
            using (var dp = SimpleIoc.Default.GetInstance <IUnitOfWorkFactory>().CreateUnitOfWork())
            {
                TagDataProvider.SetGPOState(1, false);
                TagDataProvider.SetGPOState(2, false);
                TagDataProvider.SetGPOState(3, false);

                GinName        = dp.SettingsRepository.GetSettingWithDefault(BridgeSettingKeys.GIN_NAME, "");
                WelcomeMessage = "WELCOME, TO " + GinName;
                TagDataProvider.ClearBuffer();
                Logging.Logger.WriteBuffer();

                /*Task.Run(() =>
                 * {
                 *  TagDataProvider.DisconnectIfUptimeLimitReached(1);
                 * });*/

                lock (_trucks)
                {
                    _trucks = dp.TruckRepository.GetAll().ToList();
                }
            }
        }
        private void handleWeightReported(ScaleWeightReportMessage msg)
        {
            try
            {
                if (!_initialized || _canceled || _loadCreated)
                {
                    return;                                              //load was created so we need to ignore events that may have gotten queued
                }
                Logging.Logger.Log("INFO", "Entering handleWeightReported: " + msg.Weight.ToString());
                //lock (_dataLocker)
                //{

                if (!_weight1Acquired)
                {
                    Weight1 = msg.Weight;
                }
                else if (!_weight2Acquired && !_waitingForMotion && IsSplitWeight)
                {
                    Weight2 = msg.Weight;
                }

                if (waitingForExit)
                {
                    if (msg.Weight < 200)     //changed from 100
                    {
                        zeroWeightCount++;
                        Logging.Logger.Log("INFO", "Zero weight count " + zeroWeightCount.ToString());
                    }
                    else
                    {
                        zeroWeightCount = 0;
                    }

                    if (zeroWeightCount >= 5)     //changed from 20
                    {
                        TagDataProvider.SetGPOState(2, false);
                        TagDataProvider.SetGPOState(3, false);

                        if (isUnattendedMode && !_loadCreated)
                        {
                            bool newLoad = true;
                            setGinTicketToAuto();
                            Logging.Logger.Log("INFO", "CREATE LOAD WEIGHT REPORTED EVENT");

                            if (!_loadCreated)
                            {
                                CreateLoad(ref newLoad);
                            }

                            ExitToIdlePage();
                        }
                    }
                }

                GrossWeight = Weight1 + Weight2;
                //}
            }
            catch (Exception exc)
            {
                Logging.Logger.Log(exc);
            }
        }