public string ReadFromHistory(string beginDate, string endDate, int code) { string ret = ""; DateTime firstDate, secondDate; SignalCode sCode; List <IModule2Property> list = new List <IModule2Property>(); logger.LogNewInfo("Reader is validating data."); ValidateParameters(beginDate, endDate, code); logger.LogNewInfo("Reader successfully validated data."); firstDate = DateTime.Parse(beginDate); secondDate = DateTime.Parse(endDate); sCode = (SignalCode)code; list = module2proxy.ReadHistory(firstDate, secondDate, sCode); logger.LogNewInfo("Reader took data from Module2 database."); foreach (IModule2Property p in list) { ret += String.Format("Code: {0}\t Value: {1}\n", p.Code, p.Value); } return(ret); }
/// /// <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."); } } }
/// /// <param name="property">Module 1 property</param> public void AddOrReplaceProperty(IModule1Property property) { logger.LogNewInfo(string.Format("Add or replace property called with signal code {0} and value {1}", property.Code, property.Module1Value)); if (DoesPropertyExist(property.Code)) { Properties.RemoveAll(x => x.Code == property.Code); } Properties.Add(property); }
public void GenerateSignals() { while (true) { Random rand = new Random(); int code = rand.Next(8); double value = rand.NextDouble() * 1001; logger.LogNewInfo(String.Format("Input started generating signals and sending it to Modul1 with values {0} - {1}.", code, value)); module1Proxy.UpdateDataset(value, (SignalCode)code); Thread.Sleep(3000); } }
/// /// <param name="periodStart">Beginning of the search period</param> /// <param name="periodEnd">End of the search period</param> public List<IModule2Property> ReadPropertiesByTimeframe(DateTime periodStart, DateTime periodEnd, SignalCode code) { List<IModule2Property> returnList = new List<IModule2Property>(); logger.LogNewInfo(string.Format("Reading properties for code {0} with starting date {1}, end date {2}", code, periodEnd, periodEnd)); Dataset set = DatasetRepository.GetDataset(code); string tableName = DatabaseTableNamesRepository.GetTableNameByDataset(set); string signalCode = code.ToString(); string dateStart= periodStart.ToUniversalTime().ToString("yyyy'-'MM'-'dd' 'HH':'mm':'ss"); string dateEnd = periodEnd.ToUniversalTime().ToString("yyyy'-'MM'-'dd' 'HH':'mm':'ss"); string query = @"SELECT signalCode, signalValue FROM " + tableName + " WHERE signalCode=@code AND " + "strftime('%s', timestamp) BETWEEN strftime('%s', @startDate) AND strftime('%s', @endDate) " + "ORDER BY timestamp DESC "; using (SQLiteCommand command = new SQLiteCommand(query, databaseConnection)) { command.Parameters.AddWithValue("@code", signalCode); command.Parameters.AddWithValue("@startDate", dateStart); command.Parameters.AddWithValue("@endDate", dateEnd); try { 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); returnList.Add(property); } } catch (Exception ex) { logger.LogNewWarning(string.Format("ERROR occured reading database. MESSAGE: {0}", ex.Message)); return returnList; } } logger.LogNewInfo(string.Format("Found {0} properties within the requested timeframe", returnList.Count)); return returnList; }
/// /// <param name="value">Signal value</param> /// <param name="signalCode">Code for given signal</param> public bool UpdateDataset(double value, SignalCode signalCode) { logging.LogNewInfo(string.Format("Update dataset called for signal {0} and value {1}", signalCode, value)); Dataset dataset = DatasetRepository.GetDataset(signalCode); IModule1Property property = dataAdapter.PackToModule1Property(signalCode, value); listDescription.AddOrReplaceProperty(property); if (listDescription.IsDatasetFull(dataset)) { logging.LogNewInfo(string.Format("Dataset {0} is full, sending the whole list to module 2", dataset.ToString())); module2Proxy.UpdateDatabase(listDescription); } logging.LogNewInfo("Calling database manager to write new property.."); databaseManager.WriteProperty(property); return(true); }
public void AddOrReplaceProperty(IModule1Property property) { logger.LogNewInfo("Trying to replace property in list description."); Dataset dataset = DatasetRepository.GetDataset(property.Code); IDescription description; if (DoesDescriptionExist(dataset)) { description = GetDescriptionByDataset(dataset); } else { description = new Description(dataset, logger); } description.AddOrReplaceProperty(property); AddOrReplaceDescription(description); }
/// /// <param name="oldValue">Module 2 property of the last saved value for a given signal code</param> /// <param name="newValue">Module 2 property of the new value for a given signal code</param> /// <param name="deadbandPercentage">Percentage deadband to be checked</param> public bool IsDeadbandSatisfied(IModule2Property oldValue, IModule2Property newValue, double deadbandPercentage) { logger.LogNewInfo(string.Format("Checking deadband for old value {0}, and new value {1}, with percentage {2}", oldValue.Value, newValue.Value, deadbandPercentage)); double difference = Math.Abs(newValue.Value - oldValue.Value); double percentageDifference = (difference / oldValue.Value) * 100; if (percentageDifference > deadbandPercentage || oldValue.Code == SignalCode.CODE_DIGITAL) { logger.LogNewInfo("Deadband satisfied"); return true; } else { logger.LogNewInfo("Deadband not satisfied"); return false; } }
/// /// <param name="signal">Signal code</param> /// <param name="value">Signal value</param> public IModule1Property PackToModule1Property(SignalCode signal, double value) { logger.LogNewInfo(string.Format("Packing signal {0} and value {1} to module 1 property", signal.ToString(), value)); return(new Module1Property(signal, value)); }
/// /// <param name="signal">Signal code</param> /// <param name="value">Signal value</param> public IModule2Property PackToModule2Property(SignalCode signal, double value) { logger.LogNewInfo(string.Format("Packing module 2 property for signal {0} and value {1}", signal, value)); return(new Module2Property(signal, value)); }