private static bool PingStationIDToUnregisteredTable(string route, string stationId) { try { using (var client = new HttpClient()) { client.BaseAddress = new Uri(hostUrl); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); //client.PostAsync(route,,,) var formatter = new JsonMediaTypeFormatter(); formatter.SerializerSettings = new JsonSerializerSettings { Formatting = Formatting.Indented, ContractResolver = new CamelCasePropertyNamesContractResolver() }; UnregisteredStation unregisteredStation = new UnregisteredStation(); unregisteredStation.Id = stationId; Task <HttpResponseMessage> responsePost = client.PostAsync(route, unregisteredStation, formatter); if (responsePost.Result.IsSuccessStatusCode) { HttpResponseMessage httpMsg = responsePost.Result; Console.WriteLine(httpMsg.StatusCode + ": Unregistered station pinged"); return(true); } else { Log("Error: Unable to ping unregistered station!"); return(false); } } } catch (TaskCanceledException e) { Log("A task canceled exception occurred: " + e.Message); return(false); } catch (Exception e) { Log("An unknown exception occurred in SetAirUDataPoint: " + e.Message); return(false); } }
public void AddThirdPartyDevice(UnregisteredStation station) { Station existingStation = db.Stations.Where(s => station.Id == s.Id).FirstOrDefault(); if (!ReferenceEquals(existingStation, null)) { return; // already registered } UnregisteredStation unregisteredStation = db.UnregisteredStations.Where(s => station.Id == s.Id).FirstOrDefault(); if (!ReferenceEquals(unregisteredStation, null)) { return; // already in the unregistered list } db.UnregisteredStations.Add(station); db.SaveChanges(); }
public Object CreateStation(Station station) { if (!StationExists(station.Id)) { UnregisteredStation unregisteredStation = db.UnregisteredStations.Where(s => station.Id == s.Id).FirstOrDefault(); if (ReferenceEquals(unregisteredStation, null)) { return("Unknown station. Please make sure it is powered on and connected to a network."); } // remove from the unregistered table and move to registered table db.UnregisteredStations.Remove(unregisteredStation); db.Stations.Add(station); db.SaveChanges(); return(db.Stations.Find(station.Id)); } else { return("Station already registered. But hey, we love you too!"); } }
public IHttpActionResult Ping([FromBody] UnregisteredStation station) { _stationRepo.AddThirdPartyDevice(station); return(Ok()); }