1 // Copyright (C) 2017 Kazuhiro Fujieda <fujieda@users.osdn.me>
\r
3 // Licensed under the Apache License, Version 2.0 (the "License");
\r
4 // you may not use this file except in compliance with the License.
\r
5 // You may obtain a copy of the License at
\r
7 // http://www.apache.org/licenses/LICENSE-2.0
\r
9 // Unless required by applicable law or agreed to in writing, software
\r
10 // distributed under the License is distributed on an "AS IS" BASIS,
\r
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
12 // See the License for the specific language governing permissions and
\r
13 // limitations under the License.
\r
16 using System.Collections.Generic;
\r
19 namespace KancolleSniffer.Notification
\r
21 public class Scheduler
\r
23 private readonly Action<string, string, string> _alarm;
\r
24 private readonly List<Notification> _queue = new List<Notification>();
\r
25 private readonly Func<DateTime> _nowFunc = () => DateTime.Now;
\r
26 private readonly Formatter _formatter = new Formatter();
\r
27 private DateTime _lastAlarm;
\r
28 private bool _suspend;
\r
29 private string _suspendException;
\r
39 public class Notification
\r
41 public string Key { get; set; }
\r
42 public int Fleet { get; set; }
\r
43 public string Subject { get; set; }
\r
44 public int Repeat { get; set; }
\r
45 public Mode Mode { get; set; }
\r
46 public DateTime Schedule { get; set; }
\r
49 public Scheduler(Action<string, string, string> alarm, Func<DateTime> nowFunc = null)
\r
52 if (nowFunc != null)
\r
56 public void Enqueue(string key, int fleet, string subject, int repeat = 0, bool preliminary = false)
\r
58 _queue.Add(new Notification
\r
64 Mode = preliminary ? Mode.Preliminary : Mode.Normal
\r
68 public void Enqueue(string key, string subject, int repeat = 0)
\r
70 Enqueue(key, 0, subject, repeat);
\r
78 public void StopRepeat(string key, bool cont = false)
\r
82 _queue.RemoveAll(n => IsMatch(n, key));
\r
86 foreach (var n in _queue.Where(n => IsMatch(n, key)))
\r
94 public void StopRepeat(string key, int fleet)
\r
96 _queue.RemoveAll(n => IsMatch(n, key) && n.Fleet == fleet);
\r
99 public void StopRepeat(string key, string subject)
\r
101 _queue.RemoveAll(n => IsMatch(n, key) && n.Subject == subject);
\r
104 public void StopAllRepeat()
\r
106 _queue.RemoveAll(n => n.Schedule != default);
\r
109 public void SuspendRepeat(string exception = "")
\r
112 _suspendException = exception;
\r
115 public void ResumeRepeat()
\r
120 public string KeyToName(string key) => Formatter.KeyToName(key);
\r
122 private bool IsMatch(Notification n, string key) =>
\r
123 n.Key.Substring(0, 4) == key.Substring(0, 4) && n.Schedule != default;
\r
125 private void Alarm()
\r
127 var now = _nowFunc();
\r
128 if (now - _lastAlarm < TimeSpan.FromSeconds(2))
\r
130 var first = _queue.FirstOrDefault(n => n.Schedule.CompareTo(now) <= 0 &&
\r
131 !(n.Schedule != default && _suspend && n.Key != _suspendException));
\r
134 var message = _formatter.GenerateMessage(first);
\r
135 var similar = _queue.Where(n =>
\r
136 _formatter.GenerateMessage(n).Name == message.Name && n.Schedule.CompareTo(now) <= 0)
\r
138 var body = string.Join("\r\n", similar.Select(n => _formatter.GenerateMessage(n).Body));
\r
139 foreach (var n in similar)
\r
147 n.Schedule = now + TimeSpan.FromSeconds(n.Repeat);
\r
148 if (n.Mode == Mode.Normal)
\r
149 n.Mode = Mode.Repeat;
\r
152 _alarm(message.Title, body, message.Name);
\r