File size: 19,187 Bytes
cffba29 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 | # v20260920
from __future__ import annotations
from src.object_model import BoolProperty, DictProperty, FloatProperty, IntProperty, StringProperty
class Timer:
"""
Timer
Description: Countdown timer application, placed in Application. Duration is 1–60 whole minutes. Supports start, pause, resume, cancel, and queries of read-only progress; duration cannot be changed while running or paused. Reaching zero completes the timer and leaves it in the completed state. Resume preserves elapsed time; cancel resets it to zero.
Version: 1.0.0
Base: -
"""
PROCESS = {'mode_property': 'mode', 'start_function': 'start', 'pause_function': 'pause', 'resume_function': 'resume', 'modes': {'countdown': [{'phase': 'countdown', 'minutes': {'property': 'duration_minutes'}}]}}
def __init__(self, duration_minutes: int = None, mode: str = None, status: str = None, phase: str = None, remaining_minutes: int = None, started_at_minute: int = None, progress_minutes: int = None) -> None:
# define properties
self.duration_minutes = IntProperty(
name='duration_minutes',
description='Configured countdown duration in whole minutes. This setting is frozen while the timer is running or paused.',
default=3,
unit='minutes',
minimum=1,
maximum=60,
step=1,
step_origin=1
)
self.mode = StringProperty(
name='mode',
description='Selected countdown process mode.',
default='countdown',
mode=2,
choices=['countdown']
)
self.status = StringProperty(
name='status',
description='Current countdown process status.',
default='idle',
mode=2,
choices=['idle', 'running', 'paused', 'completed']
)
self.phase = StringProperty(
name='phase',
description='Current countdown phase.',
default='idle',
mode=2,
choices=['idle', 'countdown', 'completed']
)
self.remaining_minutes = IntProperty(
name='remaining_minutes',
description='Whole minutes remaining in the current countdown.',
default=0,
mode=2,
unit='minutes',
minimum=0,
maximum=60,
step=1,
step_origin=0
)
self.started_at_minute = IntProperty(
name='started_at_minute',
description='Elapsed world minute used as the monotonic origin for the current countdown progress.',
default=0,
mode=2,
unit='minutes',
minimum=0,
step=1,
step_origin=0
)
self.progress_minutes = IntProperty(
name='progress_minutes',
description='Whole minutes elapsed in the current countdown.',
default=0,
mode=2,
unit='minutes',
minimum=0,
maximum=60,
step=1,
step_origin=0
)
# initialize properties
if duration_minutes is not None: code = self.duration_minutes.internal_set(duration_minutes)
if mode is not None: code = self.mode.internal_set(mode)
if status is not None: code = self.status.internal_set(status)
if phase is not None: code = self.phase.internal_set(phase)
if remaining_minutes is not None: code = self.remaining_minutes.internal_set(remaining_minutes)
if started_at_minute is not None: code = self.started_at_minute.internal_set(started_at_minute)
if progress_minutes is not None: code = self.progress_minutes.internal_set(progress_minutes)
self._disabled = {
'properties': [],
'functions': [],
'events': []
}
def get_duration_minutes(self) -> tuple[str, dict]:
"""
Return the configured countdown duration in whole minutes.
Args:
(no arguments required)
Returns:
- Return Code: The return code of this function, either "[PASS]" or one of Error Codes built-in or listed below (type=str)
- Response Data: A dictionary containing response data (type=dict)
- duration_minutes: Configured countdown duration in whole minutes. (type=integer)
Error Codes:
(no specified error codes)
"""
code, value = self.duration_minutes.get()
if code != '[PASS]':
return code, {}
response = {
'duration_minutes': value
}
return code, response
def set_duration_minutes(self, duration_minutes: int) -> tuple[str, dict]:
"""
Set the countdown duration to a whole number from 1 through 60 minutes. The duration may be changed only while the timer is idle or completed. Invalid values or process states leave all properties unchanged.
Args:
- duration_minutes: (min=1, max=60)
Returns:
- Return Code: The return code of this function, either "[PASS]" or one of Error Codes built-in or listed below (type=str)
- Response Data: A dictionary containing response data (type=dict)
(no explicit response)
Error Codes:
- PRECONDITION_FAILED: The duration cannot be changed while the timer is running or paused.
"""
code, status = self.status.get()
if code != '[PASS]':
return code, {}
if status not in ('idle', 'completed'):
return '[PRECONDITION_FAILED]', {}
code = self.duration_minutes.set(duration_minutes)
return code, {}
def get_mode(self) -> tuple[str, dict]:
"""
Return the selected countdown process mode.
Args:
(no arguments required)
Returns:
- Return Code: The return code of this function, either "[PASS]" or one of Error Codes built-in or listed below (type=str)
- Response Data: A dictionary containing response data (type=dict)
- mode: Selected countdown process mode. (type=string)
Error Codes:
(no specified error codes)
"""
code, value = self.mode.get()
if code != '[PASS]':
return code, {}
response = {
'mode': value
}
return code, response
def get_status(self) -> tuple[str, dict]:
"""
Return the current countdown process status.
Args:
(no arguments required)
Returns:
- Return Code: The return code of this function, either "[PASS]" or one of Error Codes built-in or listed below (type=str)
- Response Data: A dictionary containing response data (type=dict)
- status: Current countdown process status. (type=string)
Error Codes:
(no specified error codes)
"""
code, value = self.status.get()
if code != '[PASS]':
return code, {}
response = {
'status': value
}
return code, response
def get_phase(self) -> tuple[str, dict]:
"""
Return the current countdown phase.
Args:
(no arguments required)
Returns:
- Return Code: The return code of this function, either "[PASS]" or one of Error Codes built-in or listed below (type=str)
- Response Data: A dictionary containing response data (type=dict)
- phase: Current countdown phase. (type=string)
Error Codes:
(no specified error codes)
"""
code, value = self.phase.get()
if code != '[PASS]':
return code, {}
response = {
'phase': value
}
return code, response
def get_remaining_minutes(self) -> tuple[str, dict]:
"""
Return the whole number of minutes remaining in the countdown.
Args:
(no arguments required)
Returns:
- Return Code: The return code of this function, either "[PASS]" or one of Error Codes built-in or listed below (type=str)
- Response Data: A dictionary containing response data (type=dict)
- remaining_minutes: Whole minutes remaining in the countdown. (type=integer)
Error Codes:
(no specified error codes)
"""
code, value = self.remaining_minutes.get()
if code != '[PASS]':
return code, {}
response = {
'remaining_minutes': value
}
return code, response
def get_started_at_minute(self) -> tuple[str, dict]:
"""
Return the elapsed world minute used as the monotonic origin for the current countdown progress.
Args:
(no arguments required)
Returns:
- Return Code: The return code of this function, either "[PASS]" or one of Error Codes built-in or listed below (type=str)
- Response Data: A dictionary containing response data (type=dict)
- started_at_minute: Elapsed world minute used as the origin for countdown progress. (type=integer)
Error Codes:
(no specified error codes)
"""
code, value = self.started_at_minute.get()
if code != '[PASS]':
return code, {}
response = {
'started_at_minute': value
}
return code, response
def get_progress_minutes(self) -> tuple[str, dict]:
"""
Return the whole number of minutes elapsed in the current countdown.
Args:
(no arguments required)
Returns:
- Return Code: The return code of this function, either "[PASS]" or one of Error Codes built-in or listed below (type=str)
- Response Data: A dictionary containing response data (type=dict)
- progress_minutes: Whole minutes elapsed in the countdown. (type=integer)
Error Codes:
(no specified error codes)
"""
code, value = self.progress_minutes.get()
if code != '[PASS]':
return code, {}
response = {
'progress_minutes': value
}
return code, response
def start(self, mode: str) -> tuple[str, dict]:
"""
Start a new countdown from idle or completed state. Starting resets progress to zero, initializes remaining time from the configured duration, and enters the countdown phase. Calls in other states fail without changing any property.
Args:
- mode: (choices=['countdown'])
Returns:
- Return Code: The return code of this function, either "[PASS]" or one of Error Codes built-in or listed below (type=str)
- Response Data: A dictionary containing response data (type=dict)
(no explicit response)
Error Codes:
- PRECONDITION_FAILED: A new countdown can start only while the timer is idle or completed.
"""
code, status = self.status.get()
if code != '[PASS]':
return code, {}
if status not in ('idle', 'completed'):
return '[PRECONDITION_FAILED]', {}
code, duration = self.duration_minutes.get()
if code != '[PASS]':
return code, {}
code = self.mode.internal_set(mode)
if code != '[PASS]':
return code, {}
code = self.phase.internal_set('countdown')
if code != '[PASS]':
return code, {}
code = self.remaining_minutes.internal_set(duration)
if code != '[PASS]':
return code, {}
code = self.started_at_minute.internal_set(0)
if code != '[PASS]':
return code, {}
code = self.progress_minutes.internal_set(0)
if code != '[PASS]':
return code, {}
code = self.status.internal_set('running')
if code != '[PASS]':
return code, {}
return '[PASS]', {}
def pause(self) -> tuple[str, dict]:
"""
Pause a running countdown while preserving its elapsed progress and remaining time. The call fails without changing state unless the timer is running.
Args:
(no arguments required)
Returns:
- Return Code: The return code of this function, either "[PASS]" or one of Error Codes built-in or listed below (type=str)
- Response Data: A dictionary containing response data (type=dict)
(no explicit response)
Error Codes:
- PRECONDITION_FAILED: The timer can be paused only while running.
"""
code, status = self.status.get()
if code != '[PASS]':
return code, {}
if status != 'running':
return '[PRECONDITION_FAILED]', {}
code = self.status.internal_set('paused')
if code != '[PASS]':
return code, {}
return '[PASS]', {}
def resume(self) -> tuple[str, dict]:
"""
Resume a paused countdown without resetting its elapsed progress or remaining time. The call fails without changing state unless the timer is paused.
Args:
(no arguments required)
Returns:
- Return Code: The return code of this function, either "[PASS]" or one of Error Codes built-in or listed below (type=str)
- Response Data: A dictionary containing response data (type=dict)
(no explicit response)
Error Codes:
- PRECONDITION_FAILED: The timer can be resumed only while paused.
"""
code, status = self.status.get()
if code != '[PASS]':
return code, {}
if status != 'paused':
return '[PRECONDITION_FAILED]', {}
code = self.started_at_minute.internal_set(0)
if code != '[PASS]':
return code, {}
code = self.status.internal_set('running')
if code != '[PASS]':
return code, {}
return '[PASS]', {}
def cancel(self) -> tuple[str, dict]:
"""
Cancel a running, paused, or completed countdown and reset its process state to idle with zero progress and zero remaining minutes. The call fails without changing state when the timer is already idle.
Args:
(no arguments required)
Returns:
- Return Code: The return code of this function, either "[PASS]" or one of Error Codes built-in or listed below (type=str)
- Response Data: A dictionary containing response data (type=dict)
(no explicit response)
Error Codes:
- PRECONDITION_FAILED: There is no active or completed countdown to cancel while the timer is idle.
"""
code, status = self.status.get()
if code != '[PASS]':
return code, {}
if status == 'idle':
return '[PRECONDITION_FAILED]', {}
if status not in ('running', 'paused', 'completed'):
return '[PRECONDITION_FAILED]', {}
code = self.phase.internal_set('idle')
if code != '[PASS]':
return code, {}
code = self.remaining_minutes.internal_set(0)
if code != '[PASS]':
return code, {}
code = self.started_at_minute.internal_set(0)
if code != '[PASS]':
return code, {}
code = self.progress_minutes.internal_set(0)
if code != '[PASS]':
return code, {}
code = self.status.internal_set('idle')
if code != '[PASS]':
return code, {}
return '[PASS]', {}
def _on_tick(self, context: dict) -> tuple[str, list[dict]]:
"""
While the countdown is running, advances elapsed progress according to monotonic elapsed whole minutes and the configured duration. Remaining time never becomes negative. At the exact duration boundary, the timer enters completed status and phase with zero remaining minutes. Idle, paused, and completed timers do not advance.
Effect properties:
- duration_minutes: Configured duration that determines the countdown completion boundary.
- mode: Selected process mode used to identify the countdown schedule.
- status: Current process status used to determine whether progress advances.
- phase: Current process phase updated when the countdown completes.
- remaining_minutes: Whole minutes remaining in the countdown.
- started_at_minute: Monotonic elapsed-minute origin used to calculate current progress.
- progress_minutes: Whole minutes already elapsed in the countdown.
"""
code, status = self.status.get()
if code != '[PASS]':
return code, []
if status != 'running':
return '[PASS]', []
code, duration = self.duration_minutes.get()
if code != '[PASS]':
return code, []
code, current_progress = self.progress_minutes.get()
if code != '[PASS]':
return code, []
code, origin = self.started_at_minute.get()
if code != '[PASS]':
return code, []
elapsed_minutes = context['time_state']['elapsed_minutes']
calculated_progress = elapsed_minutes - origin
progress = min(duration, max(current_progress, calculated_progress))
remaining = max(duration - progress, 0)
code = self.progress_minutes.internal_set(progress)
if code != '[PASS]':
return code, []
code = self.remaining_minutes.internal_set(remaining)
if code != '[PASS]':
return code, []
if progress == duration:
code = self.phase.internal_set('completed')
if code != '[PASS]':
return code, []
code = self.status.internal_set('completed')
if code != '[PASS]':
return code, []
return '[PASS]', []
def _on_after_action(self, context: dict) -> tuple[str, list[dict]]:
"""
When a successful start or resume action transitions the timer into running status, records the monotonic elapsed-minute origin so accumulated progress is preserved. Other successful actions do not advance countdown time.
Effect properties:
- duration_minutes: Configured countdown duration.
- mode: Selected countdown process mode.
- status: Current status used to detect a transition into running.
- phase: Current countdown phase.
- remaining_minutes: Whole minutes remaining after the successful action.
- started_at_minute: Monotonic elapsed-minute origin maintained for running progress.
- progress_minutes: Accumulated progress preserved when the timer resumes.
"""
previous_properties = context['pre_state']['objects'][context['object_id']]['properties']
code, status = self.status.get()
if code != '[PASS]':
return code, []
code, progress = self.progress_minutes.get()
if code != '[PASS]':
return code, []
if status == 'running' and previous_properties['status'] != 'running':
origin = context['time_state']['elapsed_minutes'] - progress
code = self.started_at_minute.internal_set(origin)
if code != '[PASS]':
return code, []
return '[PASS]', [] |