Beispiel #1
0
    """
    On status NORMAL notify
    """,
    {
        rulename: "on-temp-status-back-to-normal",
        subscribe_to: messages.SUBJECT_PROPERTY_CHANGED,
        ruledata: {
            filters: [
                CheckPayloadPropertyValue("value", "NORMAL"),
                CheckPayloadPropertyValueNotIn("old_value", (None,))  # skip initial state
            ],
            processing: [
                Route(message="temp-status-back-to-normal",
                      dispatch_policy=DispatchPolicyConst.DIRECT),
                SetSubjectProperty("m_lastTempStatusChanged", _(lambda _: datetime.now().isoformat()))

            ],
        },
    },

    """
    Status COLD or OVERHEATED schedule a new check
    """,
    {
        rulename: "on-temp-status-bad",
        subscribe_to: messages.SUBJECT_PROPERTY_CHANGED,
        ruledata: {
            filters: [
                CheckPayloadPropertyValueIn("value",  ("COLD", "OVERHEATED")),
            ],
def test_subject_functions(subject, router, asserted):
    class _CheckStoredValue(RuleFunctionBase):
        def execute(self, prop, expected_value):
            if not subject_storage_factory(
                    self.subject.name).is_concurrency_safe():
                return True  # skip test
            subject = subject_factory(self.subject.name)
            return subject.get(prop, cached=False) == expected_value

    from datetime import datetime
    RuleFactory.create(
        "test-set-subject-property",
        subscribe_to="test-set-subject-property",
        data={
            processing: [
                SetSubjectProperty(
                    "dt_prop", lambda: datetime.now().isoformat()),  # no args
                SetSubjectProperty("my_prop", 1),
                SetSubjectProperty("my_prop", lambda v: v + 10),
                SetSubjectPropertySilently("something_to_say", False),
                StoreSubjectProperty("my_prop_2", 2),
                _CheckStoredValue("my_prop_2", 2),
                StoreSubjectPropertySilently("my_prop_3", 3),
                _CheckStoredValue("my_prop_3", 3),
                SetSubjectExtendedProperty("my_ext_prop", "extpropvalue"),
                SetSubjectProperties({
                    "my_prop_4": 4,
                    "my_silent_prop_5": 5
                },
                                     unmuted=["my_prop_4"]),
                IncrementSubjectProperty("my_prop_4"),
                _CheckStoredValue("my_prop_4",
                                  1),  # cached property in not considered
                DecrementSubjectProperty("my_prop_4", amount=1.5),
                IncrementSubjectPropertySilently("my_silent_prop_6"),
                DecrementSubjectPropertySilently("my_silent_prop_6"),
            ]
        })
    from krules_core import types
    RuleFactory.create(
        "test-non-muted-property",
        subscribe_to=types.SUBJECT_PROPERTY_CHANGED,
        data={
            filters: [
                SubjectPropertyChanged(
                    "my_prop",
                    lambda value, old_value: value == 1 and old_value is None)
            ]
        })
    RuleFactory.create(
        "test-muted-property",
        subscribe_to=types.SUBJECT_PROPERTY_CHANGED,
        data={filters: [SubjectPropertyChanged("something_to_say")]})
    RuleFactory.create(
        "test-direct-property",
        subscribe_to=types.SUBJECT_PROPERTY_CHANGED,
        data={
            filters: [
                SubjectPropertyChanged(
                    "my_prop_2",
                    lambda value, old_value: value == 2 and old_value is None)
            ]
        })
    RuleFactory.create("test-muted-direct-property",
                       subscribe_to=types.SUBJECT_PROPERTY_CHANGED,
                       data={filters: [SubjectPropertyChanged("my_prop_3")]})
    RuleFactory.create("test-multi-set-properties-unmuted",
                       subscribe_to=types.SUBJECT_PROPERTY_CHANGED,
                       data={filters: [SubjectPropertyChanged("my_prop_4")]})
    RuleFactory.create(
        "test-multi-set-properties-muted",  # never processed
        subscribe_to=types.SUBJECT_PROPERTY_CHANGED,
        data={
            filters: [
                SubjectPropertyChanged(
                    lambda p: p in ("my_silent_prop_5", "my_silent_prop_6"))
            ]
        })

    proc_events_rx_factory().subscribe(
        lambda x: x[rulename] == "test-set-subject-property" and _assert(
            x[rulename], x[processed]))
    proc_events_rx_factory().subscribe(
        lambda x: x[rulename] == "test-non-muted-property" and x[
            processed] and _assert(x[rulename], True))
    proc_events_rx_factory().subscribe(
        lambda x: x[rulename] == "test-muted-property" and x[
            processed] and _assert(x[rulename], False))
    proc_events_rx_factory().subscribe(
        lambda x: x[rulename] == "test-direct-property" and x[
            processed] and _assert(x[rulename], True))
    proc_events_rx_factory().subscribe(
        lambda x: x[rulename] == "test-muted-direct-property" and x[
            processed] and _assert(x[rulename], False))
    proc_events_rx_factory().subscribe(
        lambda x: x[rulename] == "test-multi-set-properties-unmuted" and x[
            processed] and _assert(x[rulename], True))
    proc_events_rx_factory().subscribe(
        lambda x: x[rulename] == "test-multi-set-properties-muted" and x[
            processed] and _assert(x[rulename], False))

    router.route("test-set-subject-property", subject, {})

    assert subject.get("my_prop") == 11
    assert subject.get("dt_prop")[:10] == datetime.now().isoformat()[:10]
    assert subject.get_ext("my_ext_prop") == "extpropvalue"
    assert subject.get("my_prop_4") == -.5
    assert subject.get("my_silent_prop_5") == 5

    assert "test-set-subject-property" in asserted
    assert "test-non-muted-property" in asserted
    assert "test-muted-property" not in asserted
    assert "test-direct-property" in asserted
    assert "test-multi-set-properties-unmuted" in asserted
Beispiel #3
0
filters = Const.FILTERS
processing = Const.PROCESSING

rulesdata = [
    """
    Store temp property
    """,
    {
        rulename: "on-data-received-store-temp",
        subscribe_to: "data-received",
        ruledata: {
            filters:
            [Check(_(lambda _self: "tempc" in _self.payload["data"]))],
            processing: [
                SetSubjectProperty(
                    "tempc",
                    _(lambda _self: float(_self.payload["data"]["tempc"])))
            ]
        }
    },
    """
    Set temp_status COLD 
    """,
    {
        rulename: "on-tempc-changed-check-cold",
        subscribe_to: messages.SUBJECT_PROPERTY_CHANGED,
        ruledata: {
            filters: [
                OnSubjectPropertyChanged("tempc"),
                Check(
                    _(lambda _self: float(_self.payload.get("value")) < float(
Beispiel #4
0
rulesdata = [
    """
    When data received set device status 'ACTIVE',
    then, schedule the message to set it 'INACTIVE'
    Each time some data is received, the change of state to inactive is delayed
    """,
    {
        rulename: "on-data-received-set-status-active",
        subscribe_to: "data-received",
        ruledata: {
            filters: [
                IsEnabled(),
            ],
            processing: [
                SetSubjectProperty("status", 'ACTIVE'),
                Schedule("set-device-status",
                         payload={'value': 'INACTIVE'},
                         when=lambda _self: datetime.now() + timedelta(
                             seconds=int(_self.subject.rate)),
                         replace=True)
            ],
        },
    },
    """
    Set device status, used to set INACTIVE by the scheduler
    """,
    {
        rulename: 'on-set-device-status',
        subscribe_to: "set-device-status",
        ruledata: {
Beispiel #5
0
# )
results_rx_factory().subscribe(on_next=publish_results_all, )

rulename = Const.RULENAME
subscribe_to = Const.SUBSCRIBE_TO
ruledata = Const.RULEDATA
filters = Const.FILTERS
processing = Const.PROCESSING

rulesdata = [
    """
    Set the basic properties of the device and the initial status as 'READY'
    The status will become 'ACTIVE' upon receipt of the first message
    """,
    {
        rulename: "on-onboard-device-store-properties",
        subscribe_to: "onboard-device",
        ruledata: {
            filters: [CheckPayload(lambda x: "data" in x and "class" in x)],
            processing: [
                FlushSubject(),  # just4dev...
                SetSubjectProperties(_(lambda _self: _self.payload["data"])),
                SetSubjectExtendedProperty(
                    "deviceclass", _(lambda _self: _self.payload["class"])),
                SetSubjectProperty('status', 'READY'),
                SetEnabled(),
            ],
        },
    }
]
Beispiel #6
0
processing = Const.PROCESSING

rulesdata = [
    """
    Just emit "data-received" event
    Data should be handled by application specific logic
    """,
    {
        rulename: "on-data-received-propagate",
        subscribe_to: "google.pubsub.topic.publish",
        ruledata: {
            filters: [
                IsNotOutdated(lambda _self: "data-received#{}".format(
                    _self.payload.get("deviceid")))
            ],
            processing: [
                SetSubjectProperty("m_lastSeen",
                                   datetime.now().isoformat()),
                Route(
                    "data-received",  # message
                    _(lambda _self: _self.payload.pop("deviceid")),  # subject
                    _(
                        lambda _self: {  # payload
                            "receivedAt": _self.payload["_event_info"]["Time"],
                            "data": _self.payload
                        })),
            ],
        }
    }
]