Beispiel #1
0
        protected bool IsValidRestServiceConnection(bool isUsingHttps, string restUrl)
        //protected async Task<bool> IsValidRestServiceConnection(bool isUsingHttps, string restUrl)
        {
            bool bSuccess = false;

            //List<bool> result = null;

            System.Diagnostics.Debug.WriteLine("Testing REST service connection by querying for technicians.");

            if (isUsingHttps == null || restUrl == null)
            {
                return(false);
            }

            try
            {
                RestClient restClient = new RestClient(isUsingHttps, restUrl);

                //result = await restClient.TestConnection();
                bSuccess = restClient.TestConnection();

                //System.Diagnostics.Debug.WriteLine("Test call to REST service returned " + result.Count.ToString() + " results.");
                System.Diagnostics.Debug.WriteLine("Test call to REST service returned " + bSuccess.ToString() + " results.");
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception was caught!");
                System.Diagnostics.Debug.WriteLine(ex.Message);

                // dch rkl 12/07/2016 Log Error
                ErrorReporting errorReporting = new ErrorReporting();
                errorReporting.sendException(ex, "TechDashboard.TechDashboardDB_AppSettings.IsValidRestServiceConnection");
            }

            return(bSuccess);
            //return ((result.Count > 0) && (result[0]));
        }
Beispiel #2
0
        public bool HasValidSetup()
        //public async Task<bool> HasValidSetup()
        {
            bool         hasValidSetup = false;
            App_Settings appSettings   = null;

            lock (_locker)
            {
                appSettings = GetApplicationSettings();
            }
            //get version
            string version   = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            string dbVersion = (appSettings.DbVersion != null) ? appSettings.DbVersion : "0.0.0.0";

            if (version.Substring(0, 3) != dbVersion.Substring(0, 3))
            {
                return(false);
            }

            if (HasDataConnection())
            {
                try
                {
                    switch (_dataConnectionType)
                    {
                    case ConnectionType.Rest:
                        hasValidSetup =
                            IsValidRestServiceConnection(appSettings.IsUsingHttps, appSettings.RestServiceUrl);
                        //await IsValidRestServiceConnection(appSettings.IsUsingHttps, appSettings.RestServiceUrl);
                        break;

                    case ConnectionType.SData:
                        hasValidSetup =
                            IsValidSDataConnection(
                                appSettings.IsUsingHttps,
                                appSettings.SDataUrl,
                                appSettings.SDataUserId,
                                appSettings.SDataPassword
                                );
                        break;

                    default:
                        break;
                    }
                }
                catch (Exception ex)
                {
                    hasValidSetup = false;

                    System.Diagnostics.Debug.WriteLine("Exception caught in HasValidSetup() method.");
                    System.Diagnostics.Debug.WriteLine(ex.Message);

                    // dch rkl 12/07/2016 Log Error
                    ErrorReporting errorReporting = new ErrorReporting();
                    errorReporting.sendException(ex, "TechDashboard.TechDashboardDB_AppSettings.HasValidSetup");
                }
            }
            else
            {
                // it'll either have old data or it won't.
                hasValidSetup = true;
            }



            // validate device


            return(hasValidSetup);
        }
        // dch rkl 12/09/2016 This now returns an object
        //public bool InsertTransactionImportDetailRecordSync(JT_TransactionImportDetail importDetail)
        public Rkl.Erp.Sage.Sage100.TableObjects.API_Results InsertTransactionImportDetailRecordSync(JT_TransactionImportDetail importDetail)
        {
            // dch rkl 12/09/2016 This now returns an object
            //bool returnData = false;
            Rkl.Erp.Sage.Sage100.TableObjects.API_Results returnData = new Rkl.Erp.Sage.Sage100.TableObjects.API_Results();

            App_Settings appSettings = App.Database.GetApplicationSettings();

            // set up the proper URL
            string url = GetRestServiceUrl();

            if (!url.EndsWith(@"/"))
            {
                url += @"/";
            }
            url += @"i/JT_TransactionImportDetail";

            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri(url);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings();

            microsoftDateFormatSettings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
            if (appSettings.DeviceID != null)
            {
                SimpleAES encryptText = new SimpleAES("V&WWJ3d39brdR5yUh5(JQGHbi:FB@$^@", "W4aRWS!D$kgD8Xz@");
                string    authid      = encryptText.EncryptToString(appSettings.DeviceID);
                string    datetimever = encryptText.EncryptToString(DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz"));
                client.DefaultRequestHeaders.Add("x-tdws-authid", authid);
                client.DefaultRequestHeaders.Add("x-tdws-auth", datetimever);
            }

            // Make the call and get a valid response
            HttpResponseMessage response
                = client.PostAsync(client.BaseAddress, new StringContent(JsonConvert.SerializeObject(importDetail, microsoftDateFormatSettings), null, "application/json")).Result; // TODO.... await

            response.EnsureSuccessStatusCode();

            // Read out the result... it better be JSON!
            string JsonResult = response.Content.ReadAsStringAsync().Result;

            try
            {
                // dch rkl 12/09/2016 This now returns an object
                //returnData = JsonConvert.DeserializeObject<bool>(JsonResult);
                returnData = JsonConvert.DeserializeObject <Rkl.Erp.Sage.Sage100.TableObjects.API_Results>(JsonResult);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);

                // dch rkl 12/07/2016 Log Error
                ErrorReporting errorReporting = new ErrorReporting();
                errorReporting.sendException(ex, "TechDashboard.Data.RestClient");
            }

            client.Dispose();
            response.Dispose();

            return(returnData);
        }
