Beispiel #1
0
        public void Execute(Message message, AlarmOptions opt)
        {
            if (opt == AlarmOptions.clear)
            {
                foreach (Alarm a in alarms)
                {
                    StopAlarm(a);
                }
                alarms.Clear();

                message.Reply("Cleared all alarms!");
            }
            else
            {
                string s = "List of all alarms:\n";
                for (int i = 0; i < alarms.Count; i++)
                {
                    Alarm a = alarms[i];
                    if (a.proc.HasExited)
                    {
                        alarms.RemoveAt(i);
                        i--;
                    }
                    else
                    {
                        s += a.ToString() + "\n";
                    }
                }

                message.Reply(s);
            }
        }
        private DateTime CalculateNextTime(DateTime startTime, AlarmOptions alarmOptions)
        {
            DateTime calculatedDate   = startTime;
            int      selectedInterval = alarmOptions.Interval <= 0 ? 1 : alarmOptions.Interval;

            if (startTime < DateTime.Now)
            {
                switch (alarmOptions.AlarmSequence)
                {
                case Enums.AlarmSequence.Minute:
                    return(startTime.AddMinutes(selectedInterval));

                case Enums.AlarmSequence.Hourly:
                    return(startTime.AddHours(selectedInterval));

                case Enums.AlarmSequence.Daily:
                    calculatedDate = GetNextTime(startTime, alarmOptions);
                    return(calculatedDate > DateTime.Now ? calculatedDate : startTime.AddDays(selectedInterval));

                case Enums.AlarmSequence.Weekly:
                    calculatedDate = GetNextTime(startTime, alarmOptions);
                    if (calculatedDate > DateTime.Now)
                    {
                        return(calculatedDate);
                    }

                    List <DateTime> days = null;

                    if (alarmOptions?.DaysOfWeek != Enums.DaysOfWeek.None)
                    {
                        var dayList = alarmOptions?.DaysOfWeek.GetUniqueFlags();
                        foreach (DayOfWeek dayitem in dayList)
                        {
                            days.Add(GetNextWeekday(startTime, dayitem, selectedInterval));
                        }
                        calculatedDate = days.OrderBy(o => o).FirstOrDefault(w => DateTime.Now > w);
                        if (calculatedDate > DateTime.Now)
                        {
                            return(calculatedDate);
                        }
                    }

                    calculatedDate = GetNextWeekday(startTime, startTime.DayOfWeek, alarmOptions?.Interval);
                    return(calculatedDate);

                case Enums.AlarmSequence.Monthly:
                    return(startTime.AddMonths(selectedInterval));

                case Enums.AlarmSequence.Yearly:
                    return(startTime.AddYears(selectedInterval));

                default:
                    return(calculatedDate);
                }
            }

            return(startTime);
        }
