Commit 022a517bd5ea7dc6261a3ac53b2e705d9d57d8d4
1 parent
68eb64d2
Missing scheduler files
Showing
4 changed files
with
234 additions
and
0 deletions
services/MainBot/src/main/java/de/bht/beuthbot/scheduler/JAXActivator.java
0 → 100644
1 | +package de.bht.beuthbot.scheduler; | ||
2 | + | ||
3 | +import javax.ws.rs.ApplicationPath; | ||
4 | +import javax.ws.rs.core.Application; | ||
5 | + | ||
6 | +/** | ||
7 | + * JAXActivator is an arbitrary name, what is important is that javax.ws.rs.core.Application is extended | ||
8 | + * and the @ApplicationPath annotation is used with a "rest" path. Without this the rest routes linked to | ||
9 | + * from index.html would not be found. | ||
10 | + */ | ||
11 | +@ApplicationPath("") | ||
12 | +public class JAXActivator extends Application { | ||
13 | + // Left empty intentionally | ||
14 | +} |
services/MainBot/src/main/java/de/bht/beuthbot/scheduler/SchedulerBean.java
0 → 100644
1 | +package de.bht.beuthbot.scheduler; | ||
2 | + | ||
3 | +import de.bht.beuthbot.attachments.AttachmentStore; | ||
4 | +import de.bht.beuthbot.jms.Target; | ||
5 | +import de.bht.beuthbot.model.Attachment; | ||
6 | +import de.bht.beuthbot.model.Intent; | ||
7 | +import de.bht.beuthbot.model.Messenger; | ||
8 | +import de.bht.beuthbot.model.entities.AppUser; | ||
9 | +import de.bht.beuthbot.scheduler.model.Reminder; | ||
10 | +import de.bht.beuthbot.scheduler.model.ReminderCycle; | ||
11 | +import org.slf4j.Logger; | ||
12 | +import org.slf4j.LoggerFactory; | ||
13 | + | ||
14 | +import de.bht.beuthbot.conf.Application; | ||
15 | +import de.bht.beuthbot.conf.Configuration; | ||
16 | +import de.bht.beuthbot.jms.ProcessQueue; | ||
17 | +import de.bht.beuthbot.jms.TaskMessage; | ||
18 | + | ||
19 | +import java.text.ParseException; | ||
20 | +import java.text.SimpleDateFormat; | ||
21 | +import java.util.*; | ||
22 | +import javax.annotation.PostConstruct; | ||
23 | +import javax.annotation.Resource; | ||
24 | +import javax.ejb.*; | ||
25 | +import javax.ejb.Timer; | ||
26 | +import javax.ws.rs.GET; | ||
27 | +import javax.ws.rs.Path; | ||
28 | + | ||
29 | +@Singleton | ||
30 | +@Startup | ||
31 | +public class SchedulerBean { | ||
32 | + /** | ||
33 | + * slf4j Logger | ||
34 | + */ | ||
35 | + private final Logger logger = LoggerFactory.getLogger(SchedulerBean.class); | ||
36 | + //Timer object for call cycling | ||
37 | + @Resource | ||
38 | + TimerService timerService; | ||
39 | + /** Injected JMS MessageQueue */ | ||
40 | + @Resource(lookup = "java:global/global/ProcessQueueBean") | ||
41 | + private ProcessQueue processQueue; | ||
42 | + | ||
43 | + /** Injected AttachmentStore */ | ||
44 | + @Resource(lookup = "java:global/global/AttachmentStoreBean") | ||
45 | + private AttachmentStore attachmentStore; | ||
46 | + | ||
47 | + /** BeuthBot Application Bean */ | ||
48 | + @Resource(lookup = "java:global/global/ApplicationBean") | ||
49 | + private Application application; | ||
50 | + | ||
51 | + //list of reminders to be populated with data from the DB | ||
52 | + private static ArrayList<Reminder> _reminders = new ArrayList<>(); | ||
53 | + | ||
54 | + boolean isActive = true; | ||
55 | + | ||
56 | + public SchedulerBean() { | ||
57 | + | ||
58 | + } | ||
59 | + | ||
60 | + //CONSTRUCTOR | ||
61 | + @PostConstruct | ||
62 | + public void init(){ | ||
63 | + logger.debug("SchedulerBean initiated!"); | ||
64 | + } | ||
65 | + | ||
66 | + @Schedule(hour="*", minute="*") | ||
67 | + public void fetchReminders() { | ||
68 | + //TODO: Establish DB connection and retrieve list of reminders | ||
69 | + logger.debug("SchedulerBean fired!"); | ||
70 | + | ||
71 | + if (!isActive) return; | ||
72 | + | ||
73 | + SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy KK:mm"); | ||
74 | + String dateNow = formatter.format(new Date()); | ||
75 | + | ||
76 | + for (Reminder reminder : _reminders) { | ||
77 | + //Check if date time now equals reminder date time | ||
78 | + if(dateNow.equals(formatter.format(reminder.getCycleDate()))) { | ||
79 | + Long messageID = 1L; | ||
80 | + AppUser user = reminder.getUser(); //TODO: Fill with correct value from DB | ||
81 | + Long messengerId = Long.valueOf(user.getFacebookUserId() == null ? user.getTelegramUserId() : user.getFacebookUserId()); | ||
82 | + Messenger messenger = reminder.getMessenger(); //TODO: Fill with correct value from DB | ||
83 | + String text = ""; | ||
84 | + String intent = "showFood"; | ||
85 | + Map entities = Collections.emptyMap(); | ||
86 | + try { | ||
87 | + TaskMessage message = new TaskMessage(1L, Target.MAINBOT, messageID, messengerId , messenger, text, null, intent, entities); | ||
88 | + processQueue.route(new TaskMessage(message)); | ||
89 | + } catch (Exception e) { | ||
90 | + logger.error("Something went wrong while attempting to send reminder!", e); | ||
91 | + } | ||
92 | + } | ||
93 | + } | ||
94 | + } | ||
95 | + | ||
96 | + public String createReminder(TaskMessage message, AppUser user) { | ||
97 | + | ||
98 | + ReminderCycle cycle = ReminderCycle.DAILY; | ||
99 | + Messenger messener = message.getMessenger(); | ||
100 | + String intent = message.getEntities().getOrDefault("event",""); | ||
101 | + Date date; | ||
102 | + try { | ||
103 | + //Read out time for Reminder | ||
104 | + | ||
105 | + String messageTime = message.getEntities().getOrDefault("time",""); | ||
106 | + //time should not be empty | ||
107 | + if(!messageTime.equals("")) { | ||
108 | + //get Date new for day, month, year | ||
109 | + String dateNow = new SimpleDateFormat("dd.MM.yyyy").format(new Date()); | ||
110 | + //remove all chars from time String (e.g. "10 Uhr") | ||
111 | + String reminderTime = messageTime.replaceAll("[^0-9:]", ""); | ||
112 | + //trim spaces | ||
113 | + reminderTime = reminderTime.trim(); | ||
114 | + //check if given time is e.g. "11:15" | ||
115 | + if(reminderTime.contains(":")) { | ||
116 | + //split for hours and minutes | ||
117 | + String[] splittedTime = reminderTime.split(":"); | ||
118 | + //add hours and minutes to date | ||
119 | + if(splittedTime.length > 1) | ||
120 | + dateNow = dateNow + " " + splittedTime[0] + ":" + splittedTime[1]; | ||
121 | + } else { | ||
122 | + //add hours from time and set to "00" | ||
123 | + dateNow = dateNow + " " + reminderTime + ":" + "00"; | ||
124 | + } | ||
125 | + | ||
126 | + SimpleDateFormat f = new SimpleDateFormat("dd.MM.yyyy hh:mm"); | ||
127 | + Date d = f.parse(dateNow); | ||
128 | + long milliseconds = d.getTime(); | ||
129 | + date = new Date(milliseconds); | ||
130 | + | ||
131 | + } else | ||
132 | + date = new Date(); | ||
133 | + | ||
134 | + } catch (Exception e) { | ||
135 | + return "Fehler beim Anlegen der Erinnerung"; | ||
136 | + } | ||
137 | + | ||
138 | + Reminder reminder = new Reminder(user, cycle, date, messener, intent); | ||
139 | + _reminders.add(reminder); | ||
140 | + | ||
141 | + return "Erinnerung erstellt:\n" + | ||
142 | + "Wann: um " + new SimpleDateFormat("KK:mm").format(reminder.getCycleDate()) + " Uhr\n" + | ||
143 | + "Was: " + reminder.getIntent(); | ||
144 | + | ||
145 | + //TODO: Insert the new Reminder into Database | ||
146 | + } | ||
147 | + | ||
148 | +} |
services/MainBot/src/main/java/de/bht/beuthbot/scheduler/model/Reminder.java
0 → 100644
1 | +package de.bht.beuthbot.scheduler.model; | ||
2 | + | ||
3 | +import de.bht.beuthbot.model.Messenger; | ||
4 | +import de.bht.beuthbot.model.entities.AppUser; | ||
5 | + | ||
6 | +import java.util.Date; | ||
7 | + | ||
8 | +public class Reminder { | ||
9 | + //TODO: Replace with User Object | ||
10 | + private AppUser _user; | ||
11 | + //Reminder cycle | ||
12 | + private ReminderCycle _reminderCycle; | ||
13 | + //Date of Cycle | ||
14 | + private Date _cycleDate; | ||
15 | + //Used Messenger | ||
16 | + private Messenger _messenger; | ||
17 | + //Intent after Reminder is triggered | ||
18 | + private String _intent; | ||
19 | + | ||
20 | + public Reminder(AppUser user, ReminderCycle reminderCycle, Date cycleDate, Messenger messenger, String intent) { | ||
21 | + _user = user; | ||
22 | + _reminderCycle = reminderCycle; | ||
23 | + _cycleDate = cycleDate; | ||
24 | + _messenger = messenger; | ||
25 | + _intent = intent; | ||
26 | + } | ||
27 | + | ||
28 | + public Messenger getMessenger() { | ||
29 | + return _messenger; | ||
30 | + } | ||
31 | + | ||
32 | + public void setMessenger(Messenger messenger) { | ||
33 | + _messenger = messenger; | ||
34 | + } | ||
35 | + | ||
36 | + public String getIntent() { | ||
37 | + return _intent; | ||
38 | + } | ||
39 | + | ||
40 | + public void setIntent(String intent) { | ||
41 | + _intent = intent; | ||
42 | + } | ||
43 | + | ||
44 | + public AppUser getUser() { | ||
45 | + return _user; | ||
46 | + } | ||
47 | + | ||
48 | + public void setUserID(AppUser user) { | ||
49 | + _user = user; | ||
50 | + } | ||
51 | + | ||
52 | + public ReminderCycle getReminderCycle() { | ||
53 | + return _reminderCycle; | ||
54 | + } | ||
55 | + | ||
56 | + public void setReminderCycle(ReminderCycle reminderCycle) { | ||
57 | + _reminderCycle = reminderCycle; | ||
58 | + } | ||
59 | + | ||
60 | + public Date getCycleDate() { | ||
61 | + return _cycleDate; | ||
62 | + } | ||
63 | + | ||
64 | + public void setCycleDate(Date cycleDate) { | ||
65 | + _cycleDate = cycleDate; | ||
66 | + } | ||
67 | +} |