Skip to content

Commit

Permalink
Flight Overhead: Use schema.Location and provide better descriptions …
Browse files Browse the repository at this point in the history
…for default values in schema fields (#1781)
  • Loading branch information
kylebolstad authored Sep 5, 2023
1 parent a0e1107 commit 60fc8f8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 138 deletions.
37 changes: 36 additions & 1 deletion apps/flightoverhead/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
# Flight Overhead for Tidbyt

Uses AirLabs or OpenSky to find the flight overhead a location.
Use AirLabs or OpenSky to find the flight overhead a location. By default, OpenSky is the provider. An optional OpenSky account can be used to extend the request quota. An AirLabs API Key is required to use AirLabs as the provider.

>>>
name = "Provider (Required)",
desc = "The provider for the data"

name = "Location (Required)",
desc = "The decimalized latitude and longitude to search"

name = "Radius",
desc = "The radius (in nautical miles) to search"

name = "AirLabs API Key",
desc = "An AirLabs API Key is required to use AirLabs as the provider"

name = "OpenSky Username",
desc = "An OpenSky account can be used to extend the request quota"

name = "OpenSky Password",
desc = "An OpenSky account can be used to extend the request quota"

name = "Provider TTL Seconds",
desc = "The number of seconds to cache results from the provider"

name = "Show Route",
desc = "Some providers can often display incorrect routes"

name = "Limit",
desc = "Limit the number of results to display"

name = "Return Message on Empty",
desc = "The message to return if no flights are found"

## Screenshot

![Flight Overhead for Tidbyt](screenshot.png)

## Credit

Thank you to [The OpenSky Network](https://opensky-network.org), [HexDB](https://hexdb.io), and [ADSB.lol](https://www.adsb.lol) for providing free access to their data.
181 changes: 44 additions & 137 deletions apps/flightoverhead/flight_overhead.star
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Author: Kyle Bolstad
"""

load("animation.star", "animation")
load("encoding/json.star", "json")
load("http.star", "http")
load("humanize.star", "humanize")
load("math.star", "math")
Expand Down Expand Up @@ -38,21 +39,25 @@ PROVIDERS = {
}

DEFAULT_AIRLABS_API_KEY = ""
DEFAULT_DISABLE_END_HOUR = "None"
DEFAULT_DISABLE_START_HOUR = "None"
DEFAULT_IGNORE = ""
DEFAULT_LIMIT = 1
DEFAULT_LOCATION = ""
DEFAULT_LOCATION = json.encode({
"lat": "40.6969512",
"lng": "-73.9538453",
"description": "Brooklyn, NY, USA",
"locality": "Tidbyt",
"place_id": "ChIJr3Hjqu5bwokRmeukysQhFCU",
"timezone": "America/New_York",
})
DEFAULT_OPENSKY_USERNAME = ""
DEFAULT_OPENSKY_PASSWORD = ""
DEFAULT_PRINT_LOG = False
DEFAULT_PROVIDER = "None"
DEFAULT_PROVIDER = "opensky"
DEFAULT_PROVIDER_BBOX = ""
DEFAULT_PROVIDER_TTL_SECONDS = 0
DEFAULT_RADIUS = 1
DEFAULT_RETURN_MESSAGE_ON_EMPTY = ""
DEFAULT_SHOW_ROUTE = True
DEFAULT_TIMEZONE = "America/Chicago"

KN_RATIO = 1.94
KM_RATIO = 0.54
Expand All @@ -70,9 +75,7 @@ def main(config):
opensky_username = config.get("opensky_username", DEFAULT_OPENSKY_USERNAME)
opensky_password = config.get("opensky_password", DEFAULT_OPENSKY_PASSWORD)

location = DEFAULT_LOCATION
if config.get("location"):
location = re.sub("[^\\d-,\\.]", "", config.get("location")) or DEFAULT_LOCATION
location = json.decode(config.get("location", DEFAULT_LOCATION))

radius = DEFAULT_RADIUS
if config.get("radius"):
Expand All @@ -86,11 +89,6 @@ def main(config):
provider_ttl_seconds = re.sub("\\D", "", config.get("provider_ttl_seconds")) or DEFAULT_PROVIDER_TTL_SECONDS
provider_ttl_seconds = int(provider_ttl_seconds)

timezone = config.get("timezone", DEFAULT_TIMEZONE)
disable_start_hour = config.get("disable_start_hour", DEFAULT_DISABLE_START_HOUR)
disable_end_hour = config.get("disable_end_hour", DEFAULT_DISABLE_END_HOUR)
now = time.now().in_location(timezone).hour

return_message_on_empty = config.get("return_message_on_empty", DEFAULT_RETURN_MESSAGE_ON_EMPTY)

ignore = config.get("ignore", DEFAULT_IGNORE)
Expand Down Expand Up @@ -332,45 +330,30 @@ def main(config):

print_log(time.now())

if disable_start_hour != DEFAULT_DISABLE_START_HOUR and disable_end_hour != DEFAULT_DISABLE_END_HOUR:
disable_start_hour = int(disable_start_hour)
disable_end_hour = int(disable_end_hour)

print_log("Disabling between %d:00 and %d:00" % (disable_start_hour, disable_end_hour))

if (disable_end_hour >= disable_start_hour and now >= disable_start_hour and now < disable_end_hour) or (disable_end_hour < disable_start_hour and now >= disable_start_hour or now < disable_end_hour):
print_log("Disabled")

return empty_message()

if provider:
auth = ()
provider_request = ""
provider_request_url = ""
lat = 0
long = 0
la_min = 0
lo_min = 0
la_max = 0
lo_max = 0
bbox = ""

lat_lng = location.split(",")
if len(lat_lng) == 2:
lat = lat_lng[0]
long = lat_lng[1]
lat = location["lat"]
lng = location["lng"]

if lat and long:
if lat and lng:
lat = float(lat)
long = float(long)
lng = float(lng)
miles_per_deg_lat = 69.1
miles_per_deg_long = 69.1 * math.cos(lat / 180 * math.pi)
miles_per_deg_lng = 69.1 * math.cos(lat / 180 * math.pi)
lat_pm = radius / miles_per_deg_lat
long_pm = radius / miles_per_deg_long
lng_pm = radius / miles_per_deg_lng
la_min = lat - lat_pm
lo_min = long - long_pm
lo_min = lng - lng_pm
la_max = lat + lat_pm
lo_max = long + long_pm
lo_max = lng + lng_pm
bbox = "%s,%s,%s,%s" % (la_min, lo_min, la_max, lo_max)

if provider == "airlabs":
Expand Down Expand Up @@ -431,54 +414,6 @@ def get_schema():
),
)

timezones = [
schema.Option(display = "Hawaii (-10)", value = "Pacific/Honolulu"),
schema.Option(display = "Alaska (-9)", value = "America/Anchorage"),
schema.Option(display = "Pacific (-8)", value = "America/Los_Angeles"),
schema.Option(display = "Mountain (-7)", value = "America/Denver"),
schema.Option(display = "Central (-6)", value = "America/Chicago"),
schema.Option(display = "Eastern (-5)", value = "America/New_York"),
schema.Option(display = "Atlantic (-4)", value = "America/Halifax"),
schema.Option(display = "Newfoundland (-3.5)", value = "America/St_Johns"),
schema.Option(display = "Brazil (-3)", value = "America/Sao_Paulo"),
schema.Option(display = "UTC (0)", value = "UTC"),
schema.Option(display = "Central Europe (+1)", value = "Europe/Berlin"),
schema.Option(display = "Eastern Europe (+2)", value = "Europe/Moscow"),
schema.Option(display = "India (+5.5)", value = "Asia/Kolkata"),
schema.Option(display = "China (+8)", value = "Asia/Shanghai"),
schema.Option(display = "Japan (+9)", value = "Asia/Tokyo"),
schema.Option(display = "Australia Eastern (+10)", value = "Australia/Sydney"),
schema.Option(display = "New Zealand (+12)", value = "Pacific/Auckland"),
]

hours = [
schema.Option(display = "None", value = "None"),
schema.Option(display = "1 AM", value = "1"),
schema.Option(display = "2 AM", value = "2"),
schema.Option(display = "3 AM", value = "3"),
schema.Option(display = "4 AM", value = "4"),
schema.Option(display = "5 AM", value = "5"),
schema.Option(display = "6 AM", value = "6"),
schema.Option(display = "7 AM", value = "7"),
schema.Option(display = "8 AM", value = "8"),
schema.Option(display = "9 AM", value = "9"),
schema.Option(display = "10 AM", value = "10"),
schema.Option(display = "11 AM", value = "11"),
schema.Option(display = "Noon", value = "12"),
schema.Option(display = "1 PM", value = "13"),
schema.Option(display = "2 PM", value = "14"),
schema.Option(display = "3 PM", value = "15"),
schema.Option(display = "4 PM", value = "16"),
schema.Option(display = "5 PM", value = "17"),
schema.Option(display = "6 PM", value = "18"),
schema.Option(display = "7 PM", value = "19"),
schema.Option(display = "8 PM", value = "20"),
schema.Option(display = "9 PM", value = "21"),
schema.Option(display = "10 PM", value = "22"),
schema.Option(display = "11 PM", value = "23"),
schema.Option(display = "Midnight", value = "0"),
]

limits = []

for i in range(MAX_LIMIT):
Expand All @@ -494,48 +429,51 @@ def get_schema():
fields = [
schema.Dropdown(
id = "provider",
name = "Provider",
desc = "Provider",
name = "Provider (Required)",
desc = "The provider for the data",
icon = "ioxhost",
default = DEFAULT_PROVIDER,
options = providers,
),
schema.Location(
id = "location",
name = "Location (Required)",
desc = "The decimalized latitude and longitude to search",
icon = "mapLocationDot",
),
schema.Dropdown(
id = "radius",
name = "Radius",
desc = "The radius (in nautical miles) to search",
icon = "circleDot",
default = "%s" % DEFAULT_RADIUS,
options = radii,
),
schema.Text(
id = "airlabs_api_key",
name = "AirLabs API Key",
desc = "AirLabs API Key",
desc = "An AirLabs API Key is required to use AirLabs as the provider",
icon = "key",
default = DEFAULT_AIRLABS_API_KEY,
),
schema.Text(
id = "opensky_username",
name = "OpenSky Username",
desc = "OpenSky Username",
desc = "An OpenSky account can be used to extend the request quota",
icon = "user",
default = DEFAULT_OPENSKY_USERNAME,
),
schema.Text(
id = "opensky_password",
name = "OpenSky Password",
desc = "OpenSky Password",
desc = "An OpenSky account can be used to extend the request quota",
icon = "key",
),
schema.Text(
id = "location",
name = "Location",
desc = "Latitude, Longitude",
icon = "mapLocationDot",
),
schema.Dropdown(
id = "radius",
name = "Radius",
desc = "Radius in Nautical Miles",
icon = "circleDot",
default = "%s" % DEFAULT_RADIUS,
options = radii,
default = DEFAULT_OPENSKY_PASSWORD,
),
schema.Text(
id = "provider_ttl_seconds",
name = "Provider TTL Seconds",
desc = "Number of seconds to cache results",
desc = "The number of seconds to cache results from the provider",
icon = "clock",
default = "%s" % DEFAULT_PROVIDER_TTL_SECONDS,
),
Expand All @@ -549,48 +487,17 @@ def get_schema():
schema.Dropdown(
id = "limit",
name = "Limit",
desc = "Limit number of results",
desc = "Limit the number of results to display",
icon = "list",
default = "%s" % DEFAULT_LIMIT,
options = limits,
),
schema.Dropdown(
id = "timezone",
name = "Timezone",
desc = "Timezone",
icon = "clock",
default = DEFAULT_TIMEZONE,
options = timezones,
),
schema.Dropdown(
id = "disable_start_hour",
name = "Disable Start Hour",
desc = "Disable during certain timeframe",
icon = "clock",
default = DEFAULT_DISABLE_START_HOUR,
options = hours,
),
schema.Dropdown(
id = "disable_end_hour",
name = "Disable End Hour",
desc = "Disable during certain timeframe",
icon = "clock",
default = DEFAULT_DISABLE_END_HOUR,
options = hours,
),
schema.Text(
id = "return_message_on_empty",
name = "Return Message on Empty",
desc = "Message to return if no flights found",
desc = "The message to return if no flights are found",
icon = "message",
default = DEFAULT_RETURN_MESSAGE_ON_EMPTY,
),
schema.Toggle(
id = "print_log",
name = "Print Log",
desc = "Print log statements to help debug",
icon = "bug",
default = DEFAULT_PRINT_LOG,
),
],
)

0 comments on commit 60fc8f8

Please sign in to comment.