public void AddEntryToDatabase(TrackedWindow trackedWindow) { using (var connection = new SQLiteConnection(DatabaseSource)) { // Create a database command using (var command = new SQLiteCommand(connection)) { connection.Open(); // Create the table command.CommandText = CreateTableQuery; command.ExecuteNonQuery(); // Insert entries in database table command.CommandText = "INSERT INTO AppTimeLogs (User,Application,TimeSpent,InsertedDate) " + " VALUES ('Detty', @ExcelProcess, @TimeElapsed, date('now'))"; command.Parameters.Add(new SQLiteParameter("ExcelProcess", trackedWindow.Name)); command.Parameters.Add(new SQLiteParameter("TimeElapsed", trackedWindow.TimeSpent)); command.ExecuteNonQuery(); connection.Close(); // Close the connection to the database } } }
public void send(TrackedWindow window) { if (lastWindow != null) { var update = new RestRequest("updateactivity/0", Method.PUT); fillRequest(lastWindow, update, window.StartTime, lastId); var updateResponse = client.Execute(update); if (!updateResponse.IsSuccessful) { Console.WriteLine("UpdateError: " + updateResponse.Content); } } var request = new RestRequest("activity", Method.POST); fillRequest(window, request, null, null); var restResponse = client.Execute(request); if (restResponse.IsSuccessful) { var responseJson = SimpleJson.DeserializeObject(restResponse.Content) as IDictionary <string, object>; if (responseJson != null) { lastId = "" + responseJson["id"]; lastWindow = window; } } else { Console.WriteLine("ERROR!!"); } }
private void EventCallback(IntPtr hWinEventHook, uint iEvent, IntPtr hWnd, int idObject, int idChild, int dwEventThread, int dwmsEventTime) { if (ExcelProcess != null) { Console.WriteLine("Process stopped: " + ExcelProcess.MainWindowTitle + " At: " + DateTime.Now); sw.Stop(); var cleanedWindowTitle = ExcelProcess.MainWindowTitle; if (cleanedWindowTitle != "") { cleanedWindowTitle = cleanedWindowTitle.Replace("Microsoft Excel - ", ""); cleanedWindowTitle = cleanedWindowTitle.Replace(" - Excel", ""); } else { cleanedWindowTitle = ExcelProcess.ProcessName + " Started at: " + ExcelProcess.StartTime; } TrackedWindow trackedWindow = new TrackedWindow() { Name = cleanedWindowTitle, TimeSpent = (int)(DateTime.Now - started).TotalSeconds }; if (!trackedWindow.Name.Contains("apps_usage")) { _storage.AddEntryToDatabase(trackedWindow); bool found = false; foreach (var item in listOfVisitedWindows) { if (item.Name == trackedWindow.Name) { Console.WriteLine("Adding time: " + ExcelProcess.MainWindowTitle + " time: " + trackedWindow.TimeSpent); listOfVisitedWindows.Find(x => x.Name == item.Name).TimeSpent += trackedWindow.TimeSpent; found = true; break; } } if (!found) { Console.WriteLine("Process not found: " + ExcelProcess.MainWindowTitle); listOfVisitedWindows.Add(trackedWindow); } lbList.ItemsSource = listOfVisitedWindows; lbList.Items.Refresh(); } } GetForegroundProcessName(); }
private void fillRequest(TrackedWindow window, RestRequest request, DateTime?endtime, string id) { request.RequestFormat = DataFormat.Json; var o = new { id = id, userid = userid, deviceid = deviceid, ostype = "WINDOWS", starttime = window.StartTime, endtime = endtime, exename = window.AppName, windowtitle = window.WindowTitle }; Console.WriteLine("Sending: " + o); request.AddJsonBody(o); }