添加 daemon 包,添加吃药提醒功能 #35

> merged from major/0.7-sp/medication-time

- 添加 annie 的吃药提醒功能 #35
- 添加了 MornyDaemons 用于管理 morny 常驻任务
- 将 TrackerDataManager 移入 daemon 部分
- 一些 log 的更改
  - 启动提示的 "System" 改为 "Coeur"
  - 为 tracker daemon 的启动关闭也添加了log
This commit is contained in:
A.C.Sukazyo Eyre 2022-05-20 19:10:50 +08:00
parent 35fa1ed5c4
commit f963c76d9d
Signed by: Eyre_S
GPG Key ID: EFB47D98FE082FAD
8 changed files with 93 additions and 12 deletions

View File

@ -1,6 +1,6 @@
## Core ## Core
VERSION = 0.7.0.6 VERSION = 0.7.0.7
# dependencies # dependencies

View File

@ -4,6 +4,6 @@ package cc.sukazyo.cono.morny;
* the final field that will be updated by gradle automatically. * the final field that will be updated by gradle automatically.
*/ */
public class GradleProjectConfigures { public class GradleProjectConfigures {
public static final String VERSION = "0.7.0.6"; public static final String VERSION = "0.7.0.7";
public static final long COMPILE_TIMESTAMP = 1652946142762L; public static final long COMPILE_TIMESTAMP = 1653044177443L;
} }

View File

