nradulovic / pyeds

Python Event Driven System
GNU Lesser General Public License v3.0
11 stars 4 forks source link

Can't call Events with parameters from the States. #41

Closed divyamodi128 closed 3 years ago

divyamodi128 commented 3 years ago

I am trying to define Some Events with parameters as event class, AxisButtonPress(fsm.Event) from state machine as shown below from demofsm.py,

from pyeds import fsm

class MyFSM(fsm.StateMachine):
    should_autostart = False

class AxisButtonPress(fsm.Event):
    def __init__(self, direction):
        super().__init__()
        self.direction = direction

@fsm.DeclareState(MyFSM)
class Initialization(fsm.State):
    def on_init(self):
        print("Initialization: init")
        return super().on_init()

    def on_axis_button_press(self, event):
        print(event.direction)

def run():
    myfsm = MyFSM()
    event = AxisButtonPress("left")
    myfsm.do_start()
    myfsm.send(event)
    myfsm.do_terminate()

if __name__ == '__main__':
    run()

The on_axis_button_press event doesn't get called by the myfsm StateMachine, even after sending the event to the sm. And leads to

Output

>>> python demofsm.py
Initialization: init

Expected

>>> python demofsm.py
Initialization: init
Move to left
nradulovic commented 3 years ago

Hi, thanks for reporting this.

The problem with test code is that it executes do_terminate() right after sending the event. It happens that the FSM is terminated before it had a chance to process the event. Put a time.sleep(1) after sending the event to give the FSM chance to process the event.

nradulovic commented 3 years ago

Hi, if you have more questions, please, feel free to open new issue. Thanks!