Beispiel #4
0
        public App_Settings GetApplicationSettings()
        {
            // dch rkl 12/06/2016 Add Try/Catch to display exceptions
            App_Settings appSettings = new Models.App_Settings();

            //App_Settings appSettings = null;

            try
            {
                lock (_locker)
                {
                    // dch rkl 12/07/2016 This is not working.  The TableMappings count is always zero, so it keeps creating a new app settings record each time. BEGIN
                    //if (_database.TableMappings.Where(tm => tm.TableName == "App_Settings").FirstOrDefault() == null)
                    //{
                    //    _database.CreateTable<App_Settings>();
                    //    _database.Insert(new App_Settings());
                    //}
                    // dch rkl 12/07/2016 This is not working.  The TableMappings count is always zero, so it keeps creating a new app settings record each time. END

                    try
                    {
                        appSettings = _database.Table <App_Settings>().FirstOrDefault();
                    }
                    catch (Exception ex)
                    {
                        //System.Diagnostics.Debug.WriteLine("Exception caught when querying for application settings.");
                        //System.Diagnostics.Debug.WriteLine(ex.Message);
                    }

                    // dch rkl 12/07/2016 If no app settings found, add a blank record now BEGIN
                    if (appSettings == null || appSettings.ID == 0)
                    {
                        try
                        {
                            _database.CreateTable <App_Settings>();
                            _database.Insert(new App_Settings());
                            appSettings = _database.Table <App_Settings>().FirstOrDefault();
                        }
                        catch (Exception ex)
                        {
                            ErrorReporting oErrRpt = new Data.ErrorReporting();
                            // dch rkl 12/07/2016 add the call/sub/proc to log
                            //oErrRpt.sendException(ex);
                            oErrRpt.sendException(ex, "TechDashboard.TechDashboardDB_AppSettings.cs/GetApplicationSettings");
                        }
                    }
                    // dch rkl 12/07/2016 If no app settings found, add a blank record now END
                }
            }
            catch (Exception ex)
            {
                // dch rkl 12/07/2016 Log Error
                ErrorReporting errorReporting = new ErrorReporting();
                errorReporting.sendException(ex, "TechDashboard.TechDashboardDB_AppSettings.GetApplicationSettings");

                throw new Exception(String.Format(
                                        "Error occurred in TechDashboardDB_AppSettings.GetApplicationSettings.  Contact your administrator.  /nMessageDetails: {0}",
                                        ex.ToString()));
            }

            return(appSettings);
        }
        public List <T> GetDataSync <T>(string filterType, string filterText)
        {
            // Set up our return data object -- a list of typed objects.
            List <T> returnData = new List <T>();

            // dch rkl 12/07/2016 capture errors with try/catch
            try
            {
                App_Settings appSettings = App.Database.GetApplicationSettings();

                // set up the proper URL
                string url = GetRestServiceUrl();
                if (!url.EndsWith(@"/"))
                {
                    url += @"/";
                }
                if ((filterType != null) &&
                    (filterType.Length > 0) &&
                    (filterText != null) &&
                    (filterText.Length > 0))
                {
                    url += @"q/" + typeof(T).Name + @"/" + filterType + @"?v=" + Uri.EscapeDataString(filterText);
                }
                else
                {
                    url += @"all/" + typeof(T).Name;
                }

                // Create a HTTP client to call the REST service
                RestSharp.RestClient client = new RestSharp.RestClient(url);

                var request = new RestSharp.RestRequest(Method.GET);
                if (appSettings.DeviceID != null)
                {
                    SimpleAES encryptText = new SimpleAES("V&WWJ3d39brdR5yUh5(JQGHbi:FB@$^@", "W4aRWS!D$kgD8Xz@");
                    string    authid      = encryptText.EncryptToString(appSettings.DeviceID);
                    string    datetimever = encryptText.EncryptToString(DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz"));
                    request.AddHeader("x-tdws-authid", authid);
                    request.AddHeader("x-tdws-auth", datetimever);
                }

                var response = client.Execute(request);

                // dch rkl 12/09/2016 include not found
                //if (response.StatusCode != System.Net.HttpStatusCode.OK)
                if (response.StatusCode != System.Net.HttpStatusCode.OK && response.StatusCode != System.Net.HttpStatusCode.NotFound)
                {
                    //throw new Exception("Bad request");
                    ErrorReporting errorReporting = new ErrorReporting();
                    // dch rkl 12/07/2016 add the call/sub/proc to log
                    //errorReporting.sendException(new Exception(response.Content));
                    errorReporting.sendException(new Exception(response.Content), "RestClient.cs.GetDataSync");
                }

                string JsonResult = response.Content;
                returnData = JsonConvert.DeserializeObject <List <T> >(JsonResult);
            }
            catch (Exception ex)
            {
                // dch rkl 12/07/2016 Log Error
                ErrorReporting errorReporting = new ErrorReporting();
                errorReporting.sendException(ex, string.Format("TechDashboard.RestClient.GetDataSync: FilterType={0}; FilterText={1}",
                                                               filterType, filterText));
            }

            return(returnData);
        }