public void AddCapture(IImage image, CaptureSource source, GateState state = GateState.Open, DateTime?timestamp = null) { if (!IsEnabled) { return; } var dateTime = timestamp ?? DateTime.UtcNow; DateTime lastTimestamp; if (_lastTimestamps.TryGetValue(source, out lastTimestamp) && (dateTime - lastTimestamp).TotalMilliseconds < minCaptureInterval) { return; } var capture = new CaptureEntry() { Timestamp = dateTime, GateState = state, Source = source, FilePath = SaveImage(image, source.ToString()), }; var command = _db.CreateCommand(); command.CommandText = string.Format( "INSERT INTO CaptureEntries (Timestamp, GateState, Source, FilePath) VALUES ('{0}Z', {1}, {2}, '{3}')", capture.Timestamp.ToString("yyyy-MM-dd HH:mm:ss.fffffff"), (int)capture.GateState, (int)capture.Source, capture.FilePath); try { lock (_dbLock) { command.ExecuteNonQuery(); } _lastTimestamps[source] = dateTime; } catch (Exception e) { TryDeleteImage(capture.FilePath); throw new CaptureStorageException("Failed to add a capture to DB", e); } finally { command.Dispose(); } }