fix Medication Timer wrong sleep, fix 尊嘟假嘟 match

This commit is contained in:
A.C.Sukazyo Eyre 2023-10-11 16:04:17 +08:00
parent 51d0f2a75b
commit 12a49b71d1
Signed by: Eyre_S
GPG Key ID: C17CE40291207874
6 changed files with 43 additions and 14 deletions

View File

@ -5,7 +5,7 @@ MORNY_ARCHIVE_NAME = morny-coeur
MORNY_CODE_STORE = https://github.com/Eyre-S/Coeur-Morny-Cono MORNY_CODE_STORE = https://github.com/Eyre-S/Coeur-Morny-Cono
MORNY_COMMIT_PATH = https://github.com/Eyre-S/Coeur-Morny-Cono/commit/%s MORNY_COMMIT_PATH = https://github.com/Eyre-S/Coeur-Morny-Cono/commit/%s
VERSION = 1.1.1-alpha3 VERSION = 1.1.1-alpha4
USE_DELTA = false USE_DELTA = false
VERSION_DELTA = VERSION_DELTA =

View File

@ -33,6 +33,7 @@ class OnUserRandom (using coeur: MornyCoeur) {
(if rand_half then "不" else "") + _con (if rand_half then "不" else "") + _con
case _ => null case _ => null
//noinspection DuplicatedCode
if result == null then return false if result == null then return false
coeur.account exec SendMessage( coeur.account exec SendMessage(
@ -48,18 +49,23 @@ class OnUserRandom (using coeur: MornyCoeur) {
object 尊嘟假嘟 extends EventListener { object 尊嘟假嘟 extends EventListener {
private val word_pattern = "^([\\w\\W]*)?(?:尊嘟假嘟|(?:O\\.o|o\\.O))$"r private val word_pattern = "^([\\w\\W]*)?(?:尊嘟假嘟|(?:O\\.o|o\\.O))$"r
private val keywords = Array("尊嘟假嘟", "O.o", "o.O")
override def onMessage (using event: Update): Boolean = { override def onMessage (using event: Update): Boolean = {
if event.message.text == null then return false if event.message.text == null then return false
if word_pattern matches event.message.text then {} var result: String|Null = null
else return false
import cc.sukazyo.cono.morny.util.UseRandom.rand_half import cc.sukazyo.cono.morny.util.UseRandom.rand_half
for (k <- keywords)
if event.message.text endsWith k then
result = if rand_half then "尊嘟" else "假嘟"
//noinspection DuplicatedCode
if result == null then return false
coeur.account exec SendMessage( coeur.account exec SendMessage(
event.message.chat.id, event.message.chat.id,
if rand_half then "尊嘟" else "假嘟" result
).replyToMessageId(event.message.messageId) ).replyToMessageId(event.message.messageId)
true true

View File

@ -4,6 +4,7 @@ import cc.sukazyo.cono.morny.Log.{exceptionLog, logger}
import cc.sukazyo.cono.morny.MornyCoeur import cc.sukazyo.cono.morny.MornyCoeur
import cc.sukazyo.cono.morny.daemon.MedicationTimer.calcNextRoutineTimestamp import cc.sukazyo.cono.morny.daemon.MedicationTimer.calcNextRoutineTimestamp
import cc.sukazyo.cono.morny.util.tgapi.TelegramExtensions.Bot.exec import cc.sukazyo.cono.morny.util.tgapi.TelegramExtensions.Bot.exec
import cc.sukazyo.cono.morny.util.CommonFormat
import com.pengrad.telegrambot.model.{Message, MessageEntity} import com.pengrad.telegrambot.model.{Message, MessageEntity}
import com.pengrad.telegrambot.request.{EditMessageText, SendMessage} import com.pengrad.telegrambot.request.{EditMessageText, SendMessage}
import com.pengrad.telegrambot.response.SendResponse import com.pengrad.telegrambot.response.SendResponse
@ -36,8 +37,13 @@ class MedicationTimer (using coeur: MornyCoeur) extends Thread {
logger info "Medication Timer started." logger info "Medication Timer started."
while (!this.isInterrupted) { while (!this.isInterrupted) {
try { try {
waitToNextRoutine() val next_time = calcNextRoutineTimestamp(System.currentTimeMillis, use_timeZone, notify_atHour)
logger info s"medication timer will send next notify at ${CommonFormat.formatDate(next_time, use_timeZone.getTotalSeconds/60/60)} with $use_timeZone [$next_time]"
val sleep_millis = next_time - System.currentTimeMillis
logger debug s"medication timer will sleep ${CommonFormat.formatDuration(sleep_millis)} [$sleep_millis]"
Thread sleep sleep_millis
sendNotification() sendNotification()
logger info "medication notify sent."
} catch } catch
case _: InterruptedException => case _: InterruptedException =>
interrupt() interrupt()
@ -62,11 +68,6 @@ class MedicationTimer (using coeur: MornyCoeur) extends Thread {
else lastNotify_messageId = None else lastNotify_messageId = None
} }
@throws[InterruptedException | IllegalArgumentException]
private def waitToNextRoutine (): Unit = {
Thread sleep calcNextRoutineTimestamp(System.currentTimeMillis, use_timeZone, notify_atHour)
}
def refreshNotificationWrite (edited: Message): Unit = { def refreshNotificationWrite (edited: Message): Unit = {
if (lastNotify_messageId isEmpty) || (lastNotify_messageId.get != (edited.messageId toInt)) then return if (lastNotify_messageId isEmpty) || (lastNotify_messageId.get != (edited.messageId toInt)) then return
import cc.sukazyo.cono.morny.util.CommonFormat.formatDate import cc.sukazyo.cono.morny.util.CommonFormat.formatDate

View File

@ -10,21 +10,27 @@ class MornyDaemons (using val coeur: MornyCoeur) {
val eventHack: EventHacker = EventHacker() val eventHack: EventHacker = EventHacker()
def start (): Unit = { def start (): Unit = {
logger info "ALL Morny Daemons starting..." logger info "ALL Morny Daemons starting..."
// TrackerDataManager.init(); // TrackerDataManager.init();
medicationTimer.start() medicationTimer.start()
logger info "Morny Daemons started." logger info "Morny Daemons started."
} }
def stop (): Unit = { def stop (): Unit = {
logger.info("stopping All Morny Daemons...") logger.info("stopping All Morny Daemons...")
// TrackerDataManager.DAEMON.interrupt(); // TrackerDataManager.DAEMON.interrupt();
medicationTimer.interrupt() medicationTimer.interrupt()
// TrackerDataManager.trackingLock.lock(); // TrackerDataManager.trackingLock.lock();
try { medicationTimer.join() } try { medicationTimer.join() }
catch case e: InterruptedException => catch case e: InterruptedException =>
e.printStackTrace(System.out) e.printStackTrace(System.out)
logger.info("stopped ALL Morny Daemons.") logger.info("stopped ALL Morny Daemons.")
} }

View File

@ -1,5 +1,7 @@
package cc.sukazyo.cono.morny.util package cc.sukazyo.cono.morny.util
import cc.sukazyo.cono.morny.util.EpochDateTime.{DurationMillis, EpochMillis}
import java.time.{Instant, LocalDateTime, ZoneId, ZoneOffset} import java.time.{Instant, LocalDateTime, ZoneId, ZoneOffset}
import java.time.format.DateTimeFormatter import java.time.format.DateTimeFormatter
@ -40,11 +42,24 @@ object CommonFormat {
* *
* @return the time-zone local date-time-millis [[String]] describes the timestamp. * @return the time-zone local date-time-millis [[String]] describes the timestamp.
*/ */
def formatDate (timestamp: Long, utcOffset: Int): String = def formatDate (timestamp: EpochMillis, utcOffset: Int): String =
formatDate(timestamp, ZoneOffset.ofHours(utcOffset))
/** the formatted date-time-millis [[String]].
*
* time is formatted by pattern [[DATE_TIME_PATTERN_FULL_MILLIS]].
*
* @param timestamp millis timestamp. timestamp should be UTC alignment.
*
* @param tz the time-zone controls which local time describe will use.
*
* @return the time-zone local date-time-millis [[String]] describes the timestamp.
*/
def formatDate (timestamp: EpochMillis, tz: ZoneOffset): String =
DateTimeFormatter.ofPattern(DATE_TIME_PATTERN_FULL_MILLIS).format( DateTimeFormatter.ofPattern(DATE_TIME_PATTERN_FULL_MILLIS).format(
LocalDateTime.ofInstant( LocalDateTime.ofInstant(
Instant.ofEpochMilli(timestamp), Instant.ofEpochMilli(timestamp),
ZoneId.ofOffset("UTC", ZoneOffset.ofHours(utcOffset)) ZoneId.ofOffset("UTC", tz)
) )
) )
@ -64,7 +79,7 @@ object CommonFormat {
* @param duration time duration, in milliseconds * @param duration time duration, in milliseconds
* @return time duration, human readable * @return time duration, human readable
*/ */
def formatDuration (duration: Long): String = def formatDuration (duration: DurationMillis): String =
val sb = new StringBuilder() val sb = new StringBuilder()
if (duration > 1000 * 60 * 60 * 24) sb ++= (duration / (1000 * 60 * 60 * 24)).toString ++= "d " if (duration > 1000 * 60 * 60 * 24) sb ++= (duration / (1000 * 60 * 60 * 24)).toString ++= "d "
if (duration > 1000 * 60 * 60) sb ++= (duration / (1000 * 60 * 60) % 24).toString ++= "h " if (duration > 1000 * 60 * 60) sb ++= (duration / (1000 * 60 * 60) % 24).toString ++= "h "

View File

@ -6,6 +6,7 @@ import java.time.format.DateTimeFormatter
object EpochDateTime { object EpochDateTime {
type EpochMillis = Long type EpochMillis = Long
type DurationMillis = Long
object EpochMillis: object EpochMillis:
/** convert a localtime with timezone to epoch milliseconds /** convert a localtime with timezone to epoch milliseconds