Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net.Http;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using TMD.LocationServiceWRS.Models;
  9.  
  10. namespace TMD.LocationServiceWRS.Service
  11. {
  12. public class WatchdogApiClient
  13. {
  14. private static readonly HttpClient client = new HttpClient();
  15. //public async void PostWatchdogData(ServerStateDto toSend)
  16. public async void PostWatchdogData(List<ServerStateDto>toSend)
  17. {
  18. //to actually send the data
  19. WatchdogDTO toActuallySend = new WatchdogDTO();
  20. {
  21. toActuallySend.id = string.Join(",", toSend.Select(x => x.id).ToArray());
  22. toActuallySend.CameraStatus = string.Join(",", toSend.Select(a => a.CameraStatus).ToArray());
  23. toActuallySend.Status = string.Join(",", toSend.Select(b => b.Status).ToArray());
  24. };
  25.  
  26.  
  27. //Serialize data to bytestream
  28. string serializedWatchDogData = JsonConvert.SerializeObject(toActuallySend);
  29. if (string.IsNullOrEmpty(serializedWatchDogData))
  30. {
  31. return;
  32. }
  33.  
  34. try
  35. {
  36. var response = await client.PostAsync("http://localhost:53161/api/Values", new StringContent(serializedWatchDogData, Encoding.UTF8, "application/json"));
  37. }
  38. catch (Exception ex)
  39. {
  40. Console.WriteLine($"Encountered an exception during PostAsync in method PostWatchDogData: {ex.Message}");
  41. throw ex;
  42. }
  43. }
  44.  
  45. //Convert to boolean -------- help
  46. WatchdogApiClient a = new WatchdogApiClient;
  47. a.ToBoolean(WatchdogDTO.toActuallySend.Status);
  48.  
  49. public static bool? ToBoolean(ServerStateDto o)
  50. {
  51. if (o == null)
  52. return null;
  53.  
  54. var b = o as bool?;
  55. if (b != null)
  56. return b;
  57.  
  58. var s = o.ToString();
  59.  
  60. s = s.Trim();
  61.  
  62. if (s.Length == 0)
  63. return null;
  64.  
  65. if (string.Compare(s, bool.TrueString, StringComparison.OrdinalIgnoreCase) == 0)
  66. return true;
  67.  
  68. if (string.Compare(s, bool.FalseString, StringComparison.OrdinalIgnoreCase) == 0)
  69. return false;
  70.  
  71. int i;
  72. return (int.TryParse(s, out i) ? (bool?)(i != 0) : null);
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement