Example #1
0
        public static void Add(INodeItem item)
        {
            if (item is Sensor)
            {
                if (_sensors.Contains(item))
                    return;

                _sensors.Add(item as Sensor);
            }
        }
Example #2
0
        public void AddValue(INodeItem nodeItem)
        {
            if (!(nodeItem is Sensor))
                return;

            Sensor sensor = (Sensor)nodeItem;

            try
            {

                string query = "SELECT Id FROM Sensors WHERE Name = @name";

                SqlCommand cmd = new SqlCommand(query, _connection);
                cmd.Parameters.AddWithValue("name", sensor.Name);

                var sensorId = cmd.ExecuteScalar();

                if (sensorId == null)
                {
                    string insertQuery = "INSERT INTO Sensors (Name, Description, Type) VALUES (@name, @description, @type)";

                    SqlCommand insertCmd = new SqlCommand(insertQuery, _connection);
                    insertCmd.Parameters.AddWithValue("name", sensor.Name);
                    insertCmd.Parameters.AddWithValue("description", sensor.Description);
                    insertCmd.Parameters.AddWithValue("type", sensor.Type);

                    insertCmd.ExecuteNonQuery();
                    sensorId = cmd.ExecuteScalar();
                }

                string readingQuery = "INSERT INTO Readings (SensorId, Time, Value) VALUES (@sensorId, @time, @value)";

                SqlCommand readingCmd = new SqlCommand(readingQuery, _connection);
                readingCmd.Parameters.AddWithValue("sensorId", sensorId);
                readingCmd.Parameters.AddWithValue("time", sensor.Time);
                readingCmd.Parameters.AddWithValue("value", sensor.Value);

                readingCmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                string msg = string.Format("Unable to insert reading into database for sensor {0}.", sensor.Name);
                ErrorLogger.LogException(LogLevel.Error, msg, ex);

                return;
            }
        }