resources/other/long_event_handler.py

The module contains one class for working with long events - LongEventHandler .
The essence of this class - is to filter out short events, the duration of which is less than the specified one.

The module works ONLY with paired events, i.e. those for which Trassir generates separate start and end events.

Event examples

  • Motion Start/Motion Stop
  • Signal Lost/Signal Restored
  • Fire Detected/Fire Stopped
  • Output Low to High/Output High to Low
  • Input Low to High/Input High to Low
  • Connection Lost/Connection Established

Class arguments

  • event_types (Tuple[str, str])Tuple of two related events, for example ("Motion Start","Motion Stop") .
  • callback (callable[SE_Event])The callback function, which will receive the Trassir event.
  • event_filter(callable[SE_Event], optional): Filter function, which takes Trassir event SE_Event as an argument and returns True if this event matches the condition.
  • duration (int, optional)Duration of the event in seconds. 5 by default.
  • mode (int, optional)The index of the event from event_types to which we are reacting. 0 if we want to respond only to the first event, 1 - only to the second, 2 or any other value - if we respond to both events.

Class methods

  • activate_on_events() - adds handlers for all event_types - host.activate_on_events. You can also not use this method, but install the handlers manually (see examples).

Code examples

Simple example

Calling a pop-up window for Motion Start / Motion Stop events if their duration is more than 15 seconds.

import host
from long_event_handler import LongEventHandler


def handler(ev):
    host.message("%s: %s" % (ev.type, ev.origin_object.name))


leh = LongEventHandler(
	("Motion Start", "Motion Stop"),
    handler,
    duration=15,
    mode=2
)
leh.activate_on_events()
Reacting to only a single event

Calling pop-up for Motion Start events only.

import host
from long_event_handler import LongEventHandler


def handler(ev):
    host.message("%s: %s" % (ev.type, ev.origin_object.name))


leh = LongEventHandler(
	("Motion Start", "Motion Stop"),
    handler,
    duration=15,
    mode=0
)
leh.activate_on_events()
Event filter

The same thing, plus filtering only for the channel named Channel1

import host
from long_event_handler import LongEventHandler


def handler(ev):
    host.message("%s: %s" % (ev.type, ev.origin_object.name))


def channel_filter(ev):
    return ev.origin_object.name == "Channel1"


leh = LongEventHandler(
	("Motion Start", "Motion Stop"),
    handler,
    event_filter=channel_filter,
    duration=15,
    mode=2
)
leh.activate_on_events()
"Manual" activation

Same as the first example, but adding our own handlers

import host
from long_event_handler import LongEventHandler


def handler(ev):
    host.message("%s: %s" % (ev.type, ev.origin_object.name))


leh = LongEventHandler(
	("Motion Start", "Motion Stop"),
    handler,
    duration=15,
    mode=2
)

host.activate_on_events("Motion Start", "Channel1Guid", leh)
host.activate_on_events("Motion Stop", "Channel1Guid", leh)

host.activate_on_events("Motion Start", "Channel2Guid", leh)
host.activate_on_events("Motion Stop", "Channel2Guid", leh)


  • Нет меток