1
+ # validated: 2024-01-19 DS aaea85ff1656 ConditionalCommand.java
1
2
from __future__ import annotations
2
3
3
- from typing import Callable
4
+ from typing import Callable , Optional
4
5
5
- from .command import Command
6
+ from wpiutil import SendableBuilder
7
+
8
+ from .command import Command , InterruptionBehavior
6
9
from .commandscheduler import CommandScheduler
7
10
8
11
@@ -16,7 +19,7 @@ class ConditionalCommand(Command):
16
19
subsystems its components require.
17
20
"""
18
21
19
- selectedCommand : Command
22
+ selectedCommand : Optional [ Command ]
20
23
21
24
def __init__ (
22
25
self , onTrue : Command , onFalse : Command , condition : Callable [[], bool ]
@@ -47,13 +50,43 @@ def initialize(self):
47
50
self .selectedCommand .initialize ()
48
51
49
52
def execute (self ):
53
+ assert self .selectedCommand is not None
50
54
self .selectedCommand .execute ()
51
55
52
- def isFinished (self ) -> bool :
53
- return self .selectedCommand .isFinished ()
54
-
55
56
def end (self , interrupted : bool ):
57
+ assert self .selectedCommand is not None
56
58
self .selectedCommand .end (interrupted )
57
59
60
+ def isFinished (self ) -> bool :
61
+ assert self .selectedCommand is not None
62
+ return self .selectedCommand .isFinished ()
63
+
58
64
def runsWhenDisabled (self ) -> bool :
59
65
return self .onTrue .runsWhenDisabled () and self .onFalse .runsWhenDisabled ()
66
+
67
+ def getInterruptionBehavior (self ) -> InterruptionBehavior :
68
+ if (
69
+ self .onTrue .getInterruptionBehavior () == InterruptionBehavior .kCancelSelf
70
+ or self .onFalse .getInterruptionBehavior ()
71
+ == InterruptionBehavior .kCancelSelf
72
+ ):
73
+ return InterruptionBehavior .kCancelSelf
74
+ else :
75
+ return InterruptionBehavior .kCancelIncoming
76
+
77
+ def initSendable (self , builder : SendableBuilder ):
78
+ super ().initSendable (builder )
79
+ builder .addStringProperty ("onTrue" , self .onTrue .getName , lambda _ : None )
80
+ builder .addStringProperty ("onFalse" , self .onFalse .getName , lambda _ : None )
81
+
82
+ def _selected ():
83
+ if self .selectedCommand is None :
84
+ return "null"
85
+ else :
86
+ return self .selectedCommand .getName ()
87
+
88
+ builder .addStringProperty (
89
+ "selected" ,
90
+ _selected ,
91
+ lambda _ : None ,
92
+ )
0 commit comments