import subprocess
import paho.mqtt.publish as publish
import json
from statistics import median


def run():
    proc = subprocess.Popen(['./bsec_bme680'], stdout=subprocess.PIPE)
    list_iaq_accuracy = []
    list_pressure = []
    list_gas = []
    list_temperature = []
    list_iaq = []
    list_humidity = []
    lists_iaq = []
    list_c02 = []
    listb_voc = []
    list_status = []
    sequence = 0
    iaq_average = []

    for line in iter(proc.stdout.readline, ''):
        try:
            line_json = json.loads(line.decode("utf-8"))  # process line-by-line
        except json.JSONDecodeError:
            print('{}')
            continue

        line_dict = dict(line_json)

        list_iaq_accuracy.append(int(line_dict['iaq_accuracy']))
        list_pressure.append(float(line_dict['pressure']))
        list_gas.append(int(line_dict['gas']))
        list_temperature.append(float(line_dict['temperature']))
        list_iaq.append(float(line_dict['iaq']))
        list_humidity.append(float(line_dict['humidity']))
        lists_iaq.append(float(line_dict['static_iaq']))
        list_c02.append(float(line_dict['co2_equivalent']))
        listb_voc.append(float(line_dict['breath_voc_equivalent']))
        list_status.append(int(line_dict['status']))

        if len(list_iaq_accuracy) == 2:
            # generate the median for each value
            iaq_accuracy = median(list_iaq_accuracy)
            pressure = median(list_pressure)
            gas = median(list_gas)
            temperature = median(list_temperature)
            iaq = median(list_iaq)
            humidity = median(list_humidity)
            s_iaq = median(lists_iaq)
            e_co2 = median(list_c02)
            b_voc = median(listb_voc)
            status = median(list_status)
            iaq_average.append(s_iaq)
            if len(iaq_average) > 60:
                iaq_average.pop(0)

            # clear lists
            list_iaq_accuracy.clear()
            list_pressure.clear()
            list_gas.clear()
            list_temperature.clear()
            list_iaq.clear()
            list_humidity.clear()
            lists_iaq.clear()
            list_c02.clear()
            listb_voc.clear()
            list_status.clear()

            # Temperature Offset
            temperature = temperature + 2

            payload = {"iaq_accuracy": round(iaq_accuracy), "iaq": round(iaq), "temperature": round(temperature, 1),
                       "humidity": round(humidity, 1), "pressure": round(pressure, 1), "gas": gas,
                       "static_iaq": round(s_iaq, 1), "eC02": round(e_co2), "bVoc": round(b_voc, 7),
                       "iaq_avg": sum(iaq_average) / len(iaq_average),
                       "status": round(status), "seq": sequence}
            # print(payload)
            sequence += 1
            if sequence > 1000:
                sequence = 0
            publish.single("raspib.bsec_bme680", json.dumps(payload), hostname="localhost", retain=True)


if __name__ == '__main__':
    run()

import subprocess
import paho.mqtt.publish as publish
import json
from statistics import median


def run():
    proc = subprocess.Popen(['./bsec_bme680'], stdout=subprocess.PIPE)
    list_iaq_accuracy = []
    list_pressure = []
    list_gas = []
    list_temperature = []
    list_iaq = []
    list_humidity = []
    lists_iaq = []
    list_c02 = []
    listb_voc = []
    list_status = []
    sequence = 0
    iaq_average = []

    for line in iter(proc.stdout.readline, ''):
        try:
            line_json = json.loads(line.decode("utf-8"))  # process line-by-line
        except json.JSONDecodeError:
            print('{}')
            continue

        line_dict = dict(line_json)

        list_iaq_accuracy.append(int(line_dict['iaq_accuracy']))
        list_pressure.append(float(line_dict['pressure']))
        list_gas.append(int(line_dict['gas']))
        list_temperature.append(float(line_dict['temperature']))
        list_iaq.append(float(line_dict['iaq']))
        list_humidity.append(float(line_dict['humidity']))
        lists_iaq.append(float(line_dict['static_iaq']))
        list_c02.append(float(line_dict['co2_equivalent']))
        listb_voc.append(float(line_dict['breath_voc_equivalent']))
        list_status.append(int(line_dict['status']))

        if len(list_iaq_accuracy) == 2:
            # generate the median for each value
            iaq_accuracy = median(list_iaq_accuracy)
            pressure = median(list_pressure)
            gas = median(list_gas)
            temperature = median(list_temperature)
            iaq = median(list_iaq)
            humidity = median(list_humidity)
            s_iaq = median(lists_iaq)
            e_co2 = median(list_c02)
            b_voc = median(listb_voc)
            status = median(list_status)
            iaq_average.append(s_iaq)
            if len(iaq_average) > 60:
                iaq_average.pop(0)

            # clear lists
            list_iaq_accuracy.clear()
            list_pressure.clear()
            list_gas.clear()
            list_temperature.clear()
            list_iaq.clear()
            list_humidity.clear()
            lists_iaq.clear()
            list_c02.clear()
            listb_voc.clear()
            list_status.clear()

            # Temperature Offset
            temperature = temperature + 2

            payload = {"iaq_accuracy": round(iaq_accuracy), "iaq": round(iaq), "temperature": round(temperature, 1),
                       "humidity": round(humidity, 1), "pressure": round(pressure, 1), "gas": gas,
                       "static_iaq": round(s_iaq, 1), "eC02": round(e_co2), "bVoc": round(b_voc, 7),
                       "iaq_avg": sum(iaq_average) / len(iaq_average),
                       "status": round(status), "seq": sequence}
            # print(payload)
            sequence += 1
            if sequence > 1000:
                sequence = 0
            publish.single("raspib.bsec_bme680", json.dumps(payload), hostname="localhost", retain=True)


if __name__ == '__main__':
    run()

# process line-by-line # generate the median for each value # clear lists # Temperature Offset # print(payload)