async static Task <BackgroundTaskModel> PopFromBackgroundQueue(int TaskTypeId, bool getLatest = true)
        {
            Service = new SimpleDataService <BackgroundTaskModel>(Connection, TableConsts.BACKGROUND_TASK_TABLE);
            var backgroundQueue = await Service.GetItemsAsync();

            var elements = backgroundQueue;

            BackgroundTaskModel popedElement = null;

            if (TaskTypeId != -1)
            {
                elements = backgroundQueue.Where(t => t.ID_TaskType == TaskTypeId);
            }

            if (getLatest)
            {
                popedElement = elements
                               .OrderByDescending(x => x.CreationDateTime)
                               .FirstOrDefault();
            }

            popedElement = elements
                           .OrderBy(x => x.CreationDateTime)
                           .FirstOrDefault();

            if (popedElement != null)
            {
                await RemoveElementFromQueue(Connection, popedElement);
            }

            return(popedElement);
        }
        public static async Task <Guid> PushWheaterRequestToBackgroundQueue(SQLiteConnection connection, Guid RefId, double Lat, double Lng)
        {
            Connection = connection;

            try
            {
                var queueElement = new BackgroundTaskModel(true)
                {
                    ID_TaskType         = (int)EnumHelper.TaskTypeEnum.WheaterTask,
                    ID_ElementReference = RefId,
                    CreationDateTime    = DateTime.Now,
                    TaskData            = new JSONHelper <WeatherTaskRequestModel>().Serialize(
                        new WeatherTaskRequestModel()
                    {
                        CultureInfo     = CultureInfo.CurrentCulture,
                        RequestDateTime = DateTime.Now,
                        Lat             = Lat,
                        Lng             = Lng
                    })
                };

                if (await PushToBackgroundQueue(queueElement) != null)
                {
                    return(queueElement.Id);
                }
                else
                {
                    return(Guid.Empty);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Exemplo n.º 3
0
        static Task <BackgroundTaskModel> ProzessElement(BackgroundTaskModel model, CancellationToken token)
        {
            return(Task.Run(() =>
            {
                switch (model.ID_TaskType)
                {
                case (int)EnumHelper.TaskTypeEnum.WheaterTask:
                    ProzessWheaterRequest(ref model);
                    break;

                default:
                    throw new Exception("TaskType not valid!");
                }

                if (model.ProcessedSuccessfully)
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        MessagingCenter.Send(new ElementFinishedMessage()
                        {
                            BackgroundTask = model
                        }, MessageHelper.ELEMENT_FINISHED_MESSAGE);
                    });
                }

                return model;
            }, token));
        }
Exemplo n.º 4
0
        private void RegisterBackgroundTask(string taskName, string taskEntryPoint)
        {
            using (var fileService = new FileService())
            {
                var localFoldeer = FileSystem.AppDataDirectory;
                var initFilePath = $"{localFoldeer}/init.bin";
                var initFile     = Task.Run(() =>
                                            fileService.Read <InitFile>(initFilePath)).TryTo().Result;
                if (initFile.LastResetTime.TimeOfDay == DateTime.Now.TimeOfDay)
                {
                    return;
                }

                if ((uint)(DateTime.Today.AddDays(1) - DateTime.Now).TotalMinutes < 15)
                {
                    return;
                }

                var resetDailyTasks = new BackgroundTaskModel
                {
                    Title      = taskName,
                    EntryPoint = taskEntryPoint,
                    Trigger    = GetTaskTrigger(taskName),
                    OnComplete = new BackgroundTaskCompletedEventHandler(OnTaskComplete)
                };

                GlobalDataService.UnregisterBackgroundTask(taskName);
                GlobalDataService.RegisterBackgroundTask(resetDailyTasks);
            }
        }
 async static Task <BackgroundTaskModel> PushToBackgroundQueue(BackgroundTaskModel model)
 {
     try
     {
         Service = new SimpleDataService <BackgroundTaskModel>(Connection, TableConsts.BACKGROUND_TASK_TABLE);
         return(await Service.SaveItemAsync(model));
     }
     catch (Exception ex)
     {
         throw;
     }
 }
Exemplo n.º 6
0
        public static async Task RunBackgroundWorkerService(SQLiteConnection connection, CancellationToken token)
        {
            if (IsRunning)
            {
                return;
            }

            await Task.Run(async() =>
            {
                _isRunning = true;


                var cnt = await BackgroundQueueService.GetQueueElementCount(connection);
                BackgroundTaskModel element = null;

                while (cnt > 0)
                {
                    token.ThrowIfCancellationRequested();

                    element = await BackgroundQueueService.PopFromBackgroundQueue();

                    if (!element.ProcessedSuccessfully)
                    {
                        await ProzessElement(element, token);
                    }

                    if (element.ProcessedSuccessfully)
                    {
                        await BackgroundQueueService.RemoveElementFromQueue(element);
                    }

                    cnt = cnt - 1;
                }

                _isRunning = false;
            }, token);
        }
Exemplo n.º 7
0
        static void ProzessWheaterRequest(ref BackgroundTaskModel model)
        {
            try
            {
                IWeatherService ws = WeatherServiceFactory.GetWeatherServiceFactory();

                var response = ws.GetWeatherData(
                    new JSONHelper <WeatherTaskRequestModel>().Deserialize(
                        model.TaskData)
                    );

                if (response != null)
                {
                    model.TaskResponse          = new JSONHelper <WeatherTaskResponseModel>().Serialize(response.Result);
                    model.ProcessedSuccessfully = true;
                    return;
                }

                model.ProcessedSuccessfully = false;
            }catch (Exception ex)
            {
                throw;
            }
        }
        public static async Task <bool> RemoveElementFromQueue(SQLiteConnection connection, BackgroundTaskModel model)
        {
            try
            {
                Connection = connection;

                Service = new SimpleDataService <BackgroundTaskModel>(Connection, TableConsts.BACKGROUND_TASK_TABLE);
                return(await Service.DeleteItemAsync(model));
            }
            catch (Exception ex)
            {
                throw;
            }
        }
 public static async Task <bool> RemoveElementFromQueue(BackgroundTaskModel model)
 {
     return(await RemoveElementFromQueue(Connection, model));
 }