def test_getGPSData(self): self.timeStamp = datetime.strptime( "2016-11-29 12:46:20", "%Y-%m-%d %H:%M:%S") plon = -70.63098 plat = -33.46516 jsonContent = self.jsonData longitud, latitud, time, distance = Gps.getGPSData( "FLXC49", self.timeStamp, plon, plat, jsonContent) data = json.loads(self.jsonData) expectedDistance = 2.021 self.assertEqual(longitud, data['nearestGpsPoint']['longitude']) self.assertEqual(latitud, data['nearestGpsPoint']['latitude']) self.assertEqual( time, parse_datetime( data['nearestGpsPoint']['time'] + Constants.TIMEZONE)) self.assertGreaterEqual( distance, expectedDistance - expectedDistance * self.error) self.assertLessEqual( distance, expectedDistance + expectedDistance * self.error)
def get( self, request, pUserId, pUuid, pBusService, pEventID, pConfirmDecline, pLatitud=500, pLongitud=500): # here we request all the info needed to proceed aTimeStamp = timezone.now() theEvent = Event.objects.get(id=pEventID) # remove hyphen and convert to uppercase # pBusPlate = pBusPlate.replace('-', '').upper() theBus = {} theAsignment = {} try: theBus = Busv2.objects.get(uuid=pUuid) theAsignment = Busassignment.objects.get( uuid=theBus, service=pBusService) except: return JsonResponse({}, safe=False) # theBus = Bus.objects.get(service=pBusService, uuid=pUuid) # estimate the oldest time where the reported event can be usefull # if there is no event here a new one is created oldestAlertedTime = aTimeStamp - \ timezone.timedelta(minutes=theEvent.lifespam) # get the GPS data from the url responseLongitud = None responseLatitud = None responseTimeStamp = None responseDistance = None responseLongitud, responseLatitud, responseTimeStamp, responseDistance = Gps.getGPSData( theBus.registrationPlate, aTimeStamp, float(pLongitud), float(pLatitud)) # check if there is an event if EventForBusv2.objects.filter( timeStamp__gt=oldestAlertedTime, busassignment=theAsignment, event=theEvent).exists(): # get the event eventsReport = EventForBusv2.objects.filter( timeStamp__gt=oldestAlertedTime, busassignment=theAsignment, event=theEvent) eventReport = self.getLastEvent(eventsReport) # updates to the event reported eventReport.timeStamp = aTimeStamp # update the counters if pConfirmDecline == 'decline': eventReport.eventDecline += 1 else: eventReport.eventConfirm += 1 eventReport.save() StadisticDataFromRegistrationBus.objects.create( timeStamp=aTimeStamp, confirmDecline=pConfirmDecline, reportOfEvent=eventReport, longitud=pLongitud, latitud=pLatitud, userId=pUserId, gpsLongitud=responseLongitud, gpsLatitud=responseLatitud, gpsTimeStamp=responseTimeStamp, distance=responseDistance) else: # if an event was not found, create a new one aEventReport = EventForBusv2.objects.create( userId=pUserId, busassignment=theAsignment, event=theEvent, timeStamp=aTimeStamp, timeCreation=aTimeStamp) # set the initial values for this fields if pConfirmDecline == 'decline': aEventReport.eventDecline = 1 aEventReport.eventConfirm = 0 aEventReport.save() StadisticDataFromRegistrationBus.objects.create( timeStamp=aTimeStamp, confirmDecline=pConfirmDecline, reportOfEvent=aEventReport, longitud=pLongitud, latitud=pLatitud, userId=pUserId, gpsLongitud=responseLongitud, gpsLatitud=responseLatitud, gpsTimeStamp=responseTimeStamp, distance=responseDistance) # Returns updated event list for a bus eventsByBus = EventsByBusV2() return eventsByBus.get(request, pUuid)
def get( self, request, pUserId, pBusService, pBusPlate, pEventID, pConfirmDecline, pLatitud=500, pLongitud=500): # here we request all the info needed to proceed aTimeStamp = timezone.now() theEvent = Event.objects.get(id=pEventID) # remove hyphen and convert to uppercase pBusPlate = pBusPlate.replace('-', '').upper() if pBusPlate == Constants.DUMMY_LICENSE_PLATE: response = {} events = [] dictionary = {} response['registrationPlate'] = pBusPlate response['service'] = pBusService dictionary['eventConfirm'] = 1 dictionary['eventDecline'] = 0 creation = timezone.localtime(timezone.now()) stamp = timezone.localtime(timezone.now()) dictionary['timeCreation'] = creation.strftime("%d-%m-%Y %H:%M:%S") dictionary['timeStamp'] = stamp.strftime("%d-%m-%Y %H:%M:%S") eventDictionary = theEvent.getDictionary() dictionary.update(eventDictionary) events.append(dictionary) # events[0]. response['events'] = events return JsonResponse(response, safe=False) # TODO # Problem: there is no way to identify THE dummy bus without the uuid. # Return the same event. else: theBus = Busv2.objects.get_or_create( registrationPlate=pBusPlate)[0] theAssignment = Busassignment.objects.get( service=pBusService, uuid=theBus) # estimate the oldest time where the reported event can be usefull # if there is no event here a new one is created oldestAlertedTime = aTimeStamp - \ timezone.timedelta(minutes=theEvent.lifespam) # get the GPS data from the url responseLongitud = None responseLatitud = None responseTimeStamp = None responseDistance = None responseLongitud, responseLatitud, responseTimeStamp, responseDistance = Gps.getGPSData( theBus.registrationPlate, aTimeStamp, float(pLongitud), float(pLatitud)) # check if there is an event if EventForBusv2.objects.filter( timeStamp__gt=oldestAlertedTime, busassignment=theAssignment, event=theEvent).exists(): # get the event eventsReport = EventForBusv2.objects.filter( timeStamp__gt=oldestAlertedTime, busassignment=theAssignment, event=theEvent) eventReport = self.getLastEvent(eventsReport) # updates to the event reported eventReport.timeStamp = aTimeStamp # update the counters if pConfirmDecline == 'decline': eventReport.eventDecline += 1 else: eventReport.eventConfirm += 1 eventReport.save() StadisticDataFromRegistrationBus.objects.create( timeStamp=aTimeStamp, confirmDecline=pConfirmDecline, reportOfEvent=eventReport, longitud=pLongitud, latitud=pLatitud, userId=pUserId, gpsLongitud=responseLongitud, gpsLatitud=responseLatitud, gpsTimeStamp=responseTimeStamp, distance=responseDistance) else: # if an event was not found, create a new one aEventReport = EventForBusv2.objects.create( userId=pUserId, busassignment=theAssignment, event=theEvent, timeStamp=aTimeStamp, timeCreation=aTimeStamp) # set the initial values for this fields if pConfirmDecline == 'decline': aEventReport.eventDecline = 1 aEventReport.eventConfirm = 0 aEventReport.save() StadisticDataFromRegistrationBus.objects.create( timeStamp=aTimeStamp, confirmDecline=pConfirmDecline, reportOfEvent=aEventReport, longitud=pLongitud, latitud=pLatitud, userId=pUserId, gpsLongitud=responseLongitud, gpsLatitud=responseLatitud, gpsTimeStamp=responseTimeStamp, distance=responseDistance) # Returns updated event list for a bus eventsByBus = EventsByBusV2() return eventsByBus.get(request, theBus.uuid)