Exemple #1
0
def ttt_put(request):
    required_args = ['lat', 'lon', 'acc', 'secret', 'speed']
    valid_args = True
    for a in required_args:
        if a not in request.GET:
            valid_args = False
            break
    if not valid_args:
        msg = 'You must supply "lat", "lon", "acc", "speed", and "secret" arguments.'
        return json_failure(msg)

    sec = request.GET['secret'].replace(' ', '')
    try:
        secret = Secret.objects.filter(secret__exact=sec).get()
    except Secret.DoesNotExist:
        return json_failure('Bad secret: "%s"...' % sec)

    l = Location()
    l.latitude = float(request.GET['lat'])
    l.longitude = float(request.GET['lon'])
    l.accuracy = float(request.GET['acc'])
    l.speed = float(request.GET['speed'])
    l.user = secret.user
    if 'date' in request.GET:
        l.date = datetime.datetime.fromtimestamp(
            int(request.GET['date']))
    l.save()
    return json_success('Success!')
    def run(self):
        # get the vehicle record
        try:
            vehicle = Vehicle.objects.get(veh_name=self.vehicle_name)
        except:
            logger.error(
                "%s: Vehicle '%s' does not exist in database. Add it first.",
                MY_NAME, self.vehicle_name)
            sys.exit(2)

        # start GPS polling thread
        self.gps_poller.start()

        # catch signals for proper shutdown
        for sig in (SIGABRT, SIGTERM, SIGINT):
            signal(sig, self.cleanup)

        # main execution loop
        while True:
            try:
                time.sleep(self.interval)
                # If we are idle too long the database server may
                # close the connection on us, ping the server to check if
                # the connection is still up.
                if (connection.connection is not None):
                    if (connection.is_usable()):
                        logger.debug('%s: Database connection is up.', MY_NAME)
                    else:
                        logger.error('%s: Database connection is down.',
                                     MY_NAME)
                        connection.close()
                else:
                    logger.error('%s: Database connection is closed.', MY_NAME)

                # process GPS data
                session = self.gps_poller.session
                if (session.fix.mode == MODE_NO_FIX) and not self.nofix:
                    logger.info("%s: Waiting for GPS to fix...", MY_NAME)
                    continue

                if not isnan(session.fix.time):
                    if (session.fix.speed < 0.1) and (self.last_speed < 0.1):
                        continue
                    self.last_speed = session.fix.speed
                    # if the time is valid the data record is valid
                    location = Location()
                    location.loc_vehicle = vehicle
                    location.loc_time = session.utc
                    location.loc_latitude = session.fix.latitude
                    location.loc_longitude = session.fix.longitude
                    if (session.fix.mode == MODE_3D):
                        location.loc_altitude = session.fix.altitude
                    location.loc_speed = session.fix.speed
                    location.loc_climb = session.fix.climb
                    location.loc_track = session.fix.track
                    location.save()
                    logger.info("%s: Valid location: %s", MY_NAME, location)
                else:
                    logger.debug("%s: Invalid location: %s", MY_NAME)

            except KeyboardInterrupt:
                print('\n')
                break
Exemple #3
0
    def run(self):
        # get the vehicle record
        try:
            vehicle = Vehicle.objects.get(veh_name=self.vehicle_name)
        except:
            logger.error("%s: Vehicle '%s' does not exist in database. Add it first.", MY_NAME, self.vehicle_name)
            sys.exit(2)

        # start GPS polling thread
        self.gps_poller.start()

        # catch signals for proper shutdown
        for sig in (SIGABRT, SIGTERM, SIGINT):
            signal(sig, self.cleanup)

        # main execution loop
        while True:
            try:
                time.sleep(self.interval)
                # If we are idle too long the database server may
                # close the connection on us, ping the server to check if
                # the connection is still up.
                if connection.connection is not None:
                    if connection.is_usable():
                        logger.debug("%s: Database connection is up.", MY_NAME)
                    else:
                        logger.error("%s: Database connection is down.", MY_NAME)
                        connection.close()
                else:
                    logger.error("%s: Database connection is closed.", MY_NAME)

                # process GPS data
                session = self.gps_poller.session
                if (session.fix.mode == MODE_NO_FIX) and not self.nofix:
                    logger.info("%s: Waiting for GPS to fix...", MY_NAME)
                    continue

                if not isnan(session.fix.time):
                    if (session.fix.speed < 0.1) and (self.last_speed < 0.1):
                        continue
                    self.last_speed = session.fix.speed
                    # if the time is valid the data record is valid
                    location = Location()
                    location.loc_vehicle = vehicle
                    location.loc_time = session.utc
                    location.loc_latitude = session.fix.latitude
                    location.loc_longitude = session.fix.longitude
                    if session.fix.mode == MODE_3D:
                        location.loc_altitude = session.fix.altitude
                    location.loc_speed = session.fix.speed
                    location.loc_climb = session.fix.climb
                    location.loc_track = session.fix.track
                    location.save()
                    logger.info("%s: Valid location: %s", MY_NAME, location)
                else:
                    logger.debug("%s: Invalid location: %s", MY_NAME)

            except KeyboardInterrupt:
                print ("\n")
                break
