Exemple #1
0
    def run(self, user_id):
        user = User.get(user_id)
        if not user:
            print 'User with id "{}" not found.'.format(user_id)
            sys.exit(1)

        i = randint(0, 100)
        _longitude = randint(6500, 7500) / 1000.
        _latitude = randint(50500, 51500) / 1000.
        _altitude = 500

        while True:
            longitude = sin(i / 73.) * 0.001 + _longitude
            latitude = sin(i / 50.) * 0.004 + _latitude
            altitude = sin(i / 20.) * 300 + _altitude

            fix = TrackingFix()
            fix.pilot = user
            fix.set_location(longitude, latitude)
            fix.altitude = altitude
            fix.time = datetime.now()
            fix.time_visible = fix.time + timedelta(minutes=user.tracking_delay)

            db.session.add(fix)
            db.session.commit()

            print '.',
            sys.stdout.flush()

            sleep(1)

            i += 1
Exemple #2
0
def _parse_fix(pilot):
    fix = TrackingFix()
    fix.ip = request.remote_addr
    fix.pilot = pilot

    # Time
    if 'tm' not in request.values:
        raise BadRequest('`tm` (time) parameter is missing.')

    try:
        fix.time = datetime.utcfromtimestamp(int(request.values['tm']))
    except ValueError:
        raise BadRequest('`tm` (time) has to be a POSIX timestamp.')

    fix.time_visible = fix.time + timedelta(minutes=pilot.tracking_delay)

    # Location
    if 'lat' in request.values and 'lon' in request.values:
        try:
            fix.set_location(float(request.values['lon']), float(request.values['lat']))
        except ValueError:
            raise BadRequest('`lat` and `lon` have to be floating point value in degrees (WGS84).')

    # Altitude
    if 'alt' in request.values:
        try:
            fix.altitude = int(request.values['alt'])
        except ValueError:
            raise BadRequest('`alt` has to be an integer value in meters.')

        if not -1000 <= fix.altitude <= 15000:
            raise BadRequest('`alt` has to be a valid altitude in the range of -1000 to 15000 meters.')

    # Speed
    if 'sog' in request.values:
        try:
            fix.ground_speed = int(request.values['sog']) / 3.6
        except ValueError:
            raise BadRequest('`sog` (speed over ground) has to be an integer value in km/h.')

        if not 0 <= fix.ground_speed <= (500 / 3.6):
            raise BadRequest('`sog` (speed over ground) has to be a valid speed in the range of 0 to 500 km/h.')

    # Track
    if 'cog' in request.values:
        try:
            fix.track = int(request.values['cog'])
        except ValueError:
            raise BadRequest('`cog` (course over ground) has to be an integer value in degrees.')

        if not 0 <= fix.track < 360:
            raise BadRequest('`cog` (course over ground) has to be a valid angle between 0 and 360 degrees.')

    fix.elevation = Elevation.get(fix.location_wkt)

    return fix
Exemple #3
0
    def fixReceived(self, host, key, payload):
        if len(payload) != 32: return

        pilot = User.by_tracking_key(key)
        if not pilot:
            log("%s FIX unknown pilot (key: %x)" % (host, key))
            return

        data = struct.unpack('!IIiiIHHHhhH', payload)

        fix = TrackingFix()
        fix.ip = host
        fix.pilot = pilot

        # import the time stamp from the packet if it's within a
        # certain range
        time_of_day_ms = data[1] % (24 * 3600 * 1000)
        time_of_day_s = time_of_day_ms / 1000
        time_of_day = time(time_of_day_s / 3600,
                           (time_of_day_s / 60) % 60,
                           time_of_day_s % 60,
                           (time_of_day_ms % 1000) * 1000)
        now = datetime.utcnow()
        now_s = ((now.hour * 60) + now.minute) * 60 + now.second
        if now_s - 1800 < time_of_day_s < now_s + 180:
            fix.time = datetime.combine(now.date(), time_of_day)
        elif now_s < 1800 and time_of_day_s > 23 * 3600:
            # midnight rollover occurred
            fix.time = (datetime.combine(now.date(), time_of_day) -
                        timedelta(days=1))
        else:
            log("bad time stamp: " + str(time_of_day))

        flags = data[0]
        if flags & FLAG_LOCATION:
            latitude = data[2] / 1000000.
            longitude = data[3] / 1000000.
            fix.set_location(longitude, latitude)

            fix.elevation = Elevation.get(fix.location_wkt)

        if flags & FLAG_TRACK:
            fix.track = data[5]

        if flags & FLAG_GROUND_SPEED:
            fix.ground_speed = data[6] / 16.

        if flags & FLAG_AIRSPEED:
            fix.airspeed = data[7] / 16.

        if flags & FLAG_ALTITUDE:
            fix.altitude = data[8]

        if flags & FLAG_VARIO:
            fix.vario = data[9] / 256.

        if flags & FLAG_ENL:
            fix.engine_noise_level = data[10]

        log("{} FIX {} {} {}".format(
            host, unicode(pilot).encode('utf8', 'ignore'),
            fix.time and fix.time.time(), fix.location))

        db.session.add(fix)
        try:
            db.session.commit()
        except SQLAlchemyError, e:
            log('database error:' + str(e))
            db.session.rollback()
