/// <summary> /// Handles requests to save a single aircraft's data. /// </summary> /// <param name="args"></param> private void SingleAircraftSave(RequestReceivedEventArgs args) { if (args.Request.HttpMethod == "POST") { var json = new SingleAircraftSaveResultsJson(); try { var aircraftJson = args.Request.ReadBodyAsString(Encoding.UTF8); json.Aircraft = JsonConvert.DeserializeObject <BaseStationAircraft>(aircraftJson); if (json.Aircraft.ModeS != null && json.Aircraft.ModeS.Length == 6) { if (json.Aircraft.Registration != null) { json.Aircraft.Registration = json.Aircraft.Registration.ToUpper(); } if (json.Aircraft.ICAOTypeCode != null) { json.Aircraft.ICAOTypeCode = json.Aircraft.ICAOTypeCode.ToUpper(); } if (json.Aircraft.OperatorFlagCode != null) { json.Aircraft.OperatorFlagCode = json.Aircraft.OperatorFlagCode.ToUpper(); } json.Aircraft.LastModified = DateTime.Now; if (json.Aircraft.AircraftID == 0) { _BaseStationDatabase.InsertAircraft(json.Aircraft); } else { _BaseStationDatabase.UpdateAircraft(json.Aircraft); } ++_UpdateCount; UpdateStatusTotals(); } } catch (Exception ex) { var aircraftID = json.Aircraft == null ? "<no aircraft>" : json.Aircraft.AircraftID.ToString(); var icao = json.Aircraft == null ? "<no aircraft>" : json.Aircraft.ModeS; json.Exception = LogException(ex, "Exception caught during DatabaseEditor SingleAircraftSave ({0}/{1}): {2}", aircraftID, icao, ex.ToString()); } SendJsonResponse(args, json); } }
/// <summary> /// Fetches the aircraft record from the database for the ICAO24 code passed across. If there is no /// record for the ICAO24 code then one is created. /// </summary> /// <param name="localNow"></param> /// <param name="icao24"></param> /// <returns></returns> private BaseStationAircraft FetchOrCreateAircraft(DateTime now, string icao24) { var result = _Database.GetAircraftByCode(icao24); if (result == null) { var codeBlock = _StandingDataManager.FindCodeBlock(icao24); result = new BaseStationAircraft() { AircraftID = 0, ModeS = icao24, FirstCreated = now, LastModified = now, ModeSCountry = codeBlock == null ? null : codeBlock.Country, }; _Database.InsertAircraft(result); } return(result); }