public async Task <IHttpActionResult> PostPcLoadInfo(PcLoadInfo pcLoadInfo) { /* * if (!ModelState.IsValid) * { * return BadRequest(ModelState); * } * * db.PcLoadInfoes.Add(pcLoadInfo); * * try * { * await db.SaveChangesAsync(); * } * catch (DbUpdateException) * { * if (PcLoadInfoExists(pcLoadInfo.PcID)) * { * return Conflict(); * } * else * { * throw; * } * } * * return CreatedAtRoute("DefaultApi", new { id = pcLoadInfo.PcID }, pcLoadInfo); */ return(BadRequest()); }
public async Task <IHttpActionResult> GetPc(int id) { PcLoadInfo pli = await db.PcLoadInfoes.FindAsync(id); if (pli == null) { return(NotFound()); //IDK } PcLoadInfoM ret = (PcLoadInfoM)pli; return(Ok(ret)); }
PcLoadInfo GetLoadInfo() { PcLoadInfo ret = new PcLoadInfo(); //CpuByCores try { foreach (ManagementObject queryObj in searcher4.Get()) { ret.CpuLoadByCore.Add(int.Parse(queryObj["PercentProcessorTime"].ToString())); } } catch (ManagementException e) { SendLogs(e.ToString()); } // CPU% try { foreach (ManagementObject item in searcher6.Get()) { int percentCpu = int.Parse(item["LoadPercentage"].ToString()); ret.CpuLoad = percentCpu; } } catch (Exception e) { SendLogs(e.ToString()); } // %RAM% try { foreach (ManagementObject item in searcher5.Get()) { long free = long.Parse(item["FreePhysicalMemory"].ToString()); long total = long.Parse(item["TotalVisibleMemorySize"].ToString()); decimal temp = ((decimal)total - (decimal)free) / (decimal)total; ret.RamLoad = (int)(Math.Round(temp, 2) * 100); } } catch (ManagementException e) { SendLogs(e.ToString()); } return(ret); }
private void SendLoad() { PcLoadInfo pci = GetLoadInfo(); HttpMessage.MethodPut("api/PcLoadInfoes/" + pc.id, pci); }
public async Task <IHttpActionResult> PutPcLoadInfo(int id, PcLoadInfoM pcLoadInfo) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //if (id != pcLoadInfo.PcID) //{ // return BadRequest(); //} List <PcLoadInfo> pci = db.PcLoadInfoes.Where(x => x.PcID == id).ToList(); if (pci.Count > 0) { pci[0].CpuLoad = pcLoadInfo.CpuLoad; string clbc = ""; for (int i = 0; i < pcLoadInfo.CpuLoadByCore.Count; i++) { clbc += pcLoadInfo.CpuLoadByCore[i]; if (i + 1 < pcLoadInfo.CpuLoadByCore.Count) { clbc += "/"; } } pci[0].CpuLoadByCore = clbc; pci[0].RamLoad = pcLoadInfo.RamLoad; db.Entry(pci[0]).State = EntityState.Modified; } else { PcLoadInfo pci2 = new PcLoadInfo(); pci2.CpuLoad = pcLoadInfo.CpuLoad; string clbc = ""; for (int i = 0; i < pcLoadInfo.CpuLoadByCore.Count; i++) { clbc += pcLoadInfo.CpuLoadByCore[i]; if (i + 1 < pcLoadInfo.CpuLoadByCore.Count) { clbc += "/"; } } pci2.CpuLoadByCore = clbc; pci2.RamLoad = pcLoadInfo.RamLoad; pci2.PcID = id; db.PcLoadInfoes.Add(pci2); } try { await db.SaveChangesAsync(); return(StatusCode(HttpStatusCode.NoContent)); } catch (Exception) { return(StatusCode(HttpStatusCode.InternalServerError)); } }