From 1773c16eda82d7ea7ada532697bf6322993d390a Mon Sep 17 00:00:00 2001 From: Michael Ye Date: Thu, 2 Dec 2021 19:52:25 -0500 Subject: [PATCH 1/3] move out opnav-fm --- db/db.py | 79 ++++++++++++++++++++++++ db/models.py | 40 ++++++++++++ flight_modes/flight_mode.py | 62 ++++--------------- flight_modes/flight_mode_factory.py | 17 +++-- flight_modes/opnav_flightmode.py | 96 +++++++++++++++-------------- utils/globals.py | 3 + 6 files changed, 190 insertions(+), 107 deletions(-) create mode 100644 db/db.py create mode 100644 db/models.py create mode 100644 utils/globals.py diff --git a/db/db.py b/db/db.py new file mode 100644 index 00000000..539a826d --- /dev/null +++ b/db/db.py @@ -0,0 +1,79 @@ +import logging +import random +import time +from datetime import datetime + +import db.models as models +import sqlalchemy as sa + +# factory for creating sessions +Session = sa.orm.sessionmaker(models.engine) + +# TODO look into Session.future or scoped session? + + +def add_random_flight_data_rows(n=1): + with Session.begin() as session: + logging.info("Add new Flight Data row") + time.sleep(0.5) + new_rows = [ + models.FlightData(time=datetime.now(), number=random.randint(1, 101)) + for _ in range(n) + ] + session.add_all(new_rows) + # session.commit() and session.close() + + +def add_opnav_row(time, number): + with Session.begin() as session: + row = models.OpNavData(time=time, number=number) + session.add(row) + + +def query_all_flight_data(): + with Session() as session: + logging.info("Show all Flight Data rows") + result = session.query(models.FlightData).all() + + for row in result: + print(f"name: {row.name}, number: {row.number}, complete: {row.complete}") + # session.close() + + +def query_all_opnav_data(): + with Session() as session: + logging.info("Show all OpNav Data rows") + result = session.query(models.OpNavData).all() + + for row in result: + print(f"name: {row.name}, number: {row.number}") + + +def query_latest_opnav_data(): + with Session() as session: + logging.info("Get latest op-nav row") + result = session.query( + models.OpNavData, sa.func.max(models.OpNavData.time) + ).one() + if result[1] is not None: + print(f"min: {result[0].time}, {result[0].number}") + else: + print("min: None") + return result[0] if result[0] is not None else None + # session.close() + + +# TODO op nave gets last row of flight data + + +def query_earliest_flight_data(): + with Session() as session: + logging.info("Show min Flight Data row") + time.sleep(0.5) + # TODO figure out max + result = ( + session.query(sa.func.min(models.FlightData.number)) + .filter(models.FlightData.complete.is_(False)) + .one() + ) + return result[0] if result is not None else None diff --git a/db/models.py b/db/models.py new file mode 100644 index 00000000..7acc1dce --- /dev/null +++ b/db/models.py @@ -0,0 +1,40 @@ +import logging + +import sqlalchemy as sa +from sqlalchemy.ext.declarative import declarative_base +from utils.constants import CONST + +engine = sa.create_engine(CONST.DB_FILE, echo=False) # set to True for logging +Base = declarative_base() + + +class FlightData(Base): + __tablename__ = "flight_data" + id = sa.Column(sa.Integer, primary_key=True) + + time = sa.Column(sa.DateTime, nullable=False) + number = sa.Column(sa.Integer, nullable=False) + complete = sa.Column(sa.Boolean, nullable=False) + + def __init__(self, *, time, number): + logging.info(f"Add new flight row: {time}, {number}") + self.time = time + self.number = number + self.complete = False + + +class OpNavData(Base): + __tablename__ = "op_nav_data" + id = sa.Column(sa.Integer, primary_key=True) + + time = sa.Column(sa.DateTime, nullable=False) + number = sa.Column(sa.Integer, nullable=False) + + def __init__(self, *, time, number): + logging.info(f"Add new op-nav row: {time}, {number}") + self.time = time + self.number = number + + +# creates all defined table objects into metadata +Base.metadata.create_all(engine) diff --git a/flight_modes/flight_mode.py b/flight_modes/flight_mode.py index b5bf5623..a9fc5ba8 100644 --- a/flight_modes/flight_mode.py +++ b/flight_modes/flight_mode.py @@ -10,18 +10,19 @@ # It lets your IDE know what type(self._parent) is, without causing any circular imports at runtime. import gc -from time import sleep, time -from multiprocessing import Process -import subprocess +import logging from queue import Empty +from time import sleep, time from traceback import format_tb -import logging import utils.constants as consts import utils.parameters as params - from utils.exceptions import UnknownFlightModeException +# import subprocess +# from multiprocessing import Process + + no_transition_modes = [ consts.FMEnum.SensorMode.value, consts.FMEnum.TestMode.value, @@ -185,8 +186,8 @@ def __exit__(self, exc_type, exc_value, tb): class PauseBackgroundMode(FlightMode): """Model for FlightModes that require precise timing - Pause garbage collection and anything else that could - interrupt critical thread""" + Pause garbage collection and anything else that could + interrupt critical thread""" def run_mode(self): super().run_mode() @@ -300,49 +301,6 @@ def run_mode(self): self.completed_task() -class OpNavMode(FlightMode): - """FMID 5: Optical Navigation Flight Mode - This flight mode is dedicated to starting the Opnav process""" - - # TODO: Flight Software/Opnav interface - flight_mode_id = consts.FMEnum.OpNav.value - - def __init__(self, parent): - super().__init__(parent) - - def run_mode(self): - # TODO: overhaul so opnav calculation only occur while in opnav mode - if not self._parent.opnav_process.is_alive(): - logging.info("[OPNAV]: Able to run next opnav") - self._parent.last_opnav_run = time() - logging.info("[OPNAV]: Starting opnav subprocess") - self._parent.opnav_process = Process( - target=self.opnav_subprocess, args=(self._parent.opnav_proc_queue,) - ) - self._parent.opnav_process.start() - self.completed_task() - - def update_state(self) -> int: - super_fm = super().update_state() - if super_fm != consts.NO_FM_CHANGE: - return super_fm - - # check if opnav db has been updated, then set self.task_completed true - - return consts.NO_FM_CHANGE - - def opnav_subprocess(self, q): - # TODO put in try...except - # TODO change from pytest to actual opnav - # os.system("pytest OpticalNavigation/tests/test_pipeline.py::test_start") - # subprocess.run('pytest OpticalNavigation/tests/test_pipeline.py::test_start', shell=True) - subprocess.run( - "echo [OPNAV]: Subprocess Start; sleep 1m; echo [OPNAV]: Subprocess end", - shell=True, - ) - q.put("Opnav Finished") - - class SensorMode(FlightMode): """FMID 7: Sensor Mode This flight mode is not really well defined and has kinda been forgotten about. One potential idea for it is that @@ -398,7 +356,9 @@ def update_state(self) -> int: time_for_telem: bool = ( time() - self._parent.radio.last_transmit_time ) // 60 < params.TELEM_INTERVAL - need_to_electrolyze: bool = self._parent.telemetry.prs.pressure < params.IDEAL_CRACKING_PRESSURE + need_to_electrolyze: bool = ( + self._parent.telemetry.prs.pressure < params.IDEAL_CRACKING_PRESSURE + ) currently_electrolyzing = self._parent.telemetry.gom.is_electrolyzing # if we don't want to electrolyze (per GS command), set need_to_electrolyze to false diff --git a/flight_modes/flight_mode_factory.py b/flight_modes/flight_mode_factory.py index 5dc13a83..ee96f242 100644 --- a/flight_modes/flight_mode_factory.py +++ b/flight_modes/flight_mode_factory.py @@ -1,20 +1,19 @@ +from utils.constants import FMEnum +from utils.exceptions import UnknownFlightModeException + +from flight_modes.attitude_adjustment import AAMode from flight_modes.flight_mode import ( + CommandMode, + CommsMode, NormalMode, SafeMode, SensorMode, TestMode, - CommsMode, - OpNavMode, - CommandMode, ) - from flight_modes.low_battery import LowBatterySafetyMode - -from flight_modes.restart_reboot import RestartMode, BootUpMode from flight_modes.maneuver_flightmode import ManeuverMode -from flight_modes.attitude_adjustment import AAMode -from utils.constants import FMEnum -from utils.exceptions import UnknownFlightModeException +from flight_modes.opnav_flightmode import OpNavMode +from flight_modes.restart_reboot import BootUpMode, RestartMode FLIGHT_MODE_DICT = { FMEnum.Boot.value: BootUpMode, diff --git a/flight_modes/opnav_flightmode.py b/flight_modes/opnav_flightmode.py index 822bc676..0fa00750 100644 --- a/flight_modes/opnav_flightmode.py +++ b/flight_modes/opnav_flightmode.py @@ -1,70 +1,59 @@ +import logging + +# import sqlite3 from datetime import datetime -from time import sleep -import sqlite3 -from sqlalchemy.exc import SQLAlchemyError from multiprocessing import Process -from utils.constants import FMEnum -from utils.db import OpNavCoordinatesModel -import logging +import utils.constants as consts + from .flight_mode import PauseBackgroundMode +# from sqlalchemy.exc import SQLAlchemyError + + +# from time import sleep + + +# from utils.db import OpNavCoordinatesModel + class OpNavMode(PauseBackgroundMode): + """FMID 5: Optical Navigation Flight Mode + This flight mode is dedicated to starting the Opnav process""" - flight_mode_id = FMEnum.OpNav.value + flight_mode_id = consts.FMEnum.OpNav.value def __init__(self, parent): super().__init__(parent) - self.dummy_opnav_result = ((0.0, 0.0, 0.0), (0.0, 0.0, 0.0), (0.0, 0.0, 0.0)) - - def set_return_val(self, returnval): - self.dummy_opnav_result = returnval - - def acquire_images(self): - return datetime.now() - - def get_position_velocity_attitude(self): - return self.dummy_opnav_result - - def run_opnav(self): - sleep(5.0) - position_acquired_at = self.acquire_images() - self.most_recent_result = self.get_position_velocity_attitude() - opnav_model = OpNavCoordinatesModel.from_tuples( - self.most_recent_result, position_acquired_at - ) - try: - session = self._parent.create_session() - session.add(opnav_model) - session.commit() - finally: - session.close() def read_most_recent_result(self): if self.most_recent_result is not None: return self.most_recent_result else: - try: - session = self._parent.create_session() - last_measurement = ( - session.query(OpNavCoordinatesModel) - .order_by(OpNavCoordinatesModel.measurement_taken) - .first() - ) - if last_measurement is None: - raise Exception("No record of previous OpNav runs") - return self.dummy_opnav_result - except (sqlite3.Error, SQLAlchemyError): - session.close() - raise Exception("Error while reading most recent OpNav result") + pass + # try: + # session = self._parent.create_session() + # last_measurement = ( + # session.query(OpNavCoordinatesModel) + # .order_by(OpNavCoordinatesModel.measurement_taken) + # .first() + # ) + # if last_measurement is None: + # raise Exception("No record of previous OpNav runs") + # return self.dummy_opnav_result + # except (sqlite3.Error, SQLAlchemyError): + # session.close() + # raise Exception("Error while reading most recent OpNav result") def run_mode(self): + # TODO: overhaul so opnav calculation only occur while in opnav mode if not self.opnav_process.is_alive(): logging.info("[OPNAV]: Able to run next opnav") self._parent.last_opnav_run = datetime.now() logging.info("[OPNAV]: Starting opnav subprocess") - self.opnav_process = Process(target=self.opnav_subprocess, args=()) + self.opnav_process = Process( + target=self.opnav_subprocess, args=(self._parent.opnav_proc_queue,) + ) self.opnav_process.start() self.run_opnav() self.task_completed() @@ -72,9 +61,22 @@ def run_mode(self): def update_state(self) -> int: super_fm = super().update_state() - if super_fm != 0: + if super_fm != consts.NO_FM_CHANGE: return super_fm + # check if opnav db has been updated, then set self.task_completed true # return to normal mode if task completed if self.task_completed: - return FMEnum.Normal.value + return consts.NO_FM_CHANGE + + def opnav_subprocess(self, q): + # TODO put in try...except + # TODO change from pytest to actual opnav + # os.system("pytest OpticalNavigation/tests/test_pipeline.py::test_start") + # subprocess.run('pytest OpticalNavigation/tests/test_pipeline.py::test_start', shell=True) + pass + # subprocess.run( + # "echo [OPNAV]: Subprocess Start; sleep 1m; echo [OPNAV]: Subprocess end", + # shell=True, + # ) + # q.put("Opnav Finished") diff --git a/utils/globals.py b/utils/globals.py new file mode 100644 index 00000000..b45b963b --- /dev/null +++ b/utils/globals.py @@ -0,0 +1,3 @@ +import threading + +global_lock = threading.Lock() From a993c3c12d913346ac5d9073e31fb76fa979f3d7 Mon Sep 17 00:00:00 2001 From: Michael Ye Date: Thu, 2 Dec 2021 20:11:43 -0500 Subject: [PATCH 2/3] move temp project into branch --- .flake8 | 4 - .pre-commit-config.yaml | 31 ++-- _todo_project_name/.editorconfig | 21 +++ _todo_project_name/.env.example | 2 + _todo_project_name/.github/ISSUE_TEMPLATE.md | 15 ++ _todo_project_name/.github/workflows/ci.yaml | 46 +++++ _todo_project_name/.gitignore | 106 +++++++++++ _todo_project_name/.pre-commit-config.yaml | 17 ++ _todo_project_name/AUTHORS.rst | 13 ++ _todo_project_name/CONTRIBUTING.rst | 126 +++++++++++++ _todo_project_name/HISTORY.rst | 8 + _todo_project_name/LICENSE | 21 +++ _todo_project_name/MANIFEST.in | 11 ++ _todo_project_name/Makefile | 85 +++++++++ _todo_project_name/README.rst | 33 ++++ _todo_project_name/data/db.sqlite | Bin 0 -> 20480 bytes _todo_project_name/docs/Makefile | 20 +++ _todo_project_name/docs/authors.rst | 1 + _todo_project_name/docs/conf.py | 162 +++++++++++++++++ _todo_project_name/docs/contributing.rst | 1 + _todo_project_name/docs/history.rst | 1 + _todo_project_name/docs/index.rst | 20 +++ _todo_project_name/docs/installation.rst | 51 ++++++ _todo_project_name/docs/make.bat | 36 ++++ _todo_project_name/docs/modules.rst | 7 + _todo_project_name/docs/readme.rst | 1 + .../docs/todo_project_name.db.rst | 38 ++++ .../docs/todo_project_name.flight_sw.rst | 22 +++ .../docs/todo_project_name.op_nav.rst | 22 +++ _todo_project_name/docs/todo_project_name.rst | 33 ++++ .../docs/todo_project_name.threads.rst | 38 ++++ .../docs/todo_project_name.todo_basic.rst | 22 +++ .../docs/todo_project_name.utils.rst | 38 ++++ _todo_project_name/docs/usage.rst | 7 + _todo_project_name/setup.cfg | 22 +++ _todo_project_name/setup.py | 53 ++++++ _todo_project_name/tests/__init__.py | 1 + .../tests/test_todo_project_name.py | 26 +++ .../todo_project_name/__init__.py | 5 + .../todo_project_name/db/__init__.py | 0 _todo_project_name/todo_project_name/db/db.py | 168 ++++++++++++++++++ .../todo_project_name/db/models.py | 84 +++++++++ .../todo_project_name/flight_sw/__init__.py | 0 .../flight_sw/main_satellite.py | 110 ++++++++++++ _todo_project_name/todo_project_name/main.py | 86 +++++++++ .../todo_project_name/op_nav/__init__.py | 0 .../todo_project_name/op_nav/op_nav.py | 80 +++++++++ .../todo_project_name/utils/__init__.py | 0 .../todo_project_name/utils/constants.py | 71 ++++++++ .../todo_project_name/utils/globals.py | 19 ++ .../todo_project_name/utils/log.py | 7 + db/db.py | 79 -------- db/models.py | 40 ----- setup.cfg | 5 + 54 files changed, 1777 insertions(+), 138 deletions(-) delete mode 100644 .flake8 create mode 100644 _todo_project_name/.editorconfig create mode 100644 _todo_project_name/.env.example create mode 100644 _todo_project_name/.github/ISSUE_TEMPLATE.md create mode 100644 _todo_project_name/.github/workflows/ci.yaml create mode 100644 _todo_project_name/.gitignore create mode 100644 _todo_project_name/.pre-commit-config.yaml create mode 100644 _todo_project_name/AUTHORS.rst create mode 100644 _todo_project_name/CONTRIBUTING.rst create mode 100644 _todo_project_name/HISTORY.rst create mode 100644 _todo_project_name/LICENSE create mode 100644 _todo_project_name/MANIFEST.in create mode 100644 _todo_project_name/Makefile create mode 100644 _todo_project_name/README.rst create mode 100644 _todo_project_name/data/db.sqlite create mode 100644 _todo_project_name/docs/Makefile create mode 100644 _todo_project_name/docs/authors.rst create mode 100755 _todo_project_name/docs/conf.py create mode 100644 _todo_project_name/docs/contributing.rst create mode 100644 _todo_project_name/docs/history.rst create mode 100644 _todo_project_name/docs/index.rst create mode 100644 _todo_project_name/docs/installation.rst create mode 100644 _todo_project_name/docs/make.bat create mode 100644 _todo_project_name/docs/modules.rst create mode 100644 _todo_project_name/docs/readme.rst create mode 100644 _todo_project_name/docs/todo_project_name.db.rst create mode 100644 _todo_project_name/docs/todo_project_name.flight_sw.rst create mode 100644 _todo_project_name/docs/todo_project_name.op_nav.rst create mode 100644 _todo_project_name/docs/todo_project_name.rst create mode 100644 _todo_project_name/docs/todo_project_name.threads.rst create mode 100644 _todo_project_name/docs/todo_project_name.todo_basic.rst create mode 100644 _todo_project_name/docs/todo_project_name.utils.rst create mode 100644 _todo_project_name/docs/usage.rst create mode 100644 _todo_project_name/setup.cfg create mode 100644 _todo_project_name/setup.py create mode 100644 _todo_project_name/tests/__init__.py create mode 100644 _todo_project_name/tests/test_todo_project_name.py create mode 100644 _todo_project_name/todo_project_name/__init__.py create mode 100644 _todo_project_name/todo_project_name/db/__init__.py create mode 100644 _todo_project_name/todo_project_name/db/db.py create mode 100644 _todo_project_name/todo_project_name/db/models.py create mode 100644 _todo_project_name/todo_project_name/flight_sw/__init__.py create mode 100644 _todo_project_name/todo_project_name/flight_sw/main_satellite.py create mode 100644 _todo_project_name/todo_project_name/main.py create mode 100644 _todo_project_name/todo_project_name/op_nav/__init__.py create mode 100644 _todo_project_name/todo_project_name/op_nav/op_nav.py create mode 100644 _todo_project_name/todo_project_name/utils/__init__.py create mode 100644 _todo_project_name/todo_project_name/utils/constants.py create mode 100644 _todo_project_name/todo_project_name/utils/globals.py create mode 100644 _todo_project_name/todo_project_name/utils/log.py delete mode 100644 db/db.py delete mode 100644 db/models.py diff --git a/.flake8 b/.flake8 deleted file mode 100644 index 7738b3aa..00000000 --- a/.flake8 +++ /dev/null @@ -1,4 +0,0 @@ -[flake8] -max_line_length=119 -extend-ignore=F403,F405,E203,E741,F811 -exclude = docs diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6937dc6e..f5474f27 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,16 +1,17 @@ repos: -- repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.0.1 - hooks: - - id: trailing-whitespace - - id: end-of-file-fixer - - id: check-yaml - - id: check-added-large-files -- repo: https://github.com/pycqa/flake8 - rev: 4.0.1 - hooks: - - id: flake8 -- repo: https://github.com/psf/black - rev: 19.3b0 - hooks: - - id: black + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.0.1 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files + - repo: https://github.com/pycqa/flake8 + rev: 4.0.1 + hooks: + - id: flake8 + args: [--config, setup.cfg] + - repo: https://github.com/psf/black + rev: 19.3b0 + hooks: + - id: black diff --git a/_todo_project_name/.editorconfig b/_todo_project_name/.editorconfig new file mode 100644 index 00000000..d4a2c440 --- /dev/null +++ b/_todo_project_name/.editorconfig @@ -0,0 +1,21 @@ +# http://editorconfig.org + +root = true + +[*] +indent_style = space +indent_size = 4 +trim_trailing_whitespace = true +insert_final_newline = true +charset = utf-8 +end_of_line = lf + +[*.bat] +indent_style = tab +end_of_line = crlf + +[LICENSE] +insert_final_newline = false + +[Makefile] +indent_style = tab diff --git a/_todo_project_name/.env.example b/_todo_project_name/.env.example new file mode 100644 index 00000000..8399d229 --- /dev/null +++ b/_todo_project_name/.env.example @@ -0,0 +1,2 @@ +DATA_PATH=data +DATA_RESET=1 diff --git a/_todo_project_name/.github/ISSUE_TEMPLATE.md b/_todo_project_name/.github/ISSUE_TEMPLATE.md new file mode 100644 index 00000000..d70d513a --- /dev/null +++ b/_todo_project_name/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,15 @@ +* Todo Project Name version: +* Python version: +* Operating System: + +### Description + +Describe what you were trying to get done. +Tell us what happened, what went wrong, and what you expected to happen. + +### What I Did + +``` +Paste the command(s) you ran and the output. +If there was a crash, please include the traceback here. +``` diff --git a/_todo_project_name/.github/workflows/ci.yaml b/_todo_project_name/.github/workflows/ci.yaml new file mode 100644 index 00000000..ef99d834 --- /dev/null +++ b/_todo_project_name/.github/workflows/ci.yaml @@ -0,0 +1,46 @@ +# This workflow will install Python dependencies, run tests and lint with a single version of Python +# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions + +name: Python Github Actions + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Set up Python 3.7.5 + uses: actions/setup-python@v2 + with: + python-version: 3.7.5 + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -e ."[dev]" + - name: Lint + run: | + make lint + - name: Test + run: | + make test + - name: Coverage + run: | + make coverage + - name: Documentation + run: | + make docs + git clone https://github.com/mly32/todo_project_name.git --branch gh-pages --single-branch gh-pages + cp -r docs/_build/html/* gh-pages/ + cd gh-pages + git config --local user.email "action@github.com" + git config --local user.name "GitHub Action" + git add . + git commit -m "update documentation" -a || true + - name: Push changes + uses: ad-m/github-push-action@master + with: + branch: gh-pages + directory: gh-pages + github_token: ${{ secrets.GITHUB_TOKEN }} diff --git a/_todo_project_name/.gitignore b/_todo_project_name/.gitignore new file mode 100644 index 00000000..4c915d14 --- /dev/null +++ b/_todo_project_name/.gitignore @@ -0,0 +1,106 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# dotenv +.env + +# virtualenv +.venv +venv/ +ENV/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ + +# IDE settings +.vscode/ +.idea/ diff --git a/_todo_project_name/.pre-commit-config.yaml b/_todo_project_name/.pre-commit-config.yaml new file mode 100644 index 00000000..f5474f27 --- /dev/null +++ b/_todo_project_name/.pre-commit-config.yaml @@ -0,0 +1,17 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.0.1 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files + - repo: https://github.com/pycqa/flake8 + rev: 4.0.1 + hooks: + - id: flake8 + args: [--config, setup.cfg] + - repo: https://github.com/psf/black + rev: 19.3b0 + hooks: + - id: black diff --git a/_todo_project_name/AUTHORS.rst b/_todo_project_name/AUTHORS.rst new file mode 100644 index 00000000..f9286d00 --- /dev/null +++ b/_todo_project_name/AUTHORS.rst @@ -0,0 +1,13 @@ +======= +Credits +======= + +Development Lead +---------------- + +* Todo Full Name + +Contributors +------------ + +None yet. Why not be the first? diff --git a/_todo_project_name/CONTRIBUTING.rst b/_todo_project_name/CONTRIBUTING.rst new file mode 100644 index 00000000..36e33d23 --- /dev/null +++ b/_todo_project_name/CONTRIBUTING.rst @@ -0,0 +1,126 @@ +.. highlight:: shell + +============ +Contributing +============ + +Contributions are welcome, and they are greatly appreciated! Every little bit +helps, and credit will always be given. + +You can contribute in many ways: + +Types of Contributions +---------------------- + +Report Bugs +~~~~~~~~~~~ + +Report bugs at https://github.com/todo_github_username/todo_project_name/issues. + +If you are reporting a bug, please include: + +* Your operating system name and version. +* Any details about your local setup that might be helpful in troubleshooting. +* Detailed steps to reproduce the bug. + +Fix Bugs +~~~~~~~~ + +Look through the GitHub issues for bugs. Anything tagged with "bug" and "help +wanted" is open to whoever wants to implement it. + +Implement Features +~~~~~~~~~~~~~~~~~~ + +Look through the GitHub issues for features. Anything tagged with "enhancement" +and "help wanted" is open to whoever wants to implement it. + +Write Documentation +~~~~~~~~~~~~~~~~~~~ + +Todo Project Name could always use more documentation, whether as part of the +official Todo Project Name docs, in docstrings, or even on the web in blog posts, +articles, and such. + +Submit Feedback +~~~~~~~~~~~~~~~ + +The best way to send feedback is to file an issue at https://github.com/todo_github_username/todo_project_name/issues. + +If you are proposing a feature: + +* Explain in detail how it would work. +* Keep the scope as narrow as possible, to make it easier to implement. +* Remember that this is a volunteer-driven project, and that contributions + are welcome :) + +Get Started! +------------ + +Ready to contribute? Here's how to set up `todo_project_name` for local development. + +1. Fork the `todo_project_name` repo on GitHub. +2. Clone your fork locally:: + + $ git clone git@github.com:your_name_here/todo_project_name.git + +3. Install your local copy into a virtualenv:: + + $ python -m venv venv + $ source ./venv/bin/activate + $ pip install -e ."[dev]" + $ pre-commit install + +4. Create a branch for local development:: + + $ git checkout -b name-of-your-bugfix-or-feature + + Now you can make your changes locally. + +5. When you're done making changes, check that your changes pass flake8 and the + tests:: + + $ flake8 todo_project_name tests + $ python setup.py test or pytest + +6. Commit your changes and push your branch to GitHub:: + + $ git add . + $ git commit -m "Your detailed description of your changes." + $ git push origin name-of-your-bugfix-or-feature + +7. Submit a pull request through the GitHub website. + +Pull Request Guidelines +----------------------- + +Before you submit a pull request, check that it meets these guidelines: + +1. The pull request should include tests. +2. If the pull request adds functionality, the docs should be updated. Put + your new functionality into a function with a docstring, and add the + feature to the list in README.rst. +3. The pull request should work for Python 3.5, 3.6, 3.7 and 3.8, and for PyPy. Check + https://travis-ci.com/todo_github_username/todo_project_name/pull_requests + and make sure that the tests pass for all supported Python versions. + +Tips +---- + +To run a subset of tests:: + +$ pytest tests.test_todo_project_name + + +Deploying +--------- + +A reminder for the maintainers on how to deploy. +Make sure all your changes are committed (including an entry in HISTORY.rst). +Then run:: + +$ bump2version patch # possible: major / minor / patch +$ git push +$ git push --tags + +Travis will then deploy to PyPI if tests pass. diff --git a/_todo_project_name/HISTORY.rst b/_todo_project_name/HISTORY.rst new file mode 100644 index 00000000..1cd2df59 --- /dev/null +++ b/_todo_project_name/HISTORY.rst @@ -0,0 +1,8 @@ +======= +History +======= + +0.1.0 (2021-10-21) +------------------ + +* First release on PyPI. diff --git a/_todo_project_name/LICENSE b/_todo_project_name/LICENSE new file mode 100644 index 00000000..2ca9717c --- /dev/null +++ b/_todo_project_name/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021, Todo Full Name + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/_todo_project_name/MANIFEST.in b/_todo_project_name/MANIFEST.in new file mode 100644 index 00000000..965b2dda --- /dev/null +++ b/_todo_project_name/MANIFEST.in @@ -0,0 +1,11 @@ +include AUTHORS.rst +include CONTRIBUTING.rst +include HISTORY.rst +include LICENSE +include README.rst + +recursive-include tests * +recursive-exclude * __pycache__ +recursive-exclude * *.py[co] + +recursive-include docs *.rst conf.py Makefile make.bat *.jpg *.png *.gif diff --git a/_todo_project_name/Makefile b/_todo_project_name/Makefile new file mode 100644 index 00000000..2b8dd9a3 --- /dev/null +++ b/_todo_project_name/Makefile @@ -0,0 +1,85 @@ +.PHONY: clean clean-test clean-pyc clean-build docs help +.DEFAULT_GOAL := help + +define BROWSER_PYSCRIPT +import os, webbrowser, sys + +from urllib.request import pathname2url + +webbrowser.open("file://" + pathname2url(os.path.abspath(sys.argv[1]))) +endef +export BROWSER_PYSCRIPT + +define PRINT_HELP_PYSCRIPT +import re, sys + +for line in sys.stdin: + match = re.match(r'^([a-zA-Z_-]+):.*?## (.*)$$', line) + if match: + target, help = match.groups() + print("%-20s %s" % (target, help)) +endef +export PRINT_HELP_PYSCRIPT + +BROWSER := python -c "$$BROWSER_PYSCRIPT" + +help: + @python -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST) + +clean: clean-build clean-pyc clean-test ## remove all build, test, coverage and Python artifacts + +clean-build: ## remove build artifacts + rm -fr build/ + rm -fr dist/ + rm -fr .eggs/ + find . -name '*.egg-info' -exec rm -fr {} + + find . -name '*.egg' -exec rm -f {} + + +clean-pyc: ## remove Python file artifacts + find . -name '*.pyc' -exec rm -f {} + + find . -name '*.pyo' -exec rm -f {} + + find . -name '*~' -exec rm -f {} + + find . -name '__pycache__' -exec rm -fr {} + + +clean-test: ## remove test and coverage artifacts + rm -f .coverage + rm -fr htmlcov/ + rm -fr .pytest_cache + +lint/flake8: ## check style with flake8 + flake8 todo_project_name tests +lint/black: ## check style with black + black --check todo_project_name tests + +lint: lint/flake8 lint/black ## check style + +test: ## run tests quickly with the default Python + pytest + +coverage: ## check code coverage quickly with the default Python + coverage run --source todo_project_name -m pytest + coverage report -m + coverage html + $(BROWSER) htmlcov/index.html + +docs: ## generate Sphinx HTML documentation, including API docs + rm -f docs/todo_project_name.rst + rm -f docs/modules.rst + sphinx-apidoc -o docs/ todo_project_name + $(MAKE) -C docs clean + $(MAKE) -C docs html + $(BROWSER) docs/_build/html/index.html + +servedocs: docs ## compile the docs watching for changes + watchmedo shell-command -p '*.rst' -c '$(MAKE) -C docs html' -R -D . + +release: dist ## package and upload a release + twine upload dist/* + +dist: clean ## builds source and wheel package + python setup.py sdist + python setup.py bdist_wheel + ls -l dist + +install: clean ## install the package to the active Python's site-packages + python setup.py install diff --git a/_todo_project_name/README.rst b/_todo_project_name/README.rst new file mode 100644 index 00000000..e3ec05a2 --- /dev/null +++ b/_todo_project_name/README.rst @@ -0,0 +1,33 @@ +================= +Todo Project Name +================= + +.. image:: https://img.shields.io/github/workflow/status/mly32/todo_project_name/Python%20Github%20Actions?label=CI + :target: https://github.com/mly32/todo_project_name/actions + :alt: GitHub Workflow Status + +.. image:: https://img.shields.io/github/checks-status/mly32/todo_project_name/gh-pages?label=Docs + :target: https://mly32.github.io/todo_project_name/ + :alt: GitHub branch checks state + +.. image:: https://img.shields.io/github/license/mly32/todo_project_name + :target: https://github.com/mly32/todo_project_name/blob/main/LICENSE + :alt: GitHub + +Documentation: https://mly32.github.io/todo_project_name/. + +Todo short description + + +Features +-------- + +* TODO + +Credits +------- + +This package was created with Cookiecutter_ and the `audreyr/cookiecutter-pypackage`_ project template. + +.. _Cookiecutter: https://github.com/audreyr/cookiecutter +.. _`audreyr/cookiecutter-pypackage`: https://github.com/audreyr/cookiecutter-pypackage diff --git a/_todo_project_name/data/db.sqlite b/_todo_project_name/data/db.sqlite new file mode 100644 index 0000000000000000000000000000000000000000..8cf72741392e6b1eae85bc93bd792f46c3060c76 GIT binary patch literal 20480 zcmeI&PjAyO7zS{=$y6<<3m2#+K=m|0jkHDiLr1a`VT(#d7g$JoxA&b z-$&ri2Y@RdgbQB<8&<6m5okT^Fdj=*-t={(C%>Gm`ps@T<9&8;JQ}2&x#TvXlssdM z5Tc2>Am((aqL6bs=(PCDRU>Beb44srE2l(%xAb-Cjwmo7009U<00Izz00bZa0SNpz z0%!Nta&={eP86Q*AN7wvc8BR{cQhVeRtsBk7$hM}g6Hj!UH;DM+G5sYttbhbVa%de z35z=I_8QX`c{b>?ZShdj+6i-=6P}7q-rFGFdJ)8R-^evz$E}?p-eWJrJyy?phGv{S zP|DTSRXU-w@p>swFLKCIEea?bb0r*(2mAd|;cFG-tCz}E!=RH}zw)J=chla7@d=lN z`8y?-GT)mk=#?qyEakyb_WqD}CDTk<%7vV1%$4v;lyYyb=%0xGL;of&7!ZH}1Rwwb z2tWV=5P$##AOHafT(>|?QAt1@%dyvOXWep`?Qb@`O~*B@4eyEVuGLD#3mnsGcpH{= zw?>N>Sf=m0PkpN)_y6Kge+&pf00Izz00bZa0SG_<0uX=z1a7bZ_Ww8dNUL 2>NUL +if errorlevel 9009 ( + echo. + echo.The Sphinx module was not found. Make sure you have Sphinx installed, + echo.then set the SPHINXBUILD environment variable to point to the full + echo.path of the 'sphinx-build' executable. Alternatively you may add the + echo.Sphinx directory to PATH. + echo. + echo.If you don't have Sphinx installed, grab it from + echo.http://sphinx-doc.org/ + exit /b 1 +) + +%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% +goto end + +:help +%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% + +:end +popd diff --git a/_todo_project_name/docs/modules.rst b/_todo_project_name/docs/modules.rst new file mode 100644 index 00000000..d3451b2f --- /dev/null +++ b/_todo_project_name/docs/modules.rst @@ -0,0 +1,7 @@ +todo_project_name +================= + +.. toctree:: + :maxdepth: 4 + + todo_project_name diff --git a/_todo_project_name/docs/readme.rst b/_todo_project_name/docs/readme.rst new file mode 100644 index 00000000..72a33558 --- /dev/null +++ b/_todo_project_name/docs/readme.rst @@ -0,0 +1 @@ +.. include:: ../README.rst diff --git a/_todo_project_name/docs/todo_project_name.db.rst b/_todo_project_name/docs/todo_project_name.db.rst new file mode 100644 index 00000000..f4089587 --- /dev/null +++ b/_todo_project_name/docs/todo_project_name.db.rst @@ -0,0 +1,38 @@ +todo\_project\_name.db package +============================== + +Submodules +---------- + +todo\_project\_name.db.db module +-------------------------------- + +.. automodule:: todo_project_name.db.db + :members: + :undoc-members: + :show-inheritance: + +todo\_project\_name.db.db\_normal module +---------------------------------------- + +.. automodule:: todo_project_name.db.db_normal + :members: + :undoc-members: + :show-inheritance: + +todo\_project\_name.db.models module +------------------------------------ + +.. automodule:: todo_project_name.db.models + :members: + :undoc-members: + :show-inheritance: + + +Module contents +--------------- + +.. automodule:: todo_project_name.db + :members: + :undoc-members: + :show-inheritance: diff --git a/_todo_project_name/docs/todo_project_name.flight_sw.rst b/_todo_project_name/docs/todo_project_name.flight_sw.rst new file mode 100644 index 00000000..b15d1e07 --- /dev/null +++ b/_todo_project_name/docs/todo_project_name.flight_sw.rst @@ -0,0 +1,22 @@ +todo\_project\_name.flight\_sw package +====================================== + +Submodules +---------- + +todo\_project\_name.flight\_sw.main\_satellite module +----------------------------------------------------- + +.. automodule:: todo_project_name.flight_sw.main_satellite + :members: + :undoc-members: + :show-inheritance: + + +Module contents +--------------- + +.. automodule:: todo_project_name.flight_sw + :members: + :undoc-members: + :show-inheritance: diff --git a/_todo_project_name/docs/todo_project_name.op_nav.rst b/_todo_project_name/docs/todo_project_name.op_nav.rst new file mode 100644 index 00000000..cadf01e2 --- /dev/null +++ b/_todo_project_name/docs/todo_project_name.op_nav.rst @@ -0,0 +1,22 @@ +todo\_project\_name.op\_nav package +=================================== + +Submodules +---------- + +todo\_project\_name.op\_nav.op\_nav module +------------------------------------------ + +.. automodule:: todo_project_name.op_nav.op_nav + :members: + :undoc-members: + :show-inheritance: + + +Module contents +--------------- + +.. automodule:: todo_project_name.op_nav + :members: + :undoc-members: + :show-inheritance: diff --git a/_todo_project_name/docs/todo_project_name.rst b/_todo_project_name/docs/todo_project_name.rst new file mode 100644 index 00000000..77f4d0b4 --- /dev/null +++ b/_todo_project_name/docs/todo_project_name.rst @@ -0,0 +1,33 @@ +todo\_project\_name package +=========================== + +Subpackages +----------- + +.. toctree:: + + todo_project_name.db + todo_project_name.flight_sw + todo_project_name.op_nav + todo_project_name.threads + todo_project_name.utils + +Submodules +---------- + +todo\_project\_name.main module +------------------------------- + +.. automodule:: todo_project_name.main + :members: + :undoc-members: + :show-inheritance: + + +Module contents +--------------- + +.. automodule:: todo_project_name + :members: + :undoc-members: + :show-inheritance: diff --git a/_todo_project_name/docs/todo_project_name.threads.rst b/_todo_project_name/docs/todo_project_name.threads.rst new file mode 100644 index 00000000..e760c1be --- /dev/null +++ b/_todo_project_name/docs/todo_project_name.threads.rst @@ -0,0 +1,38 @@ +todo\_project\_name.threads package +=================================== + +Submodules +---------- + +todo\_project\_name.threads.producer\_consumer\_with\_event module +------------------------------------------------------------------ + +.. automodule:: todo_project_name.threads.producer_consumer_with_event + :members: + :undoc-members: + :show-inheritance: + +todo\_project\_name.threads.producer\_consumer\_with\_no\_event module +---------------------------------------------------------------------- + +.. automodule:: todo_project_name.threads.producer_consumer_with_no_event + :members: + :undoc-members: + :show-inheritance: + +todo\_project\_name.threads.run module +-------------------------------------- + +.. automodule:: todo_project_name.threads.run + :members: + :undoc-members: + :show-inheritance: + + +Module contents +--------------- + +.. automodule:: todo_project_name.threads + :members: + :undoc-members: + :show-inheritance: diff --git a/_todo_project_name/docs/todo_project_name.todo_basic.rst b/_todo_project_name/docs/todo_project_name.todo_basic.rst new file mode 100644 index 00000000..824c27fa --- /dev/null +++ b/_todo_project_name/docs/todo_project_name.todo_basic.rst @@ -0,0 +1,22 @@ +todo\_project\_name.todo\_basic package +======================================= + +Submodules +---------- + +todo\_project\_name.todo\_basic.todo\_basic module +-------------------------------------------------- + +.. automodule:: todo_project_name.todo_basic.todo_basic + :members: + :undoc-members: + :show-inheritance: + + +Module contents +--------------- + +.. automodule:: todo_project_name.todo_basic + :members: + :undoc-members: + :show-inheritance: diff --git a/_todo_project_name/docs/todo_project_name.utils.rst b/_todo_project_name/docs/todo_project_name.utils.rst new file mode 100644 index 00000000..089df686 --- /dev/null +++ b/_todo_project_name/docs/todo_project_name.utils.rst @@ -0,0 +1,38 @@ +todo\_project\_name.utils package +================================= + +Submodules +---------- + +todo\_project\_name.utils.constants module +------------------------------------------ + +.. automodule:: todo_project_name.utils.constants + :members: + :undoc-members: + :show-inheritance: + +todo\_project\_name.utils.globals module +---------------------------------------- + +.. automodule:: todo_project_name.utils.globals + :members: + :undoc-members: + :show-inheritance: + +todo\_project\_name.utils.log module +------------------------------------ + +.. automodule:: todo_project_name.utils.log + :members: + :undoc-members: + :show-inheritance: + + +Module contents +--------------- + +.. automodule:: todo_project_name.utils + :members: + :undoc-members: + :show-inheritance: diff --git a/_todo_project_name/docs/usage.rst b/_todo_project_name/docs/usage.rst new file mode 100644 index 00000000..18f8532e --- /dev/null +++ b/_todo_project_name/docs/usage.rst @@ -0,0 +1,7 @@ +===== +Usage +===== + +To use Todo Project Name in a project:: + + import todo_project_name diff --git a/_todo_project_name/setup.cfg b/_todo_project_name/setup.cfg new file mode 100644 index 00000000..7a1b86a7 --- /dev/null +++ b/_todo_project_name/setup.cfg @@ -0,0 +1,22 @@ +[bumpversion] +current_version = 0.1.0 +commit = True +tag = True + +[bumpversion:file:setup.py] +search = version='{current_version}' +replace = version='{new_version}' + +[bumpversion:file:todo_project_name/__init__.py] +search = __version__ = '{current_version}' +replace = __version__ = '{new_version}' + +[bdist_wheel] +universal = 1 + +[flake8] +max_line_length=119 +exclude = docs + +[tool:pytest] +addopts = '--ignore="setup.py"' diff --git a/_todo_project_name/setup.py b/_todo_project_name/setup.py new file mode 100644 index 00000000..e4e2b293 --- /dev/null +++ b/_todo_project_name/setup.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python + +"""The setup script.""" + +from setuptools import find_packages, setup + +with open("README.rst") as readme_file: + readme = readme_file.read() + +with open("HISTORY.rst") as history_file: + history = history_file.read() + +includes = ["todo_project_name", "todo_project_name.*"] + +requirements = ["numpy", "python-dotenv", "sqlalchemy"] + +dev_req = ["black", "flake8", "pip", "pre-commit", "sphinx", "watchdog"] + +docs_req = ["sphinx", "sphinx-rtd-theme"] + +test_req = ["coverage", "pytest"] + +release_req = ["bump2version", "wheel", "twine"] + +extra_req = {"dev": dev_req + docs_req + test_req + release_req} + +setup( + author="Todo Full Name", + author_email="todo@email.com", + python_requires=">=3.6", + classifiers=[ + "Development Status :: 2 - Pre-Alpha", + "Intended Audience :: Developers", + "License :: OSI Approved :: MIT License", + "Natural Language :: English", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + ], + description="Todo short description", + extras_require=extra_req, + install_requires=requirements, + license="MIT license", + long_description=readme + "\n\n" + history, + include_package_data=True, + keywords="todo_project_name", + name="todo_project_name", + packages=find_packages(include=includes), + url="https://github.com/todo_github_username/todo_project_name", + version="0.1.0", + zip_safe=False, +) diff --git a/_todo_project_name/tests/__init__.py b/_todo_project_name/tests/__init__.py new file mode 100644 index 00000000..56623c58 --- /dev/null +++ b/_todo_project_name/tests/__init__.py @@ -0,0 +1 @@ +"""Unit test package for todo_project_name.""" diff --git a/_todo_project_name/tests/test_todo_project_name.py b/_todo_project_name/tests/test_todo_project_name.py new file mode 100644 index 00000000..0844209f --- /dev/null +++ b/_todo_project_name/tests/test_todo_project_name.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python + +"""Tests for `todo_project_name` package.""" + +import pytest + + +@pytest.fixture +def response(): + """Sample pytest fixture. + + See more at: http://doc.pytest.org/en/latest/fixture.html + """ + # import requests + # return requests.get('https://github.com/audreyr/cookiecutter-pypackage') + + +def test_content(response): + """Sample pytest test function with the pytest fixture as an argument.""" + # from bs4 import BeautifulSoup + # assert 'GitHub' in BeautifulSoup(response.content).title.string + + +def test_todo_add(): + """A basic test.""" + assert 1 + 1 == 2 diff --git a/_todo_project_name/todo_project_name/__init__.py b/_todo_project_name/todo_project_name/__init__.py new file mode 100644 index 00000000..4d2bab71 --- /dev/null +++ b/_todo_project_name/todo_project_name/__init__.py @@ -0,0 +1,5 @@ +"""Top-level package for Todo Project Name.""" + +__author__ = """Todo Full Name""" +__email__ = "todo@email.com" +__version__ = "0.1.0" diff --git a/_todo_project_name/todo_project_name/db/__init__.py b/_todo_project_name/todo_project_name/db/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/_todo_project_name/todo_project_name/db/db.py b/_todo_project_name/todo_project_name/db/db.py new file mode 100644 index 00000000..743342dc --- /dev/null +++ b/_todo_project_name/todo_project_name/db/db.py @@ -0,0 +1,168 @@ +import logging +import typing +from datetime import datetime + +import db.models as models +import sqlalchemy as sa + +# factory for creating sessions +Session = sa.orm.sessionmaker(models.engine) + +# TODO look into Session.future or scoped session? +# TODO func.min query does it actually return min row?? +# TODO use session.begin? expunge row after creation? or session.refresh? + + +def __add_data(model: models.Base, **kwargs) -> models.Base: + with Session() as session: + row = model(**kwargs) + session.add(row) + session.commit() + logging.info(f"Add {model.__tablename__} data: {row}") + # session.close() + return row + + +def add_flight_data(number: int) -> models.FlightData: + return __add_data(models.FlightData, time=datetime.now(), number=number) + + +def add_att_adjust_data(time: datetime) -> models.AttAdjustRun: + return __add_data(models.AttAdjustRun, time=time) + + +def add_op_nav_data(time: datetime, number: int) -> models.OpNavData: + return __add_data(models.OpNavData, time=time, number=number) + + +def add_op_nav_run(time: datetime) -> models.OpNavRun: + return __add_data(models.OpNavRun, time=time) + + +def __update_data(model: models.Base, id: int, **kwargs) -> None: + with Session.begin() as session: + row = session.query(model).filter(model.id.is_(id)).first() + if row is not None: + logging.info(f"Update from {model.__tablename__} data: {row}") + for (key, val) in kwargs.items(): + setattr(row, key, val) + logging.info(f"Update to {model.__tablename__} data: {row}") + # session.commit() and session.close() + + +def update_flight_data(id: int, number) -> None: + return __update_data(models.FlightData, id, number=number) + + +def update_att_adjust(id: int, state: models.RunState) -> None: + return __update_data(models.AttAdjustRun, id, state=state) + + +def update_op_nav_run(id: int, state: models.RunState) -> None: + with Session.begin() as session: + row = session.query(models.OpNavRun).filter(models.OpNavRun.id == id).first() + if row is not None: + row.state = state + logging.info(f"Set {row} to {state}") + # session.commit() and session.close() + + +def __update_failed_runs(model: models.Base) -> int: + """sets all missed runs to FAILED + + :param model: table to update failed runs + :type model: models.Base + :return: the number of missed runs + :rtype: int + """ + with Session.begin() as session: + result = ( + session.query(model) + .filter( + model.state.is_(models.RunState.QUEUED), model.time < datetime.now() + ) + .all() + ) + for row in result: + row.state = models.RunState.FAILED + logging.info(f"Failed {model.__tablename__}: {row}") + num_failed = len(result) + # session.commit() and session.close() + if num_failed > 0: + logging.info(f"Missed {num_failed} runs") + return num_failed + + +def update_failed_att_adjust_runs() -> int: + return __update_failed_runs(models.AttAdjustRun) + + +def update_failed_op_nav_runs() -> int: + return __update_failed_runs(models.OpNavRun) + + +def __query_all_data(model: models.Base) -> typing.List[models.Base]: + with Session() as session: + result = session.query(model).all() + logging.info(f"Get all {model.__tablename__} data: {len(result)} rows") + for r in result: + print(" ", r) + session.expunge_all() + # session.close() + return result + + +def query_all_flight_data() -> None: + return __query_all_data(models.FlightData) + + +def query_all_op_nav_data() -> None: + return __query_all_data(models.OpNavData) + + +def query_earliest_flight_data() -> typing.Union[models.FlightData, None]: + with Session() as session: + result = session.query( + models.FlightData, sa.func.min(models.FlightData.number) + ).first() + session.expunge_all() + logging.info(f"Get min flight data: {result[0]}") + return result[0] + # session.close() + + +def query_latest_op_nav_data() -> typing.Union[models.OpNavData, None]: + with Session() as session: + result = session.query( + models.OpNavData, sa.func.max(models.OpNavData.time) + ).first() + session.expunge_all() + # session.close() + logging.info(f"Get latest op-nav data: {result[0]}") + return result[0] + + +def query_earliest_att_adjust_run() -> typing.Union[models.AttAdjustRun, None]: + with Session() as session: + result = ( + session.query(models.AttAdjustRun, sa.func.min(models.AttAdjustRun.time)) + .filter(models.AttAdjustRun.state.is_(models.RunState.QUEUED)) + .first() + ) + session.expunge_all() + # session.close() + logging.info(f"Get min att-adjust run: {result[0]}") + return result[0] + + +def query_earliest_op_nav_run() -> typing.Union[models.OpNavRun, None]: + with Session() as session: + result = ( + session.query(models.OpNavRun, sa.func.min(models.OpNavRun.time)) + .filter(models.OpNavRun.state.is_(models.RunState.QUEUED)) + .first() + ) + session.expunge_all() + # session.close() + logging.info(f"Get min op-nav run: {result[0]}") + return result[0] diff --git a/_todo_project_name/todo_project_name/db/models.py b/_todo_project_name/todo_project_name/db/models.py new file mode 100644 index 00000000..a29ada78 --- /dev/null +++ b/_todo_project_name/todo_project_name/db/models.py @@ -0,0 +1,84 @@ +import enum + +import sqlalchemy as sa +from sqlalchemy.ext.declarative import declarative_base +from utils.constants import CONST + +engine = sa.create_engine(CONST.DB_FILE, echo=False) # set to True for logging +Base = declarative_base() + +# TODO docstrings + + +class RunState(enum.Enum): + QUEUED = enum.auto() + RUNNING = enum.auto() + FAILED = enum.auto() + SUCCEEDED = enum.auto() + + def __str__(self) -> str: + return f"RunState.{self.name}({self.value})" + + +class FlightData(Base): + __tablename__ = "flight_data" + id = sa.Column(sa.Integer, primary_key=True) + + time = sa.Column(sa.DateTime, nullable=False) + number = sa.Column(sa.Integer, nullable=False) + + def __init__(self, *, time, number) -> None: + self.time = time + self.number = number + + def __str__(self) -> str: + return f"FlightData({self.time}, {self.number})" + + +class AttAdjustRun(Base): + __tablename__ = "att_adjust_run" + id = sa.Column(sa.Integer, primary_key=True) + + time = sa.Column(sa.DateTime, nullable=False) + state = sa.Column(sa.Enum(RunState), nullable=False) # persists string + + def __init__(self, *, time) -> None: + self.time = time + self.state = RunState.QUEUED + + def __str__(self) -> str: + return f"AttAdjustRun({self.time}, {self.state})" + + +class OpNavData(Base): + __tablename__ = "op_nav_data" + id = sa.Column(sa.Integer, primary_key=True) + + time = sa.Column(sa.DateTime, nullable=False) + number = sa.Column(sa.Integer, nullable=False) + + def __init__(self, *, time, number) -> None: + self.time = time + self.number = number + + def __str__(self) -> str: + return f"OpNavData({self.time}, {self.number})" + + +class OpNavRun(Base): + __tablename__ = "op_nav_run" + id = sa.Column(sa.Integer, primary_key=True) + + time = sa.Column(sa.DateTime, nullable=False) + state = sa.Column(sa.Enum(RunState), nullable=False) + + def __init__(self, *, time) -> None: + self.time = time + self.state = RunState.QUEUED + + def __str__(self) -> str: + return f"OpNavRun({self.time}, {self.state})" + + +# creates all defined table objects into metadata +Base.metadata.create_all(engine) diff --git a/_todo_project_name/todo_project_name/flight_sw/__init__.py b/_todo_project_name/todo_project_name/flight_sw/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/_todo_project_name/todo_project_name/flight_sw/main_satellite.py b/_todo_project_name/todo_project_name/flight_sw/main_satellite.py new file mode 100644 index 00000000..14bc1f5b --- /dev/null +++ b/_todo_project_name/todo_project_name/flight_sw/main_satellite.py @@ -0,0 +1,110 @@ +import enum +import logging +import random +import threading +import time +from datetime import datetime + +import utils.globals as globals +from db import db, models +from utils.constants import CONST + +from todo_project_name.op_nav.op_nav import OpNav + +# import sched +# TODO database size +# TODO time comparisons add small epsilon in comparisions + + +class FlightMode(enum.Enum): + Normal = enum.auto() + AttAdjust = enum.auto() + OpNav = enum.auto() + + def __str__(self) -> str: + return f"FlightMode.{self.name}({self.value})" + + +class MainSatellite: + def __init__(self): + logging.info("Main satellite initialized") + self.mode: FlightMode = FlightMode.Normal + + def run(self): + # init params & sensors + while True: + # do not run if global_lock is locked + with globals.run_camera_cond: + while globals.want_to_run_camera: + logging.info("Can to run camera set to TRUE") + globals.can_run_camera = True + globals.run_camera_cond.notify_all() + globals.run_camera_cond.wait() + globals.can_run_camera = False + logging.info(f"Flight mode: {self.mode}") # Can to run camera set to FALSE. + + if self.mode == FlightMode.Normal: + db.add_flight_data(number=random.randint(1, 101)) + # db.query_latest_op_nav_data() + time.sleep(1) + # if random.randint(0, 2) % 2: + # db.add_att_adjust_data( + # datetime.now() + timedelta(seconds=random.randint(8, 17)) + # ) + elif self.mode == FlightMode.AttAdjust: + att_adjust_run = db.query_earliest_att_adjust_run() + if att_adjust_run is not None: + time_diff = (att_adjust_run.time - datetime.now()).total_seconds() + if time_diff > 0: + time.sleep(time_diff) + with globals.global_lock: + logging.info("Begin attitude adjustment") + time.sleep(2) + db.update_op_nav_run( + att_adjust_run.id, models.RunState.SUCCEEDED + ) + logging.info("Complete attitude adjustment") + elif self.mode == FlightMode.OpNav: + # db.add_future_op_nav_run() + # time.sleep(2) + db.update_failed_op_nav_runs() + op_nav_run = db.query_earliest_op_nav_run() + if op_nav_run is not None: + op_nav = OpNav() + time_diff = (op_nav_run.time - datetime.now()).total_seconds() + logging.info(f"Op-nav run scheduled in {time_diff}s") + t = threading.Timer(max(time_diff, 0), op_nav.run, (op_nav_run.id,)) + t.name = "op-nav" + t.start() + db.update_op_nav_run(op_nav_run.id, models.RunState.RUNNING) + # :( scheduler is not threaded + # sch = sched.scheduler(time.time, time.sleep) + # sch.enterabs( + # op_nav_run.time.timestamp(), 1, op_nav.run, (op_nav_run.id,) + # ) + # sch.run() + + self.transition() + + def transition(self) -> None: + db.update_failed_att_adjust_runs() + att_adjust_run = db.query_earliest_att_adjust_run() + if att_adjust_run is not None: + time_diff = (att_adjust_run.time - datetime.now()).total_seconds() + if time_diff >= CONST.ATT_ADJUST_RUN_BUFFER: + logging.info(f"Next adjustment too far away; time diff: {time_diff}") + else: + self.mode = FlightMode.AttAdjust + return + + db.update_failed_op_nav_runs() + op_nav_run = db.query_earliest_op_nav_run() + if op_nav_run is not None: + time_diff = (op_nav_run.time - datetime.now()).total_seconds() + if time_diff >= CONST.OP_NAV_RUN_BUFFER: + logging.info(f"Next adjustment too far away; time diff: {time_diff}") + else: + self.mode = FlightMode.OpNav + return + + self.mode = FlightMode.Normal diff --git a/_todo_project_name/todo_project_name/main.py b/_todo_project_name/todo_project_name/main.py new file mode 100644 index 00000000..8ad22e22 --- /dev/null +++ b/_todo_project_name/todo_project_name/main.py @@ -0,0 +1,86 @@ +import logging +import random +import threading +from datetime import datetime, timedelta + +from db import db +from utils import log + +from todo_project_name.flight_sw.main_satellite import MainSatellite + + +def test_db_functions(): + n = random.randint(1, 101) + + flight_row = db.add_flight_data(number=n) + flight_res = db.query_all_flight_data() + assert len(flight_res) == 1 + assert flight_row.id == flight_res[0].id + + db.update_flight_data(flight_row.id, number=2 * n) + flight_res2 = db.query_all_flight_data() + assert 2 * n == flight_res2[0].number + + +def test_db_att_adjust(): + now = datetime.now() + db.add_att_adjust_data(now + timedelta(seconds=-10)) + db.add_att_adjust_data(now + timedelta(seconds=-15)) + db.add_att_adjust_data(now + timedelta(seconds=15)) + res = db.update_failed_att_adjust_runs() + assert res == 2 + res2 = db.query_earliest_att_adjust_run() + assert now + timedelta(seconds=15) == res2.time + + +def main_test(): + main_satellite = MainSatellite() + + now = datetime.now() + db.add_op_nav_run(now + timedelta(seconds=3)) + db.add_op_nav_run(now + timedelta(seconds=15)) + # db.add_att_adjust_data(now + timedelta(seconds=5)) + + threads = [ + threading.Thread(name="main-sat", target=main_satellite.run, daemon=True) + ] + + for thread in threads: + thread.start() + + try: + for thread in threads: + thread.join() + except KeyboardInterrupt: + logging.info("Keyboard Interrupt received; exiting") + + +if __name__ == "__main__": + log.set_up_logging() + main_test() + # tests = [ + # ("General functions", test_db_functions), + # ("Att adjust functions", test_db_att_adjust), + # ] + + # for (test_name, test_func) in tests: + # print(f">>>{test_name}") + # test_func() + # print("---\n") + + # op_nav mode addds new event to queue after completion + + """ + main loops, updating some sensor data every 3 sec + needs ability to be the only thread running (lock) + op_nav needs access to data + needs ability to be the only thread running + requires knowledge of propulsion events + + each tries to acquire lock A. waits until lock B + + + goal for tomorrow: main writend checks for most recent op_nav result + op_nav read main data and writes + """ + # EXPECT: 3 added rows, 2 failed status diff --git a/_todo_project_name/todo_project_name/op_nav/__init__.py b/_todo_project_name/todo_project_name/op_nav/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/_todo_project_name/todo_project_name/op_nav/op_nav.py b/_todo_project_name/todo_project_name/op_nav/op_nav.py new file mode 100644 index 00000000..9de71f29 --- /dev/null +++ b/_todo_project_name/todo_project_name/op_nav/op_nav.py @@ -0,0 +1,80 @@ +import enum +import logging +import time +from datetime import datetime + +import utils.globals as globals +from db import db, models + + +class OpNavStatus(enum.Enum): + SUCCESS = enum.auto() + FAILURE = enum.auto() + + def __str__(self) -> str: + return f"OpNavStatus.{self.name}({self.value})" + + +delay = 2 + + +class OpNav: + def __init__(self): + logging.info("Op-nav initialized") + + def __observe(self) -> models.FlightData: + # photo timing should not be stopped + # with globals.global_lock: + with globals.run_camera_cond: + logging.info("Want to run camera set to TRUE") + globals.want_to_run_camera = True + globals.run_camera_cond.notify_all() + while not globals.can_run_camera: + globals.run_camera_cond.wait() + logging.info("Camera recording started") + time.sleep(5) + res = db.query_earliest_flight_data() + logging.info("Camera recording completed") + + logging.info("Want to run camera set to FALSE") + globals.want_to_run_camera = False + globals.run_camera_cond.notify_all() + return res + + def __process_data(self, flight_data: models.FlightData): + time.sleep(2) + db.add_op_nav_data(datetime.now(), 2 * flight_data.number) + + def run(self, run_id): + # while query attitude for time < now: sleep() + logging.info(f"ID: {run_id}") + logging.info(f"Op-nav run beginning at {datetime.now()}") + res = self.__observe() + self.__process_data(res) + logging.info(f"Op-nav run ending at {datetime.now()}") + db.update_op_nav_run(run_id, models.RunState.SUCCEEDED) + + # while True: # op_nav interval + # time.sleep(delay) + # # TODO check for future attitude adjustment + # while True: + # op_nav_run = db.query_earliest_op_nav_run() + # if op_nav_run is None: + # break + # time_diff = (op_nav_run.time - datetime.now()).total_seconds() + # if time_diff > 2 * delay: + # break + # if time_diff > 0: + # logging.info(f"Upcoming op-nav run. Waiting {time_diff}") + # time.sleep(time_diff) + # else: + # logging.info("Op-nav run time passed") + # db.update_op_nav_run(op_nav_run.id, True) + + # res = self.__observe() + # time.sleep(delay) + # logging.info(f"Add new op-nav row") + # if res is not None: + # db.add_op_nav_data(datetime.now(), res) + + return OpNavStatus.SUCCESS diff --git a/_todo_project_name/todo_project_name/utils/__init__.py b/_todo_project_name/todo_project_name/utils/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/_todo_project_name/todo_project_name/utils/constants.py b/_todo_project_name/todo_project_name/utils/constants.py new file mode 100644 index 00000000..3401ff21 --- /dev/null +++ b/_todo_project_name/todo_project_name/utils/constants.py @@ -0,0 +1,71 @@ +import os +import shutil +from pathlib import Path + +from dotenv import dotenv_values + + +class __Const(object): + def __init__(self) -> None: + """Read from dotenv file and setup constants""" + self.__config = dotenv_values() + self.__project_path = (Path(__file__).parent / "../..").resolve() + + @property + def DATA_PATH(self) -> str: + """Dotenv DATA_PATH + :return: sql database parent path + :rtype: str + """ + if os.path.isabs(self.__config["DATA_PATH"]): + return self.__config["DATA_PATH"] + return os.path.join(self.__project_path, self.__config["DATA_PATH"]) + + @property + def DATA_RESET(self) -> bool: + """Dotenv DATA_RESET + + :return: whether to delete sql database at start + :rtype: bool + """ + return self.__config["DATA_RESET"] == "1" + + @property + def DB_FILE(self) -> str: + """Based off of dotenv DATA_PATH + + :return: formated database path for sqlalchemy + :rtype: str + """ + return "sqlite:///" + os.path.join(self.DATA_PATH, "db.sqlite") + + @property + def ATT_ADJUST_RUN_BUFFER(self) -> float: + """Look ahead time to switch to a different state + + :return: difference in seconds + :rtype: float + """ + return 10.0 + + @property + def OP_NAV_RUN_BUFFER(self) -> float: + """Look ahead time to switch to a different state + + :return: difference in seconds + :rtype: float + """ + return 10.0 + + +CONST = __Const() + + +def __setup_files() -> None: + """Generate directories and files""" + if CONST.DATA_RESET and os.path.exists(CONST.DATA_PATH): + shutil.rmtree(CONST.DATA_PATH) + os.makedirs(CONST.DATA_PATH, exist_ok=True) + + +__setup_files() diff --git a/_todo_project_name/todo_project_name/utils/globals.py b/_todo_project_name/todo_project_name/utils/globals.py new file mode 100644 index 00000000..34777a05 --- /dev/null +++ b/_todo_project_name/todo_project_name/utils/globals.py @@ -0,0 +1,19 @@ +import threading + +global_lock = threading.Lock() +"""Lock for op-nav camera""" + +want_to_run_camera = False +"""Whether op-nav wants to run the camera""" + +can_run_camera = False +"""Whether the camera can be run exclusively""" + +run_camera_cond = threading.Condition() +"""Condition to run op-nav camera""" + +# can_run_adjust = False +# """Whether att-adjust can be run exclusively""" + +# can_run_adjust_cond = threading.Condition() +# """Condition to run main-sat att-adjust mode""" diff --git a/_todo_project_name/todo_project_name/utils/log.py b/_todo_project_name/todo_project_name/utils/log.py new file mode 100644 index 00000000..42ee358a --- /dev/null +++ b/_todo_project_name/todo_project_name/utils/log.py @@ -0,0 +1,7 @@ +import logging + + +def set_up_logging() -> None: + format = "(%(threadName)-10s) %(asctime)s: %(message)s" + logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S") + # logging.getLogger().setLevel(logging.DEBUG) diff --git a/db/db.py b/db/db.py deleted file mode 100644 index 539a826d..00000000 --- a/db/db.py +++ /dev/null @@ -1,79 +0,0 @@ -import logging -import random -import time -from datetime import datetime - -import db.models as models -import sqlalchemy as sa - -# factory for creating sessions -Session = sa.orm.sessionmaker(models.engine) - -# TODO look into Session.future or scoped session? - - -def add_random_flight_data_rows(n=1): - with Session.begin() as session: - logging.info("Add new Flight Data row") - time.sleep(0.5) - new_rows = [ - models.FlightData(time=datetime.now(), number=random.randint(1, 101)) - for _ in range(n) - ] - session.add_all(new_rows) - # session.commit() and session.close() - - -def add_opnav_row(time, number): - with Session.begin() as session: - row = models.OpNavData(time=time, number=number) - session.add(row) - - -def query_all_flight_data(): - with Session() as session: - logging.info("Show all Flight Data rows") - result = session.query(models.FlightData).all() - - for row in result: - print(f"name: {row.name}, number: {row.number}, complete: {row.complete}") - # session.close() - - -def query_all_opnav_data(): - with Session() as session: - logging.info("Show all OpNav Data rows") - result = session.query(models.OpNavData).all() - - for row in result: - print(f"name: {row.name}, number: {row.number}") - - -def query_latest_opnav_data(): - with Session() as session: - logging.info("Get latest op-nav row") - result = session.query( - models.OpNavData, sa.func.max(models.OpNavData.time) - ).one() - if result[1] is not None: - print(f"min: {result[0].time}, {result[0].number}") - else: - print("min: None") - return result[0] if result[0] is not None else None - # session.close() - - -# TODO op nave gets last row of flight data - - -def query_earliest_flight_data(): - with Session() as session: - logging.info("Show min Flight Data row") - time.sleep(0.5) - # TODO figure out max - result = ( - session.query(sa.func.min(models.FlightData.number)) - .filter(models.FlightData.complete.is_(False)) - .one() - ) - return result[0] if result is not None else None diff --git a/db/models.py b/db/models.py deleted file mode 100644 index 7acc1dce..00000000 --- a/db/models.py +++ /dev/null @@ -1,40 +0,0 @@ -import logging - -import sqlalchemy as sa -from sqlalchemy.ext.declarative import declarative_base -from utils.constants import CONST - -engine = sa.create_engine(CONST.DB_FILE, echo=False) # set to True for logging -Base = declarative_base() - - -class FlightData(Base): - __tablename__ = "flight_data" - id = sa.Column(sa.Integer, primary_key=True) - - time = sa.Column(sa.DateTime, nullable=False) - number = sa.Column(sa.Integer, nullable=False) - complete = sa.Column(sa.Boolean, nullable=False) - - def __init__(self, *, time, number): - logging.info(f"Add new flight row: {time}, {number}") - self.time = time - self.number = number - self.complete = False - - -class OpNavData(Base): - __tablename__ = "op_nav_data" - id = sa.Column(sa.Integer, primary_key=True) - - time = sa.Column(sa.DateTime, nullable=False) - number = sa.Column(sa.Integer, nullable=False) - - def __init__(self, *, time, number): - logging.info(f"Add new op-nav row: {time}, {number}") - self.time = time - self.number = number - - -# creates all defined table objects into metadata -Base.metadata.create_all(engine) diff --git a/setup.cfg b/setup.cfg index bce83951..d0af8c70 100644 --- a/setup.cfg +++ b/setup.cfg @@ -12,3 +12,8 @@ markers= zero_noise_test: marks tests with zero starting noise small_noise_test: marks tests with small starting noise large_noise_test: marks tests with large starting noise + +[flake8] +max_line_length=119 +extend-ignore=F403,F405,E203,E741,F811 +exclude = docs, _todo_project_name/docs/conf.py From 0dad4e59c279de1af60ac1940f0b4eeae3352daa Mon Sep 17 00:00:00 2001 From: Michael Ye Date: Sat, 4 Dec 2021 15:23:29 -0500 Subject: [PATCH 3/3] add att-adjust logic --- _todo_project_name/data/db.sqlite | Bin 20480 -> 20480 bytes _todo_project_name/docs/todo_project_name.rst | 1 - _todo_project_name/todo_project_name/db/db.py | 38 +++----- .../flight_sw/main_satellite.py | 86 +++++++++++------- _todo_project_name/todo_project_name/main.py | 48 ++-------- .../todo_project_name/op_nav/op_nav.py | 42 +-------- .../todo_project_name/utils/constants.py | 31 ++++++- pyrightconfig.json | 7 +- 8 files changed, 107 insertions(+), 146 deletions(-) diff --git a/_todo_project_name/data/db.sqlite b/_todo_project_name/data/db.sqlite index 8cf72741392e6b1eae85bc93bd792f46c3060c76..b9c6c890620d41c3c482e8639ff3d6d7faf56fda 100644 GIT binary patch delta 675 zcmZwEy-or_5Ww*}L_w5Yh=mYM<_Z#Hahsj}UMvhb&csMZw4w1KEKT4Mw4gMhu&`B# zrEg$OV&iLA3Th*4|NCbqGowj7n#6YpapV3liyP0^k2E1fMD&xs(Fb}<2eeI_9!s+R`)bozb=lg;ntj9OKS8E3Y6916%;%je=?v zMKPU6bUv*o@AUX+8Nk|urDoiCsZ{?m%ksRNcVCiXtz4k9hWae_1>*F+g-My`zsOZd5%`R6k5-{;@QKXdGGGzd z%wq7DpPT;{0~3EQ1Aj077NG5o{DG< None: return __update_data(models.FlightData, id, number=number) -def update_att_adjust(id: int, state: models.RunState) -> None: +def update_att_adjust_run(id: int, state: models.RunState) -> None: return __update_data(models.AttAdjustRun, id, state=state) def update_op_nav_run(id: int, state: models.RunState) -> None: - with Session.begin() as session: - row = session.query(models.OpNavRun).filter(models.OpNavRun.id == id).first() - if row is not None: - row.state = state - logging.info(f"Set {row} to {state}") - # session.commit() and session.close() + return __update_data(models.OpNavRun, id, state=state) def __update_failed_runs(model: models.Base) -> int: @@ -112,11 +107,11 @@ def __query_all_data(model: models.Base) -> typing.List[models.Base]: return result -def query_all_flight_data() -> None: +def query_all_flight_data() -> typing.List[models.FlightData]: return __query_all_data(models.FlightData) -def query_all_op_nav_data() -> None: +def query_all_op_nav_data() -> typing.List[models.OpNavData]: return __query_all_data(models.OpNavData) @@ -142,27 +137,24 @@ def query_latest_op_nav_data() -> typing.Union[models.OpNavData, None]: return result[0] -def query_earliest_att_adjust_run() -> typing.Union[models.AttAdjustRun, None]: +def __query_earliest( + model: models.Base, +) -> typing.Union[typing.List[models.Base], None]: with Session() as session: result = ( - session.query(models.AttAdjustRun, sa.func.min(models.AttAdjustRun.time)) - .filter(models.AttAdjustRun.state.is_(models.RunState.QUEUED)) + session.query(model, sa.func.min(model.time)) + .filter(model.state.is_(models.RunState.QUEUED)) .first() ) session.expunge_all() # session.close() - logging.info(f"Get min att-adjust run: {result[0]}") + logging.info(f"Get earliest {model.__tablename__}: {result[0]}") return result[0] +def query_earliest_att_adjust_run() -> typing.Union[models.AttAdjustRun, None]: + return __query_earliest(models.AttAdjustRun) + + def query_earliest_op_nav_run() -> typing.Union[models.OpNavRun, None]: - with Session() as session: - result = ( - session.query(models.OpNavRun, sa.func.min(models.OpNavRun.time)) - .filter(models.OpNavRun.state.is_(models.RunState.QUEUED)) - .first() - ) - session.expunge_all() - # session.close() - logging.info(f"Get min op-nav run: {result[0]}") - return result[0] + return __query_earliest(models.OpNavRun) diff --git a/_todo_project_name/todo_project_name/flight_sw/main_satellite.py b/_todo_project_name/todo_project_name/flight_sw/main_satellite.py index 14bc1f5b..7bf63798 100644 --- a/_todo_project_name/todo_project_name/flight_sw/main_satellite.py +++ b/_todo_project_name/todo_project_name/flight_sw/main_satellite.py @@ -14,6 +14,7 @@ # import sched # TODO database size # TODO time comparisons add small epsilon in comparisions +# TODO does camera need to block main thread? makes att-adjust lock really weird class FlightMode(enum.Enum): @@ -25,15 +26,20 @@ def __str__(self) -> str: return f"FlightMode.{self.name}({self.value})" -class MainSatellite: - def __init__(self): +class MainSatellite(threading.Thread): + def __init__(self, **kwargs) -> None: + threading.Thread.__init__(self, **kwargs) + self.name = "main-sat" + logging.info("Main satellite initialized") self.mode: FlightMode = FlightMode.Normal + self.continue_run: bool = True + self.run_count: int = 0 - def run(self): + def run(self) -> None: # init params & sensors - while True: - # do not run if global_lock is locked + while self.continue_run: + self.name = f"main-sat-{self.run_count % 10}" with globals.run_camera_cond: while globals.want_to_run_camera: logging.info("Can to run camera set to TRUE") @@ -47,26 +53,20 @@ def run(self): db.add_flight_data(number=random.randint(1, 101)) # db.query_latest_op_nav_data() time.sleep(1) - # if random.randint(0, 2) % 2: - # db.add_att_adjust_data( - # datetime.now() + timedelta(seconds=random.randint(8, 17)) - # ) elif self.mode == FlightMode.AttAdjust: + db.update_failed_att_adjust_runs() att_adjust_run = db.query_earliest_att_adjust_run() if att_adjust_run is not None: time_diff = (att_adjust_run.time - datetime.now()).total_seconds() - if time_diff > 0: - time.sleep(time_diff) - with globals.global_lock: - logging.info("Begin attitude adjustment") - time.sleep(2) - db.update_op_nav_run( - att_adjust_run.id, models.RunState.SUCCEEDED - ) - logging.info("Complete attitude adjustment") + logging.info(f"Att-adjust run scheduled in {time_diff}s") + time.sleep(max(time_diff, 0)) # TODO replace with separate thread? + logging.info("Begin attitude adjustment") + time.sleep(3) + db.update_att_adjust_run( + att_adjust_run.id, models.RunState.SUCCEEDED + ) + logging.info("Complete attitude adjustment") elif self.mode == FlightMode.OpNav: - # db.add_future_op_nav_run() - # time.sleep(2) db.update_failed_op_nav_runs() op_nav_run = db.query_earliest_op_nav_run() if op_nav_run is not None: @@ -77,22 +77,24 @@ def run(self): t.name = "op-nav" t.start() db.update_op_nav_run(op_nav_run.id, models.RunState.RUNNING) - # :( scheduler is not threaded - # sch = sched.scheduler(time.time, time.sleep) - # sch.enterabs( - # op_nav_run.time.timestamp(), 1, op_nav.run, (op_nav_run.id,) - # ) - # sch.run() self.transition() + self.run_count += 1 + if self.run_count > 15: # For debugging purposes + self.continue_run = False def transition(self) -> None: + a_time_diff = -1 + o_time_diff = -1 + db.update_failed_att_adjust_runs() att_adjust_run = db.query_earliest_att_adjust_run() if att_adjust_run is not None: - time_diff = (att_adjust_run.time - datetime.now()).total_seconds() - if time_diff >= CONST.ATT_ADJUST_RUN_BUFFER: - logging.info(f"Next adjustment too far away; time diff: {time_diff}") + a_time_diff = (att_adjust_run.time - datetime.now()).total_seconds() + if a_time_diff >= CONST.ATT_ADJUST_RUN_BUFFER: + logging.info( + f"Next att-adjust run too far away; time diff: {a_time_diff}" + ) else: self.mode = FlightMode.AttAdjust return @@ -100,9 +102,29 @@ def transition(self) -> None: db.update_failed_op_nav_runs() op_nav_run = db.query_earliest_op_nav_run() if op_nav_run is not None: - time_diff = (op_nav_run.time - datetime.now()).total_seconds() - if time_diff >= CONST.OP_NAV_RUN_BUFFER: - logging.info(f"Next adjustment too far away; time diff: {time_diff}") + o_time_diff = (op_nav_run.time - datetime.now()).total_seconds() + # while query attitude for time < now: sleep() + if ( + (att_adjust_run is not None) + and ( + a_time_diff >= o_time_diff + and a_time_diff <= o_time_diff + CONST.MAX_OP_NAV_CAMERA_TIME + ) + or ( + a_time_diff + CONST.MAX_ATT_ADJUST_TIME >= o_time_diff + and a_time_diff + CONST.MAX_ATT_ADJUST_TIME + <= o_time_diff + CONST.MAX_OP_NAV_CAMERA_TIME + ) + ): + # Att-adjust will happen during op-nav camera + logging.info( + "Next op-nav run interferes with att-adjust. Op-nav run canceld." + ) + db.update_op_nav_run(op_nav_run.id, models.RunState.FAILED) + + # o_time_diff + CONST.MAX_OP_NAV_CAMERA_TIME <= a_time_diff + if o_time_diff >= CONST.OP_NAV_RUN_BUFFER: + logging.info(f"Next op-nav run too far away; time diff: {o_time_diff}") else: self.mode = FlightMode.OpNav return diff --git a/_todo_project_name/todo_project_name/main.py b/_todo_project_name/todo_project_name/main.py index 8ad22e22..5814ebac 100644 --- a/_todo_project_name/todo_project_name/main.py +++ b/_todo_project_name/todo_project_name/main.py @@ -1,6 +1,5 @@ import logging import random -import threading from datetime import datetime, timedelta from db import db @@ -30,27 +29,20 @@ def test_db_att_adjust(): res = db.update_failed_att_adjust_runs() assert res == 2 res2 = db.query_earliest_att_adjust_run() - assert now + timedelta(seconds=15) == res2.time + assert res2 is not None and now + timedelta(seconds=15) == res2.time def main_test(): - main_satellite = MainSatellite() - now = datetime.now() - db.add_op_nav_run(now + timedelta(seconds=3)) - db.add_op_nav_run(now + timedelta(seconds=15)) - # db.add_att_adjust_data(now + timedelta(seconds=5)) - - threads = [ - threading.Thread(name="main-sat", target=main_satellite.run, daemon=True) - ] + db.add_att_adjust_data(now + timedelta(seconds=4)) + db.add_op_nav_run(now + timedelta(seconds=2)) + db.add_op_nav_run(now + timedelta(seconds=16)) - for thread in threads: - thread.start() + th = MainSatellite(daemon=True) + th.start() try: - for thread in threads: - thread.join() + th.join() except KeyboardInterrupt: logging.info("Keyboard Interrupt received; exiting") @@ -58,29 +50,3 @@ def main_test(): if __name__ == "__main__": log.set_up_logging() main_test() - # tests = [ - # ("General functions", test_db_functions), - # ("Att adjust functions", test_db_att_adjust), - # ] - - # for (test_name, test_func) in tests: - # print(f">>>{test_name}") - # test_func() - # print("---\n") - - # op_nav mode addds new event to queue after completion - - """ - main loops, updating some sensor data every 3 sec - needs ability to be the only thread running (lock) - op_nav needs access to data - needs ability to be the only thread running - requires knowledge of propulsion events - - each tries to acquire lock A. waits until lock B - - - goal for tomorrow: main writend checks for most recent op_nav result - op_nav read main data and writes - """ - # EXPECT: 3 added rows, 2 failed status diff --git a/_todo_project_name/todo_project_name/op_nav/op_nav.py b/_todo_project_name/todo_project_name/op_nav/op_nav.py index 9de71f29..8d3cfe21 100644 --- a/_todo_project_name/todo_project_name/op_nav/op_nav.py +++ b/_todo_project_name/todo_project_name/op_nav/op_nav.py @@ -1,4 +1,3 @@ -import enum import logging import time from datetime import datetime @@ -7,17 +6,6 @@ from db import db, models -class OpNavStatus(enum.Enum): - SUCCESS = enum.auto() - FAILURE = enum.auto() - - def __str__(self) -> str: - return f"OpNavStatus.{self.name}({self.value})" - - -delay = 2 - - class OpNav: def __init__(self): logging.info("Op-nav initialized") @@ -41,40 +29,14 @@ def __observe(self) -> models.FlightData: globals.run_camera_cond.notify_all() return res - def __process_data(self, flight_data: models.FlightData): + def __process_data(self, flight_data: models.FlightData) -> None: time.sleep(2) db.add_op_nav_data(datetime.now(), 2 * flight_data.number) - def run(self, run_id): - # while query attitude for time < now: sleep() + def run(self, run_id) -> None: logging.info(f"ID: {run_id}") logging.info(f"Op-nav run beginning at {datetime.now()}") res = self.__observe() self.__process_data(res) logging.info(f"Op-nav run ending at {datetime.now()}") db.update_op_nav_run(run_id, models.RunState.SUCCEEDED) - - # while True: # op_nav interval - # time.sleep(delay) - # # TODO check for future attitude adjustment - # while True: - # op_nav_run = db.query_earliest_op_nav_run() - # if op_nav_run is None: - # break - # time_diff = (op_nav_run.time - datetime.now()).total_seconds() - # if time_diff > 2 * delay: - # break - # if time_diff > 0: - # logging.info(f"Upcoming op-nav run. Waiting {time_diff}") - # time.sleep(time_diff) - # else: - # logging.info("Op-nav run time passed") - # db.update_op_nav_run(op_nav_run.id, True) - - # res = self.__observe() - # time.sleep(delay) - # logging.info(f"Add new op-nav row") - # if res is not None: - # db.add_op_nav_data(datetime.now(), res) - - return OpNavStatus.SUCCESS diff --git a/_todo_project_name/todo_project_name/utils/constants.py b/_todo_project_name/todo_project_name/utils/constants.py index 3401ff21..af3a7178 100644 --- a/_todo_project_name/todo_project_name/utils/constants.py +++ b/_todo_project_name/todo_project_name/utils/constants.py @@ -17,9 +17,12 @@ def DATA_PATH(self) -> str: :return: sql database parent path :rtype: str """ - if os.path.isabs(self.__config["DATA_PATH"]): - return self.__config["DATA_PATH"] - return os.path.join(self.__project_path, self.__config["DATA_PATH"]) + data_path = ( + self.__config["DATA_PATH"] if self.__config["DATA_PATH"] is not None else "" + ) + if os.path.isabs(data_path): + return data_path + return os.path.join(self.__project_path, data_path) @property def DATA_RESET(self) -> bool: @@ -46,7 +49,7 @@ def ATT_ADJUST_RUN_BUFFER(self) -> float: :return: difference in seconds :rtype: float """ - return 10.0 + return 5.0 @property def OP_NAV_RUN_BUFFER(self) -> float: @@ -55,7 +58,25 @@ def OP_NAV_RUN_BUFFER(self) -> float: :return: difference in seconds :rtype: float """ - return 10.0 + return 5.0 + + @property + def MAX_ATT_ADJUST_TIME(self) -> float: + """Worst case time to complete an att-adjust + + :return: time in seconds + :rtype: float + """ + return 6.0 + + @property + def MAX_OP_NAV_CAMERA_TIME(self) -> float: + """Worst case time to complete an op-nav camera run + + :return: time in seconds + :rtype: float + """ + return 20.0 CONST = __Const() diff --git a/pyrightconfig.json b/pyrightconfig.json index 1c46d38c..ddf0d27d 100644 --- a/pyrightconfig.json +++ b/pyrightconfig.json @@ -25,11 +25,10 @@ "OpticalNavigation/core/opnav.py", "OpticalNavigation/core/find.py", "OpticalNavigation/core/find_with_blobs.py", - "OpticalNavigation/core/find_with_kmeans.py" - ], - "extraPaths": [ - "OpticalNavigation/" + "OpticalNavigation/core/find_with_kmeans.py", + "_todo_project_name" ], + "extraPaths": ["OpticalNavigation/"], "pythonVersion": "3.7", "pythonPlatform": "Linux", "reportUnboundVariable": "warning",