private string processRequest <T>(DAO <T> dao, string[] keys, string body, string method) where T : class { string json = null; bool success; switch (method) { case "GET": if (keys.Length == 1) { List <T> objectList = dao.get(); json = JsonConvert.SerializeObject(objectList); } else { if (dao is ComputadorDAO) { string nome = keys[1]; ComputadorDAO computadorDao = new ComputadorDAO(); Computador computador = computadorDao.getByNome(nome); json = JsonConvert.SerializeObject(computador); } } break; case "POST": dynamic data = (T)JsonConvert.DeserializeObject <T>(body); if (dao is ComputadorDAO) { string nome = data.name; ComputadorDAO computadorDao = new ComputadorDAO(); Computador computador = computadorDao.getByNome(nome); if (computador != null) { computador.cpus = data.cpus; computador.gpus = data.gpus; computador.storages = data.storages; computador.ram = data.ram; success = dao.update((dynamic)computador); } else { success = dao.add(data); } } json = "{\"status\":\"success\"}"; break; default: break; } return(json); }
private string processRequest(string[] keys, string body, string method) { string json = null; ComputadorDAO dao = new ComputadorDAO(); switch (method) { case "GET": dynamic readingsObj = JsonConvert.DeserializeObject(readings[keys[1]].ToString()); switch (keys[2]) { case "cpu": int cpuId = Int32.Parse(keys[3]); dynamic cpuObj = readingsObj.CPU[cpuId].Sensors; json = JsonConvert.SerializeObject(new { load = cpuObj.Load["CPU Total"].Value, temperature = cpuObj.Temperature.Average, clock = cpuObj.Clock.Average, power = cpuObj.Power["CPU Package"].Value, }); break; case "gpu": int gpuId = Int32.Parse(keys[3]); dynamic gpuObj = readingsObj.GpuNvidia[gpuId].Sensors; json = JsonConvert.SerializeObject(new { load = gpuObj.Load.GPUCore.Value, memoryload = gpuObj.Load.GPUMemory.Value, temperature = gpuObj.Temperature.GPUCore.Value, coreclock = gpuObj.Clock.GPUCore.Value, memoryclock = gpuObj.Clock.GPUMemory.Value, }); break; case "ram": dynamic ramObj = readingsObj.RAM[0].Sensors; json = JsonConvert.SerializeObject(new { load = ramObj.Load.Memory.Value, used = ramObj.Data["Used Memory"].Value, free = ramObj.Data["Available Memory"].Value, }); break; case "hdd": List <dynamic> hddObj = JsonConvert.DeserializeObject <List <dynamic> >(readingsObj.HDD.ToString()); List <dynamic> storages = new List <dynamic>(); for (int i = 0; i < hddObj.Count; i++) { storages.Add(new { load = hddObj[i].Sensors.Load["Used Space"].Value, read = hddObj[i].Sensors.Data["Host Reads"] == null ? -1 : hddObj[i].Sensors.Data["Host Reads"].Value, write = hddObj[i].Sensors.Data["Host Writes"] == null ? -1 : hddObj[i].Sensors.Data["Host Writes"].Value, }); } json = JsonConvert.SerializeObject(storages); break; } break; case "POST": dynamic data = JsonConvert.DeserializeObject(body); Computador computador = dao.getByNome(String.Format("{0}", data.Name)); if (computador == null) { return("{\"status\":\"error\"}"); } if (!readings.ContainsKey(computador.name)) { readings.Add(computador.name, data.Hardware); } else { readings[computador.name] = data.Hardware; } json = "{\"status\":\"success\"}"; break; default: break; } return(json); }