def handle(self, *args, **options):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((TCP_IP, TCP_PORT))
        #s.connect((TCP_IP, TCP_PORT))
        #sending the auth message, receiving the response and sending an ack message
        key = authentication(s)
        CPRSession.objects.all().delete()
        cprkey = CPRSession(key=key,time=datetime.now())
        cprkey.save()
        #mounting the XML response to server
        seckey_msg = "<?xml version=\"1.0\" encoding=\"ASCII\"?><Package><Header Version=\"1.0\" Id=\"2\" /><Data SessionId=\""+key+"\" /></Package>"
        close_msg =  "<?xml version=\"1.0\" encoding=\"ASCII\"?>\n<Package>\n  <Header Version=\"1.0\" Id=\"99\" />\n  <Data SessionId=\""+key+"\" />\n</Package>"
        self.stdout.write('>> Starting main loop.\n')
        #sending the response to the server, and awaiting the outbox message
        s.send(ack_msg)
        s.send(seckey_msg)
        data = s.recv(BUFFER_SIZE)
        s.send(ack_msg)
        #listening all information given by CPR.

# >> ==================================================== +-------------------------------+-\
# >> ==================================================== | MAIN EXTRACTOR LOOP           |  >
# >> ==================================================== +-------------------------------+-/

        self.stdout.write('>> Starting main loop.\n')
        while 1:
        
            # if there is messages to read in the socket
            if ([s],[],[]) == select.select([s],[],[],0):
            
                # reads and inform the acknowledgement of the message
                inbox = s.recv(BUFFER_SIZE)
                s.send(ack_msg)
                try:

# >> ==================================================== +-------------------------------+-\
# >> ==================================================== | PARSING XML                   |  >
# >> ==================================================== +-------------------------------+-/
                
                  # parse the received xml and turns it into a dict
                  self.stdout.write("================================\n"+inbox+"\n================================\n")
                  xml =  ElementTree.fromstring(inbox.strip(""))
                  xmldict = XmlDictConfig(xml)
                  # checks if it's a tracking table
                  if xmldict['Header']['Id'] == '198':
                      try:
# >> ==================================================== +-------------------------------+-\
# >> ==================================================== | DATABASE LOOKUP               |  >
# >> ==================================================== +-------------------------------+-/
                          
                          # tries to pick the equipment and the date of the tracking table
                          e = Equipment.objects.get(serial=xmldict['TCA']['SerialNumber'])
                          v = Vehicle.objects.get(equipment=e)
                          sys = lowestDepth(e.system.all())
                          searchdate = datetime.strptime(xmldict['Event']['EventDateTime'], "%Y/%m/%d %H:%M:%S")
                          try:
                              # tries to pick the tracking table with the same equipment and eventdate
                              t = Tracking.objects.get(Q(equipment=e) & Q(eventdate=searchdate))
                    
                          except ObjectDoesNotExist:
                              # probably the try statement above will raise this exception, so the script shall create the tracking
                              t = Tracking(equipment=e, eventdate=searchdate, msgtype=xmldict['Datagram']['MsgType'])
                              t.save()
                              
# >> ==================================================== +-------------------------------+-\
# >> ==================================================== | CUSTOMFIELD AND DATA MERGING  |  >
# >> ==================================================== +-------------------------------+-/                              

                              #iterates over the input dicts and saves the information for each matching custom field in the database table
                              for k_type,d_type in xmldict.items():
                                  if type(d_type).__name__ == 'dict':
                                      for k_tag,d_tag in d_type.items():
                                          try:
                                              #find the customfield to put the tracking data under, then create and save it
                                              # TODO: if needed, optimize this database lookup doing one big search out of the for statement
                                              # TODO: and iterate over the list
                                              c = CustomField.objects.get(Q(type=k_type)&Q(tag=k_tag))
                                              tdata = TrackingData(tracking=t,type=c,value=d_tag)
                                              tdata.save()
                                          except ObjectDoesNotExist:
                                              pass
                                              
