mirror of
https://github.com/Eyre-S/Coeur-Morny-Cono.git
synced 2024-11-22 19:24:53 +08:00
添加 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:
parent
35fa1ed5c4
commit
f963c76d9d
@ -1,6 +1,6 @@
|
||||
## Core
|
||||
|
||||
VERSION = 0.7.0.6
|
||||
VERSION = 0.7.0.7
|
||||
|
||||
# dependencies
|
||||
|
||||
|
@ -4,6 +4,6 @@ package cc.sukazyo.cono.morny;
|
||||
* the final field that will be updated by gradle automatically.
|
||||
*/
|
||||
public class GradleProjectConfigures {
|
||||
public static final String VERSION = "0.7.0.6";
|
||||
public static final long COMPILE_TIMESTAMP = 1652946142762L;
|
||||
public static final String VERSION = "0.7.0.7";
|
||||
public static final long COMPILE_TIMESTAMP = 1653044177443L;
|
||||
}
|
||||
|
@ -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.event.EventListeners;
|
||||
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 com.pengrad.telegrambot.TelegramBot;
|
||||
@ -133,24 +134,24 @@ public class MornyCoeur {
|
||||
boolean isAutomaticResetCommandList, boolean isRemoveCommandListWhenExit
|
||||
) {
|
||||
if (INSTANCE == null) {
|
||||
logger.info("System Starting");
|
||||
logger.info("Coeur Starting");
|
||||
INSTANCE = new MornyCoeur(
|
||||
botKey, botUsername,
|
||||
master, trustedChat,
|
||||
latestEventTimestamp,
|
||||
isRemoveCommandListWhenExit
|
||||
);
|
||||
TrackerDataManager.init();
|
||||
MornyDaemons.start();
|
||||
EventListeners.registerAllListeners();
|
||||
INSTANCE.account.setUpdatesListener(OnUpdate::onNormalUpdate);
|
||||
if (isAutomaticResetCommandList) {
|
||||
logger.info("resetting telegram command list");
|
||||
commandManager().automaticUpdateList();
|
||||
}
|
||||
logger.info("System start complete");
|
||||
logger.info("Coeur start complete");
|
||||
return;
|
||||
}
|
||||
logger.error("System already started coeur!!!");
|
||||
logger.error("Coeur already started!!!");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -166,8 +167,7 @@ public class MornyCoeur {
|
||||
*/
|
||||
private void exitCleanup () {
|
||||
logger.info("clean:save tracker data.");
|
||||
TrackerDataManager.DAEMON.interrupt();
|
||||
TrackerDataManager.trackingLock.lock();
|
||||
MornyDaemons.stop();
|
||||
if (isRemoveCommandListWhenExit) {
|
||||
commandManager.automaticRemoveList();
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package cc.sukazyo.cono.morny.bot.event;
|
||||
|
||||
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.Update;
|
||||
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
31
src/main/java/cc/sukazyo/cono/morny/daemon/MornyDaemons.java
Normal file
31
src/main/java/cc/sukazyo/cono/morny/daemon/MornyDaemons.java
Normal 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.");
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package cc.sukazyo.cono.morny.data.tracker;
|
||||
package cc.sukazyo.cono.morny.daemon;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
@ -27,6 +27,7 @@ public class TrackerDataManager {
|
||||
@Override
|
||||
public void run () {
|
||||
trackingLock.lock();
|
||||
logger.info("Tracker started.");
|
||||
long lastWaitTimestamp = System.currentTimeMillis();
|
||||
boolean postProcess = false;
|
||||
do {
|
||||
@ -48,6 +49,7 @@ public class TrackerDataManager {
|
||||
else logger.info("nothing to do yet");
|
||||
} while (!postProcess);
|
||||
trackingLock.unlock();
|
||||
logger.info("Tracker exited.");
|
||||
}
|
||||
|
||||
}
|
@ -14,5 +14,6 @@ public class TelegramStickers {
|
||||
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_SAVED = "CAACAgEAAx0CSQh32gABBExuYdB_G0srfhQldRWkBYxWzCOv4-IAApooAAJ4_MYFcjuNZszfQcQjBA";
|
||||
public static final String ID_PROGYNOVA = "CAACAgUAAxkBAAICm2KEuL7UQqNP7vSPCg2DHJIND6UsAAKLAwACH4WSBszIo722aQ3jJAQ";
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user