#!/usr/bin/python3

import obd
from obd import OBDStatus

#obd.logger.setLevel(obd.logging.DEBUG)

ocon = obd.OBD("/dev/rfcomm1",9600,None,fast=True,timeout=0.2)

# initial statistics

# VIN
# FREEZE_DTCS
Rfreeze = ocon.query(obd.commands['FREEZE_DTC'])
# STATUS since last clear
Rstatus = ocon.query(obd.commands['STATUS'])
# number of warmups since last clear
Rwarmup = ocon.query(obd.commands['WARMUPS_SINCE_DTC_CLEAR'])

flambda = open("lambda.log","a+")
tscnt = 0
fcatalyst = open("catalyst.log","a+")


while True:

    #print("RPM: " + str(ocon.query(obd.commands['RPM']).value) + " speed:" + str(ocon.query(obd.commands['SPEED']).value))
    flambda.write(str(tscnt) + ";" + str(ocon.query(obd.commands['O2_S1_WR_CURRENT']).value) + ";" + str(ocon.query(obd.commands['O2_S2_WR_CURRENT']).value) + ";" + str(ocon.query(obd.commands['O2_S3_WR_CURRENT']).value) + ";" + str(ocon.query(obd.commands['O2_S4_WR_CURRENT']).value) + ";" + str(ocon.query(obd.commands['O2_S5_WR_CURRENT']).value) + ";" + str(ocon.query(obd.commands['O2_S6_WR_CURRENT']).value) + ";" + str(ocon.query(obd.commands['O2_S7_WR_CURRENT']).value) + ";" + str(ocon.query(obd.commands['O2_S8_WR_CURRENT']).value) + "\n")

    print(str(tscnt) + ";" + str(ocon.query(obd.commands['O2_S1_WR_CURRENT']).value) + ";" + str(ocon.query(obd.commands['O2_S2_WR_CURRENT']).value) + ";" + str(ocon.query(obd.commands['O2_S3_WR_CURRENT']).value) + ";" + str(ocon.query(obd.commands['O2_S4_WR_CURRENT']).value) + ";" + str(ocon.query(obd.commands['O2_S5_WR_CURRENT']).value) + ";" + str(ocon.query(obd.commands['O2_S6_WR_CURRENT']).value) + ";" + str(ocon.query(obd.commands['O2_S7_WR_CURRENT']).value) + ";" + str(ocon.query(obd.commands['O2_S8_WR_CURRENT']).value))

    fcatalyst.write(str(tscnt) + ";" + str(ocon.query(obd.commands['CATALYST_TEMP_B1S1']).value) + ";" + str(ocon.query(obd.commands['CATALYST_TEMP_B1S2']).value) + ";" + str(ocon.query(obd.commands['CATALYST_TEMP_B2S1']).value) + ";" + str(ocon.query(obd.commands['CATALYST_TEMP_B2S2']).value) + "\n")

    print(str(tscnt) + ";" + str(ocon.query(obd.commands['CATALYST_TEMP_B1S1']).value) + ";" + str(ocon.query(obd.commands['CATALYST_TEMP_B1S2']).value) + ";" + str(ocon.query(obd.commands['CATALYST_TEMP_B2S1']).value) + ";" + str(ocon.query(obd.commands['CATALYST_TEMP_B2S2']).value))
    tscnt=tscnt + 1
