Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. class ApiManager
  2. {
  3. HttpClient client = new HttpClient();
  4. public List<ServerStateDto> CollectWatchdogData()
  5. {
  6. client.BaseAddress = new Uri("https://ncleore435.net");
  7. HttpResponseMessage response = client.GetAsync("/api/serverheartbeats").Result;
  8. if (response.IsSuccessStatusCode)
  9. {
  10. string dto = response.Content.ReadAsStringAsync().Result;
  11. List<ServerStateDto> model = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ServerStateDto>>(dto);
  12. return model;
  13.  
  14.  
  15. //ServerStateDto model = Newtonsoft.Json.JsonConvert.DeserializeObject<ServerStateDto>(dto);
  16. //var model = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ServerStateDto>>(dto);
  17. }
  18. else
  19. {
  20. Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
  21. return null;
  22. }
  23. }
  24. }
  25.  
  26. public class WatchdogApiClient
  27. {
  28. private static readonly HttpClient client = new HttpClient();
  29. public async void PostWatchdogData(List<ServerStateDto> toSend)
  30. {
  31. //to actually send the data
  32. WatchdogDTO toActuallySend = new WatchdogDTO();
  33. {
  34. toActuallySend.id = string.Join(",", toSend.Select(x => x.id).ToArray());
  35. toActuallySend.CameraStatus = string.Join(",", toSend.Select(a => a.CameraStatus).ToArray());
  36. toActuallySend.Status = string.Join(",", toSend.Select(b => b.Status).ToArray());
  37. };
  38.  
  39. //Serialize data to bytestream
  40. string serializedWatchDogData = JsonConvert.SerializeObject(toActuallySend);
  41. if (string.IsNullOrEmpty(serializedWatchDogData))
  42. {
  43. return;
  44. }
  45. try
  46. {
  47. var response = await client.PostAsync("http://localhost:53161/api/Values", new StringContent(serializedWatchDogData, Encoding.UTF8, "application/json"));
  48. }
  49. catch (Exception ex)
  50. {
  51. Console.WriteLine($"Encountered an exception during PostAsync in method PostWatchDogData: {ex.Message}");
  52. throw ex;
  53. }
  54. }
  55. }
  56.  
  57.  
  58. public class ServerStateDto
  59. {
  60. public string id { get; set; }
  61. public string Name { get; set; }
  62. public string HostName { get; set; }
  63. public bool Status { get; set; }
  64. public bool hasCamera { get; set; }
  65. public string CameraStatus { get; set; }
  66. public DateTime LastSeen { get; set; }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement