7.13. eventGenerator — An Events producing mechanism (for testing purposes)

The eventGenerator module provides functions to generate events in order to showcase the events’ handling mechanism

7.13.1. Functions

eventGenerator.startEvents()

Start the events generation and prints the response returned be the event.response() function (see event).

When an event is produced, the type of the event is sent to the Python application that handles the events, along with other required arguments. The event generator module produces two events, event_x with type 0 and event_y with type 1. The arguments sent are the “event_x”/”event_y” and 0/1 respectively.

# Events Wubby Example (showcase the events' handling service)

import event
import eventGenerator

counter = 0

# Callback handling events
def eventHandler(argtype, *args):
    global counter
    print ('Event fired! event type =',argtype)

    # Event x caught
    if argtype == 0:
        event.response(0,"test: " + str(counter))
        counter+=1
    # Event y caught
    elif argtype == 1:
        event.response(1,"test: " + str(counter))
        counter = counter-1

    # Check the arguments of the event
    i = 0
    for arg in args:
        print('argument ' + str(i) + ' is ' + str(arg))
        i+=1

def main():

        # Register the callback that handles the events
        event.onevent(eventHandler)

        # Start the events generation
        # The event generator module produces two events,
        # event_x with type 0 and event_y with type 1
        eventGenerator.startEvents()

        # Start polling for events
        while True:
                event.poll(2000)


if __name__ == "__main__":
        main()