Skip to content

Commit

Permalink
fix: optional gui and missing skilljson (#19)
Browse files Browse the repository at this point in the history
* fix: optional gui and missing skilljson

GUI is using too much CPU and makes skill unusable in rpi

* fix: optional gui and missing skilljson

GUI is using too much CPU and makes skill unusable in rpi

* tests

* tests

* tests
  • Loading branch information
JarbasAl authored Nov 15, 2024
1 parent 74035da commit 6469469
Show file tree
Hide file tree
Showing 13 changed files with 288 additions and 73 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/build_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Run Build Tests
on:
push:
branches:
- master
pull_request:
branches:
- dev
workflow_dispatch:

jobs:
build_tests:
strategy:
max-parallel: 2
matrix:
python-version: [3.8, 3.9, "3.10", "3.11" ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install Build Tools
run: |
python -m pip install build wheel
- name: Install System Dependencies
run: |
sudo apt-get update
sudo apt install python3-dev swig libssl-dev
- name: Build Source Packages
run: |
python setup.py sdist
- name: Build Distribution Packages
run: |
python setup.py bdist_wheel
- name: Install skill
run: |
pip install .[gui]
66 changes: 66 additions & 0 deletions .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Run UnitTests
on:
pull_request:
branches:
- dev
paths-ignore:
- 'version.py'
- 'examples/**'
- '.github/**'
- '.gitignore'
- 'LICENSE'
- 'CHANGELOG.md'
- 'MANIFEST.in'
- 'README.md'
- 'scripts/**'
push:
branches:
- master
paths-ignore:
- 'version.py'
- 'examples/**'
- '.github/**'
- '.gitignore'
- 'LICENSE'
- 'CHANGELOG.md'
- 'MANIFEST.in'
- 'README.md'
- 'scripts/**'
workflow_dispatch:

jobs:
unit_tests:
strategy:
matrix:
python-version: [3.9, "3.10" ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install System Dependencies
run: |
sudo apt-get update
sudo apt install python3-dev
python -m pip install build wheel
- name: Install core repo
run: |
pip install .
- name: Install test dependencies
run: |
pip install pytest pytest-timeout pytest-cov
- name: Install System Dependencies
run: |
sudo apt-get update
- name: Install ovos dependencies
run: |
pip install ovos-plugin-manager
- name: Run unittests
run: |
pytest --cov=ovos-skill-iss-location --cov-report xml test
- name: Upload coverage
env:
CODECOV_TOKEN: ${{secrets.CODECOV_TOKEN}}
uses: codecov/codecov-action@v2
3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
recursive-include locale *
recursive-include gui *
include *.txt
154 changes: 86 additions & 68 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,31 @@
from os.path import join
from time import sleep

import matplotlib.pyplot as plt
import pytz
import requests
from lingua_franca.format import nice_duration
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
from mpl_toolkits.basemap import Basemap
from ovos_date_parser import nice_duration
from ovos_utils.time import to_local, now_local
from ovos_workshop.decorators import intent_handler
from ovos_workshop.decorators import resting_screen_handler
from ovos_workshop.intents import IntentBuilder
from ovos_workshop.skills import OVOSSkill
from skyfield.api import Topos, load

try:
import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
from mpl_toolkits.basemap import Basemap
GUI = True
except ImportError:
GUI = False


class ISSLocationSkill(OVOSSkill):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if "enable_gui" not in self.settings:
self.settings["enable_gui"] = False
if "geonames_user" not in self.settings:
self.settings["geonames_user"] = "jarbas"
if "map_style" not in self.settings:
Expand All @@ -38,46 +45,51 @@ def __init__(self, *args, **kwargs):
if "dpi" not in self.settings:
self.settings["dpi"] = 500

def update_picture(self):
@property
def use_gui(self) -> bool:
return GUI and self.settings["enable_gui"]

def get_iss_data(self):
data = requests.get("http://api.open-notify.org/iss-now.json").json()
astronauts = requests.get("http://api.open-notify.org/astros.json").json()

lat = data['iss_position']['latitude']
lon = data['iss_position']['longitude']

params = {
"username": self.settings["geonames_user"],
"lat": lat,
"lng": lon
}
ocean_names = "http://api.geonames.org/oceanJSON"
land_names = "http://api.geonames.org/countryCodeJSON"

# reverse geo
data = requests.get(ocean_names, params=params).json()
try:
data = requests.get("http://api.open-notify.org/iss-now.json").json()
astronauts = requests.get("http://api.open-notify.org/astros.json").json()

lat = data['iss_position']['latitude']
lon = data['iss_position']['longitude']

params = {
"username": self.settings["geonames_user"],
"lat": lat,
"lng": lon
}
ocean_names = "http://api.geonames.org/oceanJSON"
land_names = "http://api.geonames.org/countryCodeJSON"

# reverse geo
data = requests.get(ocean_names, params=params).json()
toponym = "The " + data['ocean']['name']
except:

try:
toponym = "The " + data['ocean']['name']
params = {
"username": self.settings["geonames_user"],
"lat": lat,
"lng": lon,
"formatted": True,
"style": "full"
}
data = requests.get(land_names,
params=params).json()
toponym = data['countryName']
except:
toponym = "unknown"
if not self.lang.lower().startswith("en") and toponym != "unknown":
toponym = self.translator.translate(toponym, self.lang)
return toponym, lat, lon, astronauts

try:
params = {
"username": self.settings["geonames_user"],
"lat": lat,
"lng": lon,
"formatted": True,
"style": "full"
}
data = requests.get(land_names,
params=params).json()
toponym = data['countryName']
except:
toponym = "unknown"
if not self.lang.lower().startswith("en") and toponym != "unknown":
toponym = self.translator.translate(toponym, self.lang)

def update_picture(self, toponym, lat, lon, astronauts):
try:
image = self.generate_map(lat, lon)

self.gui['imgLink'] = image
self.gui['caption'] = f"{toponym} Lat: {lat} Lon: {lon}"
self.gui['lat'] = lat
Expand All @@ -90,7 +102,8 @@ def update_picture(self):

@resting_screen_handler("ISS")
def idle(self, message):
self.update_picture() # values available in self.gui
toponym, lat, lon, astronauts = self.get_iss_data()
self.update_picture(toponym, lat, lon, astronauts) # values available in self.gui
self.gui.show_image(self.gui['imgLink'], fill='PreserveAspectFit')

def generate_map(self, lat, lon):
Expand Down Expand Up @@ -142,20 +155,23 @@ def handle_about_iss_intent(self, message):

@intent_handler('where_iss.intent')
def handle_iss(self, message):
self.update_picture() # values available in self.gui
self.gui.show_image(self.gui['imgLink'],
caption=self.gui['caption'],
fill='PreserveAspectFit')
if self.gui['toponym'] == "unknown":
toponym, lat, lon, astronauts = self.get_iss_data()
if self.use_gui:
self.update_picture(toponym, lat, lon, astronauts)
self.gui.show_image(self.gui['imgLink'],
caption=self.gui['caption'],
fill='PreserveAspectFit')

if toponym == "unknown":
self.speak_dialog("location.unknown", {
"latitude": self.gui['lat'],
"longitude": self.gui['lon']
"latitude": lat,
"longitude": lon
}, wait=True)
else:
self.speak_dialog("location.current", {
"latitude": self.gui['lat'],
"longitude": self.gui['lon'],
"toponym": self.gui['toponym']
"latitude": lat,
"longitude":lon,
"toponym": toponym
}, wait=True)
sleep(1)
self.gui.release()
Expand All @@ -172,10 +188,10 @@ def handle_when(self, message):

duration = nice_duration(dur, lang=self.lang)
visible_dur = nice_duration(delta, lang=self.lang)
caption = self.location_pretty + " " + dt.strftime("%m/%d/%Y, %H:%M:%S")
image = self.generate_map(lat, lon)

self.gui.show_image(image, caption=caption, fill='PreserveAspectFit')
if self.use_gui:
caption = self.location_pretty + " " + dt.strftime("%m/%d/%Y, %H:%M:%S")
image = self.generate_map(lat, lon)
self.gui.show_image(image, caption=caption, fill='PreserveAspectFit')

self.speak_dialog("location.when", {
"duration": duration,
Expand All @@ -186,31 +202,32 @@ def handle_when(self, message):
}, wait=True)
self.gui.release()

@intent_handler(
IntentBuilder("WhoISSIntent").require("who").require(
"onboard").require("iss"))
@intent_handler(IntentBuilder("WhoISSIntent").require("who").
require("onboard").require("iss"))
def handle_who(self, message):
self.update_picture() # values available in self.gui
toponym, lat, lon, astronauts = self.get_iss_data()
people = [
p["name"] for p in self.gui["astronauts"]
p["name"] for p in astronauts
if p["craft"] == "ISS"
]
people = ", ".join(people)
self.gui.show_image(self.settings["iss_bg"],
override_idle=True,
fill='PreserveAspectFit',
caption=people)
if self.use_gui:
self.update_picture(toponym, lat, lon, astronauts)
self.gui.show_image(self.settings["iss_bg"],
override_idle=True,
fill='PreserveAspectFit',
caption=people)
self.speak_dialog("who", {"people": people}, wait=True)
sleep(1)
self.gui.release()

@intent_handler(
IntentBuilder("NumberISSIntent").require("how_many").require(
"onboard").require("iss"))
@intent_handler(IntentBuilder("NumberISSIntent").require("how_many")
.require("onboard").require("iss"))
def handle_number(self, message):
self.update_picture() # values available in self.gui

toponym, lat, lon, astronauts = self.get_iss_data()
people = [
p["name"] for p in self.gui["astronauts"]
p["name"] for p in astronauts
if p["craft"] == "ISS"
]
num = len(people)
Expand Down Expand Up @@ -336,6 +353,7 @@ def predict(self):
from ovos_utils.fakebus import FakeBus
from ovos_bus_client.message import Message
from ovos_config.locale import setup_locale

setup_locale()


Expand Down
3 changes: 3 additions & 0 deletions gui-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
matplotlib
basemap
pillow
File renamed without changes
File renamed without changes
File renamed without changes
17 changes: 17 additions & 0 deletions locale/en-us/skill.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"skill_id": "ovos-skill-iss-location.openvoiceos",
"source": "https://github.com/OpenVoiceOS/ovos-skill-iss-location",
"name": "ISS Tracker",
"description": "Track the location of the ISS",
"examples": [
"Where is the ISS",
"Who is on board of the space station?",
"When is the ISS passing over",
"Tell me about the IS",
"how many persons on board of the space station"
],
"tags": [
"Trivia",
"space"
]
}
7 changes: 3 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
matplotlib
basemap
pillow
requests
skyfield
ovos_workshop>=0.0.12,<2.0.0
ovos-date-parser>=0.0.3,<1.0.0
ovos-workshop>=0.0.12,<3.0.0
ovos-bus-client>=1.0.1
Loading

0 comments on commit 6469469

Please sign in to comment.