public async Task <int> StartUp()
        {
            // do we need a new access token
            if (localSettings.Values["access_token"] == null || localSettings.Values["access_token_expire"] == null ||
                !IsTokenValid(Convert.ToDateTime(localSettings.Values["access_token_expire"].ToString())))
            {
                if (await GetAPIToken() == null)
                {
                    return(1001);
                }
            }


            // get the device detail
            SystemDetail systemDetail = await CashGenicAPIService.GetSystem(localSettings.Values["access_token"].ToString());

            if (systemDetail == null)
            {
                return(1000);
            }
            else
            {
                // fire new system connected event
                NewSystemConnectedArgs args = new NewSystemConnectedArgs();
                args.system = systemDetail;
                OnNewSystemConnected(args);
                _ = Task.Factory.StartNew(() => RunSystem());
            }



            return(0);
        }
        private async Task <ApiToken> GetAPIToken()
        {
            ApiToken apiToken = await CashGenicAPIService.GetToken(localSettings.Values["apiUserName"].ToString(), localSettings.Values["apiPassword"].ToString());

            if (apiToken != null)
            {
                DateTime d = DateTime.Now.AddMilliseconds(apiToken.expires_in);
                localSettings.Values["access_token_expire"] = d.ToString();
                localSettings.Values["access_token"]        = apiToken.access_token;
            }

            return(apiToken);
        }
        private async Task RunSystem()
        {
            _runSystem = true;
            while (_runSystem)
            {
                // check for token refresh required
                if (localSettings.Values["access_token"] == null || localSettings.Values["access_token_expire"] == null ||
                    !IsTokenValid(Convert.ToDateTime(localSettings.Values["access_token_expire"].ToString())))
                {
                    if (await GetAPIToken() == null)
                    {
                        _runSystem = false;
                        NewSystemErrorArgs args = new NewSystemErrorArgs();
                        args.systemError = SystemError.no_connection;
                        OnNewSystemError(args);
                        break;
                    }
                }
                // get the status
                SystemStatus systemStatus = await CashGenicAPIService.GetStatus(localSettings.Values["access_token"].ToString());

                if (systemStatus == null)
                {
                    _runSystem = false;
                    NewSystemErrorArgs args = new NewSystemErrorArgs();
                    args.systemError = SystemError.no_connection;
                    OnNewSystemError(args);
                    break;
                }
                ParseStatus(systemStatus.Events);


                // any commands
                if (_cancelPayment)
                {
                    _cancelPayment = false;
                    SystemResponse systemResponse = await CashGenicAPIService.CancelSession(localSettings.Values["access_token"].ToString());
                }
                if (_closeSession)
                {
                    _closeSession = false;
                    SystemResponse systemResponse = await CashGenicAPIService.EndSession(localSettings.Values["access_token"].ToString());
                }


                // poll delay
                Thread.Sleep(500);
            }
        }
        public async Task <SystemResponse> StartRefundSession(int requestValue)
        {
            if (localSettings.Values["access_token"] == null || localSettings.Values["access_token_expire"] == null ||
                !IsTokenValid(Convert.ToDateTime(localSettings.Values["access_token_expire"].ToString())))
            {
                if (await GetAPIToken() == null)
                {
                    return(SystemResponse.InvalidCredentials);
                }
            }

            SystemResponse ret = await CashGenicAPIService.StartRefundSession(localSettings.Values["access_token"].ToString(), requestValue);

            return(ret);
        }