|
| 1 | +from shared.jsr223 import scope |
| 2 | + |
| 3 | +import uuid |
| 4 | +import re |
| 5 | + |
| 6 | +scriptExtension = scope.get("scriptExtension") |
| 7 | +scriptExtension.importPreset("RuleSupport") |
| 8 | + |
| 9 | +TriggerBuilder = scope.get("TriggerBuilder") |
| 10 | +Configuration = scope.get("Configuration") |
| 11 | +Trigger = scope.get("Trigger") |
| 12 | + |
| 13 | +from java.nio.file import StandardWatchEventKinds |
| 14 | +ENTRY_CREATE = StandardWatchEventKinds.ENTRY_CREATE # type: WatchEvent.Kind |
| 15 | +ENTRY_DELETE = StandardWatchEventKinds.ENTRY_DELETE # type: WatchEvent.Kind |
| 16 | +ENTRY_MODIFY = StandardWatchEventKinds.ENTRY_MODIFY # type: WatchEvent.Kind |
| 17 | + |
| 18 | +def validate_uid(uid): |
| 19 | + if uid is None: |
| 20 | + uid = uuid.uuid1().hex |
| 21 | + else: |
| 22 | + uid = re.sub(r"[^A-Za-z0-9_-]", "_", uid) |
| 23 | + uid = "{}_{}".format(uid, uuid.uuid1().hex) |
| 24 | + if not re.match("^[A-Za-z0-9]", uid):# in case the first character is still invalid |
| 25 | + uid = "{}_{}".format("jython", uid) |
| 26 | + uid = re.sub(r"__+", "_", uid) |
| 27 | + return uid |
| 28 | + |
| 29 | +class CronTrigger(Trigger): |
| 30 | + def __init__(self, cron_expression, trigger_name=None): |
| 31 | + trigger_name = validate_uid(trigger_name) |
| 32 | + configuration = {'cronExpression': cron_expression} |
| 33 | + self.trigger = TriggerBuilder.create().withId(trigger_name).withTypeUID("timer.GenericCronTrigger").withConfiguration(Configuration(configuration)).build() |
| 34 | + |
| 35 | + |
| 36 | +class ItemStateUpdateTrigger(Trigger): |
| 37 | + def __init__(self, item_name, state=None, trigger_name=None): |
| 38 | + trigger_name = validate_uid(trigger_name) |
| 39 | + configuration = {"itemName": item_name} |
| 40 | + if state is not None: |
| 41 | + configuration["state"] = state |
| 42 | + self.trigger = TriggerBuilder.create().withId(trigger_name).withTypeUID("core.ItemStateUpdateTrigger").withConfiguration(Configuration(configuration)).build() |
| 43 | + |
| 44 | + |
| 45 | +class ItemStateChangeTrigger(Trigger): |
| 46 | + def __init__(self, item_name, previous_state=None, state=None, trigger_name=None): |
| 47 | + trigger_name = validate_uid(trigger_name) |
| 48 | + configuration = {"itemName": item_name} |
| 49 | + if state is not None: |
| 50 | + configuration["state"] = state |
| 51 | + if previous_state is not None: |
| 52 | + configuration["previousState"] = previous_state |
| 53 | + self.trigger = TriggerBuilder.create().withId(trigger_name).withTypeUID("core.ItemStateChangeTrigger").withConfiguration(Configuration(configuration)).build() |
| 54 | + |
| 55 | + |
| 56 | +class ItemCommandTrigger(Trigger): |
| 57 | + def __init__(self, item_name, command=None, trigger_name=None): |
| 58 | + trigger_name = validate_uid(trigger_name) |
| 59 | + configuration = {"itemName": item_name} |
| 60 | + if command is not None: |
| 61 | + configuration["command"] = command |
| 62 | + self.trigger = TriggerBuilder.create().withId(trigger_name).withTypeUID("core.ItemCommandTrigger").withConfiguration(Configuration(configuration)).build() |
| 63 | + |
| 64 | + |
| 65 | +class ThingStatusUpdateTrigger(Trigger): |
| 66 | + def __init__(self, thing_uid, status=None, trigger_name=None): |
| 67 | + trigger_name = validate_uid(trigger_name) |
| 68 | + configuration = {"thingUID": thing_uid} |
| 69 | + if status is not None: |
| 70 | + configuration["status"] = status |
| 71 | + self.trigger = TriggerBuilder.create().withId(trigger_name).withTypeUID("core.ThingStatusUpdateTrigger").withConfiguration(Configuration(configuration)).build() |
| 72 | + |
| 73 | + |
| 74 | +class ThingStatusChangeTrigger(Trigger): |
| 75 | + def __init__(self, thing_uid, previous_status=None, status=None, trigger_name=None): |
| 76 | + trigger_name = validate_uid(trigger_name) |
| 77 | + configuration = {"thingUID": thing_uid} |
| 78 | + if previous_status is not None: |
| 79 | + configuration["previousStatus"] = previous_status |
| 80 | + if status is not None: |
| 81 | + configuration["status"] = status |
| 82 | + self.trigger = TriggerBuilder.create().withId(trigger_name).withTypeUID("core.ThingStatusChangeTrigger").withConfiguration(Configuration(configuration)).build() |
| 83 | + |
| 84 | + |
| 85 | +class ChannelEventTrigger(Trigger): |
| 86 | + def __init__(self, channel_uid, event=None, trigger_name=None): |
| 87 | + trigger_name = validate_uid(trigger_name) |
| 88 | + configuration = {"channelUID": channel_uid} |
| 89 | + if event is not None: |
| 90 | + configuration["event"] = event |
| 91 | + self.trigger = TriggerBuilder.create().withId(trigger_name).withTypeUID("core.ChannelEventTrigger").withConfiguration(Configuration(configuration)).build() |
| 92 | + |
| 93 | + |
| 94 | +class GenericEventTrigger(Trigger): |
| 95 | + def __init__(self, event_source, event_types, event_topic="*/*", trigger_name=None): |
| 96 | + trigger_name = validate_uid(trigger_name) |
| 97 | + self.trigger = TriggerBuilder.create().withId(trigger_name).withTypeUID("core.GenericEventTrigger").withConfiguration(Configuration({ |
| 98 | + "eventTopic": event_topic, |
| 99 | + "eventSource": event_source, |
| 100 | + "eventTypes": event_types |
| 101 | + })).build() |
| 102 | + |
| 103 | + |
| 104 | +class ItemEventTrigger(Trigger): |
| 105 | + def __init__(self, event_types, item_name=None, trigger_name=None): |
| 106 | + trigger_name = validate_uid(trigger_name) |
| 107 | + self.trigger = TriggerBuilder.create().withId(trigger_name).withTypeUID("core.GenericEventTrigger").withConfiguration(Configuration({ |
| 108 | + "eventTopic": "*/items/*", |
| 109 | + "eventSource": "/items/{}".format(item_name if item_name else ""), |
| 110 | + "eventTypes": event_types |
| 111 | + })).build() |
| 112 | + |
| 113 | + |
| 114 | +class ThingEventTrigger(Trigger): |
| 115 | + def __init__(self, event_types, thing_uid=None, trigger_name=None): |
| 116 | + trigger_name = validate_uid(trigger_name) |
| 117 | + self.trigger = TriggerBuilder.create().withId(trigger_name).withTypeUID("core.GenericEventTrigger").withConfiguration(Configuration({ |
| 118 | + "eventTopic": "*/things/*", |
| 119 | + "eventSource": "/things/{}".format(thing_uid if thing_uid else ""), |
| 120 | + "eventTypes": event_types |
| 121 | + })).build() |
| 122 | + |
| 123 | + |
| 124 | +class StartupTrigger(Trigger): |
| 125 | + def __init__(self, trigger_name=None): |
| 126 | + trigger_name = validate_uid(trigger_name) |
| 127 | + self.trigger = TriggerBuilder.create().withId(trigger_name).withTypeUID("jsr223.StartupTrigger").withConfiguration(Configuration()).build() |
| 128 | + |
| 129 | + |
| 130 | +class DirectoryEventTrigger(Trigger): |
| 131 | + def __init__(self, path, event_kinds=[ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY], watch_subdirectories=False, trigger_name=None): |
| 132 | + trigger_name = validate_uid(trigger_name) |
| 133 | + configuration = { |
| 134 | + 'path': path, |
| 135 | + 'event_kinds': str(event_kinds), |
| 136 | + 'watch_subdirectories': watch_subdirectories, |
| 137 | + } |
| 138 | + self.trigger = TriggerBuilder.create().withId(trigger_name).withTypeUID("jsr223.DirectoryEventTrigger").withConfiguration(Configuration(configuration)).build() |
0 commit comments