Example #1
0
        /// <summary>
        ///     Task which is used for scanning pending commands and broadcast to external terminals.
        /// </summary>
        private void SearchWarehousePendingCommands()
        {
            while (true)
            {
                // Task is not allowed to run.
                if (!_isTaskRunning)
                {
                    Thread.Sleep(Setting.CommandScanTaskInterval);
                    continue;
                }

                // Wait for signal from another thread to start this one.
                _manualResetScannerEvent.WaitOne();

                try
                {
                    // Message will be built base on the being activated automated warehouse controller.
                    var message = "";

                    #region Message construction

                    // Operating warehouse controller is material.
                    if (TerminalName.Material.Equals(_terminalSetting.Key))
                    {
                        #region Automated warehouse status validation.

                        // No material warehouse controller has been configured in application setting file.
                        if (string.IsNullOrEmpty(AutoController.MaterialAutoWarehouseDeviceCode))
                        {
                            Message.InitiateMessage(DateTime.Now, MessageType.Information,
                                                    $"No material warehouse controller device code has been configured into application setting file. Restart in {Setting.CommandScanTaskInterval} milliseconds.");

                            // Sleep the thread for 3 secs.
                            Thread.Sleep(Setting.CommandScanTaskInterval);
                            continue;
                        }

                        // Material auto warehouse controller is offline.
                        if (!MaterialAutoWarehouseController.IsAutoWarehouseOnline(AutoController.MaterialAutoWarehouseDeviceCode))
                        {
                            Message.InitiateMessage(DateTime.Now, MessageType.Information,
                                                    $"Material automated warehouse is offline. Restart in {Setting.CommandScanTaskInterval} milliseconds.");

                            // Sleep the thread for awhile.
                            Thread.Sleep(Setting.CommandScanTaskInterval);
                            continue;
                        }

                        #endregion

                        #region Message initialization

                        try
                        {
                            // Find pending material command in database.
                            var commands = AutoController.FindPendingMaterialCommands();

                            // Command is not valid.
                            if (commands == null || commands.Count < 1)
                            {
                                //Message.InitiateMessage(DateTime.Now, MessageType.Information,
                                //$"No pending command has been found. Broadcaster will be restarted in {Setting.CommandScanTaskInterval} milliseconds.");

                                Thread.Sleep(Setting.CommandScanTaskInterval);
                                continue;
                            }

                            // ProceedIncommingCommand database records.
                            MaterialAutoWarehouseController.ProcessDataBase(commands, AutoController.MaterialAutoWarehouseDeviceCode, _terminalSetting.Value.Outgoing);

                            Thread.Sleep(Setting.CommandScanTaskInterval);
                            continue;
                        }
                        catch (Exception exception)
                        {
                            Log.Error(exception.Message, exception);
                            Message.InitiateMessage(DateTime.Now, MessageType.Error, exception.Message);

                            Thread.Sleep(Setting.CommandScanTaskInterval);
                            continue;
                        }

                        #endregion
                    }
                    else if (TerminalName.PreProduct.Equals(_terminalSetting.Key))
                    {
                        using (var context = new KCSGDbContext())
                            using (var unitOfWork = new UnitOfWork(context))
                            {
                                // delete off empty command
                                var preProduct = unitOfWork.PreProductWarehouseCommandRepository.GetMany(
                                    i => i.F50_From.Trim().Equals(""))
                                                 .FirstOrDefault();

                                if (preProduct != null)
                                {
                                    unitOfWork.PreProductWarehouseCommandRepository.Delete(preProduct);
                                    unitOfWork.Commit();
                                }
                            }

                        #region Automated warehouse status validation.

                        // No material warehouse controller has been configured in application setting file.
                        if (string.IsNullOrEmpty(AutoController.PreProductAutoWarehouseDeviceCode))
                        {
                            Message.InitiateMessage(DateTime.Now, MessageType.Information,
                                                    $"No pre-product warehouse controller device code has been configured into application setting file. Restart in {Setting.CommandScanTaskInterval} milliseconds.");

                            // Sleep the thread for 3 secs.
                            Thread.Sleep(3000);
                            continue;
                        }

                        // Material auto warehouse controller is offline.
                        if (!PreProductAutoWarehouseController.IsAutoWarehouseOnline(AutoController.PreProductAutoWarehouseDeviceCode))
                        {
                            Message.InitiateMessage(DateTime.Now, MessageType.Information,
                                                    $"Pre-product automated warehouse is offline. Restart in {Setting.CommandScanTaskInterval} milliseconds.");

                            // Sleep the thread for awhile.
                            Thread.Sleep(Setting.CommandScanTaskInterval);
                            continue;
                        }

                        #endregion

                        #region Pre-product initialization

                        try
                        {
                            var commands = AutoController.FindPendingPreProductCommands();
                            if (commands == null || commands.Count < 1)
                            {
                                //Message.InitiateMessage(DateTime.Now, MessageType.Information,
                                //$"No pending command has been found. Listener will be restarted in {Setting.CommandScanTaskInterval} milliseconds.");

                                Thread.Sleep(Setting.CommandScanTaskInterval);
                                continue;
                            }

                            // ProceedIncommingCommand messages list.
                            PreProductAutoWarehouseController.ProceedMessages(commands,
                                                                              AutoController.PreProductAutoWarehouseDeviceCode, _terminalSetting.Value.Outgoing);

                            Thread.Sleep(Setting.CommandScanTaskInterval);
                        }
                        catch (Exception exception)
                        {
                            Log.Error(exception.Message, exception);
                            Message.InitiateMessage(DateTime.Now, MessageType.Error, exception.Message);
                        }

                        #endregion
                    }
                    else if (TerminalName.Product.Equals(_terminalSetting.Key))
                    {
                        #region Automated warehouse status validation.

                        // No material warehouse controller has been configured in application setting file.
                        if (string.IsNullOrEmpty(AutoController.ProductAutoWarehouseDeviceCode))
                        {
                            Message.InitiateMessage(DateTime.Now, MessageType.Information,
                                                    $"No product warehouse controller device code has been configured into application setting file. Restart in {Setting.CommandScanTaskInterval} milliseconds.");

                            // Sleep the thread for 3 secs.
                            Thread.Sleep(3000);
                            continue;
                        }

                        // Material auto warehouse controller is offline.
                        if (!ProductAutoWarehouseController.IsAutoWarehouseOnline(AutoController.ProductAutoWarehouseDeviceCode))
                        {
                            Message.InitiateMessage(DateTime.Now, MessageType.Information,
                                                    $"Product automated warehouse is offline. Restart in {Setting.CommandScanTaskInterval} milliseconds.");

                            // Sleep the thread for awhile.
                            Thread.Sleep(Setting.CommandScanTaskInterval);
                            continue;
                        }

                        #endregion

                        #region Products initialization

                        try
                        {
                            // TODO: Disabled it at 2017-05-17 (Consider enable it back)
                            //var commands = AutoController.FindPendingProductCommands();
                            //if (commands == null || commands.Count < 1)
                            //{
                            //    //Message.InitiateMessage(DateTime.Now, MessageType.Information,
                            //    //$"No pending command has been found. Listener will be restarted in {Setting.CommandScanTaskInterval} milliseconds.");
                            //    Thread.Sleep(Setting.CommandScanTaskInterval);
                            //    continue;
                            //}
                            //ProductAutoWarehouseController.ProcessDataList(commands,
                            //    AutoController.ProductAutoWarehouseDeviceCode, _terminalSetting.Value.Outgoing);
                            //Thread.Sleep(Setting.CommandScanTaskInterval);

                            var command = AutoController.FindPendingProductCommands().FirstOrDefault();
                            if (command == null)
                            {
                                Thread.Sleep(Setting.CommandScanTaskInterval);
                                continue;
                            }

                            ProductAutoWarehouseController.ProcessDataList(new[] { command },
                                                                           AutoController.ProductAutoWarehouseDeviceCode, _terminalSetting.Value.Outgoing);
                            Thread.Sleep(Setting.CommandScanTaskInterval);
                        }
                        catch (Exception exception)
                        {
                            Log.Error(exception.Message, exception);
                            Message.InitiateMessage(DateTime.Now, MessageType.Error, exception.Message);
                        }

                        #endregion
                    }
                    else
                    {
                        Message.InitiateMessage(DateTime.Now, MessageType.Error,
                                                $"Material, Pre-Product, Product should be selected to broadcast message. Restart message broadcaster in {Setting.CommandScanTaskInterval} milliseconds.");
                        Thread.Sleep(Setting.CommandScanTaskInterval);
                        continue;
                    }

                    #endregion
                }
                catch (EntityException entityException)
                {
                    Log.Error(entityException.Message, entityException);
                    Message.ShowMessageBox(View.FindView(),
                                           "There is something wrong with database. Please check database configuration in configuration file.",
                                           "System Message", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);

                    Application.Current.Dispatcher.Invoke(() => { IsTaskRunning = false; });
                }
                catch (Exception exception)
                {
                    Log.Error(exception.Message, exception);
                }
            }
        }