# Path: test/settings/mqttkit.py
# PROCESS_DELAY_MQTT = 0.3
# PROCESS_DELAY_HTTP = 0.3
# class TestSettings:
# 
# Path: test/util.py
# def mqtt_json_sensor(topic, data):
#     payload = json.dumps(data)
#     return mqtt_sensor(topic, payload)
# 
# def sleep(secs):
#     # https://gist.github.com/jhorman/891717
#     return deferLater(reactor, secs, lambda: None)
# 
# def http_json_sensor(topic, data):
#     uri = 'http://localhost:24642/api{}'.format(topic)
#     logger.info('HTTP: Submitting reading to {} using JSON'.format(uri))
#     return requests.post(uri, json=data)
# 
# def http_form_sensor(topic, data):
#     uri = 'http://localhost:24642/api{}'.format(topic)
#     logger.info('HTTP: Submitting reading to {} using x-www-form-urlencoded'.format(uri))
#     return requests.post(uri, data=data)


import logging
import pytest
import pytest_twisted
from twisted.internet import threads
from test.settings.mqttkit import settings, influx_events, PROCESS_DELAY_MQTT
from test.util import mqtt_json_sensor, sleep, http_json_sensor, http_form_sensor
# -*- coding: utf-8 -*-
# (c) 2020-2021 Andreas Motl <andreas@getkotori.org>



logger = logging.getLogger(__name__)

event_data = {
    'title': 'Some event',
    'text': '<a href="https://somewhere.example.org/events?reference=482a38ce-791e-11e6-b152-7cd1c55000be">see also</a>',
    'tags': 'event,alert,important',
    'reference': '482a38ce-791e-11e6-b152-7cd1c55000be',
}


@pytest_twisted.inlineCallbacks
@pytest.mark.mqtt
@pytest.mark.events
def test_event_mqtt(machinery, create_influxdb, reset_influxdb_events):
    """
    Publish event in JSON format to MQTT broker
    and proof it is stored in the InfluxDB database.
    """

    # Submit a single event, without timestamp.
    yield threads.deferToThread(mqtt_json_sensor, settings.mqtt_topic_event, event_data)

    # Wait for some time to process the message.

# Path: test/settings/mqttkit.py
# PROCESS_DELAY_MQTT = 0.3
# PROCESS_DELAY_HTTP = 0.3
# class TestSettings:
# 
# Path: test/util.py
# def mqtt_json_sensor(topic, data):
#     payload = json.dumps(data)
#     return mqtt_sensor(topic, payload)
# 
# def sleep(secs):
#     # https://gist.github.com/jhorman/891717
#     return deferLater(reactor, secs, lambda: None)
# 
# def http_json_sensor(topic, data):
#     uri = 'http://localhost:24642/api{}'.format(topic)
#     logger.info('HTTP: Submitting reading to {} using JSON'.format(uri))
#     return requests.post(uri, json=data)
# 
# def http_form_sensor(topic, data):
#     uri = 'http://localhost:24642/api{}'.format(topic)
#     logger.info('HTTP: Submitting reading to {} using x-www-form-urlencoded'.format(uri))
#     return requests.post(uri, data=data)


# Path: test/test_daq_events.py
import logging
import pytest
import pytest_twisted
from twisted.internet import threads
from test.settings.mqttkit import settings, influx_events, PROCESS_DELAY_MQTT
from test.util import mqtt_json_sensor, sleep, http_json_sensor, http_form_sensor

# -*- coding: utf-8 -*-
# (c) 2020-2021 Andreas Motl <andreas@getkotori.org>



logger = logging.getLogger(__name__)

event_data = {
    'title': 'Some event',
    'text': '<a href="https://somewhere.example.org/events?reference=482a38ce-791e-11e6-b152-7cd1c55000be">see also</a>',
    'tags': 'event,alert,important',
    'reference': '482a38ce-791e-11e6-b152-7cd1c55000be',
}


@pytest_twisted.inlineCallbacks
@pytest.mark.mqtt
@pytest.mark.events
def test_event_mqtt(machinery, create_influxdb, reset_influxdb_events):
    """
    Publish event in JSON format to MQTT broker
    and proof it is stored in the InfluxDB database.
    """

    # Submit a single event, without timestamp.
    yield threads.deferToThread(mqtt_json_sensor, settings.mqtt_topic_event, event_data)

    # Wait for some time to process the message.

    yield sleep(PROCESS_DELAY_MQTT)