Beispiel #3
0
        public override void OnReceive(Context context, Intent intent)
        {
            Log.Debug("Alarm", "AlarmNotificationReceiver Started : " + DateTime.Now.ToString());
            var alarmer = new AlarmerImplementation();

            if (intent.Action != null && intent.Action == Intent.ActionBootCompleted)
            {
                Log.Debug("Alarm", "AlarmNotificationReceiver Reboot Started : " + DateTime.Now.ToString());
                StartAfterReboot(alarmer);
            }
            else
            {
                try
                {
                    var notificationId = intent.GetIntExtra(Consts.NotificationIdKey, 0);

                    var message         = intent.GetStringExtra(Consts.MessageKey);
                    var title           = intent.GetStringExtra(Consts.TitleKey);
                    int alarmRunCounter = intent.GetIntExtra(Consts.AlarmCounterKey, 0);

                    Log.Debug("Alarm", "AlarmNotificationReceiver Started : " + notificationId.ToString());

                    NotificationOptions options      = JsonConvert.DeserializeObject <NotificationOptions>(intent.GetStringExtra(Consts.OptionsKey));
                    AlarmOptions        alarmOptions = JsonConvert.DeserializeObject <AlarmOptions>(intent.GetStringExtra(Consts.AlarmOptionsKey));
                    DateTime            dateTime     = JsonConvert.DeserializeObject <DateTime>(intent.GetStringExtra(Consts.StartDateKey));


                    if (alarmOptions?.AlarmSequence != Shared.Enums.AlarmSequence.OneTime)
                    {
                        Log.Debug("Alarm", "AlarmNotificationReceiver alarmRunCounter : " + alarmRunCounter.ToString());
                        alarmer.AlarmCounter = alarmRunCounter;
                        Log.Debug("Alarm", "AlarmNotificationReceiver alarmRunCounter : " + alarmer.AlarmCounter.ToString());
                        alarmer.Schedule(notificationId, title, message, dateTime, alarmOptions, options);
                    }

                    Log.Debug("Alarm", "AlarmNotificationReceiver Started : " + options.ToString());

                    alarmer.Notify(title, message, notificationId, options);
                    Log.Debug("Alarm", "AlarmNotificationReceiver finished : " + DateTime.Now.ToString());
                }
                catch (Exception ex)
                {
                    var messages = new List <string>();
                    do
                    {
                        messages.Add(ex.Message);
                        ex = ex.InnerException;
                    }while (ex != null);
                    var message = string.Join(" - ", messages);

                    Console.WriteLine("AlarmNotificationReceiver : " + message);
                    Log.Error("Alarm", "AlarmNotificationReceiver : " + ex.Message);
                }
            }
        }
        private DateTime GetNextTime(DateTime startTime, AlarmOptions alarmOptions)
        {
            if (alarmOptions?.AdditionalTimes != null)
            {
                var nxt = alarmOptions.AdditionalTimes.OrderBy(o => o).First(w => DateTime.Now < new DateTime(startTime.Year, startTime.Month, startTime.Day, w.Hours, w.Minutes, w.Seconds));
                if (nxt != null)
                {
                    return(new DateTime(startTime.Year, startTime.Month, startTime.Day, nxt.Hours, nxt.Minutes, nxt.Seconds));
                }
            }

            return(startTime);
        }
        public async Task <AlarmModel> GetAlarm(int id)
        {
            var alarmEntity = await _alarmRepo.GetAsync(id);

            var customActionEntity = await _customActionRepository.QueryAsync().Where(w => w.AlarmId == id).ToArrayAsync();

            var timingEntity = await _timingRepository.QueryAsync().Where(w => w.AlarmId == id).ToArrayAsync();

            if (alarmEntity != null)
            {
                AlarmModel          model        = new AlarmModel();
                AlarmOptions        options      = new AlarmOptions();
                NotificationOptions notification = new NotificationOptions();

                options = new AlarmOptions
                {
                    AdditionalTimes = timingEntity?.Select(s => s.Time).ToArray(),
                    AlarmSequence   = alarmEntity.AlarmSequence,
                    DaysOfWeek      = alarmEntity.DaysOfWeek,
                    EndDate         = alarmEntity.EndDate,
                    Interval        = alarmEntity.Interval,
                    TotalAlarmCount = alarmEntity.TotalAlarmCount
                };
                model.AlarmOptions = options;

                notification = new NotificationOptions
                {
                    EnableSound     = alarmEntity.EnableSound,
                    EnableVibration = alarmEntity.EnableVibration,
                    LargeIcon       = alarmEntity.LargeIcon,
                    SmallIcon       = alarmEntity.SmallIcon,
                    CustomActions   = customActionEntity?.Select(s => { return(new CustomAction {
                            Icon = s.Icon, Name = s.Name
                        }); }).ToArray()
                };

                model.NotificationOptions = notification;


                model.Id        = alarmEntity.Id;
                model.Message   = alarmEntity.Message;
                model.StartDate = alarmEntity.StartDate;
                model.Title     = alarmEntity.Title;

                return(model);
            }

            return(null);
        }
