예제 #1
0
        ///
        /// <param name="description">Description of dataset from module 1</param>
        public ICollectionDescription RepackToCollectionDescription(IDescription description)
        {
            int     id      = description.ID;
            Dataset dataset = description.Dataset;

            logger.LogNewInfo(string.Format("Repacking description with dataset {0} and id {1} to Collection description.", dataset, id));
            List <IModule2Property> properties       = new List <IModule2Property>();
            List <SignalCode>       processedSignals = new List <SignalCode>();

            foreach (IModule1Property property in description.Properties)
            {
                if (processedSignals.Contains(property.Code))
                {
                    logger.LogNewWarning("Two signals of the same type in one description");
                    throw new ArgumentException("ERROR Two signals of the same type in one description");
                }
                if (DatasetRepository.GetDataset(property.Code) != dataset)
                {
                    logger.LogNewWarning(string.Format("Received dataset {0} does not match dataset for signal {1}", dataset, property.Code));
                    throw new ArgumentException("Data set does not match signal.");
                }

                processedSignals.Add(property.Code);
                properties.Add(RepackToModule2Property(property));
            }

            HistoricalCollection  historicalCollection = new HistoricalCollection(properties);
            CollectionDescription repackedData         = new CollectionDescription(id, dataset, historicalCollection);

            logger.LogNewInfo("Data repacked to collection description successfully");
            return(repackedData);
        }
        ///
        /// <param name="property">Module 1 property</param>
        public void WriteProperty(IModule1Property property)
        {
            logger.LogNewInfo(string.Format("Trying to write property with signal code {0} and value {1} to database", property.Code, property.Module1Value));
            Dataset set       = DatasetRepository.GetDataset(property.Code);
            string  tableName = DatabaseTableNamesRepository.GetTableNameByDataset(set);

            string signalCode = property.Code.ToString();
            double value      = property.Module1Value;
            string query      = @"DELETE FROM " + tableName + " WHERE signalCode=@codeToDelete; INSERT INTO " + tableName + " (signalCode, signalValue) VALUES(@codeToInsert, @value)";

            using (SQLiteCommand command = new SQLiteCommand(query, databaseConnection))
            {
                command.Parameters.AddWithValue("@codeToDelete", signalCode);
                command.Parameters.AddWithValue("@codeToInsert", signalCode);
                command.Parameters.AddWithValue("@value", value);

                if (command.ExecuteNonQuery() == 0)
                {
                    logger.LogNewWarning("Could not write to database.");
                }
                else
                {
                    logger.LogNewInfo("Property written successfully.");
                }
            }
        }
예제 #3
0
 public void SendSignal(int signal, double value)
 {
     if (value < 0)
     {
         logger.LogNewWarning("User sent invalid data for value.");
         throw new Exception("The value does not match specified interval!");
     }
     else if (signal < 0 || signal > 7)
     {
         logger.LogNewWarning("User sent invalid data for signal.");
         throw new Exception("The value of signal does not match specified interval!");
     }
     else
     {
         logger.LogNewInfo(String.Format("Input sending signal directly to Modul2 with values {0} - {1}.", (SignalCode)signal, value));
         historyWritingProxy.WriteToHistory((SignalCode)signal, value);
     }
 }
        /// 
        /// <param name="property">List description form module1</param>
        public bool UpdateDatabase(IListDescription property)
        {
            logger.LogNewInfo("New list description arrived. Performing update on database..");
            IModule2Property lastProperty = null;
            List<ICollectionDescription> collectionDescriptions = null;


            try
            {
                collectionDescriptions = dataAdapter.RepackToCollectionDescriptionArray(property);
            }catch(ArgumentException)
            {
                logger.LogNewWarning("Argument exception thrown by data adapter, aborting all operations.");
                return false;
            }catch(Exception)
            {
                logger.LogNewWarning("Unknown exception thrown by data adapter, aborting all operations.");
                return false;
            }
            
            List<IModule2Property> allProperties = new List<IModule2Property>();
            
            foreach(ICollectionDescription cd in collectionDescriptions)
            {
                allProperties.AddRange(cd.Collection.Properties);
            }

            foreach(IModule2Property module2property in allProperties)
            {
                lastProperty = databaseManager.ReadLastByCode(module2property.Code);
                if(lastProperty == null)
                {
                    logger.LogNewInfo(string.Format("No property found in database for signal code {0}. Writing directly without deadband checking..", module2property.Code));
                    databaseManager.WriteProperty(module2property);
                }
                else if(IsDeadbandSatisfied(lastProperty, module2property, deadbandPercentage))
                {
                    databaseManager.WriteProperty(module2property);
                }
            }
            return true;
        }
예제 #5
0
        /// 
        /// <param name="code">Signal code</param>
        public IModule2Property ReadLastByCode(SignalCode code)
        {
            Dataset set = DatasetRepository.GetDataset(code);
            string tableName = DatabaseTableNamesRepository.GetTableNameByDataset(set);

            string signalCode = code.ToString();
            string query = "SELECT ID, signalCode, signalValue FROM " + tableName +  " WHERE(signalCode=@code) " +
                           "ORDER BY timestamp DESC LIMIT 1";

            using (SQLiteCommand command = new SQLiteCommand(query, databaseConnection))
            {
                command.Parameters.AddWithValue("@code", signalCode);
                try
                {
                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string retrievedSignal = reader["signalCode"].ToString();
                            string value = reader["signalValue"].ToString();

                            SignalCode retrievedCode = (SignalCode)Enum.Parse(typeof(SignalCode), retrievedSignal);
                            double valueRetrieved = double.Parse(value);
                            Module2Property property = new Module2Property(retrievedCode, valueRetrieved);
                            return property;
                        }

                        return null;
                    }          
                }catch(Exception ex)
                {
                    logger.LogNewWarning(string.Format("ERROR occured reading database. MESSAGE: {0}", ex.Message));
                    return null;
                }
            }
                          
            
                
        }
예제 #6
0
 public void ValidateParameters(string beginDate, string endDate, int code)
 {
     if (!DateTime.TryParse(beginDate, out DateTime firstDate))
     {
         logger.LogNewWarning("Reader: Invalid value for startDate.");
         throw new Exception("The first date value is not valid!");
     }
     else if (!DateTime.TryParse(endDate, out DateTime secondDate))
     {
         logger.LogNewWarning("Reader: Invalid value for endDate.");
         throw new Exception("The second date value is not valid!");
     }
     else if (DateTime.Compare(firstDate, secondDate) >= 0)
     {
         logger.LogNewWarning("Reader: Ending date has to be older than starting date.");
         throw new Exception("Ending date has to be older than starting date.");
     }
     else if (code < 0 || code > 7)
     {
         logger.LogNewWarning("Reader: Invalid value for code.");
         throw new Exception("The value of code is not in range!");
     }
 }