예제 #1
0
        public bool CheckIfDevicePortIdIsOne(Workplace workplace, ILogger logger)
        {
            var data       = 0;
            var connection = new MySqlConnection($"server={Program.IpAddress};port={Program.Port};userid={Program.Login};password={Program.Password};database={Program.Database};");

            try {
                connection.Open();
                var selectQuery =
                    $"select Data from device_input_digital where DevicePortId=(SELECT `DevicePortID` FROM `workplace_port` WHERE `WorkplaceID` = '{workplace.Oid}' AND `Type` LIKE '%running%') order by DT desc limit 1";
                var command = new MySqlCommand(selectQuery, connection);
                try {
                    var reader = command.ExecuteReader();
                    if (reader.Read())
                    {
                        data = Convert.ToInt32(reader["Data"]);
                    }

                    reader.Close();
                    reader.Dispose();
                } catch (Exception error) {
                    LogError("[ " + Name + " ] --ERR-- Problem checking active idle: " + error.Message + selectQuery, logger);
                } finally {
                    command.Dispose();
                }

                connection.Close();
            } catch (Exception error) {
                LogError("[ " + Name + " ] --ERR-- Problem with database: " + error.Message, logger);
            } finally {
                connection.Dispose();
            }

            return(data == 1);
        }
예제 #2
0
        public int GetNumberOfImpulsesForWorkplace(Workplace workplace, ILogger logger)
        {
            var numberOfImpulses = 0;
            var connection       = new MySqlConnection($"server={Program.IpAddress};port={Program.Port};userid={Program.Login};password={Program.Password};database={Program.Database};");

            numberOfImpulses = DownloadNumberOfImpulsesForWorkplace(workplace, logger, connection, numberOfImpulses);
            UpdateWorkplaceUpdatedColumn(workplace, logger, connection);
            return(numberOfImpulses);
        }
예제 #3
0
        private void UpdateWorkplaceUpdatedColumn(Workplace workplace, ILogger logger, MySqlConnection connection)
        {
            try {
                connection.Open();
                var updateQuery = $"update zapsi2.workplace set Updated = NOW() where workplaceId={workplace.Oid})";
                var command     = new MySqlCommand(updateQuery, connection);
                try {
                    command.ExecuteNonQuery();
                } catch (Exception error) {
                    LogError("[ " + Name + " ] --ERR-- Problem updating : " + error.Message + updateQuery, logger);
                } finally {
                    command.Dispose();
                }

                connection.Close();
            } catch (Exception error) {
                LogError("[ " + Name + " ] --ERR-- Problem with database: " + error.Message, logger);
            } finally {
                connection.Dispose();
            }
        }
예제 #4
0
        private static List <Workplace> GetListOfWorkplacesFromDatabase(ILogger logger)
        {
            var workplaces = new List <Workplace>();
            var connection = new MySqlConnection($"server={IpAddress};port={Port};userid={Login};password={Password};database={Database};");

            try {
                connection.Open();
                const string selectQuery = "SELECT * from zapsi2.workplace where DeviceID is not NULL";
                var          command     = new MySqlCommand(selectQuery, connection);
                try {
                    var reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        var workplace = new Workplace {
                            Oid                 = Convert.ToInt32(reader["OID"]),
                            Name                = Convert.ToString(reader["Name"]),
                            DeviceOid           = Convert.ToInt32(reader["DeviceID"]),
                            WorkplaceDivisionId = Convert.ToInt32(reader["WorkplaceDivisionID"])
                        };
                        workplaces.Add(workplace);
                    }

                    reader.Close();
                    reader.Dispose();
                } catch (Exception error) {
                    LogError("[ MAIN ] --ERR-- Problem getting list of workplaces " + error.Message + selectQuery, logger);
                } finally {
                    command.Dispose();
                }

                connection.Close();
            } catch (Exception error) {
                LogError("[ MAIN ] --ERR-- problem with database: " + error.Message, logger);
            } finally {
                connection.Dispose();
            }

            return(workplaces);
        }
예제 #5
0
        private int DownloadNumberOfImpulsesForWorkplace(Workplace workplace, ILogger logger, MySqlConnection connection, int numberOfImpulses)
        {
            try {
                connection.Open();
                var selectQuery =
                    $"select SUM(Data) as sum from zapsi2.device_input_analog where DevicePortId=(SELECT DevicePortId FROM workplace_port WHERE HighValue = 100 and WorkplaceId={workplace.Oid}) and DT>(select Updated from zapsi2.workplace where OID={workplace.Oid})";
                var command = new MySqlCommand(selectQuery, connection);
                try {
                    var reader = command.ExecuteReader();

                    if (reader.Read())
                    {
                        var value = reader["sum"];
                        if (value != DBNull.Value)
                        {
                            numberOfImpulses = (int)Convert.ToDouble(value);
                        }
                    }

                    reader.Close();
                    reader.Dispose();
                } catch (Exception error) {
                    LogError("[ " + Name + " ] --ERR-- Problem getting sum of encoder: " + error.Message + selectQuery, logger);
                } finally {
                    command.Dispose();
                }

                connection.Close();
            } catch (Exception error) {
                LogError("[ " + Name + " ] --ERR-- Problem with database: " + error.Message, logger);
            } finally {
                connection.Dispose();
            }

            return(numberOfImpulses);
        }
예제 #6
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--;
            }
        }