protected void ValidateAccessToken()
        {
            Log.Debug(TAG, "Enter: ValidateAccessToken()");
            Log.Debug(TAG, $"AccessToken: {this.AccessToken}");
            Log.Debug(TAG, $"AccessTokenExpiration: {this.AccessTokenExpiration}");

            if (String.IsNullOrEmpty(this.AccessToken) || this.AccessTokenExpiration <= DateTime.Now)
            {
                // No valid access token.
                var email    = PrefHelper.GetPrefString(Constants.PrefKeys.USER_EMAIL);
                var password = PrefHelper.GetPrefString(Constants.PrefKeys.USER_PASSWORD);

                Log.Debug(TAG, "GetAccessToken...");
                var accessToken = TeslaAPI.GetAccessToken(email, password).Result;


                if (accessToken != null)
                {
                    PrefHelper.SetPref(Constants.PrefKeys.USER_EMAIL, email);
                    PrefHelper.SetPref(Constants.PrefKeys.USER_PASSWORD, password);

                    this.AccessToken           = accessToken.Token;
                    this.AccessTokenExpiration = accessToken.ExpirationDate;

                    Log.Debug(TAG, $"AccessToken: {this.AccessToken}");
                    Log.Debug(TAG, $"AccessTokenExpiration: {this.AccessTokenExpiration}");
                }
            }

            Log.Debug(TAG, "Exit: ValidateAccessToken()");
        }
Example #2
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            this._prefHelper = new PrefHelper(this.ApplicationContext);
            this._teslaAPI   = new TeslaAPI(this.AccessToken);
        }
        protected void HandleTimerCallback(object state)
        {
            Log.Debug(TAG, "Enter: HandleTimerCallback()");

            if (this.IsWithinMonitorTimeframe)
            {
                ValidateAccessToken();

                var vehicleIds = PrefHelper.GetPrefStrings(Constants.PrefKeys.VEHICLES);

                vehicleIds.ForEach((id) =>
                {
                    var vehicleId   = Convert.ToInt64(id);
                    var vehicleName = PrefHelper.GetPrefString(String.Format(Constants.PrefKeys.VEHICLE_NAME, id));

                    Log.Debug(TAG, $"vehicleId: {vehicleId}");
                    Log.Debug(TAG, $"vehicleName: {vehicleName}");

                    var isAtChargingLocation = IsAtChargingLocation(vehicleId);

                    if (isAtChargingLocation)
                    {
                        Log.Debug(TAG, "GetChargeState...");
                        var chargeState = _teslaAPI.GetChargeState(vehicleId).Result;

                        Log.Debug(TAG, $"charging_state: {chargeState.charging_state}");

                        //if (chargeState.charging_state == "Disconnected")
                        //{
                        ShowNotification(vehicleName, chargeState.charging_state, chargeState.battery_level.Value, chargeState.battery_range.Value);
                        //}
                    }
                });
            }

            Log.Debug(TAG, "Exit: HandleTimerCallback()");
        }