Exemple #4
0
def report(timestamp, vin, data):
    logger.info('Tracking Callback Server: Report request: Time: %s, VIN: %s, Data: %s.', timestamp, vin, data)

    # get the vehicle record from the database
    try:
        vehicle = Vehicle.objects.get(veh_vin = vin)
    except Exception as e:
        logger.error("Tracking Callback Server: Cannot retrieve vehicle '%s' from database. Error: %s", vin, e)
        return {u'status': 0}

    location = Location()
    location.loc_vehicle = vehicle
    location.loc_time = timestamp

    for channel in data:
        key = channel['channel']
        value = channel['value']
        if key == 'location':
            location.loc_latitude = value['lat']
            location.loc_longitude = value['lon']
            location.loc_altitude = value['alt']
        elif key == 'speed':
            location.loc_speed = float(value)
        elif key == 'odometer':
            location.loc_odometer = float(value)

    location.save()

    return {u'status': 0}
Exemple #5
0
def new_test_point(request):
    # check for required args:
    required_args = ['lat_step', 'lon_step', 'acc_step', 'speed_step']
    valid_args = True
    for a in required_args:
        if a not in request.GET:
            valid_args = False
            break
    if not valid_args:
        msg = 'You must supply ' + ', '.join(required_args) + ' arguments.'
        return json_failure(msg)

    user = request.user
    newl = Location()

    try:
        l = Location.objects.filter(user__exact=user).latest('date')
        newl.latitude = l.latitude + float(request.GET['lat_step'])
        newl.longitude= l.longitude + float(request.GET['lon_step'])
        newl.accuracy = l.accuracy + float(request.GET['acc_step'])
        newl.speed = l.speed + float(request.GET['speed_step'])
    except Location.DoesNotExist:
        newl.latitude = 32.95736
        newl.longitude = -117.233133
        newl.accuracy = 10.0
        newl.speed = 42.0

    newl.user = user
    newl.save()

    msg = 'Test point added: ' + str(newl)
    return json_success(msg, title='Success!')
Exemple #6
0
def report(timestamp, vin, data):
    logger.info(
        'Tracking Callback Server: Report request: Time: %s, VIN: %s, Data: %s.',
        timestamp, vin, data)

    # get the vehicle record from the database
    try:
        vehicle = Vehicle.objects.get(veh_vin=vin)
    except Exception as e:
        logger.error(
            "Tracking Callback Server: Cannot retrieve vehicle '%s' from database. Error: %s",
            vin, e)
        return {u'status': 0}

    location = Location()
    location.loc_vehicle = vehicle
    try:
        location.loc_time = datetime.fromtimestamp(float(timestamp))
    except:
        location.loc_time = timestamp

    if isinstance(data, str) or isinstance(data, unicode):
        logger.info('data is string')
        data = ast.literal_eval(data)

    for channel in data:
        key = channel['channel']
        value = channel['value']
        logger.info('%s: %s', key, value)
        if key == 'location':
            location.loc_latitude = value['lat']
            location.loc_longitude = value['lon']
            location.loc_altitude = value['alt']
        elif key == 'speed':
            location.loc_speed = float(value)
        elif key == 'odometer':
            location.loc_odometer = float(value)

    print 'Saving', location
    location.save()
    logger.info('Saved')

    return {u'status': 0}