Ejemplo n.º 1
0
        public async Task <EbMobileSolutionData> GetSolutionDataAsync(Loader loader)
        {
            if (App.Settings.SyncInProgress)
            {
                EbLog.Info(App.Settings.Sid + ": Sync in progress...");
                return(null);
            }
            App.Settings.SyncInProgress = true;
            EbLog.BackupLogFiles();
            EbMobileSolutionData solutionData = null;
            bool flag = false;

            try
            {
                loader.Message = "Sync started...";
                EbLog.Info("Sync started...");

                LocalDBServie service  = new LocalDBServie();
                SyncResponse  response = await service.PushDataToCloud(loader);

                if (response.Status)
                {
                    loader.Message = string.Empty;
                }
                else
                {
                    loader.Message = response.Message + " \n";
                }

                loader.Message += "Fetching data from server...";
                EbLog.Info("Fetching data from server...");

                RestClient client = new RestClient(App.Settings.RootUrl)
                {
                    Timeout = ApiConstants.TIMEOUT_IMPORT
                };
                RestRequest request = new RestRequest(ApiConstants.GET_SOLUTION_DATAv2, Method.POST);

                request.AddHeader(AppConst.BTOKEN, App.Settings.BToken);
                request.AddHeader(AppConst.RTOKEN, App.Settings.RToken);
                Dictionary <string, object> metaDict = new Dictionary <string, object>();
                metaDict.Add(AppConst.draft_ids, GetErrorDraftIds());
                INativeHelper helper = DependencyService.Get <INativeHelper>();
                metaDict.Add(AppConst.app_version, helper.AppVersion);
                metaDict.Add(AppConst.device_id, helper.DeviceId);
                request.AddParameter(AppConst.metadata, JsonConvert.SerializeObject(metaDict));

                IRestResponse resp = await client.ExecuteAsync(request);

                if (resp.IsSuccessful)
                {
                    loader.Message = "Processing pulled data...";

                    LastSyncInfo syncInfo = App.Settings.SyncInfo;
                    if (syncInfo == null)
                    {
                        syncInfo = new LastSyncInfo();
                    }
                    solutionData = JsonConvert.DeserializeObject <EbMobileSolutionData>(resp.Content);

                    (bool incorrectDate, bool maintenanceMode, string msg) = await UpdateLastSyncInfo(solutionData, syncInfo, helper.AppVersion);

                    if (!maintenanceMode)
                    {
                        await ImportSolutionData(solutionData);

                        loader.Message = "Importing latest document ids...";
                        EbLog.Info("Importing latest document ids...");
                        if (await GetLatestAutoId(solutionData.Applications))
                        {
                            if (!incorrectDate)
                            {
                                syncInfo.LastSyncTs        = solutionData.last_sync_ts;
                                syncInfo.LastOfflineSaveTs = solutionData.last_sync_ts;
                            }
                            else
                            {
                                Utils.Toast(msg);
                            }

                            syncInfo.PullSuccess = true;
                            await Store.SetJSONAsync(AppConst.LAST_SYNC_INFO, syncInfo);

                            flag = true;
                        }
                        else
                        {
                            Utils.Toast("Failed to import latest doc ids");
                        }
                    }
                    else
                    {
                        Utils.Toast(msg);
                        syncInfo.PullSuccess = true;
                        await Store.SetJSONAsync(AppConst.LAST_SYNC_INFO, syncInfo);
                    }
                }
                else
                {
                    Utils.Toast(response.Message ?? "Sync failed");
                    EbLog.Warning(response.Message ?? "Sync failed");
                }
            }
            catch (Exception ex)
            {
                EbLog.Error("Error on [GetSolutionData] request" + ex.Message);
                Utils.Toast(ex.Message);
            }
            loader.IsVisible            = false;
            App.Settings.SyncInProgress = false;
            return(flag ? solutionData : null);
        }
Ejemplo n.º 2
0
        //version 2
        public async Task <SyncResponse> GetSolutionDataAsyncV2(Loader loader)
        {
            EbLog.BackupLogFiles();
            SyncResponse resp   = new SyncResponse();
            RestClient   client = new RestClient(App.Settings.RootUrl)
            {
                Timeout = ApiConstants.TIMEOUT_IMPORT
            };
            RestRequest request = new RestRequest(ApiConstants.GET_SOLUTION_DATAv2, Method.POST);

            request.AddHeader(AppConst.BTOKEN, App.Settings.BToken);
            request.AddHeader(AppConst.RTOKEN, App.Settings.RToken);

            Dictionary <string, object> metaDict = new Dictionary <string, object>();
            LastSyncInfo syncInfo = App.Settings.SyncInfo;

            if (syncInfo == null)
            {
                syncInfo = new LastSyncInfo();
            }

            if (syncInfo.LastSyncTs != DateTime.MinValue)
            {
                metaDict.Add(AppConst.last_sync_ts, syncInfo.LastSyncTs);
            }
            metaDict.Add(AppConst.draft_ids, GetErrorDraftIds());
            INativeHelper helper = DependencyService.Get <INativeHelper>();

            metaDict.Add(AppConst.app_version, helper.AppVersion);
            metaDict.Add(AppConst.device_id, helper.DeviceId);
            request.AddParameter(AppConst.metadata, JsonConvert.SerializeObject(metaDict));

            EbMobileSolutionData solutionData = null;

            try
            {
                IRestResponse response = await client.ExecuteAsync(request);

                if (response.IsSuccessful)
                {
                    Device.BeginInvokeOnMainThread(() => { loader.Message = "Processing pulled data..."; });

                    solutionData = JsonConvert.DeserializeObject <EbMobileSolutionData>(response.Content);

                    (bool incorrectDate, bool maintenanceMode, string msg) = await UpdateLastSyncInfo(solutionData, syncInfo, helper.AppVersion);

                    resp.Message = msg;

                    if (!maintenanceMode)
                    {
                        List <AppData> oldAppData = Store.GetJSON <List <AppData> >(AppConst.APP_COLLECTION);
                        MergeObjectsInSolutionData(solutionData, oldAppData);
                        await ImportSolutionData(solutionData);

                        Device.BeginInvokeOnMainThread(() => { loader.Message = "Importing latest document ids..."; });
                        EbLog.Info("Importing latest document ids...");
                        if (await GetLatestAutoId(solutionData.Applications))
                        {
                            if (!incorrectDate)
                            {
                                syncInfo.LastSyncTs        = solutionData.last_sync_ts;
                                syncInfo.LastOfflineSaveTs = solutionData.last_sync_ts;
                            }
                            resp.Status          = true;
                            syncInfo.PullSuccess = true;
                            await Store.SetJSONAsync(AppConst.LAST_SYNC_INFO, syncInfo);

                            EbLog.Info("[GetSolutionDataAsyncV2] api success");
                        }
                        else
                        {
                            resp.Message = "Failed to import latest doc ids";
                        }
                    }
                    else
                    {
                        syncInfo.PullSuccess = true;
                        await Store.SetJSONAsync(AppConst.LAST_SYNC_INFO, syncInfo);
                    }
                }
                else
                {
                    //callback?.Invoke(response.ResponseStatus);
                    resp.Message = "Pull failed. Try after sometime.";
                    EbLog.Info("[GetSolutionData] api failure, callback invoked");
                }
            }
            catch (Exception ex)
            {
                resp.Message = "Exception: " + ex.Message;
                EbLog.Error("Error on [GetSolutionData] request" + ex.Message);
            }
            return(resp);
        }