Exemple #4
0
    def fixReceived(self, host, key, payload):
        if len(payload) != 32: return

        pilot = User.by_tracking_key(key)
        if not pilot:
            log.err("No such pilot: %x" % key)
            return

        data = struct.unpack('!IIiiIHHHhhH', payload)

        fix = TrackingFix()
        fix.ip = host
        fix.pilot = pilot

        # import the time stamp from the packet if it's within a
        # certain range
        time_of_day_ms = data[1] % (24 * 3600 * 1000)
        time_of_day_s = time_of_day_ms / 1000
        time_of_day = time(time_of_day_s / 3600, (time_of_day_s / 60) % 60,
                           time_of_day_s % 60, (time_of_day_ms % 1000) * 1000)
        now = datetime.utcnow()
        now_s = ((now.hour * 60) + now.minute) * 60 + now.second
        if now_s - 1800 < time_of_day_s < now_s + 180:
            fix.time = datetime.combine(now.date(), time_of_day)
        elif now_s < 1800 and time_of_day_s > 23 * 3600:
            # midnight rollover occurred
            fix.time = (datetime.combine(now.date(), time_of_day) -
                        timedelta(days=1))
        else:
            log.msg("ignoring time stamp from FIX packet: " + str(time_of_day))

        flags = data[0]
        if flags & FLAG_LOCATION:
            latitude = data[2] / 1000000.
            longitude = data[3] / 1000000.
            fix.set_location(longitude, latitude)

            fix.elevation = Elevation.get(fix.location_wkt)

        if flags & FLAG_TRACK:
            fix.track = data[5]

        if flags & FLAG_GROUND_SPEED:
            fix.ground_speed = data[6] / 16.

        if flags & FLAG_AIRSPEED:
            fix.airspeed = data[7] / 16.

        if flags & FLAG_ALTITUDE:
            fix.altitude = data[8]

        if flags & FLAG_VARIO:
            fix.vario = data[9] / 256.

        if flags & FLAG_ENL:
            fix.engine_noise_level = data[10]

        log.msg("{} {} {} {}".format(fix.time and fix.time.time(), host,
                                     unicode(pilot).encode('utf8', 'ignore'),
                                     fix.location))

        db.session.add(fix)
        try:
            db.session.commit()
        except SQLAlchemyError, e:
            log.err(e, 'database error')
            db.session.rollback()
Exemple #5
0
    def fixReceived(self, host, key, payload):
        if len(payload) != 32: return
        data = struct.unpack('!IIiiIHHHhhH', payload)

        pilot = User.by_tracking_key(key)
        if not pilot:
            log.err("No such pilot: %d" % key)
            return

        flags = data[0]

        fix = TrackingFix()
        fix.ip = host
        fix.pilot = pilot

        # import the time stamp from the packet if it's within a
        # certain range
        time_of_day_ms = data[1] % (24 * 3600 * 1000)
        time_of_day_s = time_of_day_ms / 1000
        time_of_day = datetime.time(time_of_day_s / 3600,
                                    (time_of_day_s / 60) % 60,
                                    time_of_day_s % 60,
                                    (time_of_day_ms % 1000) * 1000)
        now = datetime.datetime.utcnow()
        now_s = ((now.hour * 60) + now.minute) * 60 + now.second
        if now_s - 1800 < time_of_day_s < now_s + 180:
            fix.time = datetime.datetime.combine(now.date(), time_of_day)
        elif now_s < 1800 and time_of_day_s > 23 * 3600:
            # midnight rollover occurred
            fix.time = datetime.datetime.combine(now.date(), time_of_day) \
                       - datetime.timedelta(days=1)
        else:
            log.msg("ignoring time stamp from FIX packet: " + str(time_of_day))

        if flags & FLAG_LOCATION:
            fix.location = Location(latitude=data[2] / 1000000.,
                                    longitude=data[3] / 1000000.)

        if flags & FLAG_TRACK:
            fix.track = data[5]

        if flags & FLAG_GROUND_SPEED:
            fix.ground_speed = data[6] / 16.

        if flags & FLAG_AIRSPEED:
            fix.airspeed = data[7] / 16.

        if flags & FLAG_ALTITUDE:
            fix.altitude = data[8]

        if flags & FLAG_VARIO:
            fix.vario = data[9] / 256.

        if flags & FLAG_ENL:
            fix.engine_noise_level = data[10]

        log.msg(u"%s %s %s %s" % (fix.time and fix.time.time(), host, pilot, fix.location))

        DBSession.add(fix)
        try:
            transaction.commit()
        except SQLAlchemyError, e:
            log.err(e, 'database error')
            transaction.abort()
Exemple #6
0
def _parse_fix(pilot):
    fix = TrackingFix()
    fix.ip = request.remote_addr
    fix.pilot = pilot

    # Time
    if 'tm' not in request.values:
        raise BadRequest('`tm` (time) parameter is missing.')

    try:
        fix.time = datetime.utcfromtimestamp(int(request.values['tm']))
    except ValueError:
        raise BadRequest('`tm` (time) has to be a POSIX timestamp.')

    fix.time_visible = fix.time + timedelta(minutes=pilot.tracking_delay)

    # Location
    if 'lat' in request.values and 'lon' in request.values:
        try:
            fix.set_location(float(request.values['lon']),
                             float(request.values['lat']))
        except ValueError:
            raise BadRequest(
                '`lat` and `lon` have to be floating point value in degrees (WGS84).'
            )

    # Altitude
    if 'alt' in request.values:
        try:
            fix.altitude = int(request.values['alt'])
        except ValueError:
            raise BadRequest('`alt` has to be an integer value in meters.')

        if not -1000 <= fix.altitude <= 15000:
            raise BadRequest(
                '`alt` has to be a valid altitude in the range of -1000 to 15000 meters.'
            )

    # Speed
    if 'sog' in request.values:
        try:
            fix.ground_speed = int(request.values['sog']) / 3.6
        except ValueError:
            raise BadRequest(
                '`sog` (speed over ground) has to be an integer value in km/h.'
            )

        if not 0 <= fix.ground_speed <= (500 / 3.6):
            raise BadRequest(
                '`sog` (speed over ground) has to be a valid speed in the range of 0 to 500 km/h.'
            )

    # Track
    if 'cog' in request.values:
        try:
            fix.track = int(request.values['cog'])
        except ValueError:
            raise BadRequest(
                '`cog` (course over ground) has to be an integer value in degrees.'
            )

        if not 0 <= fix.track < 360:
            raise BadRequest(
                '`cog` (course over ground) has to be a valid angle between 0 and 360 degrees.'
            )

    fix.elevation = Elevation.get(fix.location_wkt)

    return fix
Exemple #7
0
    def fix_received(self, host, key, payload):
        if len(payload) != 32:
            return

        pilot = User.by_tracking_key(key)
        if not pilot:
            log("%s FIX unknown pilot (key: %x)" % (host, key))
            return

        data = struct.unpack("!IIiiIHHHhhH", payload)

        fix = TrackingFix()
        fix.ip = host
        fix.pilot = pilot

        # import the time stamp from the packet if it's within a
        # certain range
        time_of_day_ms = data[1] % (24 * 3600 * 1000)
        time_of_day_s = time_of_day_ms / 1000
        time_of_day = ms_to_time(data[1])

        now = datetime.utcnow()
        now_s = ((now.hour * 60) + now.minute) * 60 + now.second
        if now_s - 1800 < time_of_day_s < now_s + 180:
            fix.time = datetime.combine(now.date(), time_of_day)
        elif now_s < 1800 and time_of_day_s > 23 * 3600:
            # midnight rollover occurred
            fix.time = datetime.combine(now.date(),
                                        time_of_day) - timedelta(days=1)
        else:
            log("bad time stamp: " + str(time_of_day))
            fix.time = datetime.utcnow()

        fix.time_visible = fix.time + timedelta(minutes=pilot.tracking_delay)

        flags = data[0]
        if flags & FLAG_LOCATION:
            latitude = data[2] / 1000000.0
            longitude = data[3] / 1000000.0
            fix.set_location(longitude, latitude)

            fix.elevation = Elevation.get(fix.location_wkt)

        if flags & FLAG_TRACK:
            fix.track = data[5]

        if flags & FLAG_GROUND_SPEED:
            fix.ground_speed = data[6] / 16.0

        if flags & FLAG_AIRSPEED:
            fix.airspeed = data[7] / 16.0

        if flags & FLAG_ALTITUDE:
            fix.altitude = data[8]

        if flags & FLAG_VARIO:
            fix.vario = data[9] / 256.0

        if flags & FLAG_ENL:
            fix.engine_noise_level = data[10]

        log("{} FIX {} {} {}".format(
            host,
            pilot.name.encode("utf8", "ignore"),
            fix.time and fix.time.time(),
            fix.location,
        ))

        db.session.add(fix)
        try:
            db.session.commit()
        except SQLAlchemyError as e:
            log("database error:" + str(e))
            db.session.rollback()