Esempio n. 1
0
 /// <summary>
 /// Saves the property in DB
 /// </summary>
 /// <param name='smartProperty'>SmartProperty</param>
 /// <param name='valueType'>Value type</param>
 public override void SaveProperty(ISmartPropertyCommon smartProperty, Type valueType)
 {
     try
     {
         lock (_dbLock)
         {
             bool wasOpened = false;
             if (_connection.State != ConnectionState.Open)
             {
                 _connection.Open();
                 wasOpened = true;
             }
             string table = GetTableByValueType(valueType);
             _propertyCommand.Parameters["subsystem"].Value = smartProperty.SubSystem;
             _propertyCommand.Parameters["key"].Value = smartProperty.Key;
             _propertyCommand.Parameters["description"].Value = smartProperty.Description;
             _propertyCommand.Parameters["units"].Value = smartProperty.Unit;
             _propertyCommand.Parameters["val_table"].Value = table;
             _propertyCommand.ExecuteNonQuery();
             _propertyIdCommand.Parameters["subsystem"].Value = smartProperty.SubSystem;
             _propertyIdCommand.Parameters["key"].Value = smartProperty.Key;
             object id = _propertyIdCommand.ExecuteScalar();
             smartProperty.IdDB = (id != null ? id.ToString() : "");
             smartProperty.TableDB = table;
             if (wasOpened)
                 _connection.Close();
         }
     }
     catch (Exception ex)
     {
         LogMessage(this, new LogMessageEventArgs(ex.Message, ex, LogLevel.Error));
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Saves the value in DB
        /// </summary>
        /// <param name='smartProperty'>SmartProperty</param>
        /// <param name='value'>Value</param>
        /// <param name='previousValue'>Previous value</param>
        public override void SaveValue(ISmartPropertyCommon smartProperty, string value, string previousValue)
        {
            try
            {
                lock (_dbLock)
                {
                    if (value != previousValue)
                    {
                        if (_connection.State != ConnectionState.Open && _connection.State != ConnectionState.Executing)
                            _connection.Open();
                        switch (smartProperty.TableDB)
                        {
                            case "val_number":
                                double doubleVal;
                                bool boolVal;
                                if (double.TryParse(value, out doubleVal))
                                {
                                    _valueNumberCommand.Parameters["id"].Value = smartProperty.IdDB;
                                    _valueNumberCommand.Parameters["value"].Value = doubleVal;
                                    _valueNumberCommand.ExecuteNonQuery();
                                }
                                else if (bool.TryParse(value, out boolVal))
                                {
                                    _valueNumberCommand.Parameters["id"].Value = smartProperty.IdDB;
                                    _valueNumberCommand.Parameters["value"].Value = Convert.ToDouble(boolVal);
                                    _valueNumberCommand.ExecuteNonQuery();
                                }
                                break;

                            case "val_string":
                                _valueStringCommand.Parameters["id"].Value = smartProperty.IdDB;
                                _valueStringCommand.Parameters["value"].Value = value;
                                _valueStringCommand.ExecuteNonQuery();
                                break;

                            default:
                                break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogMessage(this, new LogMessageEventArgs(ex.Message, ex, LogLevel.Error));
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Get values from DB
        /// </summary>
        /// <param name='smartProperty'>Smart property</param>
        public override DataTable GetValues(ISmartPropertyCommon smartProperty)
        {
            DataTable table = new DataTable();
            try
            {
                lock (_dbLock)
                {
                    bool wasOpened = false;
                    if (_connection.State != ConnectionState.Open && _connection.State != ConnectionState.Executing)
                    {
                        _connection.Open();
                        wasOpened = true;
                    }

                    if (DateTime.Now.AddHours(-1) > _lastDelete)
                    {
                        _sw.Restart();
                        _deleteNumberCommand.ExecuteNonQuery(); //TODO make it faster
                        _sw.Stop();
                        LogMessage(this, new LogMessageEventArgs("1) " + _sw.ElapsedMilliseconds.ToString(), null, LogLevel.Info));

                        _sw.Restart();
                        _deleteStringCommand.ExecuteNonQuery(); //TODO make it faster
                        _sw.Stop();
                        LogMessage(this, new LogMessageEventArgs("2) " + _sw.ElapsedMilliseconds.ToString(), null, LogLevel.Info));
                        _lastDelete = DateTime.Now;
                    }

                    IDataReader reader;
                    switch (smartProperty.TableDB)
                    {
                        case "val_number":
                            _sw.Restart();
                            _historyNumberCommand.Parameters["id_in_properties"].Value = smartProperty.IdDB;
                            reader = _historyNumberCommand.ExecuteReader();
                            table.Load(reader);                     //TODO make it faster
                            _sw.Stop();
                            LogMessage(this, new LogMessageEventArgs("3) " + _sw.ElapsedMilliseconds.ToString(), null, LogLevel.Info));
                            break;

                        case "val_string":
                            _sw.Restart();
                            _historyStringCommand.Parameters["id_in_properties"].Value = smartProperty.IdDB;
                            reader = _historyStringCommand.ExecuteReader();
                            table.Load(reader);                     //TODO make it faster
                            _sw.Stop();
                            LogMessage(this, new LogMessageEventArgs("3) " + _sw.ElapsedMilliseconds.ToString(), null, LogLevel.Info));
                            break;

                        default:
                            break;
                    }
                    if (wasOpened)
                        _connection.Close();
                }
            }
            catch (Exception ex)
            {
                LogMessage(this, new LogMessageEventArgs(ex.Message, ex, LogLevel.Error));
            }
            return table;
        }