diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..5ca7137 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,13 @@ +# These are supported funding model platforms + +github: ampledata +patreon: # Replace with a single Patreon username +open_collective: # Replace with a single Open Collective username +ko_fi: ampledata +tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel +community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry +liberapay: # Replace with a single Liberapay username +issuehunt: # Replace with a single IssueHunt username +otechie: # Replace with a single Otechie username +lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry +custom: https://www.buymeacoffee.com/ampledata diff --git a/.github/workflows/debian.yml b/.github/workflows/debian.yml new file mode 100644 index 0000000..9a558d4 --- /dev/null +++ b/.github/workflows/debian.yml @@ -0,0 +1,66 @@ +name: Build Debian package + +on: + push: + tags: + - '*' + +env: + DEB_BUILD_OPTIONS: nocheck + +jobs: + build-artifact: + runs-on: ubuntu-22.04 + + steps: + - uses: actions/checkout@master + + - name: Install packaging dependencies + run: | + sudo apt-get update -qq + sudo apt-get install -y \ + python3 python3-dev python3-pip python3-venv python3-all \ + dh-python debhelper devscripts dput software-properties-common \ + python3-distutils python3-setuptools python3-wheel python3-stdeb \ + python3-all + + - name: Build Debian/Apt sdist_dsc + run: | + python3 -m pip install . + rm -Rf deb_dist/* + python3 setup.py --command-packages=stdeb.command sdist_dsc + + - name: Build Debian/Apt bdist_deb + run: | + python3 -m pip install . + export REPO_NAME=$(echo ${{ github.repository }} | awk -F"/" '{print $2}') + python3 setup.py --command-packages=stdeb.command bdist_deb + ls -al deb_dist/ + cp deb_dist/python3-${REPO_NAME}_*_all.deb deb_dist/python3-${REPO_NAME}_latest_all.deb + + - uses: actions/upload-artifact@master + with: + name: artifact-deb + path: | + deb_dist/*.deb + + - name: Create Release + id: create_release + uses: actions/create-release@master + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ github.ref }} + release_name: Release ${{ github.ref }} + draft: false + prerelease: false + + - name: Upload Release Asset + id: upload-release-asset + uses: svenstaro/upload-release-action@v2 + with: + repo_token: ${{ secrets.GITHUB_TOKEN }} + file: deb_dist/*.deb + tag: ${{ github.ref }} + overwrite: true + file_glob: true \ No newline at end of file diff --git a/.github/workflows/python-publish_to_pypi.yml b/.github/workflows/python-publish_to_pypi.yml new file mode 100644 index 0000000..ecfcaf3 --- /dev/null +++ b/.github/workflows/python-publish_to_pypi.yml @@ -0,0 +1,41 @@ +# This workflow will upload a Python Package using Twine when a release is created +name: Publish package to PyPI + +on: + push: + tags: + - '*' + +jobs: + deploy: + + runs-on: ubuntu-22.04 + # Specifying a GitHub environment is optional, but strongly encouraged + environment: release + + permissions: + # IMPORTANT: this permission is mandatory for trusted publishing + id-token: write + + steps: + - uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.x' + + - name: Install dependencies + run: | + python3 -m pip install --upgrade pip + python3 -m pip install setuptools wheel twine + + - name: Build + run: | + python3 setup.py sdist bdist_wheel + + - name: Publish package + uses: pypa/gh-action-pypi-publish@release/v1 + with: + user: __token__ + password: ${{ secrets.PYPI_API_TOKEN }} diff --git a/.github/workflows/python-test_and_lint.yml b/.github/workflows/python-test_and_lint.yml new file mode 100644 index 0000000..f80dec2 --- /dev/null +++ b/.github/workflows/python-test_and_lint.yml @@ -0,0 +1,38 @@ +name: Lint & Test Code + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + build: + + runs-on: ubuntu-22.04 + strategy: + fail-fast: false + matrix: + python-version: [3.9, "3.10", "3.11", "3.12"] + + 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 test requirements + run: | + make install_test_requirements + - name: Install package itself (editable) + run: | + make editable + - name: Lint with pylint + run: | + make pylint + - name: Lint with flake8 + run: | + make flake8 + - name: Test with pytest-cov + run: | + make test_cov diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..44e1e85 --- /dev/null +++ b/.gitignore @@ -0,0 +1,37 @@ +.vscode/ +.devcontainer/ +*.deb +*.egg +*.egg-info/ +*.egg/ +*.ignore +*.py[co] +*.py[oc] +*.spl +*.vagrant +.DS_Store +.coverage +.eggs/ +.eggs/* +.idea +.idea/ +.pt +.vagrant/ +RELEASE-VERSION.txt +build/ +cover/ +dist/ +dump.rdb +flake8.log +local/ +local_* +metadata/ +nosetests.xml +output.xml +pylint.log +redis-server.log +redis-server/ +__pycache__ +known_craft.csv + .mypy_cache/ + .pytest_cache/config.ini diff --git a/.readthedocs.yml b/.readthedocs.yml new file mode 100644 index 0000000..e941dac --- /dev/null +++ b/.readthedocs.yml @@ -0,0 +1,17 @@ +# .readthedocs.yaml +# Read the Docs configuration file +# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details + +version: 2 + +build: + os: ubuntu-22.04 + tools: + python: "3.11" + +mkdocs: + configuration: mkdocs.yml + +python: + install: + - requirements: docs/requirements.txt diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..9113406 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +## MeshCOT 1.0.0 + +Initial release of MeshCOT. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..52df472 --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + +Apache License +Version 2.0, January 2004 +http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + +"License" shall mean the terms and conditions for use, reproduction, +and distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by +the copyright owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and all +other entities that control, are controlled by, or are under common +control with that entity. For the purposes of this definition, +"control" means (i) the power, direct or indirect, to cause the +direction or management of such entity, whether by contract or +otherwise, or (ii) ownership of fifty percent (50%) or more of the +outstanding shares, or (iii) beneficial ownership of such entity. + +"You" (or "Your") shall mean an individual or Legal Entity +exercising permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, +including but not limited to software source code, documentation +source, and configuration files. + +"Object" form shall mean any form resulting from mechanical +transformation or translation of a Source form, including but +not limited to compiled object code, generated documentation, +and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or +Object form, made available under the License, as indicated by a +copyright notice that is included in or attached to the work +(an example is provided in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object +form, that is based on (or derived from) the Work and for which the +editorial revisions, annotations, elaborations, or other modifications +represent, as a whole, an original work of authorship. For the purposes +of this License, Derivative Works shall not include works that remain +separable from, or merely link (or bind by name) to the interfaces of, +the Work and Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including +the original version of the Work and any modifications or additions +to that Work or Derivative Works thereof, that is intentionally +submitted to Licensor for inclusion in the Work by the copyright owner +or by an individual or Legal Entity authorized to submit on behalf of +the copyright owner. For the purposes of this definition, "submitted" +means any form of electronic, verbal, or written communication sent +to the Licensor or its representatives, including but not limited to +communication on electronic mailing lists, source code control systems, +and issue tracking systems that are managed by, or on behalf of, the +Licensor for the purpose of discussing and improving the Work, but +excluding communication that is conspicuously marked or otherwise +designated in writing by the copyright owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity +on behalf of whom a Contribution has been received by Licensor and +subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of +this License, each Contributor hereby grants to You a perpetual, +worldwide, non-exclusive, no-charge, royalty-free, irrevocable +copyright license to reproduce, prepare Derivative Works of, +publicly display, publicly perform, sublicense, and distribute the +Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of +this License, each Contributor hereby grants to You a perpetual, +worldwide, non-exclusive, no-charge, royalty-free, irrevocable +(except as stated in this section) patent license to make, have made, +use, offer to sell, sell, import, and otherwise transfer the Work, +where such license applies only to those patent claims licensable +by such Contributor that are necessarily infringed by their +Contribution(s) alone or by combination of their Contribution(s) +with the Work to which such Contribution(s) was submitted. If You +institute patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Work +or a Contribution incorporated within the Work constitutes direct +or contributory patent infringement, then any patent licenses +granted to You under this License for that Work shall terminate +as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the +Work or Derivative Works thereof in any medium, with or without +modifications, and in Source or Object form, provided that You +meet the following conditions: + +(a) You must give any other recipients of the Work or +Derivative Works a copy of this License; and + +(b) You must cause any modified files to carry prominent notices +stating that You changed the files; and + +(c) You must retain, in the Source form of any Derivative Works +that You distribute, all copyright, patent, trademark, and +attribution notices from the Source form of the Work, +excluding those notices that do not pertain to any part of +the Derivative Works; and + +(d) If the Work includes a "NOTICE" text file as part of its +distribution, then any Derivative Works that You distribute must +include a readable copy of the attribution notices contained +within such NOTICE file, excluding those notices that do not +pertain to any part of the Derivative Works, in at least one +of the following places: within a NOTICE text file distributed +as part of the Derivative Works; within the Source form or +documentation, if provided along with the Derivative Works; or, +within a display generated by the Derivative Works, if and +wherever such third-party notices normally appear. The contents +of the NOTICE file are for informational purposes only and +do not modify the License. You may add Your own attribution +notices within Derivative Works that You distribute, alongside +or as an addendum to the NOTICE text from the Work, provided +that such additional attribution notices cannot be construed +as modifying the License. + +You may add Your own copyright statement to Your modifications and +may provide additional or different license terms and conditions +for use, reproduction, or distribution of Your modifications, or +for any such Derivative Works as a whole, provided Your use, +reproduction, and distribution of the Work otherwise complies with +the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, +any Contribution intentionally submitted for inclusion in the Work +by You to the Licensor shall be under the terms and conditions of +this License, without any additional terms or conditions. +Notwithstanding the above, nothing herein shall supersede or modify +the terms of any separate license agreement you may have executed +with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade +names, trademarks, service marks, or product names of the Licensor, +except as required for reasonable and customary use in describing the +origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or +agreed to in writing, Licensor provides the Work (and each +Contributor provides its Contributions) on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +implied, including, without limitation, any warranties or conditions +of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A +PARTICULAR PURPOSE. You are solely responsible for determining the +appropriateness of using or redistributing the Work and assume any +risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, +whether in tort (including negligence), contract, or otherwise, +unless required by applicable law (such as deliberate and grossly +negligent acts) or agreed to in writing, shall any Contributor be +liable to You for damages, including any direct, indirect, special, +incidental, or consequential damages of any character arising as a +result of this License or out of the use or inability to use the +Work (including but not limited to damages for loss of goodwill, +work stoppage, computer failure or malfunction, or any and all +other commercial damages or losses), even if such Contributor +has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing +the Work or Derivative Works thereof, You may choose to offer, +and charge a fee for, acceptance of support, warranty, indemnity, +or other liability obligations and/or rights consistent with this +License. However, in accepting such obligations, You may act only +on Your own behalf and on Your sole responsibility, not on behalf +of any other Contributor, and only if You agree to indemnify, +defend, and hold each Contributor harmless for any liability +incurred by, or claims asserted against, such Contributor by reason +of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + +To apply the Apache License to your work, attach the following +boilerplate notice, with the fields enclosed by brackets "[]" +replaced with your own identifying information. (Don't include +the brackets!) The text should be enclosed in the appropriate +comment syntax for the file format. We also recommend that a +file or class name and description of purpose be included on the +same "printed page" as the copyright notice for easier +identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..645a28c --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +include README.rst LICENSE requirements.txt diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..faffa47 --- /dev/null +++ b/Makefile @@ -0,0 +1,78 @@ +# +# Copyright Sensors & Signals LLC https://www.snstac.com +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +this_app = meshcot +.DEFAULT_GOAL := all + +all: editable + +develop: + python3 setup.py develop + +editable: + python3 -m pip install -e . + +install_test_requirements: + python3 -m pip install -r requirements_test.txt + +install: + python3 setup.py install + +uninstall: + python3 -m pip uninstall -y $(this_app) + +reinstall: uninstall install + +publish: + python3 setup.py publish + +clean: + @rm -rf *.egg* build dist *.py[oc] */*.py[co] cover doctest_pypi.cfg \ + nosetests.xml pylint.log output.xml flake8.log tests.log \ + test-result.xml htmlcov fab.log .coverage __pycache__ \ + */__pycache__ + +pep8: + flake8 --max-line-length=88 --extend-ignore=E203 --exit-zero $(this_app)/*.py + +flake8: pep8 + +lint: + pylint --msg-template="{path}:{line}: [{msg_id}({symbol}), {obj}] {msg}" \ + --max-line-length=88 -r n $(this_app)/*.py || exit 0 + +pylint: lint + +checkmetadata: + python3 setup.py check -s --restructuredtext + +mypy: + mypy --strict . + +pytest: + pytest + +test: editable install_test_requirements pytest + +test_cov: + pytest --cov=$(this_app) --cov-report term-missing + +black: + black . + +mkdocs: + pip install -r docs/requirements.txt + mkdocs serve \ No newline at end of file diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..76ffd8b --- /dev/null +++ b/README.rst @@ -0,0 +1,54 @@ +.. image:: https://adsbxcot.readthedocs.io/en/latest/atak_screenshot_with_pytak_logo-x25.png + :alt: ATAK screenshot with PyTAK logo. + +Display Drones in TAK +********************* + +MeshCOT is software for monitoring and analyzing Direct Remote Identification (DRI), Remote ID (RID) or Open Drone ID (ODID) broadcasts from Uncrewed Aircraft (UA) via the Team Awareness Kit (TAK) ecosystem of products. + +MeshCOT captures & reports real-time data received from drones into TAK products using native TAK protocols, including Cursor on Target (CoT). + +MeshCOT has been evaluated with WinTAK, iTAK, ATAK & TAK Server. + +MeshCOT is in active use today in a variety of missions. + +`Documentation is available here. `_ + +License +======= + +## MeshCOT + +Copyright Sensors & Signals LLC https://www.snstac.com + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +## open_drone_id.py + +Copyright (c) 2022 BlueMark Innovations BV + +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/docs/aircraft_data.md b/docs/aircraft_data.md new file mode 100644 index 0000000..c4910d9 --- /dev/null +++ b/docs/aircraft_data.md @@ -0,0 +1,5 @@ +![ADS-B flight data displayed in ATAK](screenshots/screenshot_1705531116_13879.png) + +Tracks within TAK retain the aircraft's position, course & speed, and remarks contain the received Flight, Tail & Category ADS-B data. + +MeshCOT includes a set of filters and transforms in known_craft format. TK diff --git a/docs/atak_screenshot_with_pytak_logo-x25.png b/docs/atak_screenshot_with_pytak_logo-x25.png new file mode 100644 index 0000000..14d0a3e Binary files /dev/null and b/docs/atak_screenshot_with_pytak_logo-x25.png differ diff --git a/docs/atak_screenshot_with_pytak_logo.jpg b/docs/atak_screenshot_with_pytak_logo.jpg new file mode 100755 index 0000000..c342df2 Binary files /dev/null and b/docs/atak_screenshot_with_pytak_logo.jpg differ diff --git a/docs/changelog.md b/docs/changelog.md new file mode 100644 index 0000000..f4d16fd --- /dev/null +++ b/docs/changelog.md @@ -0,0 +1 @@ +{!CHANGELOG.md!} diff --git a/docs/configuration.md b/docs/configuration.md new file mode 100644 index 0000000..fc18ff6 --- /dev/null +++ b/docs/configuration.md @@ -0,0 +1,59 @@ +MeshCOT's configuration parameters can be set two ways: + +1. In an INI-style configuration file. (ex. ``meshcot -c config.ini``) +2. As environment variables. (ex. ``export DEBUG=1;meshcot``) + +MeshCOT has the following built-in configuration parameters: + +* **`FEED_URL`**: + * Default: ``file:///run/dump1090-fa/aircraft.json`` + + ADS-B data source URL. Supported URL types: + + - ``file://`` The absolute local folder path to an ADS-B data file in JSON format. + - ``http://`` The local piaware web server aircraft data JSON URL. (ex. ``http://piaware.local:8080/data/aircraft.json``) + - ``tcp://`` A dump1090 BaseStation (SBS-1, "raw") host & port URL (ex. ``tcp://sensor.example.com:30003``). + - ``tcp+raw://`` A dump1090 BaseStation (SBS-1, "raw") host & port URL (ex. ``tcp+raw://sensor.example.com:30003``). + - ``tcp+beast://`` A dump1090 Beast binary mode host & port URL (ex. ``tcp+beast://sensor.example.com:30005``). + +* **`POLL_INTERVAL`**: + * Default: ``3`` seconds + + If the `FEED_URL` is of type HTTP, the period, in seconds, to poll this URL. + +* **`ALT_UPPER`**: + * Default: unset + + Upper Altitude Limit, geometric (GNSS / INS) altitude in feet referenced to the WGS84 ellipsoid. + +* **`ALT_LOWER`**: + * Default: unset + + Lower Altitude Limit, geometric (GNSS / INS) altitude in feet referenced to the WGS84 ellipsoid. + +* **`KNOWN_CRAFT`**: + * Default: unset + + CSV-style aircraft hints file for overriding callsign, icon, COT Type, etc. + +* **`INCLUDE_ALL_CRAFT`**: + * Default: ``False`` + + If ``True`` and ``KNOWN_CRAFT`` is set, will forward all aircraft, including those transformed by the ``KNOWN_CRAFT`` database. + +* **`INCLUDE_TISB`**: + * Default: ``False`` + + If ``True``, includes TIS-B tracks. + +* **`TISB_ONLY`**: + * Default: ``False`` + + If `True`, only passes TIS-B tracks (`INCLUDE_TISB` must also be `True`). + +Additional configuration parameters, including TAK Server configuration, are included in the [PyTAK Configuration](https://pytak.readthedocs.io/en/latest/configuration/) documentation. + + + + + diff --git a/docs/index.md b/docs/index.md new file mode 100644 index 0000000..1c9ad8b --- /dev/null +++ b/docs/index.md @@ -0,0 +1,13 @@ +![ATAK Screenshot with MeshCOT aircraft tracks.](atak_screenshot_with_pytak_logo-x25.png) + +# Display Aircraft in TAK + +MeshCOT is software for monitoring and analyzing Direct Remote Identification (DRI), Remote ID (RID) or Open Drone ID (ODID) broadcasts from Uncrewed Aircraft (UA) via the [Team Awareness Kit (TAK)](https://www.tak.gov) ecosystem of products. + +MeshCOT captures & reports real-time data received from drones into TAK products using native TAK protocols, including Cursor on Target (CoT). + +MeshCOT has been evaluated with WinTAK, iTAK, ATAK & TAK Server. + +MeshCOT is in active use today in a variety of missions. + +[Documentation is available here.](https://meshcot.rtfd.io) diff --git a/docs/installation.md b/docs/installation.md new file mode 100644 index 0000000..f1d5b27 --- /dev/null +++ b/docs/installation.md @@ -0,0 +1,33 @@ +MeshCOT's functionality provided by a command-line program called `meshcot`. + +There are several methods of installing MeshCOT. They are listed below, in order of complexity. + +## Debian, Ubuntu, Raspberry Pi + +Install MeshCOT, and prerequisite packages of [PyTAK](https://pytak.rtfd.io). + +```sh linenums="1" +sudo apt update +wget https://github.com/ampledata/pytak/releases/latest/download/python3-pytak_latest_all.deb +sudo apt install -f ./python3-pytak_latest_all.deb +wget https://github.com/ampledata/meshcot/releases/latest/download/python3-meshcot_latest_all.deb +sudo apt install -f ./python3-meshcot_latest_all.deb +``` + +## Windows, Linux + +Install from the Python Package Index (PyPI) [Advanced Users]:: + +```sh +sudo python3 -m pip install meshcot +``` + +## Developers + +PRs welcome! + +```sh linenums="1" +git clone https://github.com/snstac/meshcot.git +cd meshcot/ +python3 setup.py install +``` diff --git a/docs/requirements.md b/docs/requirements.md new file mode 100644 index 0000000..10b89b3 --- /dev/null +++ b/docs/requirements.md @@ -0,0 +1 @@ +MeshCOT runs in any Python 3.9+ environment, including Windows, Linux & Raspberry Pi. diff --git a/docs/requirements.txt b/docs/requirements.txt new file mode 100644 index 0000000..b00f781 --- /dev/null +++ b/docs/requirements.txt @@ -0,0 +1,23 @@ +click +ghp-import +griffe +importlib-metadata +jinja2 +markdown +markdown-include +markupsafe +mergedeep +mkdocs +mkdocs-autorefs +mkdocs-include-markdown-plugin +mkdocs-material +mkdocstrings-python +mkdocstrings[python] +packaging +pymdown-extensions +pyparsing +python-dateutil +pyyaml +pyyaml-env-tag +six +zipp diff --git a/docs/screenshots/Screenshot_20201026-141841_ATAK.jpg b/docs/screenshots/Screenshot_20201026-141841_ATAK.jpg new file mode 100644 index 0000000..dc7fed2 Binary files /dev/null and b/docs/screenshots/Screenshot_20201026-141841_ATAK.jpg differ diff --git a/docs/screenshots/Screenshot_20201026-142037_ATAK.jpg b/docs/screenshots/Screenshot_20201026-142037_ATAK.jpg new file mode 100644 index 0000000..5b4c9ba Binary files /dev/null and b/docs/screenshots/Screenshot_20201026-142037_ATAK.jpg differ diff --git a/docs/screenshots/screenshot-1604561417.png b/docs/screenshots/screenshot-1604561417.png new file mode 100644 index 0000000..84a1220 Binary files /dev/null and b/docs/screenshots/screenshot-1604561417.png differ diff --git a/docs/screenshots/screenshot-1604561442.png b/docs/screenshots/screenshot-1604561442.png new file mode 100644 index 0000000..338ad7d Binary files /dev/null and b/docs/screenshots/screenshot-1604561442.png differ diff --git a/docs/screenshots/screenshot-1604561447-25.png b/docs/screenshots/screenshot-1604561447-25.png new file mode 100644 index 0000000..9c87661 Binary files /dev/null and b/docs/screenshots/screenshot-1604561447-25.png differ diff --git a/docs/screenshots/screenshot-1604561447.png b/docs/screenshots/screenshot-1604561447.png new file mode 100644 index 0000000..fe76d32 Binary files /dev/null and b/docs/screenshots/screenshot-1604561447.png differ diff --git a/docs/screenshots/screenshot-1604561453.png b/docs/screenshots/screenshot-1604561453.png new file mode 100644 index 0000000..fd4383a Binary files /dev/null and b/docs/screenshots/screenshot-1604561453.png differ diff --git a/docs/screenshots/screenshot-1604561459.png b/docs/screenshots/screenshot-1604561459.png new file mode 100644 index 0000000..cb2b036 Binary files /dev/null and b/docs/screenshots/screenshot-1604561459.png differ diff --git a/docs/screenshots/screenshot-1604561473.png b/docs/screenshots/screenshot-1604561473.png new file mode 100644 index 0000000..5f9b516 Binary files /dev/null and b/docs/screenshots/screenshot-1604561473.png differ diff --git a/docs/screenshots/screenshot-1604561483.png b/docs/screenshots/screenshot-1604561483.png new file mode 100644 index 0000000..fe710a1 Binary files /dev/null and b/docs/screenshots/screenshot-1604561483.png differ diff --git a/docs/screenshots/screenshot_1705531116_13879.png b/docs/screenshots/screenshot_1705531116_13879.png new file mode 100644 index 0000000..7e71999 Binary files /dev/null and b/docs/screenshots/screenshot_1705531116_13879.png differ diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md new file mode 100644 index 0000000..24eee69 --- /dev/null +++ b/docs/troubleshooting.md @@ -0,0 +1,26 @@ + +To report bugs, please set the DEBUG=1 environment variable to collect logs: + +```sh +DEBUG=1 meshcot +``` + +Or: + +```sh linenums="1" +export DEBUG=1 +meshcot +``` + +Or: + +```sh linenums="1" +echo 'DEBUG=1' >> meshcot.ini +meshcot -c meshcot.ini +``` + +You can view systemd/systemctl/service logs via: + +```journalctl -fu meshcot``` + +Please use GitHub issues for support requests. Please note that MeshCOT is free open source software and comes with no warranty. See LICENSE. \ No newline at end of file diff --git a/docs/usage.md b/docs/usage.md new file mode 100644 index 0000000..33a443e --- /dev/null +++ b/docs/usage.md @@ -0,0 +1,49 @@ +## Command-line + +Command-line usage is available by running ``meshcot -h``. + +``` +usage: meshcot [-h] [-c CONFIG_FILE] [-p PREF_PACKAGE] + +options: + -h, --help show this help message and exit + -c CONFIG_FILE, --CONFIG_FILE CONFIG_FILE + Optional configuration file. Default: config.ini + -p PREF_PACKAGE, --PREF_PACKAGE PREF_PACKAGE + Optional connection preferences package zip file (aka data package). +``` + +## Run as a service / Run forever + +1. Add the text contents below a file named `/etc/systemd/system/meshcot.service` + You can use `nano` or `vi` editors: `sudo nano /etc/systemd/system/meshcot.service` +2. Reload systemctl: `sudo systemctl daemon-reload` +3. Enable MeshCOT: `sudo systemctl enable meshcot` +4. Start MeshCOT: `sudo systemctl start meshcot` + +### `meshcot.service` Content +```ini +[Unit] +Description=MeshCOT - Display Drones in TAK +Documentation=https://meshcot.rtfd.io +Wants=network.target +After=network.target +StartLimitIntervalSec=0 +# Uncomment this line if you're running dump1090 & meshcot on the same computer: +# After=dump1090-fa.service + +[Service] +RuntimeDirectoryMode=0755 +ExecStart=/usr/local/bin/meshcot -c /etc/meshcot.ini +SyslogIdentifier=meshcot +Type=simple +Restart=always +RestartSec=30 +RestartPreventExitStatus=64 +Nice=-5 + +[Install] +WantedBy=default.target +``` + +> Pay special attention to the `ExecStart` line above. You'll need to provide the full local filesystem path to both your meshcot executable & meshcot configuration files. \ No newline at end of file diff --git a/meshcot/__init__.py b/meshcot/__init__.py new file mode 100644 index 0000000..e5dd8bc --- /dev/null +++ b/meshcot/__init__.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# Copyright Sensors & Signals LLC https://www.snstac.com +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +"""Meshtastic to TAK Gateway. + +:source: +""" + +__version__ = "1.0.0-beta5" +__author__ = "Greg Albrecht " +__copyright__ = "Copyright Sensors & Signals LLC https://www.snstac.com" +__license__ = "Apache License, Version 2.0" + +from .constants import ( # NOQA + DEFAULT_MQTT_BROKER, + DEFAULT_MQTT_PORT, + DEFAULT_MQTT_TOPIC, + DEFAULT_GPS_INFO_CMD, + DEFAULT_MESH_IFACE, +) +from .functions import ( # NOQA + xml_to_cot, + create_tasks, +) +from .classes import MQTTWorker, MeshWorker # NOQA diff --git a/meshcot/classes.py b/meshcot/classes.py new file mode 100644 index 0000000..9556284 --- /dev/null +++ b/meshcot/classes.py @@ -0,0 +1,144 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# Copyright Sensors & Signals LLC https://www.snstac.com +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +"""MeshCOT Class Definitions.""" + +import asyncio + +import json +import platform +from typing import Optional + +import asyncio_mqtt as aiomqtt + +import pytak +import meshcot + +from pubsub import pub + +import pytak + +import meshtastic +import meshtastic.serial_interface +from meshtastic.util import ( + Timeout, + camel_to_snake, + fromPSK, + our_exit, + pskToString, + stripnl, + message_to_json, +) + +from dotmap import DotMap +from pubsub import pub + +import json + +__author__ = "Greg Albrecht " +__copyright__ = "Copyright Sensors & Signals LLC https://www.snstac.com" +__license__ = "Apache License, Version 2.0" + + +class MQTTWorker(pytak.QueueWorker): + """Queue Worker for MQTT.""" + + async def run(self, _=-1) -> None: + """Run this Thread, Reads from Pollers.""" + self._logger.info("Run: MQTTWorker") + + client_id = self.config.get("MQTT_CLIENT_ID", "meshcot") + topic = self.config.get("MQTT_TOPIC", meshcot.DEFAULT_MQTT_TOPIC) + broker = self.config.get("MQTT_BROKER", meshcot.DEFAULT_MQTT_BROKER) + port = self.config.get("MQTT_PORT", meshcot.DEFAULT_MQTT_PORT) + mqtt_username = self.config.get("MQTT_USERNAME") + mqtt_password = self.config.get("MQTT_PASSWORD") + + self._logger.info("Using MQTT Broker: %s:%d/%s", broker, port, topic) + + async with aiomqtt.Client( + hostname=broker, + port=port, + username=mqtt_username, + password=mqtt_password, + client_id=client_id, + ) as client: + self._logger.info("Connected to MQTT Broker %s:%d/%s", broker, port, topic) + while 1: + data = await self.queue.get() + if not data: + await asyncio.sleep(0.01) + j_data = json.dumps(data) + self._logger.debug("Publishing %s", j_data) + await client.publish("meshcot", payload=j_data) + + +class MeshWorker(pytak.QueueWorker): + """Queue Worker for Mesh.""" + + def __init__(self, queue, net_queue, config): + """Initialize this class.""" + super().__init__(queue, config) + self.net_queue = net_queue + self.config = config + self.interface = None + self.mesh_iface = self.config.get("MESH_IFACE", meshcot.DEFAULT_MESH_IFACE) + + def on_receive(self, packet, interface): # called when a packet arrives + payload = { + "meta": { + "node_id": platform.node(), + "interface": str(interface), + "mesh_iface": self.mesh_iface, + "packet_len": len(packet), + } + } + + raw = packet.get("raw") + if raw: + pl = json.loads(message_to_json(raw)) + payload = pl | payload + + decoded = packet.get("decoded") + if decoded: + payload["meta"]["decoded_len"] = len(decoded) + for d_key, d_val in decoded.items(): + if d_key not in ["payload", "requestId", "portnum"]: + if not isinstance(d_val, dict): + continue + if d_val.get("raw"): + del d_val["raw"] + payload["decoded"] = payload["decoded"] | d_val + payload["decoded"]["requestId"] = decoded.get("requestId") + payload["decoded"]["portnum"] = decoded.get("portnum") + + self._logger.debug("payload=%s", payload) + self.net_queue.put_nowait(payload) + + def on_connection(self, interface, topic=pub.AUTO_TOPIC): + self._logger.info("Connected to %s/%s", interface, topic) + + async def run(self, _=-1) -> None: + """Run the main process loop.""" + self._logger.info("Run: MeshWorker") + self._logger.info("Using MESH_IFACE=%s", self.mesh_iface) + pub.subscribe(self.on_receive, "meshtastic.receive") + pub.subscribe(self.on_connection, "meshtastic.connection.established") + self.interface = meshtastic.serial_interface.SerialInterface(self.mesh_iface) + while 1: + await asyncio.sleep(0.01) diff --git a/meshcot/commands.py b/meshcot/commands.py new file mode 100644 index 0000000..a7db558 --- /dev/null +++ b/meshcot/commands.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# Copyright Sensors & Signals LLC https://www.snstac.com +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +"""PyTAK Command Line.""" + +import pytak + +__author__ = "Greg Albrecht " +__copyright__ = "Copyright Sensors & Signals LLC https://www.snstac.com" +__license__ = "Apache License, Version 2.0" + + +def main() -> None: + """Boilerplate main func.""" + # PyTAK CLI tool boilerplate: + pytak.cli(__name__.split(".", maxsplit=1)[0]) + + +if __name__ == "__main__": + main() diff --git a/meshcot/constants.py b/meshcot/constants.py new file mode 100644 index 0000000..7b2b016 --- /dev/null +++ b/meshcot/constants.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# Copyright Sensors & Signals LLC https://www.snstac.com +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +"""MeshCOT Constants.""" + +__author__ = "Greg Albrecht " +__copyright__ = "Copyright Sensors & Signals LLC https://www.snstac.com" +__license__ = "Apache License, Version 2.0" + + +DEFAULT_MQTT_BROKER: str = "172.17.2.60" +DEFAULT_MQTT_PORT: int = 1883 +DEFAULT_MQTT_TOPIC: str = "#" +DEFAULT_GPS_INFO_CMD: str = "gpspipe --json -n 5" +DEFAULT_MESH_IFACE: str = "/dev/cu.usbmodem401" diff --git a/meshcot/functions.py b/meshcot/functions.py new file mode 100644 index 0000000..6d29682 --- /dev/null +++ b/meshcot/functions.py @@ -0,0 +1,351 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# Copyright Sensors & Signals LLC https://www.snstac.com +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +"""MeshCOT Functions.""" + +import asyncio +import json +import os +import xml.etree.ElementTree as ET + +from configparser import SectionProxy +from typing import Optional, Set, Union + +import pytak +import meshcot + +__author__ = "Greg Albrecht " +__copyright__ = "Copyright Sensors & Signals LLC https://www.snstac.com" +__license__ = "Apache License, Version 2.0" + + +APP_NAME = "meshcot" + + +def create_tasks(config: SectionProxy, clitool: pytak.CLITool) -> Set[pytak.Worker,]: + """Create specific coroutine task set for this application. + + Parameters + ---------- + config : `SectionProxy` + Configuration options & values. + clitool : `pytak.CLITool` + A PyTAK Worker class instance. + + Returns + ------- + `set` + Set of PyTAK Worker classes for this application. + """ + tasks = set() + + net_queue: asyncio.Queue = asyncio.Queue() + + tasks.add(meshcot.MQTTWorker(net_queue, config)) + tasks.add(meshcot.MeshWorker(clitool.tx_queue, net_queue, config)) + + return tasks + + +def rid_op_to_cot_xml( # NOQA pylint: disable=too-many-locals,too-many-branches,too-many-statements + data: dict, + config: Union[SectionProxy, dict, None] = None, +) -> Optional[ET.Element]: + """ + Serialize Open Drone ID data as Cursor on Target. + + Parameters + ---------- + craft : `dict` + Key/Value data struct of decoded ADS-B aircraft data. + config : `configparser.SectionProxy` + Configuration options and values. + Uses config options: UID_KEY, COT_STALE, COT_HOST_ID + kown_craft : `dict` + Optional list of know craft to transform CoT data. + + Returns + ------- + `xml.etree.ElementTree.Element` + Cursor-On-Target XML ElementTree object. + """ + lat = data.get("OperatorLatitude") + lon = data.get("OperatorLongitude") + + if lat is None or lon is None: + return None + + config = config or {} + remarks_fields: list = [] + + op_id = data.get("OperatorID", "Unknown-OperatorID") + uasid = data.get("BasicID_0", "Unknown-BasicID_0") + + cot_uid: str = f"RID.{op_id}" + cot_type: str = "a-n-G" + + cot_stale: int = int(config.get("COT_STALE", pytak.DEFAULT_COT_STALE)) + cot_host_id: str = config.get("COT_HOST_ID", pytak.DEFAULT_HOST_ID) + + cotx = ET.Element("_meshcot_") + cotx.set("cot_host_id", cot_host_id) + + remarks_fields.append(f"UAS ID={uasid} OperatorID={op_id}") + cotx.set("OperatorID", op_id) + cotx.set("UASID", op_id) + callsign = op_id + + contact: ET.Element = ET.Element("contact") + contact.set("callsign", callsign) + + detail = ET.Element("detail") + detail.append(contact) + detail.append(cotx) + + remarks = ET.Element("remarks") + remarks_fields.append(f"{cot_host_id}") + _remarks = " ".join(list(filter(None, remarks_fields))) + remarks.text = _remarks + detail.append(remarks) + + cot_d = { + "lat": str(lat), + "lon": str(lon), + "ce": str(data.get("HorizAccuracy", "9999999.0")), + "le": str(data.get("VertAccuracy", "9999999.0")), + "hae": str(data.get("OperatorAltitudeGeo", "9999999.0")), + "uid": cot_uid, + "cot_type": cot_type, + "stale": cot_stale, + } + cot = pytak.gen_cot_xml(**cot_d) + cot.set("access", config.get("COT_ACCESS", pytak.DEFAULT_COT_ACCESS)) + + _detail = cot.findall("detail")[0] + flowtags = _detail.findall("_flow-tags_") + detail.extend(flowtags) + cot.remove(_detail) + cot.append(detail) + + return cot + + +def rid_uas_to_cot_xml( # NOQA pylint: disable=too-many-locals,too-many-branches,too-many-statements + data: dict, + config: Union[SectionProxy, dict, None] = None, +) -> Optional[ET.Element]: + """ + Serialize Open Drone ID data as Cursor on Target. + + Parameters + ---------- + craft : `dict` + Key/Value data struct of decoded ADS-B aircraft data. + config : `configparser.SectionProxy` + Configuration options and values. + Uses config options: UID_KEY, COT_STALE, COT_HOST_ID + kown_craft : `dict` + Optional list of know craft to transform CoT data. + + Returns + ------- + `xml.etree.ElementTree.Element` + Cursor-On-Target XML ElementTree object. + """ + lat = data.get("Latitude") + lon = data.get("Longitude") + + if lat is None or lon is None: + return None + + config = config or {} + remarks_fields: list = [] + + op_id = data.get("OperatorID", "Unknown-OperatorID") + uasid = data.get("BasicID_0", "Unknown-BasicID_0") + + cot_uid: str = f"RID.{uasid}" + cot_type: str = "a-n-A-M-F-Q" + + cot_stale: int = int(config.get("COT_STALE", pytak.DEFAULT_COT_STALE)) + cot_host_id: str = config.get("COT_HOST_ID", pytak.DEFAULT_HOST_ID) + + cotx = ET.Element("_meshcot_") + cotx.set("cot_host_id", cot_host_id) + + remarks_fields.append(f"OperatorID={op_id}") + cotx.set("OperatorID", op_id) + callsign = uasid + + contact: ET.Element = ET.Element("contact") + contact.set("callsign", callsign) + + track: ET.Element = ET.Element("track") + track.set("speed", str(data.get("SpeedHorizontal", 0))) + + link: ET.Element = ET.Element("link") + link.set("uid", op_id) + link.set("production_time", pytak.cot_time()) + link.set("type", "a-n-G") + link.set("parent_callsign", op_id) + link.set("relation", "p-p") + + detail = ET.Element("detail") + detail.append(contact) + detail.append(track) + detail.append(cotx) + detail.append(link) + + remarks = ET.Element("remarks") + remarks_fields.append(f"{cot_host_id}") + _remarks = " ".join(list(filter(None, remarks_fields))) + remarks.text = _remarks + detail.append(remarks) + + cot_d = { + "lat": str(lat), + "lon": str(lon), + "ce": str(data.get("HorizAccuracy", "9999999.0")), + "le": str(data.get("VertAccuracy", "9999999.0")), + "hae": str(data.get("AltitudeGeo", "9999999.0")), + "uid": cot_uid, + "cot_type": cot_type, + "stale": cot_stale, + } + cot = pytak.gen_cot_xml(**cot_d) + cot.set("access", config.get("COT_ACCESS", pytak.DEFAULT_COT_ACCESS)) + + _detail = cot.findall("detail")[0] + flowtags = _detail.findall("_flow-tags_") + detail.extend(flowtags) + cot.remove(_detail) + cot.append(detail) + + return cot + + +def sensor_status_to_cot( # NOQA pylint: disable=too-many-locals,too-many-branches,too-many-statements + data: dict, + config: Union[SectionProxy, dict, None] = None, +) -> Optional[ET.Element]: + """Serialize sensor status data s Cursor on Target.""" + config = config or {} + lat = config.get("SENSOR_LAT") + lon = config.get("SENSOR_LON") + alt = config.get("SENSOR_ALT", "9999999.0") + + if lat is None or lon is None: + gps_info = get_gps_info(config) + if gps_info: + print(gps_info) + else: + return None + + if lat is None or lon is None: + return None + + config = config or {} + remarks_fields: list = [] + + sensor_id = config.get("SENSOR_ID", "Unknown-SENSOR_ID") + + cot_uid: str = f"CUAS.{sensor_id}" + cot_type: str = "a-f-G-E-S" + + cot_stale: int = int(config.get("COT_STALE", pytak.DEFAULT_COT_STALE)) + cot_host_id: str = config.get("COT_HOST_ID", pytak.DEFAULT_HOST_ID) + + cotx = ET.Element("_meshcot_") + cotx.set("cot_host_id", cot_host_id) + + remarks_fields.append(f"C-UAS Sensor {sensor_id}") + cotx.set("sensor_id", sensor_id) + callsign = sensor_id + + contact: ET.Element = ET.Element("contact") + contact.set("callsign", callsign) + + track: ET.Element = ET.Element("track") + track.set("speed", str(data.get("SpeedHorizontal", 0))) + + detail = ET.Element("detail") + detail.append(contact) + detail.append(track) + detail.append(cotx) + + remarks = ET.Element("remarks") + remarks_fields.append(f"{cot_host_id}") + _remarks = " ".join(list(filter(None, remarks_fields))) + remarks.text = _remarks + detail.append(remarks) + + cot_d = { + "lat": str(lat), + "lon": str(lon), + "ce": str(data.get("HorizAccuracy", "9999999.0")), + "le": str(data.get("VertAccuracy", "9999999.0")), + "hae": str(data.get("AltitudeGeo", "9999999.0")), + "uid": cot_uid, + "cot_type": cot_type, + "stale": cot_stale, + } + cot = pytak.gen_cot_xml(**cot_d) + cot.set("access", config.get("COT_ACCESS", pytak.DEFAULT_COT_ACCESS)) + + _detail = cot.findall("detail")[0] + flowtags = _detail.findall("_flow-tags_") + detail.extend(flowtags) + cot.remove(_detail) + cot.append(detail) + + return cot + + +def xml_to_cot( + data: dict, config: Union[SectionProxy, dict, None] = None, func=None +) -> Optional[bytes]: + """Return CoT XML object as an XML string.""" + cot: Optional[ET.Element] = getattr(meshcot.functions, func)(data, config) + return ( + b"\n".join([pytak.DEFAULT_XML_DECLARATION, ET.tostring(cot)]) if cot else None + ) + + +def get_gps_info(config) -> Optional[dict]: + """Get GPS Info data.""" + gpspipe_data: Optional[str] = None + gps_data: Optional[str] = None + gps_info_cmd = config.get("GPS_INFO_CMD", meshcot.DEFAULT_GPS_INFO_CMD) + + with os.popen(gps_info_cmd) as gps_info_cmd: + gpspipe_data = gps_info_cmd.read() + + if not gpspipe_data: + return None + + if "\n" in gpspipe_data: + for data in gpspipe_data.split("\n"): + if "TPV" in data: + gps_data = data + continue + + if not gps_data: + return None + + gps_info = json.loads(gps_data) + return gps_info diff --git a/mkdocs.yml b/mkdocs.yml new file mode 100644 index 0000000..a47c078 --- /dev/null +++ b/mkdocs.yml @@ -0,0 +1,40 @@ +site_name: MeshCOT - Display Meshtastic Nodes in TAK. +site_url: https://meshcot.rtfd.io/ +repo_url: https://github.com/snstac/meshcot/ +site_description: Software for monitoring and analyzing Meshtastic data via the Team Awareness Kit (TAK) ecosystem of products. +site_author: Greg Albrecht +copyright: Copyright Sensors & Signals LLC https://www.snstac.com + +theme: + name: material + highlightjs: true + features: + - content.code.copy + - content.code.select + - content.code.annotate + +plugins: + - include-markdown: + opening_tag: "{!" + closing_tag: "!}" + - search + - mkdocstrings: + handlers: + # See: https://mkdocstrings.github.io/python/usage/ + python: + options: + docstring_style: sphinx + +markdown_extensions: + - markdown_include.include: + base_path: . + - admonition + - toc: + permalink: True + - pymdownx.highlight: + anchor_linenums: true + line_spans: __span + pygments_lang_class: true + - pymdownx.inlinehilite + - pymdownx.snippets + - pymdownx.superfences \ No newline at end of file diff --git a/requirements_test.txt b/requirements_test.txt new file mode 100644 index 0000000..9120a8a --- /dev/null +++ b/requirements_test.txt @@ -0,0 +1,5 @@ +pytest-asyncio +pytest-cov +pylint +flake8 +black diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..4497a83 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,100 @@ +# Setup configuration for MeshCOT: TK +# +# Copyright Sensors & Signals LLC https://www.snstac.com +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +[metadata] +name = meshcot +version = attr: meshcot.__version__ +url = https://github.com/snstac/meshcot +project_urls = + CI: GitHub Actions = https://github.com/snstac/meshcot/actions + GitHub: issues = https://github.com/snstac/meshcot/issues + GitHub: repo = https://github.com/snstac/meshcot +description = TK +long_description = file: README.rst +long_description_content_type = text/x-rst +maintainer = Greg Albrecht +maintainer_email = oss@undef.net +license = Apache 2.0 +license_files = LICENSE +classifiers = + License :: OSI Approved :: Apache Software License + Intended Audience :: Developers + Programming Language :: Python + Programming Language :: Python :: 3 + Programming Language :: Python :: 3 :: Only + Programming Language :: Python :: 3.9 + Programming Language :: Python :: 3.10 + Programming Language :: Python :: 3.11 + Programming Language :: Python :: 3.12 + Development Status :: 5 - Production/Stable + Operating System :: POSIX + Operating System :: MacOS :: MacOS X + Operating System :: Microsoft :: Windows + Operating System :: OS Independent +keywords = + Cursor on Target + CoT + ATAK + TAK + WinTAK + TAK + TAK Server + Aircraft + Drones + UAV + UAS + Remote ID + Open Drone ID + +[options.entry_points] +console_scripts = + meshcot = meshcot.commands:main + +[options] +packages = meshcot +package_dir = + meshcot = meshcot +python_requires = >=3.6, <4 +install_requires = + pytak >= 5.4.0 + paho-mqtt < 2.0.0 + bitstruct + pytz + asyncio_mqtt + +[options.extras_require] +with_takproto = takproto >= 2.0.0 +test = + pytest-asyncio + pytest-cov + pylint + flake8 + black + +[isort] +profile = black + +[flake8] +max-line-length = 88 +extend-ignore = E203, E704 + +[pylint] +max-line-length = 88 + +[pycodestyle] +ignore = E203 +max_line_length = 88 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..6cc616e --- /dev/null +++ b/setup.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# Copyright Sensors & Signals LLC https://www.snstac.com +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +"""Setup for MeshCOT. + +:source: +""" + +from setuptools import setup + +if __name__ == "__main__": + setup() diff --git a/tests/test_functions.py b/tests/test_functions.py new file mode 100644 index 0000000..e57c4c6 --- /dev/null +++ b/tests/test_functions.py @@ -0,0 +1,193 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# Copyright 2023 Sensors & Signals LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +"""MeshCOT Function Tests.""" + +import unittest +import xml.etree.ElementTree as etree + +import meshcot + +__author__ = "Greg Albrecht " +__copyright__ = "Copyright 2023 Sensors & Signals LLC" +__license__ = "Apache License, Version 2.0" + + +# TEST_FEED = { +# "aircraft": [ +# { +# "alt_baro": 3700, +# "alt_geom": 3750, +# "category": "A1", +# "flight": "N739UL ", +# "geom_rate": 512, +# "gs": 79.5, +# "gva": 2, +# "hex": "a9ee47", +# "lat": 37.836449, +# "lon": -122.030281, +# "messages": 34, +# "mlat": [], +# "nac_p": 10, +# "nac_v": 2, +# "nic": 9, +# "nic_baro": 0, +# "rc": 75, +# "rssi": -15.8, +# "sda": 2, +# "seen": 0.2, +# "seen_pos": 1.0, +# "sil": 3, +# "sil_type": "perhour", +# "tisb": [], +# "track": 50.1, +# "version": 2, +# "reg": "test_reg_1234", +# "squawk": "3514", +# "t": "test_craft_type_1234", +# }, +# { +# "alt_baro": 37000, +# "alt_geom": 38650, +# "baro_rate": 0, +# "gs": 487.6, +# "hex": "3c4586", +# "messages": 10, +# "mlat": [], +# "nac_v": 1, +# "rssi": -18.2, +# "seen": 17.0, +# "tisb": [], +# "track": 171.3, +# "version": 0, +# }, +# { +# "alt_baro": 39000, +# "hex": "a18b41", +# "lat": 39.455023, +# "lon": -120.402344, +# "messages": 17, +# "mlat": [], +# "nac_p": 10, +# "nav_altitude_mcp": 39008, +# "nav_modes": ["autopilot", "vnav", "tcas"], +# "nav_qnh": 1013.6, +# "nic": 8, +# "nic_baro": 1, +# "rc": 186, +# "rssi": -18.9, +# "seen": 3.2, +# "seen_pos": 12.6, +# "sil": 3, +# "sil_type": "unknown", +# "squawk": "3514", +# "tisb": [], +# "version": 0, +# }, +# { +# "alt_baro": 17650, +# "alt_geom": 18650, +# "baro_rate": -2112, +# "category": "A3", +# "flight": "SWA1241 ", +# "gs": 353.4, +# "hex": "abd994", +# "messages": 14, +# "mlat": [], +# "nac_p": 8, +# "nac_v": 1, +# "nav_altitude_mcp": 3392, +# "nav_heading": 308.0, +# "nav_qnh": 1013.6, +# "nic_baro": 1, +# "rssi": -18.1, +# "seen": 16.7, +# "sil": 2, +# "sil_type": "unknown", +# "tisb": [], +# "track": 321.1, +# "version": 0, +# }, +# ], +# "messages": 1799595081, +# "now": 1602849987.1, +# } + + +class FunctionsTestCase(unittest.TestCase): + """ + Test class for functions... functions. + """ + + def test_test(self): + assert True + + +# def test_adsb_to_cot_xml(self): +# """Test that adsb_to_cot serializses ADS-B as valid Cursor on Target XML Object.""" +# aircraft = TEST_FEED["aircraft"] +# craft = aircraft[0] +# cot = meshcot.functions.adsb_to_cot_xml(craft) +# print("COT: %s", cot) +# assert isinstance(cot, etree.Element) +# assert cot.tag == "event" +# assert cot.attrib["version"] == "2.0" +# assert cot.attrib["type"] == "a-n-A-C-F" +# assert cot.attrib["uid"] == "ICAO-A9EE47" + +# point = cot.findall("point") +# assert point[0].tag == "point" +# assert point[0].attrib["lat"] == "37.836449" +# assert point[0].attrib["lon"] == "-122.030281" +# assert point[0].attrib["hae"] == "1143.0" + +# detail = cot.findall("detail") +# assert detail[0].tag == "detail" + +# track = detail[0].findall("track") +# assert track[0].attrib["course"] == "50.1" +# assert track[0].attrib["speed"] == "40.898298000000004" + +# def test_adsb_to_cot(self): +# """Test that adsb_to_cot serializses ADS-B as valid Cursor on Target XML String.""" +# aircraft = TEST_FEED["aircraft"] +# craft = aircraft[0] +# cot = meshcot.functions.adsb_to_cot(craft) +# assert b"ICAO-A9EE47" in cot +# assert b"a-n-A-C-F" in cot +# assert b"37.836449" in cot +# assert b"-122.030281" in cot +# assert b"1143.0" in cot + +# def test_adsb_to_cot_no_lat(self): +# """Test that adsb_to_cot rejects adsb with not valid latitude.""" +# aircraft = TEST_FEED["aircraft"] +# craft = aircraft[2] +# del craft["lat"] +# cot = meshcot.functions.adsb_to_cot_xml(craft) +# assert cot is None + +# def test_adsb_to_cot_no_lon(self): +# """Test that adsb_to_cot rejects adsb with not valid longitude.""" +# aircraft = TEST_FEED["aircraft"] +# craft = aircraft[2] +# del craft["lon"] +# cot = meshcot.functions.adsb_to_cot_xml(craft) +# assert cot is None + + +if __name__ == "__main__": + unittest.main()