# >> ================================================= +----------------------------------+-\
# >> ================================================= | GEOCODING AND MORE DATA ADDITION |  >
# >> ================================================= +----------------------------------+-/

                              #reverse geocoding in the background
                              geocodeinfo = ReverseGeocode(str(xmldict['GPS']['Lat']),str(xmldict['GPS']['Long']))
                              
                              #get the custom fields for the right things
                              geocodefields = CustomField.objects.filter(type='Geocode')
                              geodict = {}
                              for field in geocodefields:
                                geodict[field.tag] = field
                              
                              #saving the tracking datas to the tracking
                              TrackingData(tracking=t, type=geodict['Address'],value=geocodeinfo[1]).save()
                              TrackingData(tracking=t, type=geodict['City'],value=geocodeinfo[2]).save()
                              TrackingData(tracking=t, type=geodict['State'],value=geocodeinfo[3]).save()
                              TrackingData(tracking=t, type=geodict['PostalCode'],value=geocodeinfo[4]).save()
                              
                              #saving the vehicle tracking data
                              field = CustomField.objects.get(tag="Vehicle")
                              TrackingData(tracking=t,type=field,value=v.id).save()
                              
                              #saving the system tracking data
                              field = CustomField.objects.get(tag="System")
                              TrackingData(tracking=t,type=field,value=sys.id).save()
                              
                              # print the success message            
                              self.stdout.write('>> The tracking table sent on '+str(searchdate)+' for the equipment '+ xmldict['TCA']['SerialNumber'] +' has been saved successfully.\n')

# >> ==================================================== +-------------------------------+-\
# >> ==================================================== | ALERTS AND DATA COMPARISON    |  >
# >> ==================================================== +-------------------------------+-/
                        
      # Here is the main alert handler. First, queries the database looking if there's some alert that matches the 
      # received tracking. After that, for each alert in the result of the query, alert in each way available.
                        
                          #queries the vehicle in the database
                          vehicle = v #(has been done before)
                          
                          #if the last alert sent for the vehicle is not null
                          if vehicle.last_alert_date is not None:
                              #calculate the difference between the last alert and a possibly new one                        
                              total_seconds = (searchdate - vehicle.last_alert_date).days * 24 * 60 * 60 + (searchdate - vehicle.last_alert_date).seconds
                              #check if there's enough time between the last alert sent and a possibly new one
                              if total_seconds > vehicle.threshold_time*60:
                                  self.stdout.write('>> Alert threshold reached.\n')
                                  vehicle.last_alert_date = searchdate
                                  vehicle.save()
                                  #pick the alert records to check                                  
                                  alerts = Alert.objects.filter(Q(vehicle=vehicle) & Q(time_end__gte=searchdate) & Q(time_start__lte=searchdate) & Q(active=True))                              
                                  
                                  #iterates over the inputs and checks if it is needed to send the alert
                                  for k_type,d_type in dict(xmldict['Input'].items() + xmldict['LinearInput'].items()).items():
                                      
                                      try:
                                          
                                          #dont look for GPS information now (will be done for the geofences)
                                          c = CustomField.objects.get(Q(tag=k_type)& ~Q(type='GPS'))
                                          
                                          #function that returns true if the alert shall be sent, and false if not.
                                          for alert in alerts:
                                              
                                              if AlertComparison(self,alert,c,d_type):         
                                                  #function that sends in the proper ways the alerts
                                                  AlertSender(self,alert,vehicle,searchdate,geocodeinfo)
                                              else:
                                                  pass
                                                  # self.stdout.write('>> Nao entrou no Alert Comparison.\n')
                                                                     
                                      except ObjectDoesNotExist:
                                          # self.stdout.write('>> Entrou no except "objectdoesnotexist".\n')
                                          #exception thrown for the inputs and linear inputs that didn't match
                                          #any field in the database
                                          pass
                                          
