private static void SyncIntervalTimeLogs() { _computer = JsonLocalDatabase.Instance.Computer; //var users = _computer.ComputerUsers? // .Where(u => u.Synced && u.Enabled && u.ComputerUserId > 0).ToList(); var intervals = _computer.LogTimeIntervals.Where(l => !l.Synced).ToList(); if (intervals.Count == 0) { return; } var intervalsResource = intervals.Select(i => i.ToResource(_computer.ComputerId, i.ComputerUser)); //LocalLogger.Log(intervalsResource); var client = new ServerClient(); var response = client.PostToServer(Settings.ApiUrl, "api/LogTimeIntervals", intervalsResource, _jsonWebToken); var returnLogs = response.Content.ReadAsAsync <List <LogTimeIntervalResource> >().Result.ToList(); //LocalLogger.Log(returnLogs); foreach (var log in returnLogs) { var interval = intervals.FirstOrDefault(i => i.StartTime == log.StartTime); if (interval != null) { interval.LogTimeIntervalId = log.LogTimeIntervalId; interval.Synced = true; } } JsonLocalDatabase.Instance.Computer = _computer; }
private static void SyncUsers() { _computer = JsonLocalDatabase.Instance.Computer; var users = _computer.ComputerUsers? .Where(u => !u.Synced && u.Enabled) .Select(u => u.ToResource(_computer.ComputerId)).ToList(); if (users == null || users.Count == 0) { return; } LocalLogger.Log($"Syncing {users.Count} {(users.Count > 1 ? "users" : "user")}."); var client = new ServerClient(); var response = client.PostToServer(Settings.ApiUrl, "api/ComputerUsers", users, _jsonWebToken); if (!response.IsSuccessStatusCode) { return; } //LocalLogger.Log(response); var returnUsers = response.Content.ReadAsAsync <List <ComputerUserResource> >().Result.Select(d => d.ToLocal()).ToList(); foreach (var user in returnUsers) { var oldUser = _computer.ComputerUsers.FirstOrDefault(u => u.Name == user.Name); if (oldUser == null) { continue; } oldUser.Synced = true; oldUser.ComputerUserId = user.ComputerUserId; } JsonLocalDatabase.Instance.Computer = _computer; }
public static ComputerResourceModel ToResourceModel(this ComputerLocal computer) { return(new ComputerResourceModel() { Name = computer.Name, Cpu = computer.Cpu, Gpu = computer.Gpu, Ram = computer.Ram, OperatingSystem = computer.OperatingSystem, MacAddress = computer.MacAddress, ComputerId = computer.ComputerId }); }
private static void SyncBlockedSites() { _computer = JsonLocalDatabase.Instance.Computer; var client = new ServerClient(); var response = client.GetFromServer(Settings.ApiUrl, $"api/BlockedSites/{_computer.ComputerId}", _jsonWebToken); var returnSites = response.Content.ReadAsAsync <List <BlockedSiteResource> >().Result.ToList(); foreach (var user in _computer.ComputerUsers) { user.BlockedSites.Clear(); var userSites = returnSites.Where(rs => rs.ComputerUserId == user.ComputerUserId) .Select(s => s.ToLocal()); user.BlockedSites.AddRange(userSites); } JsonLocalDatabase.Instance.Computer = _computer; }
public static ComputerLocal GetComputer() { var computer = new ComputerLocal() { Cpu = GetCpuName().Replace("(tm)", "™").Replace("(R)", "®").Replace("(TM)", "™").Replace("(C)", "©"), Gpu = GetGpuName(), Name = Environment.MachineName, Ram = GetTotalRamInGigaBytes(), OperatingSystem = GetWindowsName(), MacAddress = GetMacAddress(), Disks = GetDisks(), ComputerUsers = ComputerUsersHelper.GetComputerUsers(), LogTimeIntervals = new List <LogTimeIntervalLocal>() }; return(computer); }
private static void GenerateDisks(ComputerLocal computer) { var disksAmount = Settings.MaxAmountOfDisks.Random(1); var diskLetters = GenerateUniqueLetters(disksAmount).ToArray(); for (var i = 0; i < disksAmount; i++) { var totalSpace = 2048.0.Random(10.0); var disk = new DiskLocal() { TotalSpaceInGigaBytes = totalSpace, FreeSpaceInGigaBytes = totalSpace.Random(0.5), Letter = $"{char.ToUpper(diskLetters.ElementAt(i))}:\\", Name = Samples.DiskNames.Random() }; computer.Disks.Add(disk); } }
public static ComputerLocal GenerateComputer() { var computer = new ComputerLocal() { Name = $"{Samples.Manufacturers.Random()} - PC", Cpu = Samples.Cpus.Random(), Gpu = Samples.Gpus.Random(), Ram = Math.Pow(2, 6.Random()), OperatingSystem = $"Microsoft Windows {Samples.OperatingSystems.Random()}", ComputerUsers = new List <ComputerUserLocal>(), LogTimeIntervals = new List <LogTimeIntervalLocal>(), Disks = new List <DiskLocal>() }; GenerateDisks(computer); GenerateComputerUsers(computer); GenerateLogTimeIntervals(computer); return(computer); }
private static void GenerateComputerUsers(ComputerLocal computer) { var userAmount = Settings.MaxAmountOfComputerUsers.Random(5); var names = GenerateUniqueUserNames(userAmount); for (var i = 0; i < userAmount; i++) { var computerUser = new ComputerUserLocal() { Name = names.ElementAtOrDefault(i), FullName = 2.Random() == 0 ? names.ElementAtOrDefault(i) : "", PrivilegeLevel = i == 0 ? "Administrator" : (3.Random() == 0 ? "Administrator" : "Standard user"), BlockedApps = new List <BlockedAppLocal>(), BlockedSites = new List <BlockedSiteLocal>() }; GenerateBlockedApps(computerUser); GenerateBlockedSites(computerUser); computer.ComputerUsers.Add(computerUser); } }
public static bool NeedsUpdate(this ComputerLocal oldComputer, ComputerLocal newComputer) { if (oldComputer == null) { return(true); } if (newComputer == null) { return(false); } var isDifferent = !(oldComputer.Name == newComputer.Name && oldComputer.Cpu == newComputer.Cpu && oldComputer.Gpu == newComputer.Gpu && Math.Abs(oldComputer.Ram - newComputer.Ram) < 0.10 && oldComputer.OperatingSystem == newComputer.OperatingSystem && oldComputer.MacAddress == newComputer.MacAddress); return(isDifferent); }
private static void GenerateLogTimeIntervals(ComputerLocal computer) { var now = DateTime.Now; computer.ComputerUsers.Add(null); var dateTime = new DateTime(now.Year, now.Month, now.Day, 2.Random(), 60.Random(), 0); for (var i = 0; i < Settings.MaxAmountOfLogTimeIntervals.Random(6); i++) { var timeSpan = new TimeSpan(2.Random(), 57.Random(), 0); var log = new LogTimeIntervalLocal() { ComputerUser = computer.ComputerUsers.Random().Name, StartTime = dateTime, Duration = timeSpan, State = computer.ComputerUsers.Random() == null ? "Idle" : (2.Random() == 0 ? "Idle" : "Active"), }; computer.LogTimeIntervals.Add(log); computer.ComputerUsers.RemoveAll(u => u == null); } }
private static void SyncComputer() { _computer = JsonLocalDatabase.Instance.Computer; if (_computer.Synced) { return; } var client = new ServerClient(); var response = client.PostToServer(Settings.ApiUrl, "api/Computers", _computer.ToResourceModel(), _jsonWebToken); if (!response.IsSuccessStatusCode) { LocalLogger.Log(response.Content.ReadAsStringAsync()); return; } var computerId = response.Content.ReadAsAsync <int>().Result; _computer.Synced = true; _computer.ComputerId = computerId; JsonLocalDatabase.Instance.Computer = _computer; //LocalLogger.Log($"ComputerId: {computerId}"); }
private static void SyncDisks() { _computer = JsonLocalDatabase.Instance.Computer; //var disks = _computer.Disks.Where(d => !d.Synced).Select(d => d.ToResourceModel(_computer.ComputerId)).ToList(); var disks = _computer.Disks; if (disks.All(d => d.Synced)) { return; } LocalLogger.Log($"Syncing {disks.Count}."); var client = new ServerClient(); var response = client.PostToServer(Settings.ApiUrl, "api/Disks", disks.Select(d => d.ToResourceModel(_computer.ComputerId)).ToList(), _jsonWebToken); //LocalLogger.Log(response); if (!response.IsSuccessStatusCode) { return; } var returnDisks = response.Content.ReadAsAsync <List <DiskResource> >().Result.Select(d => d.ToLocal()).ToList(); _computer.Disks = returnDisks; JsonLocalDatabase.Instance.Computer = _computer; }
private static void SyncBlockedApps() { _computer = JsonLocalDatabase.Instance.Computer; var users = _computer.ComputerUsers? .Where(u => u.Synced && u.Enabled && u.ComputerUserId > 0 && u.BlockedApps.Any(a => !a.Synced)).ToList(); var client = new ServerClient(); if (users != null && users.Count > 0) { var apps = new List <BlockedAppLocal>(); var appsResource = new List <BlockedAppResource>(); foreach (var user in users) { apps.AddRange(user.BlockedApps.Where(a => !a.Synced)); appsResource.AddRange(user.BlockedApps.Where(a => !a.Synced).Select(a => a.ToResource(user.ComputerUserId))); } if (apps.Count == 0) { return; } LocalLogger.Log($"Syncing {apps.Count} {(apps.Count == 1 ? "app" : "apps")}."); var response = client.PostToServer(Settings.ApiUrl, $"api/BlockedApps/{_computer.ComputerId}", appsResource, _jsonWebToken); var returnApps = response.Content.ReadAsAsync <List <BlockedAppResource> >().Result.ToList(); if (!response.IsSuccessStatusCode) { return; } foreach (var user in users) { if (user.BlockedApps == null) { continue; } foreach (var app in user.BlockedApps) { var tempApp = returnApps.FirstOrDefault(a => a.Path == app.Path && a.ComputerUserId == user.ComputerUserId); if (tempApp == null) { continue; } app.BlockedAppId = tempApp.BlockedAppId; app.Synced = true; } } } var syncResponse = client.GetFromServer(Settings.ApiUrl, $"api/BlockedApps/{_computer.ComputerId}", _jsonWebToken); var allLocalApps = new List <BlockedAppLocal>(); if (_computer.ComputerUsers != null) { foreach (var user in _computer.ComputerUsers) { allLocalApps.AddRange(user.BlockedApps.Where(a => a.Synced)); } } var allApps = syncResponse.Content.ReadAsAsync <List <BlockedAppResource> >().Result.ToList(); //LocalLogger.Log(allApps); var removedAppsIds = allLocalApps.Select(a => a.BlockedAppId).ToList(); foreach (var app in allApps) { var user = _computer.ComputerUsers?.FirstOrDefault(u => u.ComputerUserId == app.ComputerUserId); if (user == null) { continue; } if (user.BlockedApps.All(a => a.Path != app.Path)) { user.BlockedApps.Add(new BlockedAppLocal { Active = app.Active, BlockedAppId = app.BlockedAppId, Name = app.Name, Path = app.Path, Synced = true }); } else { var oldApp = user.BlockedApps.FirstOrDefault(a => a.Path == app.Path); if (oldApp == null) { continue; } oldApp.Name = app.Name; oldApp.Path = app.Path; oldApp.Active = app.Active; oldApp.Synced = true; oldApp.BlockedAppId = app.BlockedAppId; } removedAppsIds.RemoveAll(i => i == app.BlockedAppId); } // Removes locally all blocked apps removed via web app. if (_computer.ComputerUsers != null) { foreach (var user in _computer.ComputerUsers) { user.BlockedApps.RemoveAll(a => removedAppsIds.Contains(a.BlockedAppId)); } } //LocalLogger.Log(returnApps); JsonLocalDatabase.Instance.Computer = _computer; }