Exemplo n.º 1
0
        // ReSharper disable once CognitiveComplexity
        private static void RunWorkplace(Workplace workplace)
        {
            var outputPath = CreateLogFileIfNotExists(workplace.Oid + "-" + workplace.Name + ".txt");

            using (var factory = CreateLogger(outputPath, out var logger)) {
                LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Started running", logger);
                var timer = Stopwatch.StartNew();
                while (_databaseIsOnline && _loopCanRun && _systemIsActivated)
                {
                    workplace.UpdateActualStateForWorkplace(logger);
                    workplace.UpdateIntervalForIdle(logger);
                    var idForWorkplaceModeTypeTisk     = workplace.GetWorkplaceModeTypeIdFor("Tisk", logger);
                    var idForWorkplaceModeTypePriprava = workplace.GetWorkplaceModeTypeIdFor("Příprava", logger);
                    var idForWorkplaceModeTypeUklid    = workplace.GetWorkplaceModeTypeIdFor("Úklid", logger);
                    var idForWorkplaceModeTisk         = workplace.GetWorkplaceModeIdFor(idForWorkplaceModeTypeTisk, logger);
                    var idForWorkplaceModePriprava     = workplace.GetWorkplaceModeIdFor(idForWorkplaceModeTypePriprava, logger);
                    var idForWorkplaceModeUklid        = workplace.GetWorkplaceModeIdFor(idForWorkplaceModeTypeUklid, logger);
                    var devicePortIdIsOne = workplace.CheckIfDevicePortIdIsOne(workplace, logger);
                    LogDeviceInfo($"[ {workplace.Name} ] --INF-- State od deviceport: " + devicePortIdIsOne, logger);
                    if (devicePortIdIsOne)
                    {
                        LogDeviceInfo($"[ {workplace.Name} ] --INF-- Workplace has port in state 1", logger);
                        var specialIdleOpened = workplace.CheckIfWorkplaceHasSpecialIdleOpened(logger);
                        var normalIdleOpened  = workplace.CheckIfWorkplaceHasNormalIdleOpened(logger);
                        LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Special idle: " + specialIdleOpened, logger);
                        LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Normal idle: " + normalIdleOpened, logger);
                        if (specialIdleOpened)
                        {
                            LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Special idle opened, doing nothing", logger);
                        }
                        else if (normalIdleOpened)
                        {
                            LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Non-special idle opened, checking for note Myti", logger);
                            var idleHasNoteMyti = workplace.CheckIfWorkplaceHasNoteMyti(logger);
                            if (idleHasNoteMyti)
                            {
                                LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Idle has note Myti, closing idle, creating idle Myti", logger);
                                var closed = workplace.CloseIdleForWorkplace(DateTime.Now, logger);
                                if (closed)
                                {
                                    LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Idle closed", logger);
                                    workplace.CreateIdleMytiForWorkplace(logger, true, DateTime.Now);
                                    var openTerminalInputOrder = workplace.GetOpenTerminalInputOrderFor(idForWorkplaceModePriprava, logger);
                                    if (openTerminalInputOrder == 0)
                                    {
                                        LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Open order is NOT type Priprava", logger);
                                    }
                                    else
                                    {
                                        LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Open order is type Priprava", logger);
                                        var orderId          = workplace.GetOrderIdFor(openTerminalInputOrder, logger);
                                        var userId           = workplace.GetUserIdFor(openTerminalInputOrder, logger);
                                        var numberOfImpulses = workplace.GetNumberOfImpulsesForWorkplace(workplace, logger);

                                        workplace.SaveToK2("117", userId, orderId, true, numberOfImpulses, logger);
                                        workplace.SaveToK2("110", userId, orderId, false, 0, logger);
                                    }

                                    openTerminalInputOrder = workplace.GetOpenTerminalInputOrderFor(idForWorkplaceModeTisk, logger);
                                    if (openTerminalInputOrder == 0)
                                    {
                                        LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Open order is NOT type Tisk", logger);
                                    }
                                    else
                                    {
                                        LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Open order is type Tisk", logger);
                                        var orderId          = workplace.GetOrderIdFor(openTerminalInputOrder, logger);
                                        var userId           = workplace.GetUserIdFor(openTerminalInputOrder, logger);
                                        var numberOfImpulses = workplace.GetNumberOfImpulsesForWorkplace(workplace, logger);
                                        workplace.SaveToK2("118", userId, orderId, true, numberOfImpulses, logger);
                                        workplace.SaveToK2("114", userId, orderId, false, 0, logger);
                                    }
                                }
                            }
                            else
                            {
                                LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Idle does NOT have note Myti, checking if idle is internal", logger);
                                var idleIsInternal = workplace.CheckIfWorkplaceIdleIsInternal(logger);
                                if (idleIsInternal)
                                {
                                    LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Idle is internal, closing idle", logger);
                                    workplace.CloseIdleForWorkplace(DateTime.Now, logger);
                                }
                                else
                                {
                                    LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Idle is NOT internal, closing idle", logger);
                                    var closed = workplace.CloseIdleForWorkplace(DateTime.Now, logger);
                                    if (closed)
                                    {
                                        LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Idle closed", logger);
                                        var openTerminalInputOrder = workplace.GetOpenTerminalInputOrderFor(idForWorkplaceModePriprava, logger);
                                        if (openTerminalInputOrder == 0)
                                        {
                                            LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Open order is NOT type Priprava: " + openTerminalInputOrder, logger);
                                        }
                                        else
                                        {
                                            LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Open order is type Priprava: " + openTerminalInputOrder, logger);
                                            var orderId          = workplace.GetOrderIdFor(openTerminalInputOrder, logger);
                                            var userId           = workplace.GetUserIdFor(openTerminalInputOrder, logger);
                                            var numberOfImpulses = workplace.GetNumberOfImpulsesForWorkplace(workplace, logger);
                                            workplace.SaveToK2("117", userId, orderId, true, numberOfImpulses, logger);
                                        }

                                        openTerminalInputOrder = workplace.GetOpenTerminalInputOrderFor(idForWorkplaceModeTisk, logger);
                                        if (openTerminalInputOrder == 0)
                                        {
                                            LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Open order is NOT type Tisk: " + openTerminalInputOrder, logger);
                                        }
                                        else
                                        {
                                            LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Open order is type Tisk: " + openTerminalInputOrder, logger);
                                            var orderId          = workplace.GetOrderIdFor(openTerminalInputOrder, logger);
                                            var userId           = workplace.GetUserIdFor(openTerminalInputOrder, logger);
                                            var numberOfImpulses = workplace.GetNumberOfImpulsesForWorkplace(workplace, logger);
                                            workplace.SaveToK2("118", userId, orderId, true, numberOfImpulses, logger);
                                        }

                                        openTerminalInputOrder = workplace.GetOpenTerminalInputOrderFor(idForWorkplaceModeUklid, logger);
                                        if (openTerminalInputOrder == 0)
                                        {
                                            LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Open order is NOT type Uklid: " + openTerminalInputOrder, logger);
                                        }
                                        else
                                        {
                                            LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Open order is type Uklid: " + openTerminalInputOrder, logger);
                                            var orderId = workplace.GetOrderIdFor(openTerminalInputOrder, logger);
                                            var userId  = workplace.GetUserIdFor(openTerminalInputOrder, logger);
                                            workplace.SaveToK2("116", userId, orderId, false, 0, logger);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else if (workplace.ActualStateType == StateType.Idle)
                    {
                        LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Workplace in idle", logger);
                        var workplaceHasActiveIdle = workplace.CheckIfWorkplaceHasOpenIdle(logger);
                        if (workplaceHasActiveIdle)
                        {
                            LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Terminal idle opened, doing nothing", logger);
                        }
                        else
                        {
                            var actualDate             = DateTime.Now;
                            var openTerminalInputOrder = workplace.CheckOpenTerminalInputOrder(logger);
                            if (openTerminalInputOrder)
                            {
                                var openTerminalInputOrderTisk = workplace.GetOpenTerminalInputOrderFor(idForWorkplaceModeTisk, logger);
                                if (openTerminalInputOrderTisk == 0)
                                {
                                    LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Terminal idle not opened, order type tisk NOT opened, NOT creating idle", logger);
                                }
                                else
                                {
                                    LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Terminal idle not opened, order type tisk opened, creating idle", logger);
                                    workplace.CreateIdleInternalForWorkplace(logger, true, actualDate);
                                }
                            }
                            else
                            {
                                LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Terminal idle not opened, order not opened, creating idle", logger);
                                workplace.CreateIdleInternalForWorkplace(logger, false, actualDate);
                            }

                            LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Terminal_input_idle created", logger);
                        }
                    }
                    else if (workplace.ActualStateType == StateType.PowerOff)
                    {
                        LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Workplace offline", logger);
                        var openInternalIdle = workplace.CheckIfWorkplaceHasOpenInternalIdle(logger);
                        if (openInternalIdle)
                        {
                            LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Internal idle opened, closing", logger);
                            workplace.CloseIdleForWorkplace(DateTime.Now, logger);
                        }
                        else
                        {
                            LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Internal idle NOT opened, doing nothing", logger);
                        }
                    }

                    workplace.UpdateCountFromAnalog(logger);
                    var sleepTime = Convert.ToDouble(_downloadEvery);
                    var waitTime  = sleepTime - timer.ElapsedMilliseconds;
                    if ((waitTime) > 0)
                    {
                        LogDeviceInfo($"[ {workplace.Name} ] --INF-- Sleeping for {waitTime} ms", logger);
                        Thread.Sleep((int)(waitTime));
                    }
                    else
                    {
                        LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Processing takes more than" + _downloadEvery + " ms", logger);
                    }

                    timer.Restart();
                }

                factory.Dispose();
                LogDeviceInfo("[ " + workplace.Name + " ] --INF-- Process ended.", logger);
                _numberOfRunningWorkplaces--;
            }
        }