Beispiel #6
0
        private async Task StartAfterReboot(AlarmerImplementation alarmer)
        {
            AlarmRepository        _alarmRepo              = new AlarmRepository();
            TimingRepository       _timingRepository       = new TimingRepository();
            CustomActionRepository _customActionRepository = new CustomActionRepository();

            try
            {
                var alarmList = await _alarmRepo.GetListAsync();

                if (alarmList != null && alarmList.Count > 0)
                {
                    foreach (var alarm in alarmList)
                    {
                        var timings = await _timingRepository.QueryAsync().Where(w => w.AlarmId == alarm.Id).ToArrayAsync();

                        var customs = await _customActionRepository.QueryAsync().Where(w => w.AlarmId == alarm.Id).ToArrayAsync();

                        var alarmOption = new AlarmOptions
                        {
                            EndDate         = alarm.EndDate,
                            AdditionalTimes = timings.Select(s => s.Time).ToArray(),
                            AlarmSequence   = alarm.AlarmSequence,
                            DaysOfWeek      = alarm.DaysOfWeek,
                            Interval        = alarm.Interval,
                            TotalAlarmCount = alarm.TotalAlarmCount
                        };

                        var notification = new NotificationOptions
                        {
                            EnableSound     = alarm.EnableSound,
                            EnableVibration = alarm.EnableVibration,
                            LargeIcon       = alarm.LargeIcon,
                            SmallIcon       = alarm.SmallIcon,
                            CustomActions   = customs.Select(s => { return(new CustomAction {
                                    Icon = s.Icon, Name = s.Name
                                }); }).ToArray()
                        };

                        alarmer.Schedule(alarm.Id, alarm.Title, alarm.Message, alarm.StartDate, alarmOption, notification, isNew: false);
                    }
                }
            }
            catch (Exception)
            {
            }
        }
 public Task <int> Schedule(int?id, string title, string message, DateTime startTime, AlarmOptions alarmOptions, NotificationOptions options)
 {
     throw new NotImplementedException();
 }
        public async Task <int> Schedule(int?notificationId, string title, string message, DateTime startTime, AlarmOptions alarmOptions, NotificationOptions options, bool isNew = false)
        {
            var list = GetAlarmIds();

            if (notificationId == null)
            {
                if (list.Count > 0)
                {
                    notificationId = list.Max() + 1;
                }
                else
                {
                    notificationId = 1;
                }
            }

            if (alarmOptions.AlarmSequence != Enums.AlarmSequence.OneTime)
            {
                AlarmCounter++;
            }

            if (alarmOptions?.TotalAlarmCount != null && alarmOptions?.TotalAlarmCount > 0 && AlarmCounter > alarmOptions.TotalAlarmCount)
            {
                return(notificationId.Value);
            }

            if (alarmOptions?.EndDate != null && alarmOptions.EndDate < DateTime.Now)
            {
                return(notificationId.Value);
            }
            DateTime nextTime = CalculateNextTime(startTime, alarmOptions);

            Log.Debug("Alarm", "AlarmNotificationReceiver Schedule : " + AndroidApp.Application.Context.ToString());
            var alarmIntent   = CreateAlarmIntent(title, message, nextTime, alarmOptions, notificationId.Value, options);
            var pendingIntent = PendingIntent.GetBroadcast(AndroidApp.Application.Context, notificationId.Value, alarmIntent, PendingIntentFlags.CancelCurrent);

            SetAlarmManager(nextTime, pendingIntent);

            if (isNew)
            {
                SaveAlarmId(notificationId.Value);

                await SaveAlarm(notificationId.Value, title, message, nextTime, alarmOptions, options);
            }

            return(notificationId.Value);
        }
 public Task <int> Schedule(int?notificationId, string title, string message, DateTime startTime, AlarmOptions alarmOptions, NotificationOptions options)
 {
     return(this.Schedule(notificationId, title, message, startTime, alarmOptions, options, true));
 }
        private Intent CreateAlarmIntent(string title, string message, DateTime startTime, AlarmOptions alarmOptions, int notificationId, NotificationOptions options)
        {
            Intent intent = new Intent(AndroidApp.Application.Context, typeof(AlarmNotificationReceiver));

            intent.SetAction(GetActionName(notificationId));
            intent.PutExtra(Consts.NotificationIdKey, notificationId);
            intent.PutExtra(Consts.TitleKey, title);
            intent.PutExtra(Consts.MessageKey, message);
            intent.PutExtra(Consts.AlarmCounterKey, AlarmCounter);
            intent.PutExtra(Consts.StartDateKey, JsonConvert.SerializeObject(startTime));
            if (alarmOptions != null)
            {
                intent.PutExtra(Consts.AlarmOptionsKey, JsonConvert.SerializeObject(alarmOptions));
            }
            if (options != null)
            {
                intent.PutExtra(Consts.OptionsKey, JsonConvert.SerializeObject(options));
            }

            return(intent);
        }
        private async Task SaveAlarm(int notificationId, string title, string message, DateTime startDate, AlarmOptions options, NotificationOptions notification)
        {
            try
            {
                var entity = new AlarmEntity
                {
                    Id        = notificationId,
                    Message   = message,
                    StartDate = startDate,
                    Title     = title
                };

                if (options != null)
                {
                    entity.AlarmSequence   = options.AlarmSequence;
                    entity.DaysOfWeek      = options.DaysOfWeek;
                    entity.EndDate         = options.EndDate;
                    entity.Interval        = options.Interval;
                    entity.TotalAlarmCount = options.TotalAlarmCount;
                    entity.IsEnabled       = options.IsEnabled;
                    entity.EnableSound     = notification.EnableSound;
                    entity.EnableVibration = notification.EnableVibration;
                    entity.LargeIcon       = notification.LargeIcon;
                    entity.SmallIcon       = notification.SmallIcon;
                }

                await _alarmRepo.UpdateOrInsertAsync(entity, notificationId);

                if (options?.AdditionalTimes != null)
                {
                    foreach (var item in options.AdditionalTimes)
                    {
                        await _timingRepository.UpdateOrInsertAsync(new TimingEntity
                        {
                            AlarmId = notificationId,
                            Time    = item
                        }, notificationId);
                    }
                }
                if (notification.CustomActions != null)
                {
                    foreach (var item in notification.CustomActions)
                    {
                        await _customActionRepository.UpdateOrInsertAsync(new CustomActionEntity
                        {
                            AlarmId = notificationId,
                            Icon    = item.Icon,
                            Name    = item.Name
                        }, notificationId);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("SQLite : " + ex.Message);
            }
        }