Esempio n. 1
0
def test_receive_message(set_environ):
    test_message = "test message"
    my_receiver = Receiver(routingKey="pytest")
    my_receiver.connect()
    my_publisher = Publisher()
    my_publisher.publish(test_message, routingKey="pytest")
    for method, properties, body in my_receiver.channel.consume(
            my_receiver.queue_name, auto_ack=True, inactivity_timeout=5):
        print(body)
        message = body.decode()
        break
    my_receiver.channel.cancel()
    assert message == test_message
Esempio n. 2
0
class tracked_events:
    def __init__(self, rules):
        self.events = []
        self.rules = rules
        self.publisher = Publisher()

    def add_event(self, event: event_details):
        self.events.append(event)
        log.info(f"currently storing {len(self.events)}")

        # process expired
        # Todo this should be a thread running on a schedule
        self.purge_expired()

        # Todo trigger rules
        self.process_rules()

    def purge_expired(self):
        # Simply remove anything older than a constant age at the moment
        x = 0
        current_time = arrow.now()
        while self.events[x].timestamp < current_time.shift(minutes=-2):
            x += 1

        del self.events[:x]
        log.info(f"deleted {x} expired events")

    def process_rules(self) -> bool:

        # Run trigger rules against current item
        for rule in self.rules:
            if rule.type == rule_types.trigger:
                matches = False
                # Without the try, the rule will get an attribute error when the
                # rule attribute does not exits
                try:
                    matches = rule.test.matches(self.events[-1])
                except Exception:
                    log.debug(
                        f"did not match {self.events[-1]._routingKey} to {rule.action}"
                    )
                if matches:
                    self.events[-1].matched = True
                    log.info(
                        f"matched {self.events[-1]._routingKey} to {rule.action}"
                    )
                    log.debug(f"rule criteria: {rule.test.text}")

                    # We'll do some sort of submit here.
                    self.publisher.publish(rule.action, "run.action")
Esempio n. 3
0
 def __init__(self, rules):
     self.events = []
     self.rules = rules
     self.publisher = Publisher()
Esempio n. 4
0
from cava.models.tomorrow_io import weather_observation

# from cava.models.climacell import weather_forecast as weather
import cava

log = cava.log()

# Declare our application
app = FastAPI(
    description="A Simple API for accepting events from the SniderPad",
    version="0.0.1",
    title="Cava",
)

# Get a connection to RabbitMQ
publisher = Publisher()


@app.put("/api/v01/motion")
async def motion(motion: amcrest_motion):
    """
    Accepts
    """
    log.info(f"Received motion from {motion.camera}")
    str_obj = motion.json()
    log.debug(f"motion object received: {motion}")
    publisher.publish(str_obj, "incoming.motion")


@app.put("/api/v01/weather")
async def weather(weather: weather_observation):
Esempio n. 5
0
def test_publish_init_missing_env(setup_module, monkeypatch):
    monkeypatch.delenv("RABBITMQ_DEFAULT_USER")

    with pytest.raises(SystemExit):
        Publisher()
Esempio n. 6
0
def test_publish_init(set_environ):
    my_publisher = Publisher()
    assert my_publisher._config["userName"] == "guest"
Esempio n. 7
0
def test_publish_message(set_environ):
    my_publisher = Publisher()
    my_publisher.publish("test message")  # If this fails, test fails