"""

FILENAME: logfileparse.py

ABOUT: this is a python function that
 parses through a log file from 
 BlueTooth Explorer for onSemiconductors
 COM to UART dongle
 
AUTHOR: Rebecca Hoehne
    
UPDATED: 3/24/19
"""

#load needed libs
import csv

# load log file
fileName='ambient.log'

#output csv file
header= ['LED_code', 'Hex_Output']

#loop through log file
#Makes each line in the file a string in the list lines
lines = []
with open (fileName, 'rt') as in_file:
    for line in in_file:
        lines.append(line)
in_file.close()
    
#Identifies lines where a hydrowatch notification has been received
goodLines = []       
for eachLine in lines:
    result = eachLine.find("INFO GATT Notification (Connection:0x00 Handle:0x")
    if (result!=-1):
        goodLines.append(eachLine)
                
values = [] #values is the received values


with open('Output.csv', 'wt') as f:
    csv_writer = csv.writer(f)
    
    # write header
    csv_writer.writerow(header) 
    
    #Identify the time and values from each goodLine
    for eachLine in goodLines:
        #find the hex value being read out
        startLocation = eachLine.find('Value:0x')+5
        numValues = (len(eachLine)-startLocation-4)    
        myValue = ""
        code = ""
        
        #create array of just 8 byte hex value
        for x in range(1,int(numValues)):        
            myValue = myValue + eachLine[startLocation+2]
            startLocation = startLocation +1        
        
        #seperate the code and the value
        code= myValue[1]
        val=myValue[3:numValues]
        row=[code,val]
        
        #write the row to csv file
        csv_writer.writerow(row)
        
        values.append(myValue)




