Beispiel #1
0
        private async void InsertAssetData(double progressCounter, Stack <SitesClass> StackSites_Assets, bool new_ = false)
        {
            try
            {
                //Insert Asset Data
                pg1Label.Text = "Getting Asset data from eQuip.";
                var AssetResponse = await SyncClass.NewSyncClass.API_GetAssets(_userID, _url, StackSites_Assets);

                pg1Label.Text = "Converting data";
                //convert string to ist of JSON Object
                var AssetRecords = JsonConvert.DeserializeObject <List <AssetJsonObject> >(AssetResponse);
                //Create new list to parse records and make into AssetClass
                var AssetClassList = new List <AssetClass>();
                foreach (AssetJsonObject AssetJSON in AssetRecords)
                {
                    AssetClassList.Add(new AssetClass(AssetJSON, true));
                }
                //Convert asset class to string then to DbClass to be stored
                var AssetDbList = JsonConvert.DeserializeObject <IEnumerable <AssetDbClass> >(JsonConvert.SerializeObject(AssetClassList));
                pg1Label.Text = "Storing Assets to database.";
                Database.Assets.DeleteAndInsert(_connection, AssetDbList);
                pg1.Progress = pg1.Progress + progressCounter;

                pg1.Progress = pg1.Progress + progressCounter;

                //Get Time using Login method
                var LoginInfo = JsonConvert.DeserializeObject <List <LoginDataClass> >(await SyncClass.API_Login(Application.Current.Properties["user"].ToString(), Application.Current.Properties["password"].ToString(), _url));
                //Store the LastSync Date
                Application.Current.Properties["LastSyncdate"] = LoginInfo[0].ServerTime.ToString();
                Database.Login.UpdateSync(_connection, LoginInfo[0].ServerTime.ToString());
                await Application.Current.SavePropertiesAsync();

                pg1.Progress = pg1.Progress + progressCounter;
                ActivityIndicator.IsVisible = false;
                ActivityIndicator.IsRunning = false;
                if (new_)
                {
                    await DisplayAlert("Sync Status", "New Synchronization Complete!", "OK");
                }
                else
                {
                    await DisplayAlert("Sync Status", "Update Synchronization Complete!", "OK");
                }
                await Navigation.PopToRootAsync();
            }
            catch (Exception exc)
            {
                DependencyService.Get <IError>().SendRaygunError(exc, Application.Current.Properties["user"].ToString() ?? "unsynced", Application.Current.Properties["url"].ToString() ?? "unsynced", null);
                await DisplayAlert("Error!", exc.Message + ". Please wipe database and try again.", "OK");
            }
        }
Beispiel #2
0
        private async void LogIntoEquip(string username, string password, string url)
        {
            string returnedData = "";

            try
            {
                returnedData = await SyncClass.API_Login(username, password, url);


                var ParsedLoginData = JsonConvert.DeserializeObject <List <LoginDataClass> >(returnedData);

                //Store login data to variables to be used to sore to database
                var Servertime_     = ParsedLoginData[0].ServerTime.ToString();
                var UserID_         = ParsedLoginData[0].UserID;
                var RoleID_         = ParsedLoginData[0].RoleID;
                var AllowDup_       = ParsedLoginData[0].AllowDuplicateBarcode.ToString();
                var eQuipVer_       = ParsedLoginData[0].eQuipVersion;
                var Framework_      = ParsedLoginData[0].Framework;
                var RequiredFields_ = JsonConvert.SerializeObject(ParsedLoginData[0].RequiredFields);
                var MobileSites_    = ParsedLoginData[0].GetMobileSites();
                var Permissions     = JsonConvert.SerializeObject(ParsedLoginData[0].Permissions);
                var LoginData       = new List <LoginDbTable> {
                    new LoginDbTable {
                        ServerTime            = Servertime_,
                        UserID                = UserID_,
                        RoleID                = RoleID_.ToString(),
                        AllowDuplicateBarcode = AllowDup_,
                        eQuipVersion          = eQuipVer_,
                        Framework             = Framework_,
                        RequiredFields        = RequiredFields_,
                        MobileSites           = MobileSites_,
                        LastSyncDate          = _LastSyncDate,
                        Permissions           = Permissions
                    }
                };
                foreach (var loginRecord in LoginData)
                {
                    var query = Database.Login.GetTableData(_connection);
                    if (query.Count() > 0)
                    {
                        Database.Login.Update(_connection, loginRecord);
                    }
                    else
                    {
                        await DisplayAlert("Error", "Please log in", "OK");
                    }
                }
            }
            catch (Exception e)
            {
                DependencyService.Get <IError>().SendRaygunError(e, Application.Current.Properties["user"].ToString() ?? "unsynced", Application.Current.Properties["url"].ToString() ?? "unsynced", null);
                if (e is LoginException)
                {
                    await DisplayAlert("Error", e.Message, "OK");
                }
                else
                {
                    await DisplayAlert("Error", e.Message + ". Please check the url or connection.", "OK");
                }
            }
        }
