#Handles data & serial from arduino


#Imports
import subprocess
import sys
import time
import traceback
import serial
import datetime

#Handler class
class SerialDataHandler():
    
    #Init
    def __init__(self, logger):
        
        #Whether time is set to GPS UTC
        self.time_set = False
        
        #Logger
        self.logger = logger
        
        #Log
        self.logger.log("Serial data handler started", "INFO")
        
    def connect_to_serial(self):
        
        try:
            
            #Initialise serial connection
            self.serial = serial.Serial("/dev/ttyAMA0", 57600, timeout=1.5)
            
            #Handshake
            self.serial.readline()
            
            #Read 1 line of data
            data = self.serial.readline()
            
            #Log
            if data != b'' or data != '': self.logger.log("Could not connect to serial", "WARN")
            
            else: self.logger.log("Connected to Arduino via /dev/ttyAMA0", "INFO")
            
            #Return whether serial connected
            return data != b'' or data != ''
        
        #Error
        except:
            
            self.logger.log("Exception while attempting to connect to serial:".format(str(error.args)), "ERROR")
            self.logger.log(traceback.format_exc(), "ERROR")
            self.logger.log("Is the port in use?", "ERROR")
        
    def parse(self, data):
        
        #Split message
        message = data.split()
        
        #Dictionary to store parsed data in
        data = {}
        
        #Header
        data["serial-header"] = message.pop(0)
        
        #Parse telemetry data
        data["latitude"] = message.pop(0)
        data["longitude"] = message.pop(0)
        data["altitude"] = message.pop(0)
        data["course"] = message.pop(0)
        data["heading"] = message.pop(0)
        data["speed"] = message.pop(0)

            
        #Parse time data
        data["year"] = message.pop(0)
        data["month"] = message.pop(0)
        data["day"] = message.pop(0)
        data["hour"] = message.pop(0)
        data["minute"] = message.pop(0)
        data["second"] = message.pop(0)
        
        #Parse environment data
        data["temperature"] = message.pop(0)
        data["humidity"] = message.pop(0)
        data["pitch"] = message.pop(0)
        data["roll"] = message.pop(0)
        data["objdistfront"] = message.pop(0)
        data["objdistback"] = message.pop(0)
        
        #Other data, e.g command
        data["other"] = message.pop(0)
        
        return data
        
    
    def handle(self):
            
        #Parse data
        try:
            
            data_raw = str(self.serial.readline()).replace("b", "", 1)
            data = self.parse(data_raw)
            
            #Set time
            if not self.time_set:
                
                #Try set the system time to GPS time
                try:
                    
                    #Check date is valid
                    if int(data["year"]) > 2000:
                        
                        #Create datetime object
                        gps_datetime = datetime.datetime(int(data["year"]), int(data["month"]), int(data["day"]), int(data["hour"]), int(data["minute"]), int(data["second"]))
                            
                        #Sets the time according to GPS reading
                        subprocess.check_call(["sudo", "date", "--set", "{0}".format(gps_datetime.strftime("%Y-%m-%d %H:%M:%S"))])
                    
                    #Set variable tracking whether using gps time to true
                    self.time_set = True
                
                    #Log
                    self.logger.log("System time set to GPS time", "INFO")
            
                #Error
                except:
                    
                    #Log
                    self.logger.log("Exception while setting system time:", "ERROR")
                    self.logger.log(traceback.format_exc(), "ERROR")
                    
        
            #Create data log format
            log_message_format = "".join([
                "Latitude: {0}; ",
                "Longitude: {1}; ",
                "Altitude: {2}; ",
                "Course: {3}; ",
                "Heading: {4}; ",
                "Speed: {5}; ",
                "Temperature: {5}; ",
                "Humidity: {7}; ",
                "Pitch: {8}; ",
                "Roll: {9}; ",
                "ObjectDistanceFront: {10}; ",
                "ObjectDistanceBack: {11}; ",
                "Other: {12};\n"
            ])
        
            #Create message
            log_message = log_message_format.format(
                data["latitude"], 
                data["longitude"], 
                data["altitude"], 
                data["course"], 
                data["heading"], 
                data["speed"], 
                data["temperature"],
                data["humidity"], data["pitch"], 
                data["roll"], 
                data["objdistfront"], 
                data["objdistback"], 
                data["other"]
            )
            
            #Log
            self.logger.log(log_message, "DATA")
            
            #Shutdown Pi
            if data["other"] == "CMD:SHUTDOWN":
                
                self.logger.log("Received shutdown command, shutting down", "INFO")
                
                #Send halt command
                subprocess.call(["sudo", "halt"])
                
                #Exit program
                sys.exit(0)
        
        #Exception
        except:
            
            self.logger.log("Exception while parsing \"{0}\":".format(data_raw), "ERROR")
            self.logger.log(traceback.format_exc(), "ERROR")
            self.logger.log("Bad packet?", "ERROR")              

@@ -2773,16 +2773,36 @@
 , %22%22, 1)
+.replace(%22%5Cr%5Cn%22, %22%22)
 %0A       
@@ -4435,17 +4435,17 @@
 ature: %7B
-5
+6
 %7D; %22,%0A  

Fix temp logging