'''
Read data from sensor (only once)
/usr/bin/python3 /var/www/moduleair_pro_4g/sensirion/SFA30_read.py
'''

import time
import json

from sensirion_shdlc_driver import ShdlcSerialPort, ShdlcConnection
from sensirion_shdlc_sfa3x import Sfa3xShdlcDevice

# Connect to the device with default settings:
#  - baudrate:      115200
#  - slave address: 0
with ShdlcSerialPort(port='/dev/ttyAMA5', baudrate=115200) as port:
    device = Sfa3xShdlcDevice(ShdlcConnection(port), slave_address=0)
    device.device_reset()

    # Print device information
    #print("Device Marking: {}".format(device.get_device_marking()))

    # Start measurement
    device.start_measurement()
    #print("Measurement started... ")
    
    time.sleep(5.)
    hcho, humidity, temperature = device.read_measured_values()

    # Prepare data as a JSON object
    data = {
        "formaldehyde_ppb": hcho.ppb,
        "humidity_percent": humidity.percent_rh,
        "temperature_celsius": temperature.degrees_celsius,
    }
        
    # Convert the dictionary to a JSON string
    json_output = json.dumps(data, indent=4)
    print(json_output)
