public async Task <bool> AssignToMe(int interventionId)
        {
            var response = await restClient.GetAsync(ApiURI.URL_BASE(Settings.CurrentAccount) + ApiURI.URL_SET_INTERVENTION_ASSIGN(Settings.CurrentUserName, Settings.CurrentPassword, interventionId));

            var content = await response.Content.ReadAsStringAsync();

            if (response.IsSuccessStatusCode)
            {
                var result = JsonConvert.DeserializeObject <JObject>(content);
                if (result != null)
                {
                    if (result.GetValue("MESSAGE") != null)
                    {
                        Debug.WriteLine("Assiggn Intervention successed: " + content);
                        return(await Task.FromResult(true));
                    }
                    else if (result.GetValue("ERROR") != null)
                    {
                        Debug.WriteLine("Intervention already assigned: " + content);
                        return(await Task.FromResult(false));
                    }
                }
                throw new Exception(content);
            }
            else
            {
                Debug.WriteLine("Assiggn Intervention failed: " + content);
                throw new Exception(content);
            }
        }
        public async Task <List <Intervention> > GetHistory(int interventionId)
        {
            var response = await restClient.GetAsync(ApiURI.URL_BASE(Settings.CurrentAccount) + ApiURI.URL_GET_INTERVENTION_HISTORY(Settings.CurrentUserName, Settings.CurrentPassword, interventionId));

            var content = await response.Content.ReadAsStringAsync();

            if (response.IsSuccessStatusCode)
            {
                var result = JsonConvert.DeserializeObject <InterventionHistoryRespone>(content);
                if (result != null)
                {
                    List <Intervention> interventions = new List <Intervention>();
                    if (result.Interventions != null)
                    {
                        foreach (var intRes in result.Interventions)
                        {
                            interventions.Add(new Intervention(intRes));
                        }
                    }
                    return(interventions);
                }
            }
            else
            {
                Debug.WriteLine("Get Intervention History failed: " + content);
                return(null);
            }

            return(null);
        }
        public async Task <bool> SyncFromServer(int method)
        {
            timeStart        = DateTime.Now;
            totalRowInserted = 0;
            totalRowUpdated  = 0;

            Debug.WriteLine("Sync Product From Server Started!");

            bool NeedToSync = true;
            int  page       = 0;

            while (NeedToSync)
            {
                var response = await restClient.GetStringAsync(ApiURI.URL_BASE(CurrentAccount) + ApiURI.URL_GET_PRODUCTS(CurrentUserName, Settings.CurrentPassword, Settings.LastSyncInvoice, page));

                if (!string.IsNullOrWhiteSpace(response) && !response.Equals("[]") && !(response.Contains("SUCCESS\":\"No result") || response.Equals("{\"SUCCESS\":\"No result\"}")))
                {
                    ProcessData(response);
                    page++;
                }
                else
                {
                    SetLastSyncProduct();
                    NeedToSync = false;
                }
            }

            Debug.WriteLine("Sync Product Time: " + DateTime.Now.Subtract(timeStart).TotalSeconds);
            Debug.WriteLine("Total Product Inserted: " + totalRowInserted);
            Debug.WriteLine("Total Product Updated: " + totalRowUpdated);

            Debug.WriteLine("Sync Product From Server Ended!");

            return(await Task.FromResult(true));
        }
        public async Task <bool> SyncToServer(int method)
        {
            try {
                var equipments = App.LocalDb.Table <Equipment>().Where(inv => inv.UserId == CurrentUser.Id && inv.IsToSync).ToArray();
                var interventions_euipments = App.LocalDb.Table <LinkInterventionEquipment>().Where(lie => lie.UserId == CurrentUser.Id && lie.IsToSync).ToArray();

                JObject @params = new JObject();

                if (equipments.LongLength > 0)
                {
                    @params.Add(new JProperty("equipments", JArray.FromObject(equipments).RemoveEmptyChildren(equipments.FirstOrDefault()?.PropertyIgnore)));
                }

                if (interventions_euipments.LongLength > 0)
                {
                    @params.Add(new JProperty("equipements_links", JArray.FromObject(interventions_euipments).RemoveEmptyChildren(interventions_euipments.FirstOrDefault()?.PropertyIgnore)));
                }

                if (@params.Count > 0)
                {
                    @params.Add(new JProperty("api_version", ApiURI.API_MOBILE_TO_SERVER_VERSION));
                    @params.Add(new JProperty("appVersion", ApiURI.APP_VERSION));
                }
                else
                {
                    return(await Task.FromResult(true));
                }

                var response = await restClient.PostAsync(ApiURI.URL_BASE(CurrentAccount) + ApiURI.URL_SET_EQUIPMENT(CurrentUserName, Settings.CurrentPassword), @params);

                var responseContent = await response.Content.ReadAsStringAsync();

                Debug.WriteLine("RESPONSE: " + responseContent);
                if (response.IsSuccessStatusCode)
                {
                    var result = JsonConvert.DeserializeObject <SetSyncEquipment>(responseContent, App.DefaultDeserializeSettings);
                    if (result != null && result.Success.Equals("OK"))
                    {
                        foreach (var inv in equipments)
                        {
                            if (result.Equipements.FirstOrDefault(c => ((c.ServerId > 0 && c.ServerId == inv.ServerId) || Guid.TryParse(c.AppId, out Guid id) && inv.Id.Equals(id))) is EquipmentResponse invr && invr.ServerId > 0)
                            {
                                var equipment = inv.DeepCopy();
                                App.LocalDb.Delete(inv);
                                equipment.ServerId = invr.ServerId;
                                equipment.CodeId   = invr.CodeId;
                                //invoice.SynchronizationDate = DateTime.Now;

                                equipment.IsToSync = false;
                                App.LocalDb.Insert(equipment);
                            }
                            else
                            {
                                inv.IsToSync = false;
                                //inv.SynchronizationDate = DateTime.Now;
                                App.LocalDb.Update(inv);
                            }
                        }

                        /*
                         * foreach (var invl in interventions_euipments)
                         * {
                         *  if (result.InvoicesLines.FirstOrDefault(a => ((a.ServerId > 0 && a.ServerId == invl.ServerId) || Guid.TryParse(a.AppId, out Guid id) && invl.Id.Equals(id))) is Models.SetSync.InvoiceLineResponse invlr && invlr.ServerId > 0)
                         *  {
                         *      var invoiceProduct = invl.DeepCopy();
                         *      App.LocalDb.Delete(invl);
                         *      invoiceProduct.ServerId = invlr.ServerId;
                         *      //invoiceProduct.SynchronizationDate = DateTime.Now;
                         *      invoiceProduct.IsToSync = false;
                         *      App.LocalDb.Insert(invoiceProduct);
                         *  }
                         *  else
                         *  {
                         *      invl.IsToSync = false;
                         *      //invl.SynchronizationDate = DateTime.Now;
                         *      App.LocalDb.Update(invl);
                         *  }
                         * }*/

                        Debug.WriteLine("SET_SYNC_Equipment Successed!");
                    }
                }
Exemple #5
0
        public async Task <LoginResponse> Login(string account, string userName, string password)
        {
            var result = await restClient.GetStringAsync <LoginResponse>(ApiURI.URL_BASE(account) + ApiURI.URL_GET_LOGIN(userName, password));

            return(await Task.FromResult(result));
        }