In order to execute a function at the desired time, you must bind it to a system event:
-
Activation based on object state:
cam1 = object("Camera 1") def f(): message("Motion: %s" % cam1.state["motion"]) cam1.activate_on_state_changes(f)
-
Activation based on changed settings:
h = settings("health") def f(): message("Database health: %s" % h["db_connected"]) h.activate_on_changes(f)
-
Activation based on keypresses:
def f(): message("Hello world!") activate_on_shortcut("F9", f)
Only keys F1-F12 and modifiers Ctrl, Alt, and Shift are available for scripts and rules. Joystick buttons also work. Open the rule editor to see the names of keys.
-
Activation from the context menu:
def func1(guid): alert(guid.name) def func2(guid): alert(guid.name) action1 = activate_on_context_menu("xeLzkjpd", "Perform function 1", func1) action2 = activate_on_context_menu("Channel", "Perform function 2", func2)
Herewith Perform function 2 item will appear in context menus called on each device of "Channel" class, and Perform function 1 item - only on one device with GUID "xeLzkjpd".
-
Activation based on an event in the log:
def f(ev): message("Event %s" % ev.type) activate_on_events("", "", f)
Note that the function must have a parameter to pass in the event. For more information, see the 2_event example. The button to load examples is located below the code editor.
-
Activation based on a timeout:
Sometimes a script needs a delay. The function time.sleep() is not appropriate, because causes the program to hang for the specified time. To wait, use timeout(). The indicated function will be called after the specified time:
def f(): alert("2 seconds have passed!") timeout(3000, g) def g(): alert("And another 3!") timeout(1000, lambda: h(1,2,3)) def h(param1, param2, param3): alert("To continue running after the delay, I need the " + "parameters %i, %i, %i" % (param1, param2, param3)) timeout(2000, f)
-
Activation based on an AutoTRASSIR event:
def f(ev): message("Vehicle with license plate number %s passed" % ev.plate) activate_on_lpr_events(f)
-
Activation based on an ActivePOS event:
def f(ev): if ev.type=="POS_POSITION_ADD": message("%s added to receipt" % ev.text) activate_on_pos_events(f)
-
Activation on the event of ArUco Detector:
def f(ev): for detection in ev.detections: message("The following marker has been detected: %s" % detection.decoded_value) activate_on_aruco_detection_events(f)
To find out what other fields an event holds, it use dir()
def f(ev): alert( dir(ev) )