public static void Initialise(string strUser, string strPassword, ManualResetEvent eventStop, HttpClient httpClient)
        {
            Thread threadMonitor;

            _strUser     = strUser;
            _strPassword = strPassword;
            _eventStop   = eventStop;
            _httpClient  = httpClient;

            // Get Device Id
            try
            {
                if (File.Exists(_strDeviceIdFile))
                {
                    _strDeviceUniqueIdentifier = JsonConvert.DeserializeObject <string>(File.ReadAllText(_strDeviceIdFile));
                }
            }
            catch (Exception eException)
            {
                // Handle Error
            }

            // Get Pairing Token
            try
            {
                if (File.Exists(_strPairingTokenFile))
                {
                    _pairingToken = JsonConvert.DeserializeObject <PairingToken>(File.ReadAllText(_strPairingTokenFile));
                }
            }
            catch (Exception eException)
            {
                // Handle Error
            }

            threadMonitor = new Thread(new ThreadStart(TokenMonitor));
            threadMonitor.Start();
        }
        private static async Task <bool> GeneratePairingToken()
        {
            HttpResponseMessage         httpResponse      = null;
            CancellationTokenSource     cancellationToken = null;
            Dictionary <string, string> dtFormContent     = new Dictionary <string, string>();
            string  strPageURL = "/api/authenticate";
            string  strResponse;
            dynamic jsonResponse;
            bool    bRetVal = true;

            if (_strDeviceUniqueIdentifier == "")
            {
                _strDeviceUniqueIdentifier = GenerateDeviceId();

                // Update Device Id File
                try
                {
                    File.WriteAllText(_strDeviceIdFile, JsonConvert.SerializeObject(_strDeviceUniqueIdentifier));
                }
                catch (Exception eException)
                {
                    // Handle Error
                }
            }

            // Add Authentication Parameters
            dtFormContent.Add("username", _strUser);
            dtFormContent.Add("password", _strPassword);
            dtFormContent.Add("deviceUniqueIdentifier", _strDeviceUniqueIdentifier);

            try
            {
                cancellationToken = new CancellationTokenSource();
                cancellationToken.CancelAfter(TimeSpan.FromSeconds(_iCancellationTime));

                httpResponse = await _httpClientAuth.PostAsync(strPageURL, new FormUrlEncodedContent(dtFormContent), cancellationToken.Token);

                if (httpResponse.IsSuccessStatusCode)
                {
                    strResponse = await httpResponse.Content.ReadAsStringAsync();

                    jsonResponse = JsonConvert.DeserializeObject(strResponse);

                    _pairingToken = new PairingToken(jsonResponse.pairingToken.ToString());

                    // Update Token File
                    try
                    {
                        File.WriteAllText(_strPairingTokenFile, JsonConvert.SerializeObject(_pairingToken));
                    }
                    catch (Exception eException)
                    {
                        // Handle Error
                    }
                }
                else
                {
                    // Handle Error
                    bRetVal = false;
                    goto Cleanup;
                }
            }
            catch (OperationCanceledException eException)
            {
                // Handle Error

                bRetVal = false;
                goto Cleanup;
            }
            catch (Exception eException)
            {
                // Handle Error

                bRetVal = false;
                goto Cleanup;
            }

Cleanup:
            cancellationToken?.Dispose();
            httpResponse?.Dispose();

            if (!bRetVal)
            {
                _pairingToken = null;
            }

            return(bRetVal);
        }
        private static async Task <bool> GenerateBearerToken()
        {
            HttpResponseMessage         httpResponse      = null;
            CancellationTokenSource     cancellationToken = null;
            Dictionary <string, string> dtFormContent     = new Dictionary <string, string>();
            BearerToken bearerToken = null;
            string      strPageURL  = "/api/gettoken";
            string      strResponse;
            dynamic     jsonResponse;
            bool        bRetVal = true;

            // Add Authenticaiton Parameters
            dtFormContent.Add("refresh_token", _pairingToken.Token);


            try
            {
                cancellationToken = new CancellationTokenSource();
                cancellationToken.CancelAfter(TimeSpan.FromSeconds(_iCancellationTime));

                httpResponse = await _httpClientAuth.PostAsync(strPageURL, new FormUrlEncodedContent(dtFormContent), cancellationToken.Token);

                if (httpResponse.IsSuccessStatusCode)
                {
                    strResponse = await httpResponse.Content.ReadAsStringAsync();

                    jsonResponse = JsonConvert.DeserializeObject(strResponse);

                    bearerToken              = new BearerToken();
                    bearerToken.Token        = jsonResponse.access_token;
                    bearerToken.TokenExpires = DateTime.Now.AddSeconds(int.Parse(jsonResponse.expires_in.ToString()));

                    _bearerToken = bearerToken;

                    _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _bearerToken.Token);

                    // Update Token File
                    try
                    {
                        File.WriteAllText(_strBearerTokenFile, JsonConvert.SerializeObject(_bearerToken));
                    }
                    catch (Exception eException)
                    {
                        // Handle Error
                    }
                }
                else
                {
                    if (httpResponse.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                    {
                        // Pairing Token Invalid
                        _pairingToken = null;
                    }
                    else
                    {
                        // Handle Error
                    }

                    bRetVal = false;
                    goto Cleanup;
                }
            }
            catch (OperationCanceledException eException)
            {
                // Handle Error

                bRetVal = false;
                goto Cleanup;
            }
            catch (Exception eException)
            {
                // Handle Error

                bRetVal = false;
                goto Cleanup;
            }

Cleanup:
            cancellationToken?.Dispose();
            httpResponse?.Dispose();

            if (!bRetVal)
            {
                _bearerToken = null;
            }

            return(bRetVal);
        }