Beispiel #3
0
        private async void LogIntoEquip(string username, string password, string url, bool second_attempt)
        {
            if (url.ToLower().IndexOf("https://") != 0)
            {
                await DisplayAlert("Error", "Please check the url, there is no http present", "OK");
            }
            else
            {
                string returnedData = "";
                try
                {
                    url_passed = url;
                    ActivityIndicator.IsRunning = true;
                    ActivityIndicator.IsVisible = true;
                    returnedData = await SyncClass.API_Login(username.Trim(), password.Trim(), url.Trim());

                    ActivityIndicator.IsRunning = false;
                    ActivityIndicator.IsVisible = false;
                    //check if data is erroroneous

                    var ParsedLoginData = JsonConvert.DeserializeObject <List <LoginDataClass> >(returnedData);
                    //stores login, user and password to Application storage
                    Application.Current.Properties["url"]      = url;
                    Application.Current.Properties["user"]     = username;
                    Application.Current.Properties["password"] = password;
                    Application.Current.Properties["UserID"]   = ParsedLoginData[0].UserID;

                    //Store login data to variables to be used to sore to database
                    var Servertime_     = ParsedLoginData[0].ServerTime.ToString();
                    var UserID_         = ParsedLoginData[0].UserID;
                    var RoleID_         = ParsedLoginData[0].RoleID;
                    var AllowDup_       = ParsedLoginData[0].AllowDuplicateBarcode.ToString();
                    var eQuipVer_       = ParsedLoginData[0].eQuipVersion;
                    var Framework_      = ParsedLoginData[0].Framework;
                    var RequiredFields_ = JsonConvert.SerializeObject(ParsedLoginData[0].RequiredFields);
                    var MobileSites_    = ParsedLoginData[0].GetMobileSites();
                    var Permissions     = JsonConvert.SerializeObject(ParsedLoginData[0].Permissions);
                    var LoginData       = new List <LoginDbTable> {
                        new LoginDbTable {
                            ServerTime            = Servertime_,
                            UserID                = UserID_,
                            RoleID                = RoleID_.ToString(),
                            AllowDuplicateBarcode = AllowDup_,
                            eQuipVersion          = eQuipVer_,
                            Framework             = Framework_,
                            RequiredFields        = RequiredFields_,
                            MobileSites           = MobileSites_,
                            LastSyncDate          = _LastSyncDate.Text,
                            Permissions           = Permissions
                        }
                    };
                    //Adds data to storage
                    //check if value alredy present, then update
                    //https://components.xamarin.com/gettingstarted/sqlite-net
                    foreach (var loginRecord in LoginData)
                    {
                        //TODO
                        //Change it so that it checks for specific parameter
                        var query = Database.Login.GetTableData(_connection); //QueryValuations(_connection, LoginData[0]);
                        if (query.Count() > 0)
                        {
                            if (loginRecord.UserID == query.First().UserID&& loginRecord.MobileSites == query.First().MobileSites)
                            {
                                //Same user is logging in
                                //loginRecord.Id = query.First().Id;
                                Database.Login.Update(_connection, loginRecord);// _connection.Update(loginRecord);
                                if (!second_attempt)
                                {
                                    await DisplayAlert("Ok", "Same user logging in : " + loginRecord.ServerTime, "Ok");
                                }
                                await Application.Current.SavePropertiesAsync();

                                await Navigation.PushAsync(new SyncPage(_connection, UserID_, url_passed));
                            }
                            else
                            {
                                //Different user logging in
                                var AssetsToUpdate = CheckforSyncData();
                                if (AssetsToUpdate)
                                {
                                    await DisplayAlert("Warning", "New user. There is data that has not been Synced. Sync before continuing", "Ok");
                                }
                                else
                                {
                                    //clear database and login with new user data
                                    if (second_attempt)
                                    {
                                        await Navigation.PushAsync(new SyncPage(_connection, UserID_, url_passed));
                                    }
                                    else
                                    {
                                        await DisplayAlert("Alert", "Different user/mobile sites logging in. Clearing data for login", "Ok");

                                        Reinitialize(true);
                                    }
                                }
                            }
                        }
                        else
                        {
                            //Completely new user
                            Database.Login.Insert(_connection, loginRecord);//_connection.Insert(loginRecord);
                            if (!second_attempt)
                            {
                                await DisplayAlert("Ok", "No data present, inserting Data: " + loginRecord.ServerTime, "Ok");
                            }
                            await Navigation.PushAsync(new SyncPage(_connection, UserID_, url_passed));
                        }
                    }
                }
                catch (Exception e)
                {
                    try
                    {
                        DependencyService.Get <IError>().SendRaygunError(e, Application.Current.Properties["user"].ToString() ?? "unsynced", Application.Current.Properties["url"].ToString() ?? "unsynced", null);
                        //TODO - send raygun Error
                        ActivityIndicator.IsRunning = false;
                        ActivityIndicator.IsVisible = false;
                        if (e is LoginException)
                        {
                            await DisplayAlert("Error", e.Message, "OK");

                            LoginPassWord.Text = "";
                        }
                        else if (e.Message == "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.")
                        {
                            LoginUrl.Text = LoginUrl.Text.Replace("http", "https");
                            LogIntoEquip(LoginUserName.Text, LoginPassWord.Text, LoginUrl.Text, second_attempt);
                        }
                        else
                        {
                            await DisplayAlert("Error", e.Message + " Please check the url or connection.", "OK");
                        }
                    }
                    catch (Exception) { }
                }
            }
        }