# >> ==================================================== +-------------------------------+-\
# >> ==================================================== | GEOFENCE COMPARISONS          |  >
# >> ==================================================== +-------------------------------+-/

                                  #check if the position is inside (or outside) some geofence alert
                                  geoalerts = alerts.filter(trigger__custom_field__tag='GeoFence')

                                  for alert in geoalerts:
                                    if GeofenceComparison(self,alert,xmldict["GPS"]["Lat"], xmldict["GPS"]["Long"]):
                                        AlertSender(self,alert,vehicle,searchdate)
                          else: #if the vehicle never had thrown alerts, give him a last alert date
                              vehicle.last_alert_date = searchdate
                              vehicle.save()
                
                      #exceptions thrown if the equipment does not exist.
                      # TODO: create the equipments that isn't in the equipments database table
                      except ObjectDoesNotExist:
                          pass
                      except KeyError:
                          pass
                          
                  #if the message is a command status message
                  elif xmldict['Header']['Id'] == '106':
                  # TODO: update the status for the given command
                    print xmldict
                    e = Vehicle.objects.get(equipment__serial=xmldict['Data']['Serial'])
                    c = ItrackCommand.objects.filter(equipment=e)
                    c = c.get(state=u'0')
                    self.stdout.write(str(c)+","+xmldict['Data']['Status']+ "\n")
                    if xmldict['Data']['Status'] == '3': #message was sent to the GPRS network
                        #as we only have one command per time in the command table, change the command status
                        self.stdout.write('here!\n')
                        c.state = u"1"
                        c.time_received = datetime.strptime(xmldict['Header']['TimeStamp'], "%Y/%m/%d %H:%M:%S")                        
                        c.save()
                                    
                                     
                #except ParseError:       
                #TODO : parse the things when two tracking tables comes to the inbox - this case is kind of rare,
                #TODO : but makes the extractor crash when the 'except ParseError' is turned on.
                except:
                  pass
    def handle(self, *args, **options):

        q = Quanta()
        q.connect()
        while True:
            cmd = raw_input()
            print "EXECUTING [" + cmd + "]"
            q.send(str(cmd))
        q.disconnect()
        return

        f = open("debug.txt","w")

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((TCP_IP, TCP_PORT))
        
        
        key = authentication(s)
        CPRSession.objects.all().delete()
        cprkey = CPRSession(key=key,time=datetime.now())
        cprkey.save()
        
        #mounting the XML response to server
        seckey_msg = "<?xml version=\"1.0\" encoding=\"ASCII\"?><Package><Header Version=\"1.0\" Id=\"2\" /><Data SessionId=\""+key+"\" /></Package>"
        
        s.send(ack_msg)
        s.send(seckey_msg)
        
        self.stdout.write('>> Starting main loop.\n')
        while True:
            # if there is messages to read in the socket

            if ([s],[],[]) == select.select([s],[],[],0):
            
                # reads and inform the acknowledgement of the message
                inbox = s.recv(BUFFER_SIZE)
                print(inbox)

                s.send(ack_msg)
                                
                xml =  ElementTree.fromstring(inbox.strip(""))
                xmldict = XmlDictConfig(xml)
                #mounting the dict to be processed. The format used here should be the same for any other extractor build.
                formatted_output = {}
                
                # the XML is a tracking table
                if xmldict['Header']['Id'] == '198':
                    formatted_output['Type'] = 'Tracking'
                    formatted_output['Identification'] = {}
                    formatted_output['Identification']['EquipType'] = productName(xmldict['TCA']['ProductId'])
                    formatted_output['Identification']['Serial'] = xmldict['TCA']['SerialNumber']
                    formatted_output['Identification']['Date'] = xmldict['Event']['EventDateTime']

                    formatted_output['Input'] = xmldict['Input']
                    formatted_output['LinearInput'] = xmldict['LinearInput']
                    formatted_output['Output'] = xmldict['Output']
                    formatted_output['GPS'] = xmldict['GPS']
                
                #the XML is a command response
                elif xmldict['Header']['Id'] == '106': 
                    formatted_output['Type'] = 'Command'
                    formatted_output['Identification'] = {}
                    formatted_output['Identification']['EquipType'] = xmldict['Data']['ProductId']
                    formatted_output['Identification']['Serial'] = xmldict['Data']['Serial']
                    formatted_output['Identification']['Date'] = xmldict['Header']['TimeStamp']
                    formatted_output['Status'] = xmldict['Data']['Status']
                    
                elif xmldict['Header']['Id'] == '702':
                    formatted_output['Type'] = 'CarMeter'
                    formatted_output['Identification'] = {}
                    formatted_output['Identification']['EquipType'] = productName(xmldict['TCA']['ProductId'])
                    formatted_output['Identification']['Serial'] = xmldict['TCA']['SerialNumber']
                    formatted_output['Identification']['Date'] = xmldict['Datagram']['MsgDateTime']
                    formatted_output['Identification']['NetId'] = xmldict['Frame']['NetId']
                    formatted_output['Identification']['Address'] = xmldict['Frame']['Address']
                    formatted_output['Data'] = {}
                    formatted_output['Data']['Value'] = xmldict['Frame']['Message']
                    carm_msg = "<?xml version=\"1.0\" encoding=\"ASCII\"?><Package><Header Version=\"1.00\" Id=\"41\" /><Data Account=\"2\" ProductId=\"41\" Serial=\"000017E8\" Priority=\"3\" NetId=\"E7\" Address=\"00\" Format=\"HEX\" Value=\"0300000003\"/></Package>"
                    s.send(carm_msg)
                    
                    
                processor_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                processor_client.connect((PROCESSOR_IP, PROCESSOR_PORT))
                processor_client.send(json.dumps(formatted_output))

                print  json.dumps(formatted_output, indent=4)
                        
                processor_client.close()
                exit(0)
    def handle(self, *args, **options):

        # q = Quanta()
        # q.connect()
        # return

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((TCP_IP, TCP_PORT))
        s.settimeout(3)
        key = authentication(s)
        CPRSession.objects.all().delete()
        cprkey = CPRSession(key=key, time=datetime.now())
        cprkey.save()

        # mounting the XML response to server
        list_chars = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"]

        seckey_msg = (
            '<?xml version="1.0" encoding="ASCII"?><Package><Header Version="1.0" Id="2" /><Data SessionId="'
            + key
            + '" /></Package>'
        )
        request_msg = '<?xml version="1.0" encoding="ASCII"?><Package><Header Version="1.00" Id="41" />'
        request_msg += '<Data Account="2" ProductId="42" Serial="00003A7B" '
        request_msg += 'Priority="3" NetId="E7" Address="E7" '
        request_msg += 'Format="HEX" Value="0300000003"/></Package>'
        turnoff = (
            '<?xml version="1.0" encoding="ASCII"?><Package><Header Version="1.0" Id="99" SessionId="'
            + key
            + '" /></Package>'
        )
        xk = 0
        yk = 6
        s.send(ack_msg)

        #        s.send(seckey_msg)

        s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s2.connect((TCP_IP, TCP_PORT))
        s2.send(seckey_msg)
        s2.settimeout(3)
        try:
            print "OUT:" + s2.recv(BUFFER_SIZE)
        except:
            pass

        self.stdout.write(">> Starting main loop.\n")
        while True:
            # if there is messages to read in the socket
            if ([s], [], []) == select.select([s], [], [], 0):

                # reads and inform the acknowledgement of the message
                try:
                    inbox = s.recv(BUFFER_SIZE)
                    print "IN:" + inbox
                    print inbox[len(inbox) - 1 :]
                    #                s.send(ack_msg)
                    msg_ts = mount_msg(list_chars[yk] + list_chars[xk], "00") + inbox[len(inbox) - 1 :]
                except:
                    pass

                xml = ElementTree.fromstring(inbox.strip(""))
                xmldict = XmlDictConfig(xml)
                # mounting the dict to be processed. The format used here should be the same for any other extractor build.

                formatted_output = {}
                # the XML is a tracking table
                if xmldict["Header"]["Id"] == "198":
                    formatted_output["Type"] = "Tracking"
                    formatted_output["Identification"] = {}
                    formatted_output["Identification"]["EquipType"] = productName(xmldict["TCA"]["ProductId"])
                    formatted_output["Identification"]["Serial"] = xmldict["TCA"]["SerialNumber"]
                    formatted_output["Identification"]["Date"] = xmldict["Event"]["EventDateTime"]
                    formatted_output["Input"] = xmldict["Input"]
                    formatted_output["LinearInput"] = xmldict["LinearInput"]
                    formatted_output["Output"] = xmldict["Output"]
                    formatted_output["GPS"] = xmldict["GPS"]
                    print "##" + xmldict["Control"]["Port"]

                # the XML is a command response
                elif xmldict["Header"]["Id"] == "106":
                    formatted_output["Type"] = "Command"
                    formatted_output["Identification"] = {}
                    formatted_output["Identification"]["EquipType"] = xmldict["Data"]["ProductId"]
                    formatted_output["Identification"]["Serial"] = xmldict["Data"]["Serial"]
                    formatted_output["Identification"]["Date"] = xmldict["Header"]["TimeStamp"]
                    formatted_output["Status"] = xmldict["Data"]["Status"]

                processor_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                processor_client.connect((PROCESSOR_IP, PROCESSOR_PORT))
                processor_client.send(json.dumps(formatted_output))
                print json.dumps(formatted_output, indent=4)
                processor_client.close()
    def handle(self, *args, **options):
                
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((TCP_IP, TCP_PORT))
        
        key = authentication(s)
        CPRSession.objects.all().delete()
        cprkey = CPRSession(key=key,time=datetime.now())
        cprkey.save()
        
        #mounting the XML response to server
        seckey_msg = "<?xml version=\"1.0\" encoding=\"ASCII\"?><Package><Header Version=\"1.0\" Id=\"2\" /><Data SessionId=\""+key+"\" /></Package>"
        
        s.send(ack_msg)
        s.send(seckey_msg)
        
        self.stdout.write('>> Starting main loop.\n')
        while True:
            # if there is messages to read in the socket

            if ([s],[],[]) == select.select([s],[],[],0):
            
                # reads and inform the acknowledgement of the message
                inbox = s.recv(BUFFER_SIZE)
                s.send(ack_msg)
                                
                xml =  ElementTree.fromstring(inbox.strip(""))
                print inbox
                print '>>','--------------------------------------------------------'
                xmldict = XmlDictConfig(xml)
                
                #mounting the dict to be processed. The format used here should be the same for any other extractor build.
                
                formatted_output = {}
                
                # the XML is a tracking table
                if xmldict['Header']['Id'] == '198':
                    formatted_output['Type'] = 'Tracking'
                    formatted_output['Identification'] = {}
                    formatted_output['Identification']['EquipType'] = productName(xmldict['TCA']['ProductId'])
                    formatted_output['Identification']['Serial'] = xmldict['TCA']['SerialNumber']
                    formatted_output['Identification']['Date'] = xmldict['Event']['EventDateTime']

                    formatted_output['Input'] = xmldict['Input']
                    formatted_output['LinearInput'] = xmldict['LinearInput']
                    formatted_output['Output'] = xmldict['Output']
                    formatted_output['GPS'] = xmldict['GPS']
                
                #the XML is a command response
                elif xmldict['Header']['Id'] == '106': 
                    formatted_output['Type'] = 'Command'
                    formatted_output['Identification'] = {}
                    formatted_output['Identification']['EquipType'] = xmldict['Data']['ProductId']
                    formatted_output['Identification']['Serial'] = xmldict['Data']['Serial']
                    formatted_output['Identification']['Date'] = xmldict['Header']['TimeStamp']
                    formatted_output['Status'] = xmldict['Data']['Status']
                    
                processor_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                processor_client.connect((PROCESSOR_IP, PROCESSOR_PORT))
                processor_client.send(json.dumps(formatted_output))
                processor_client.close()