Пример #1
0
def station_edit(request):
    """Edit or add a single station."""
    if request.POST['id']:
        pk = request.POST.get('id')
        station = get_object_or_404(Station, id=pk, owner=request.user)
        form = StationForm(request.POST, request.FILES, instance=station)
    else:
        form = StationForm(request.POST, request.FILES)
    if form.is_valid():
        f = form.save(commit=False)
        f.owner = request.user
        f.save()
        form.save_m2m()
        if f.online:
            messages.success(request, 'Successfully saved Ground Station.')
        else:
            messages.success(
                request,
                ('Successfully saved Ground Station. It will appear online '
                 'as soon as it connects with our API.'))

        return redirect(reverse('base:station_view', kwargs={'id': f.id}))
    else:
        messages.error(
            request,
            'Your Station submission had some errors.{0}'.format(form.errors))
        return redirect(
            reverse('users:view_user',
                    kwargs={'username': request.user.username}))
Пример #2
0
def view_user(request, username):
    """View for user page."""
    user = get_object_or_404(User, username=username)
    observations = Observation.objects.filter(author=user)[0:10]
    stations = Station.objects.filter(owner=user)

    can_schedule = False
    if request.user.is_authenticated():
        can_schedule = schedule_perms(request.user)

    try:
        token = Token.objects.get(user=user)
    except Token.DoesNotExist:
        token = Token.objects.create(user=user)
    form = StationForm()
    antennas = Antenna.objects.all()
    rigs = Rig.objects.all()

    return render(
        request, 'users/user_detail.html', {
            'user': user,
            'observations': observations,
            'stations': stations,
            'token': token,
            'form': form,
            'antennas': antennas,
            'rigs': rigs,
            'can_schedule': can_schedule
        })
Пример #3
0
def stations_list(request):
    """View to render Stations page."""
    stations = Station.objects.all()
    form = StationForm()
    antennas = Antenna.objects.all()

    return render(request, 'base/stations.html',
                  {'stations': stations, 'form': form, 'antennas': antennas})
Пример #4
0
def stations_list(request):
    """View to render Stations page."""
    stations = Station.objects.annotate(total_obs=Count('observations'))
    form = StationForm()
    antennas = Antenna.objects.all()
    online = stations.filter(status=2).count()
    testing = stations.filter(status=1).count()

    return render(request, 'base/stations.html',
                  {'stations': stations, 'form': form, 'antennas': antennas,
                   'online': online, 'testing': testing,
                   'mapbox_id': settings.MAPBOX_MAP_ID, 'mapbox_token': settings.MAPBOX_TOKEN})
Пример #5
0
def station_edit(request):
    """Edit or add a single station."""
    if request.POST["id"]:
        pk = request.POST.get("id")
        station = get_object_or_404(Station, id=pk, owner=request.user)
        form = StationForm(request.POST, request.FILES, instance=station)
    else:
        form = StationForm(request.POST, request.FILES)
    if form.is_valid():
        f = form.save(commit=False)
        f.owner = request.user
        f.save()
        form.save_m2m()
        if f.online:
            messages.success(request, "Successfully saved Ground Station.")
        else:
            messages.success(
                request,
                ("Successfully saved Ground Station. It will appear online " "as soon as it connects with our API."),
            )

        return redirect(reverse("base:station_view", kwargs={"id": f.id}))
    else:
        messages.error(request, "Some fields missing on the form")
        return redirect(reverse("users:view_user", kwargs={"username": request.user.username}))
Пример #6
0
def station_view(request, id):
    """View for single station page."""
    station = get_object_or_404(Station, id=id)
    form = StationForm(instance=station)
    antennas = Antenna.objects.all()
    rigs = Rig.objects.all()
    unsupported_frequencies = request.GET.get('unsupported_frequencies', '0')

    can_schedule = schedule_perms(request.user, station)

    # Calculate uptime
    uptime = '-'
    try:
        latest = StationStatusLog.objects.filter(station=station)[0]
    except IndexError:
        latest = None
    if latest:
        if latest.status:
            try:
                offline = StationStatusLog.objects.filter(station=station, status=0)[0]
                uptime = latest.changed - offline.changed
            except IndexError:
                uptime = now() - latest.changed
            uptime = str(uptime).split('.')[0]

    if request.user.is_authenticated():
        if request.user == station.owner:
            wiki_help = ('<a href="{0}" target="_blank" class="wiki-help"><span class="glyphicon '
                         'glyphicon-question-sign" aria-hidden="true"></span>'
                         '</a>'.format(settings.WIKI_STATION_URL))
            if station.is_offline:
                messages.error(request, ('Your Station is offline. You should make '
                                         'sure it can successfully connect to the Network API. '
                                         '{0}'.format(wiki_help)))
            if station.is_testing:
                messages.warning(request, ('Your Station is in Testing mode. Once you are sure '
                                           'it returns good observations you can put it online. '
                                           '{0}'.format(wiki_help)))

    return render(request, 'base/station_view.html',
                  {'station': station, 'form': form, 'antennas': antennas,
                   'mapbox_id': settings.MAPBOX_MAP_ID,
                   'mapbox_token': settings.MAPBOX_TOKEN,
                   'rigs': rigs, 'can_schedule': can_schedule,
                   'unsupported_frequencies': unsupported_frequencies,
                   'uptime': uptime})
Пример #7
0
def view_user(request, username):
    """View for user page."""
    user = User.objects.get(username=username)
    observations = Observation.objects.filter(author=user)[0:10]
    stations = Station.objects.filter(owner=user)
    try:
        token = Token.objects.get(user=user)
    except:
        token = Token.objects.create(user=user)
    form = StationForm()
    antennas = Antenna.objects.all()
    rigs = Rig.objects.all()

    return render(
        request, 'users/user_detail.html', {
            'user': user,
            'observations': observations,
            'stations': stations,
            'token': token,
            'form': form,
            'antennas': antennas,
            'rigs': rigs
        })
Пример #8
0
def station_edit(request):
    """Edit or add a single station."""
    if request.POST['id']:
        pk = request.POST.get('id')
        station = get_object_or_404(Station, id=pk, owner=request.user)
        form = StationForm(request.POST, request.FILES, instance=station)
    else:
        form = StationForm(request.POST, request.FILES)
    if form.is_valid():
        f = form.save(commit=False)
        f.owner = request.user
        f.save()
        form.save_m2m()
        messages.success(request, 'Successfully saved Ground Station')
        return redirect(reverse('base:station_view', kwargs={'id': f.id}))
    else:
        messages.error(request, 'Some fields missing on the form')
        return redirect(reverse('users:view_user', kwargs={'username': request.user.username}))
Пример #9
0
def station_view(request, id):
    """View for single station page."""
    station = get_object_or_404(Station, id=id)
    form = StationForm(instance=station)
    antennas = Antenna.objects.all()
    rigs = Rig.objects.all()
    unsupported_frequencies = request.GET.get('unsupported_frequencies', '0')

    try:
        satellites = Satellite.objects.filter(
            transmitters__alive=True).distinct()
    except:
        pass  # we won't have any next passes to display

    # Load the station information and invoke ephem so we can
    # calculate upcoming passes for the station
    observer = ephem.Observer()
    observer.lon = str(station.lng)
    observer.lat = str(station.lat)
    observer.elevation = station.alt

    nextpasses = []
    passid = 0

    for satellite in satellites:
        # look for a match between transmitters from the satellite and
        # ground station antenna frequency capabilities
        if int(unsupported_frequencies) == 0:
            frequency_supported = False
            transmitters = Transmitter.objects.filter(satellite=satellite)
            for gs_antenna in station.antenna.all():
                for transmitter in transmitters:
                    if transmitter.downlink_low:
                        if (gs_antenna.frequency <= transmitter.downlink_low <=
                                gs_antenna.frequency_max):
                            frequency_supported = True
            if not frequency_supported:
                continue

        observer.date = ephem.date(datetime.today())

        try:
            sat_ephem = ephem.readtle(str(satellite.latest_tle.tle0),
                                      str(satellite.latest_tle.tle1),
                                      str(satellite.latest_tle.tle2))
        except (ValueError, AttributeError):
            continue

        # Here we are going to iterate over each satellite to
        # find its appropriate passes within a given time constraint
        keep_digging = True
        while keep_digging:
            try:
                tr, azr, tt, altt, ts, azs = observer.next_pass(sat_ephem)
            except ValueError:
                break  # there will be sats in our list that fall below horizon, skip
            except TypeError:
                break  # if there happens to be a non-EarthSatellite object in the list
            except Exception:
                break

            if tr is None:
                break

            # using the angles module convert the sexagesimal degree into
            # something more easily read by a human
            elevation = format(math.degrees(altt), '.0f')
            azimuth_r = format(math.degrees(azr), '.0f')
            azimuth_s = format(math.degrees(azs), '.0f')
            passid += 1

            # show only if >= configured horizon and in next 6 hours,
            # and not directly overhead (tr < ts see issue 199)
            if tr < ephem.date(datetime.today() +
                               timedelta(hours=settings.STATION_UPCOMING_END)):
                if (float(elevation) >= station.horizon and tr < ts):
                    valid = True
                    if tr < ephem.Date(datetime.now() + timedelta(
                            minutes=int(settings.DATE_MIN_START))):
                        valid = False
                    polar_data = calculate_polar_data(observer, sat_ephem,
                                                      tr.datetime(),
                                                      ts.datetime(), 10)
                    sat_pass = {
                        'passid': passid,
                        'mytime': str(observer.date),
                        'debug': observer.next_pass(sat_ephem),
                        'name': str(satellite.name),
                        'id': str(satellite.id),
                        'success_rate': str(satellite.success_rate),
                        'unknown_rate': str(satellite.unknown_rate),
                        'empty_rate': str(satellite.empty_rate),
                        'data_count': str(satellite.data_count),
                        'verified_count': str(satellite.verified_count),
                        'empty_count': str(satellite.empty_count),
                        'unknown_count': str(satellite.unknown_count),
                        'norad_cat_id': str(satellite.norad_cat_id),
                        'tr': tr.datetime(),  # Rise time
                        'azr': azimuth_r,  # Rise Azimuth
                        'tt': tt,  # Max altitude time
                        'altt': elevation,  # Max altitude
                        'ts': ts.datetime(),  # Set time
                        'azs': azimuth_s,  # Set azimuth
                        'valid': valid,
                        'polar_data': polar_data
                    }
                    nextpasses.append(sat_pass)
                observer.date = ephem.Date(ts).datetime() + timedelta(
                    minutes=1)
            else:
                keep_digging = False

    return render(
        request, 'base/station_view.html', {
            'station': station,
            'form': form,
            'antennas': antennas,
            'mapbox_id': settings.MAPBOX_MAP_ID,
            'mapbox_token': settings.MAPBOX_TOKEN,
            'nextpasses': sorted(nextpasses, key=itemgetter('tr')),
            'rigs': rigs,
            'unsupported_frequencies': unsupported_frequencies
        })
