#
# file: py_dhr_auto.py

# Python script to ask  XY-MD01 to read Temperature and humidity using auto
#
# This has examples of converting the bytes received.
# py_dhr_serial_XT_MD01.py reads temperature and humidity
# and ouputs to gbFat.js
# graphPiT.htm includes gbFat.js and plots it
# 
#data = ser.read( ser.in_waiting )
# print received bytes as list
#print( list( data ) )

# if you convert to string, it gets in a muddle
# ret=str(ser.read(ser.in_waiting))

from datetime import datetime

# example:-
# Create a datetime object
current_time = datetime.now()
# Convert to ISO 8601 format
iso_formatted_time = current_time.isoformat()
print(iso_formatted_time)

temperature = str( 253433 )

#gbFa( "2025-03-14 06:15 t=15687" )
#gbFa( "2025-03-15 05:00 t=16750" )

opStr = "gbFa( \""

#opStr += iso_formatted_time + " "
#we want
opStr += iso_formatted_time[0:10] + " " + iso_formatted_time[11:16] + " "
opStr += "t="
opStr += temperature
opStr += " \" ) \n"


#with open("gbFat.js", "a") as f:
#  f.write("gbFa( \"2025-03-14 06:15 t=15687\" )")
#  f.write("gbFa( \" ")
   #f.write( "\n" )
   #f.write( opStr )


import time
import sys

#needed sudo apt install python3-serial
import serial

#time.sleep(10)
#sys.stdin.read(1)

# on windows port="comX"
# mode

# on RaspberryPi port="/dev/ttyUSB0"
# ls /dev to find out 

# 
ser = serial.Serial(
	#port="com4", baudrate=9600,
	#port="com15", baudrate=9600,
	#port="/dev/ttyUSB0", baudrate=9600,
	port="/dev/ttyACM0", baudrate=9600,
	parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE,
	bytesize=serial.EIGHTBITS, xonxoff=False,
#	rtscts=True, dsrdtr=True, timeout=3,	
	rtscts=False, dsrdtr=False, timeout=2 )

print(ser.name)



# read out PARAMETERS

ser.write( b'PARAM' )
time.sleep(0.1)
data = ser.read( ser.in_waiting )
# print received bytes as list
print( str( data ) )


# Get module to stream readings 
ser.write( b'AUTO' )
time.sleep( 0.1 )
data = ser.read( ser.in_waiting )
# print received bytes as list
print( str( data ) )

print( "reciving data... " )

countdown = 100

now = time.time()

nexttime = now
opStr = ""
while countdown > 0 :
  time.sleep( 0.4 )
  data = ser.read( ser.in_waiting )
  opStr = opStr + str( data )
  
  # print received bytes as list
  #  print( " "  )

  #  print(  data  )
  countdown = countdown -1

  # poll time and  if moved on enough do something
  now = time.time()
  if nexttime < now :
    nexttime = nexttime + 5
    print( "----------" )
    print( time.asctime( time.gmtime() )  )
    current_time = datetime.now()
    # Convert to ISO 8601 format
    iso_formatted_time = current_time.isoformat()
    print(iso_formatted_time)

    #print  buffered data
    print( opStr )


    with open("gbFaAuto.js", "a") as f:
     # f.write("gbFa( \"2025-03-14 06:15 t=15687\" )")
      f.write("gbFa( \" ")
      f.write( iso_formatted_time )
 
      f.write( opStr )
      # close the string
      f.write( " \" ) \n" ) 
    opStr = ""

print( "Stopping stream ... " )

ser.write( b'STOP' )
time.sleep(0.2)
data = ser.read( ser.in_waiting )
# print received bytes as list
print( str( data ) )

ser.write( b'PARAM' )
time.sleep(0.2)
data = ser.read( ser.in_waiting )
# print received bytes as list
print( str( data ) )

# close serial port
ser.close()
