public static void SyncData() { LocalLogger.Log($"Method {nameof(SyncData)} is running"); HubService.LogOnPage("Syncing data started.."); try { _jsonWebToken = Credentials.GetJsonWebToken(); if (string.IsNullOrWhiteSpace(_jsonWebToken)) { LocalLogger.Log($"[{nameof(SyncData)}] no web api authentication"); } SyncComputer(); SyncDisks(); SyncUsers(); SyncIntervalTimeLogs(); SyncBlockedApps(); SyncBlockedSites(); SyncTimeLimits(); } catch (Exception e) { LocalLogger.Log($"{nameof(SyncManager)} > {nameof(Credentials.GetJsonWebToken)}", e); } LocalLogger.Log($"Method {nameof(SyncData)} completed"); HubService.LogOnPage("Syncing done!"); }
public static void UpdateComputer() { LocalLogger.Log($"Method {nameof(UpdateComputer)} is running"); HubService.LogOnPage("Updating computer data"); var newComputer = ComputerHelper.GetComputer(); try { var db = JsonLocalDatabase.Instance; var computer = db.Computer; var needsUpdate = computer.NeedsUpdate(newComputer); if (!needsUpdate) { return; } computer.Name = newComputer.Name; computer.Cpu = newComputer.Cpu; computer.Gpu = newComputer.Gpu; computer.Ram = newComputer.Ram; computer.MacAddress = newComputer.MacAddress; computer.OperatingSystem = newComputer.OperatingSystem; computer.Synced = false; db.Computer = computer; } catch (Exception e) { LocalLogger.Log("UpdateComputer", e); } }
public static void Run() { LocalLogger.Log($"{nameof(TimeLimitsManager)} is running"); try { var computer = JsonLocalDatabase.Instance.Computer; var userName = SessionManager.GetActive(); if (string.IsNullOrWhiteSpace(userName)) { LocalLogger.Log($"User {userName} not found"); return; } var curerentUser = computer.ComputerUsers.FirstOrDefault(u => u.Name == userName); var limit = curerentUser?.DailyTimeLimits?.FirstOrDefault(l => l.Active); if (limit == null) { LocalLogger.Log($"Time limit for user {userName} not found"); return; } var totalTime = new TimeSpan(); var today = DateTime.Today; var logTimeIntervals = computer.LogTimeIntervals .Where(l => l.ComputerUser == userName && l.StartTime.Date == today); totalTime = logTimeIntervals.Aggregate(totalTime, (duration, log) => duration + log.Duration); var timeLeft = limit.TimeAmount - totalTime; LocalLogger.Log($"User {userName} has limit set to {limit.TimeAmount}, Time used: {totalTime} Time left: {timeLeft}"); HubService.LogOnPage($"Current user {userName} has limit set to {limit.TimeAmount}, Time used: {totalTime} Time left: {timeLeft}"); if (timeLeft.Hours == 0 && timeLeft.Minutes == 30) { TextToSpeechHelper.Speak($"Because of the time limit set to {limit.TimeAmount.Hours} hours and {limit.TimeAmount.Minutes} minutes, You have {(int)timeLeft.TotalMinutes} {(timeLeft.Minutes == 1 ? "minute" : "minutes")} left of using the computer today!"); } var reminederMinutes = new[] { 20, 10, 5, 2, 1 }; if (timeLeft.Hours == 0 && reminederMinutes.Contains(timeLeft.Minutes)) { TextToSpeechHelper.Speak($"You have {timeLeft.Minutes} {(timeLeft.Minutes == 1 ? "minute":"minutes")} left!"); } if (!(timeLeft.TotalSeconds < 0)) { return; } LocalLogger.Log($"Logging off user {userName}"); TextToSpeechHelper.Speak("End of time, logging off!"); HubService.LogOnPage($"Logging off.."); SessionManager.LogOutCurrentUser(); HubService.LogOnPage($"User {userName} has been logged of"); } catch (Exception e) { LocalLogger.Log(nameof(TimeLimitsManager), e); } }
public static void UpdateUsers() { LocalLogger.Log($"Method {nameof(UpdateUsers)} is running"); HubService.LogOnPage("Updating users"); try { var newComputerUsers = ComputerUsersHelper.GetComputerUsers(); var db = JsonLocalDatabase.Instance; var computer = db.Computer; var equal = db.Computer.ComputerUsers != null && newComputerUsers.All(computer.ComputerUsers.Contains); if (equal && db.Computer.ComputerUsers.Count == newComputerUsers.Count) { return; } if (computer.ComputerUsers == null) { computer.ComputerUsers = newComputerUsers; } foreach (var newComputerUser in newComputerUsers) { var user = computer.ComputerUsers.FirstOrDefault(u => u.Name == newComputerUser.Name); if (user == null) { computer.ComputerUsers.Add(newComputerUser); } else { var userChanged = user.FullName != newComputerUser.FullName || user.PrivilegeLevel != newComputerUser.PrivilegeLevel; if (userChanged) { user.Synced = false; user.FullName = newComputerUser.FullName; user.PrivilegeLevel = newComputerUser.PrivilegeLevel; } } } var removedComputerUsers = computer.ComputerUsers.Except(newComputerUsers).ToList(); removedComputerUsers.ForEach(u => { var removedComputerUser = computer.ComputerUsers.FirstOrDefault(cu => cu.Name == u.Name); if (removedComputerUser != null) { removedComputerUser.Removed = true; } }); db.Computer = computer; } catch (Exception e) { LocalLogger.Log("UpdateUsers", e); } }
public static void Stop() { LocalLogger.Log("Stopping service"); HubService.LogOnPage("Stopping service!"); Scheduler.Stop(); ActionsManager.Stop(); HubService.Stop(); SessionManager.KillIdleTimeCounters(); SitesBlocker.ClearHostsFile(); LocalLogger.Log("Service has been stopped!"); }
public static void UpdateDisks() { LocalLogger.Log($"Method {nameof(UpdateDisks)} is running"); HubService.LogOnPage("Updating disks data"); var newDisks = ComputerHelper.GetDisks(); try { var db = JsonLocalDatabase.Instance; var computer = db.Computer; if (computer.Disks == null) { computer.Disks = newDisks; } foreach (var newDisk in newDisks) { var disk = computer.Disks.FirstOrDefault(d => d.Letter == newDisk.Letter); if (disk == null) { computer.Disks.Add(newDisk); } else { var diskChanged = disk.Name != newDisk.Name || Math.Abs(disk.TotalSpaceInGigaBytes - newDisk.TotalSpaceInGigaBytes) > 0.05 || Math.Abs(disk.FreeSpaceInGigaBytes - newDisk.FreeSpaceInGigaBytes) > 0.05; // Tolerance up to 50 MB. if (diskChanged) { disk.Name = newDisk.Name; disk.FreeSpaceInGigaBytes = newDisk.FreeSpaceInGigaBytes; disk.TotalSpaceInGigaBytes = newDisk.TotalSpaceInGigaBytes; disk.Synced = false; } } } var removedDisks = computer.Disks.Except(newDisks).ToList(); removedDisks.ForEach(rd => computer.Disks.Remove(rd)); if (removedDisks.Any() && computer.Disks.FirstOrDefault() != null) { computer.Disks.FirstOrDefault().Synced = false; } db.Computer = computer; } catch (Exception e) { LocalLogger.Log("UpdateDisks", e); } }
public static bool KillProcessesById(int processId) { try { var process = Process.GetProcessById(processId); process.Kill(); HubService.LogOnPage($"Killed process: {process.ProcessName} : {process.Id}"); return(false); } catch (Exception e) { LocalLogger.Log(nameof(KillProcessesByName), e); return(true); } }
public static void StartAppKiller() { LocalLogger.Log($"Method {nameof(StartAppKiller)} is running"); //if (!SessionManager.Unlocked) return; try { var user = JsonLocalDatabase.Instance.Computer.ComputerUsers? .FirstOrDefault(u => u.Name == ComputerUsersHelper.CurrentlyLoggedInUser()); if (user?.BlockedApps == null || user.BlockedApps.Count == 0) { return; } var processes = ArktinMonitor.Helpers.Processes.GetProcesses(); var count = 0; foreach (var process in processes) { if (user.BlockedApps.Where(a => a.Active).All(app => app.Path != process.Path)) { continue; } try { Process.GetProcessById(process.ProcessId).Kill(); LocalLogger.Log($"App {process.Path} with PID {process.ProcessId} has been closed!"); count++; } catch (Exception e) { LocalLogger.Log("AppBlocker", e); } } if (count > 0) { HubService.LogOnPage($"Killed {count} apps!"); } } catch (Exception e) { LocalLogger.Log("AppBlocker", e); } }
public static int KillProcessesByName(string name) { var processes = ArktinMonitor.Helpers.Processes.GetProcesses(name); var basicProcesses = processes as Processes.BasicProcess[] ?? processes.ToArray(); foreach (var process in basicProcesses) { try { // LocalLogger.Log($"Killing process: {process.Name} : {process.ProcessId}"); Process.GetProcessById(process.ProcessId).Kill(); HubService.LogOnPage($"Killed process: {process.Name} : {process.ProcessId}"); } catch (Exception e) { LocalLogger.Log(nameof(KillProcessesByName), e); } } return(basicProcesses.Count()); }
public static void SendMessageToCurrentUser(string text) { CallAppInUsersSession($"message \"{text}\"");; HubService.LogOnPage($"Text message received: \"{text}\""); }