@ -4,7 +4,8 @@ import cc.sukazyo.cono.morny.bot.api.OnUpdate;
import cc.sukazyo.cono.morny.bot.command.MornyCommands; import cc.sukazyo.cono.morny.bot.command.MornyCommands;
import cc.sukazyo.cono.morny.bot.event.EventListeners; import cc.sukazyo.cono.morny.bot.event.EventListeners;
import cc.sukazyo.cono.morny.bot.query.MornyQueries; import cc.sukazyo.cono.morny.bot.query.MornyQueries;
import cc.sukazyo.cono.morny.data.tracker.TrackerDataManager; import cc.sukazyo.cono.morny.daemon.MornyDaemons;
import cc.sukazyo.cono.morny.daemon.TrackerDataManager;
import cc.sukazyo.untitled.telegram.api.extra.ExtraAction; import cc.sukazyo.untitled.telegram.api.extra.ExtraAction;
import com.pengrad.telegrambot.TelegramBot; import com.pengrad.telegrambot.TelegramBot;
@ -133,24 +134,24 @@ public class MornyCoeur {
boolean isAutomaticResetCommandList, boolean isRemoveCommandListWhenExit boolean isAutomaticResetCommandList, boolean isRemoveCommandListWhenExit
) { ) {
if (INSTANCE == null) { if (INSTANCE == null) {
logger.info("System Starting"); logger.info("Coeur Starting");
INSTANCE = new MornyCoeur( INSTANCE = new MornyCoeur(
botKey, botUsername, botKey, botUsername,
master, trustedChat, master, trustedChat,
latestEventTimestamp, latestEventTimestamp,
isRemoveCommandListWhenExit isRemoveCommandListWhenExit
); );
TrackerDataManager.init(); MornyDaemons.start();
EventListeners.registerAllListeners(); EventListeners.registerAllListeners();
INSTANCE.account.setUpdatesListener(OnUpdate::onNormalUpdate); INSTANCE.account.setUpdatesListener(OnUpdate::onNormalUpdate);
if (isAutomaticResetCommandList) { if (isAutomaticResetCommandList) {
logger.info("resetting telegram command list"); logger.info("resetting telegram command list");
commandManager().automaticUpdateList(); commandManager().automaticUpdateList();
} }
logger.info("System start complete"); logger.info("Coeur start complete");
return; return;
} }
logger.error("System already started coeur!!!"); logger.error("Coeur already started!!!");
} }
/** /**
@ -166,8 +167,7 @@ public class MornyCoeur {
*/ */
private void exitCleanup () { private void exitCleanup () {
logger.info("clean:save tracker data."); logger.info("clean:save tracker data.");
TrackerDataManager.DAEMON.interrupt(); MornyDaemons.stop();
TrackerDataManager.trackingLock.lock();
if (isRemoveCommandListWhenExit) { if (isRemoveCommandListWhenExit) {
commandManager.automaticRemoveList(); commandManager.automaticRemoveList();
} }

View File

@ -1,7 +1,7 @@
package cc.sukazyo.cono.morny.bot.event; package cc.sukazyo.cono.morny.bot.event;
import cc.sukazyo.cono.morny.bot.api.EventListener; import cc.sukazyo.cono.morny.bot.api.EventListener;
import cc.sukazyo.cono.morny.data.tracker.TrackerDataManager; import cc.sukazyo.cono.morny.daemon.TrackerDataManager;
import com.pengrad.telegrambot.model.Chat; import com.pengrad.telegrambot.model.Chat;
import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.Update;

View File

@ -0,0 +1,47 @@
package cc.sukazyo.cono.morny.daemon;
import cc.sukazyo.cono.morny.MornyCoeur;
import cc.sukazyo.cono.morny.data.TelegramStickers;
import com.pengrad.telegrambot.request.SendSticker;
import static cc.sukazyo.cono.morny.Log.logger;
public class MedicationTimer extends Thread {
public static final long NOTIFY_RECEIVE_CHAT = 5028252995L;
MedicationTimer () {
super("TIMER_Medication");
}
@Override
public void run () {
logger.info("MedicationTimer started");
while (!interrupted()) {
try {
waitToNextRoutine();
sendNotification();
} catch (InterruptedException e) {
interrupt();
logger.info("MedicationTimer was interrupted, will be exit now");
} catch (Exception e) {
logger.error("Unexpected error occurred");
e.printStackTrace(System.out);
}
}
logger.info("MedicationTimer stopped");
}
private static void sendNotification () {
MornyCoeur.extra().exec(new SendSticker(NOTIFY_RECEIVE_CHAT, TelegramStickers.ID_PROGYNOVA));
}
private static long calcNextRoutineTimestamp () {
return ((System.currentTimeMillis()+8*60*60*1000) / (12*60*60*1000) + 1) * 12*60*60*1000 - 8*60*60*1000;
}
private void waitToNextRoutine () throws InterruptedException {
sleep(calcNextRoutineTimestamp() - System.currentTimeMillis());
}
}

View File

@ -0,0 +1,31 @@
package cc.sukazyo.cono.morny.daemon;
import static cc.sukazyo.cono.morny.Log.logger;
public class MornyDaemons {
static MedicationTimer medicationTimerInstance;
public static void start () {
logger.info("ALL Morny Daemons starting...");
TrackerDataManager.init();
medicationTimerInstance = new MedicationTimer();
medicationTimerInstance.start();
logger.info("Morny Daemons started.");
}
public static void stop () {
logger.info("ALL Morny Daemons stopping...");
TrackerDataManager.DAEMON.interrupt();
medicationTimerInstance.interrupt();
TrackerDataManager.trackingLock.lock();
try { medicationTimerInstance.join(); } catch (InterruptedException e) { e.printStackTrace(System.out); }
logger.info("ALL Morny Daemons STOPPED.");
}
}

View File

@ -1,4 +1,4 @@
package cc.sukazyo.cono.morny.data.tracker; package cc.sukazyo.cono.morny.daemon;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -27,6 +27,7 @@ public class TrackerDataManager {
@Override @Override
public void run () { public void run () {
trackingLock.lock(); trackingLock.lock();
logger.info("Tracker started.");
long lastWaitTimestamp = System.currentTimeMillis(); long lastWaitTimestamp = System.currentTimeMillis();
boolean postProcess = false; boolean postProcess = false;
do { do {
@ -48,6 +49,7 @@ public class TrackerDataManager {
else logger.info("nothing to do yet"); else logger.info("nothing to do yet");
} while (!postProcess); } while (!postProcess);
trackingLock.unlock(); trackingLock.unlock();
logger.info("Tracker exited.");
} }
} }

View File

@ -14,5 +14,6 @@ public class TelegramStickers {
public static final String ID_WAITING = "CAACAgEAAx0CSQh32gABA-8DYbh7W2VhJ490ucfZMUMrgMR2FW4AAm4nAAJ4_MYFjx6zpxJPWsQjBA"; public static final String ID_WAITING = "CAACAgEAAx0CSQh32gABA-8DYbh7W2VhJ490ucfZMUMrgMR2FW4AAm4nAAJ4_MYFjx6zpxJPWsQjBA";
public static final String ID_SENT = "CAACAgEAAx0CSQh32gABA--zYbiyU_wOijEitp-0tSl_k7W6l3gAAgMmAAJ4_MYF4GrompjXPx4jBA"; public static final String ID_SENT = "CAACAgEAAx0CSQh32gABA--zYbiyU_wOijEitp-0tSl_k7W6l3gAAgMmAAJ4_MYF4GrompjXPx4jBA";
public static final String ID_SAVED = "CAACAgEAAx0CSQh32gABBExuYdB_G0srfhQldRWkBYxWzCOv4-IAApooAAJ4_MYFcjuNZszfQcQjBA"; public static final String ID_SAVED = "CAACAgEAAx0CSQh32gABBExuYdB_G0srfhQldRWkBYxWzCOv4-IAApooAAJ4_MYFcjuNZszfQcQjBA";
public static final String ID_PROGYNOVA = "CAACAgUAAxkBAAICm2KEuL7UQqNP7vSPCg2DHJIND6UsAAKLAwACH4WSBszIo722aQ3jJAQ";
} }