From 12a49b71d17f3d5b0c3711adb66c0236894787ff Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Wed, 11 Oct 2023 16:04:17 +0800 Subject: [PATCH] =?UTF-8?q?fix=20Medication=20Timer=20wrong=20sleep,=20fix?= =?UTF-8?q?=20=E5=B0=8A=E5=98=9F=E5=81=87=E5=98=9F=20match?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cono/morny/bot/event/OnUserRandom.scala | 14 +++++++++---- .../cono/morny/daemon/MedicationTimer.scala | 13 ++++++------ .../cono/morny/daemon/MornyDaemons.scala | 6 ++++++ .../cono/morny/util/CommonFormat.scala | 21 ++++++++++++++++--- .../cono/morny/util/EpochDateTime.scala | 1 + 6 files changed, 43 insertions(+), 14 deletions(-) diff --git a/gradle.properties b/gradle.properties index ff2c645..5394b14 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ MORNY_ARCHIVE_NAME = morny-coeur MORNY_CODE_STORE = https://github.com/Eyre-S/Coeur-Morny-Cono 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 VERSION_DELTA = diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnUserRandom.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnUserRandom.scala index 02eba33..caa2dc8 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnUserRandom.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnUserRandom.scala @@ -33,6 +33,7 @@ class OnUserRandom (using coeur: MornyCoeur) { (if rand_half then "不" else "") + _con case _ => null + //noinspection DuplicatedCode if result == null then return false coeur.account exec SendMessage( @@ -48,18 +49,23 @@ class OnUserRandom (using coeur: MornyCoeur) { object 尊嘟假嘟 extends EventListener { 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 = { if event.message.text == null then return false - if word_pattern matches event.message.text then {} - else return false - + var result: String|Null = null 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( event.message.chat.id, - if rand_half then "尊嘟" else "假嘟" + result ).replyToMessageId(event.message.messageId) true diff --git a/src/main/scala/cc/sukazyo/cono/morny/daemon/MedicationTimer.scala b/src/main/scala/cc/sukazyo/cono/morny/daemon/MedicationTimer.scala index 25c181d..6e0ed09 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/daemon/MedicationTimer.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/daemon/MedicationTimer.scala @@ -4,6 +4,7 @@ import cc.sukazyo.cono.morny.Log.{exceptionLog, logger} import cc.sukazyo.cono.morny.MornyCoeur import cc.sukazyo.cono.morny.daemon.MedicationTimer.calcNextRoutineTimestamp 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.request.{EditMessageText, SendMessage} import com.pengrad.telegrambot.response.SendResponse @@ -36,8 +37,13 @@ class MedicationTimer (using coeur: MornyCoeur) extends Thread { logger info "Medication Timer started." while (!this.isInterrupted) { 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() + logger info "medication notify sent." } catch case _: InterruptedException => interrupt() @@ -62,11 +68,6 @@ class MedicationTimer (using coeur: MornyCoeur) extends Thread { 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 = { if (lastNotify_messageId isEmpty) || (lastNotify_messageId.get != (edited.messageId toInt)) then return import cc.sukazyo.cono.morny.util.CommonFormat.formatDate diff --git a/src/main/scala/cc/sukazyo/cono/morny/daemon/MornyDaemons.scala b/src/main/scala/cc/sukazyo/cono/morny/daemon/MornyDaemons.scala index 96ae18c..b748480 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/daemon/MornyDaemons.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/daemon/MornyDaemons.scala @@ -10,21 +10,27 @@ class MornyDaemons (using val coeur: MornyCoeur) { val eventHack: EventHacker = EventHacker() def start (): Unit = { + logger info "ALL Morny Daemons starting..." + // TrackerDataManager.init(); medicationTimer.start() + logger info "Morny Daemons started." } def stop (): Unit = { + logger.info("stopping All Morny Daemons...") + // TrackerDataManager.DAEMON.interrupt(); medicationTimer.interrupt() // TrackerDataManager.trackingLock.lock(); try { medicationTimer.join() } catch case e: InterruptedException => e.printStackTrace(System.out) + logger.info("stopped ALL Morny Daemons.") } diff --git a/src/main/scala/cc/sukazyo/cono/morny/util/CommonFormat.scala b/src/main/scala/cc/sukazyo/cono/morny/util/CommonFormat.scala index 0ab5d36..d562d58 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/util/CommonFormat.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/util/CommonFormat.scala @@ -1,5 +1,7 @@ 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.format.DateTimeFormatter @@ -40,11 +42,24 @@ object CommonFormat { * * @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( LocalDateTime.ofInstant( 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 * @return time duration, human readable */ - def formatDuration (duration: Long): String = + def formatDuration (duration: DurationMillis): String = val sb = new StringBuilder() 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 " diff --git a/src/main/scala/cc/sukazyo/cono/morny/util/EpochDateTime.scala b/src/main/scala/cc/sukazyo/cono/morny/util/EpochDateTime.scala index af102f3..0bc623f 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/util/EpochDateTime.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/util/EpochDateTime.scala @@ -6,6 +6,7 @@ import java.time.format.DateTimeFormatter object EpochDateTime { type EpochMillis = Long + type DurationMillis = Long object EpochMillis: /** convert a localtime with timezone to epoch milliseconds