Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Http;
- using System.Text;
- using System.Threading.Tasks;
- using TMD.LocationServiceWRS.Models;
- namespace TMD.LocationServiceWRS.Service
- {
- public class WatchdogApiClient
- {
- private static readonly HttpClient client = new HttpClient();
- //public async void PostWatchdogData(ServerStateDto toSend)
- public async void PostWatchdogData(List<ServerStateDto>toSend)
- {
- //to actually send the data
- WatchdogDTO toActuallySend = new WatchdogDTO();
- {
- toActuallySend.id = string.Join(",", toSend.Select(x => x.id).ToArray());
- toActuallySend.CameraStatus = string.Join(",", toSend.Select(a => a.CameraStatus).ToArray());
- toActuallySend.Status = string.Join(",", toSend.Select(b => b.Status).ToArray());
- };
- //Serialize data to bytestream
- string serializedWatchDogData = JsonConvert.SerializeObject(toActuallySend);
- if (string.IsNullOrEmpty(serializedWatchDogData))
- {
- return;
- }
- try
- {
- var response = await client.PostAsync("http://localhost:53161/api/Values", new StringContent(serializedWatchDogData, Encoding.UTF8, "application/json"));
- }
- catch (Exception ex)
- {
- Console.WriteLine($"Encountered an exception during PostAsync in method PostWatchDogData: {ex.Message}");
- throw ex;
- }
- }
- //Convert to boolean -------- help
- WatchdogApiClient a = new WatchdogApiClient;
- a.ToBoolean(WatchdogDTO.toActuallySend.Status);
- public static bool? ToBoolean(ServerStateDto o)
- {
- if (o == null)
- return null;
- var b = o as bool?;
- if (b != null)
- return b;
- var s = o.ToString();
- s = s.Trim();
- if (s.Length == 0)
- return null;
- if (string.Compare(s, bool.TrueString, StringComparison.OrdinalIgnoreCase) == 0)
- return true;
- if (string.Compare(s, bool.FalseString, StringComparison.OrdinalIgnoreCase) == 0)
- return false;
- int i;
- return (int.TryParse(s, out i) ? (bool?)(i != 0) : null);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement