Example #1
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
Example #2
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()
Example #3
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()
Example #4
0
def _parse_fix(pilot_id):
    fix = TrackingFix()
    fix.ip = request.remote_addr
    fix.pilot_id = pilot_id

    # 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.')

    # 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
Example #5
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()
Example #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