# 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 mqtt_sensor(topic, payload):
# 
#     logger.info('MQTT: Submitting reading')
# 
#     # When running on CI (GHA), run ``mosquitto_pub`` from Docker image.
#     # https://stackoverflow.com/questions/24319662
#     if os.environ.get("CI"):
#         if sys.platform == "linux":
#             mosquitto_pub = "docker run --rm --network=host eclipse-mosquitto:1.6 mosquitto_pub -h localhost"
#         elif sys.platform == "darwin":
#             mosquitto_pub = "docker run --rm eclipse-mosquitto:1.6 mosquitto_pub -h host.docker.internal"
#         else:
#             raise NotImplementedError("Invoking 'mosquitto_pub' through Docker on '{}' not supported yet".format(sys.platform))
#     else:
#         mosquitto_pub = "mosquitto_pub -h localhost"
#     command = "{mosquitto_pub} -t '{topic}' -m '{payload}'".format(mosquitto_pub=mosquitto_pub, topic=topic, payload=payload)
# 
#     logger.info('Running command {}'.format(command))
#     exitcode = os.system(command)
#     if exitcode != 0:
#         raise ChildProcessError("Invoking command failed: {command}. Exit code: {exitcode}".format(command=command, exitcode=exitcode))


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



logger = logging.getLogger(__name__)


@pytest_twisted.inlineCallbacks
@pytest.mark.mqtt
def test_mqtt_to_influxdb_json_single(machinery, create_influxdb, reset_influxdb):
    """
    Publish single reading in JSON format to MQTT broker
    and proof it is stored in the InfluxDB database.
    """

    # Submit a single measurement, without timestamp.
    data = {
        'temperature': 42.84,
        'humidity': 83.1,
    }

# 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 mqtt_sensor(topic, payload):
# 
#     logger.info('MQTT: Submitting reading')
# 
#     # When running on CI (GHA), run ``mosquitto_pub`` from Docker image.
#     # https://stackoverflow.com/questions/24319662
#     if os.environ.get("CI"):
#         if sys.platform == "linux":
#             mosquitto_pub = "docker run --rm --network=host eclipse-mosquitto:1.6 mosquitto_pub -h localhost"
#         elif sys.platform == "darwin":
#             mosquitto_pub = "docker run --rm eclipse-mosquitto:1.6 mosquitto_pub -h host.docker.internal"
#         else:
#             raise NotImplementedError("Invoking 'mosquitto_pub' through Docker on '{}' not supported yet".format(sys.platform))
#     else:
#         mosquitto_pub = "mosquitto_pub -h localhost"
#     command = "{mosquitto_pub} -t '{topic}' -m '{payload}'".format(mosquitto_pub=mosquitto_pub, topic=topic, payload=payload)
# 
#     logger.info('Running command {}'.format(command))
#     exitcode = os.system(command)
#     if exitcode != 0:
#         raise ChildProcessError("Invoking command failed: {command}. Exit code: {exitcode}".format(command=command, exitcode=exitcode))


# Path: test/test_daq_mqtt.py
import logging
import pytest
import pytest_twisted
from twisted.internet import threads
from test.settings.mqttkit import settings, influx_sensors, PROCESS_DELAY_MQTT
from test.util import mqtt_json_sensor, sleep, mqtt_sensor

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



logger = logging.getLogger(__name__)


@pytest_twisted.inlineCallbacks
@pytest.mark.mqtt
def test_mqtt_to_influxdb_json_single(machinery, create_influxdb, reset_influxdb):
    """
    Publish single reading in JSON format to MQTT broker
    and proof it is stored in the InfluxDB database.
    """

    # Submit a single measurement, without timestamp.
    data = {
        'temperature': 42.84,
        'humidity': 83.1,
    }

    yield threads.deferToThread(mqtt_json_sensor, settings.mqtt_topic_json, data)