Пример #10
0
def station_view(request, id):
    """View for single station page."""
    station = get_object_or_404(Station, id=id)
    form = StationForm(instance=station)
    antennas = Antenna.objects.all()
    rigs = Rig.objects.all()

    try:
        satellites = Satellite.objects.filter(
            transmitters__alive=True).distinct()
    except:
        pass  # we won't have any next passes to display

    # Load the station information and invoke ephem so we can
    # calculate upcoming passes for the station
    observer = ephem.Observer()
    observer.lon = str(station.lng)
    observer.lat = str(station.lat)
    observer.elevation = station.alt

    nextpasses = []
    passid = 0

    for satellite in satellites:
        observer.date = ephem.date(datetime.today())

        try:
            sat_ephem = ephem.readtle(str(satellite.latest_tle.tle0),
                                      str(satellite.latest_tle.tle1),
                                      str(satellite.latest_tle.tle2))

            # Here we are going to iterate over each satellite to
            # find its appropriate passes within a given time constraint
            keep_digging = True
            while keep_digging:
                try:
                    tr, azr, tt, altt, ts, azs = observer.next_pass(sat_ephem)

                    if tr is None:
                        break

                    # bug in pyephem causes overhead sats to appear in the result
                    # mixing next-pass data with current pass data, resulting in
                    # satnogs/satnogs-network#199. As a workaround, pyephem does
                    # return set time for current pass while rise time for next
                    # pass so when this happens we want to toss the entry out
                    # not a break as this sat might have another valid pass
                    if ts < tr:
                        pass

                    # using the angles module convert the sexagesimal degree into
                    # something more easily read by a human
                    elevation = format(math.degrees(altt), '.0f')
                    azimuth = format(math.degrees(azr), '.0f')
                    passid += 1

                    # show only if >= configured horizon and in next 6 hours
                    if tr < ephem.date(datetime.today() + timedelta(hours=6)):
                        if float(elevation) >= station.horizon:
                            sat_pass = {
                                'passid': passid,
                                'mytime': str(observer.date),
                                'debug': observer.next_pass(sat_ephem),
                                'name': str(satellite.name),
                                'id': str(satellite.id),
                                'norad_cat_id': str(satellite.norad_cat_id),
                                'tr': tr,  # Rise time
                                'azr': azimuth,  # Rise Azimuth
                                'tt': tt,  # Max altitude time
                                'altt': elevation,  # Max altitude
                                'ts': ts,  # Set time
                                'azs': azs
                            }  # Set azimuth
                            nextpasses.append(sat_pass)
                        observer.date = ephem.Date(ts).datetime() + timedelta(
                            minutes=1)
                        continue
                    else:
                        keep_digging = False
                    continue
                except ValueError:
                    break  # there will be sats in our list that fall below horizon, skip
                except TypeError:
                    break  # if there happens to be a non-EarthSatellite object in the list
                except Exception:
                    break
        except (ValueError, AttributeError):
            pass  # TODO: if something does not have a proper TLE line we need to know/fix

    return render(
        request, 'base/station_view.html', {
            'station': station,
            'form': form,
            'antennas': antennas,
            'mapbox_id': settings.MAPBOX_MAP_ID,
            'mapbox_token': settings.MAPBOX_TOKEN,
            'nextpasses': sorted(nextpasses, key=itemgetter('tr')),
            'rigs': rigs
        })
Пример #11
0
def station_edit(request, id=None):
    """Edit or add a single station."""
    station = None
    antennas = Antenna.objects.all()
    rigs = Rig.objects.all()
    if id:
        station = get_object_or_404(Station, id=id, owner=request.user)

    if request.method == 'POST':
        if station:
            form = StationForm(request.POST, request.FILES, instance=station)
        else:
            form = StationForm(request.POST, request.FILES)
        if form.is_valid():
            f = form.save(commit=False)
            if not station:
                f.testing = True
            f.owner = request.user
            f.save()
            form.save_m2m()
            messages.success(request, 'Ground Station saved successfully.')
            return redirect(reverse('base:station_view', kwargs={'id': f.id}))
        else:
            messages.error(request, ('Your Ground Station submission has some '
                                     'errors. {0}').format(form.errors))
            return render(request, 'base/station_edit.html',
                          {'form': form, 'station': station, 'antennas': antennas, 'rigs': rigs})
    else:
        if station:
            form = StationForm(instance=station)
        else:
            form = StationForm()
        return render(request, 'base/station_edit.html',
                      {'form': form, 'station': station, 'antennas': antennas, 'rigs': rigs})