State types for systems and factions
 public DockedEvent(DateTime timestamp, string system, string station, Superpower allegiance, string faction, State factionstate, Economy economy, Government government, SecurityLevel security) : base(timestamp, NAME)
 {
     this.system = system;
     this.station = station;
     this.allegiance = (allegiance == null ? Superpower.None.name : allegiance.name);
     this.faction = faction;
     this.factionstate = (factionstate == null ? State.None.name : factionstate.name);
     this.economy = (economy == null ? Economy.None.name : economy.name);
     this.government = (government == null ? Government.None.name : government.name);
     this.security = (security == null ? SecurityLevel.Low.name : security.name);
 }
 public JumpedEvent(DateTime timestamp, string system, decimal x, decimal y, decimal z, decimal fuelused, decimal fuelremaining, Superpower allegiance, string faction, State factionstate, Economy economy, Government government, SecurityLevel security) : base(timestamp, NAME)
 {
     this.system = system;
     this.x = x;
     this.y = y;
     this.z = z;
     this.fuelused = fuelused;
     this.fuelremaining = fuelremaining;
     this.allegiance = (allegiance == null ? Superpower.None.name : allegiance.name) ;
     this.faction = faction;
     this.factionstate = (factionstate == null ? State.None.name : factionstate.name);
     this.economy = (economy == null ? Economy.None.name : economy.name);
     this.government = (government == null ? Government.None.name : government.name);
     this.security = (security == null ? SecurityLevel.None.name : security.name);
 }
        public static State FromEDName(string from)
        {
            string tidiedFrom = from == null ? null : from.ToLowerInvariant();

            State result;
            if (from == null || from == "")
            {
                result = None;
            }
            else
            {
                result = STATES.FirstOrDefault(v => v.edname.ToLowerInvariant() == tidiedFrom);
            }
            if (result == null)
            {
                Logging.Report("Unknown State ED name " + from);
                result = new State(from, tidiedFrom);
            }
            return result;
        }
        private CompanionAppService()
        {
            Credentials = CompanionAppCredentials.FromFile();

            // Need to work out our current state.

            //If we're missing username and password then we need to log in again
            if (string.IsNullOrEmpty(Credentials.email) || string.IsNullOrEmpty(Credentials.password))
            {
                CurrentState = State.NEEDS_LOGIN;
            }
            else if (string.IsNullOrEmpty(Credentials.machineId) || string.IsNullOrEmpty(Credentials.machineToken))
            {
                CurrentState = State.NEEDS_LOGIN;
            }
            else
            {
                // Looks like we're ready but test it to find out
                CurrentState = State.READY;
                try
                {
                    Profile();
                }
                catch (EliteDangerousCompanionAppException ex)
                {
                    Logging.Warn("Failed to obtain profile: " + ex.ToString());
                }
            }
        }
 /**
  * Try to relogin if there is some issue that requires it.
  * Throws an exception if it failed to log in.
  */
 private void relogin()
 {
     // Need to log in again.
     CurrentState = State.NEEDS_LOGIN;
     Login();
     if (CurrentState != State.READY)
     {
         Logging.Debug("Service in incorrect state to provide profile (" + CurrentState + ")");
         Logging.Debug("Leaving");
         throw new EliteDangerousCompanionAppIllegalStateException("Service in incorrect state to provide profile (" + CurrentState + ")");
     }
 }
        ///<summary>Confirm a login.  Throws an exception if it fails</summary>
        public void Confirm(string code)
        {
            if (CurrentState != State.NEEDS_CONFIRMATION)
            {
                // Shouldn't be here
                throw new EliteDangerousCompanionAppIllegalStateException("Service in incorrect state to confirm login (" + CurrentState + ")");
            }

            HttpWebRequest request = GetRequest(BASE_URL + CONFIRM_URL);

            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "POST";
            string encodedCode = WebUtility.UrlEncode(code);
            byte[] data = Encoding.UTF8.GetBytes("code=" + encodedCode);
            request.ContentLength = data.Length;
            using (Stream dataStream = request.GetRequestStream())
            {
                dataStream.Write(data, 0, data.Length);
            }

            using (HttpWebResponse response = GetResponse(request))
            {
                if (response == null)
                {
                    throw new EliteDangerousCompanionAppException("Failed to contact API server");
                }

                if (response.StatusCode == HttpStatusCode.Found && response.Headers["Location"] == ROOT_URL)
                {
                    CurrentState = State.READY;
                }
                else if (response.StatusCode == HttpStatusCode.Found && response.Headers["Location"] == LOGIN_URL)
                {
                    CurrentState = State.NEEDS_LOGIN;
                    throw new EliteDangerousCompanionAppAuthenticationException("Confirmation code incorrect or expired");
                }
            }
        }