From 1a9249971179ff0c9c5c271adc96789d4c3c43eb Mon Sep 17 00:00:00 2001 From: Hemna Date: Fri, 3 Sep 2021 11:53:50 -0400 Subject: [PATCH] first hack at it working --- Makefile | 105 ++++++----------------- aprsd_weewx_plugin/aprsd_weewx_plugin.py | 82 ++++++++++++++++-- 2 files changed, 100 insertions(+), 87 deletions(-) diff --git a/Makefile b/Makefile index 1b33209..2395355 100644 --- a/Makefile +++ b/Makefile @@ -1,87 +1,34 @@ -.PHONY: clean clean-test clean-pyc clean-build docs help -.DEFAULT_GOAL := help +include Makefile.venv +Makefile.venv: + curl \ + -o Makefile.fetched \ + -L "https://github.com/sio/Makefile.venv/raw/v2020.08.14/Makefile.venv" + echo "5afbcf51a82f629cd65ff23185acde90ebe4dec889ef80bbdc12562fbd0b2611 *Makefile.fetched" \ + | sha256sum --check - \ + && mv Makefile.fetched Makefile.venv -define BROWSER_PYSCRIPT -import os, webbrowser, sys +all: pip dev -from urllib.request import pathname2url +.PHONY: dev +dev: venv -webbrowser.open("file://" + pathname2url(os.path.abspath(sys.argv[1]))) -endef -export BROWSER_PYSCRIPT +clean: clean-venv + rm -rf dist/* -define PRINT_HELP_PYSCRIPT -import re, sys +.PHONY: test +test: dev + tox -p all -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 +build: test + $(VENV)/python3 setup.py sdist bdist_wheel + $(VENV)/twine check dist/* -BROWSER := python -c "$$BROWSER_PYSCRIPT" +upload: build + $(VENV)/twine upload dist/*a -help: - @python -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST) +check: dev # Code format check with isort and black + tox -efmt-check + tox -epep8 -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 -fr .tox/ - rm -f .coverage - rm -fr htmlcov/ - rm -fr .pytest_cache - -lint/flake8: ## check style with flake8 - flake8 aprsd_weewx_plugin tests - -lint: lint/flake8 ## check style - -test: ## run tests quickly with the default Python - pytest - -test-all: ## run tests on every Python version with tox - tox - -coverage: ## check code coverage quickly with the default Python - coverage run --source aprsd_weewx_plugin -m pytest - coverage report -m - coverage html - $(BROWSER) htmlcov/index.html - -docs: ## generate Sphinx HTML documentation, including API docs - rm -f docs/aprsd_weewx_plugin.rst - rm -f docs/modules.rst - sphinx-apidoc -o docs/ aprsd_weewx_plugin - $(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 +fix: dev # fixes code formatting with isort and black + tox -efmt diff --git a/aprsd_weewx_plugin/aprsd_weewx_plugin.py b/aprsd_weewx_plugin/aprsd_weewx_plugin.py index 7fd803f..477d46d 100644 --- a/aprsd_weewx_plugin/aprsd_weewx_plugin.py +++ b/aprsd_weewx_plugin/aprsd_weewx_plugin.py @@ -1,4 +1,6 @@ """Main module.""" +import datetime +import json import logging import queue @@ -29,6 +31,7 @@ class WeewxMQTTPlugin(plugin.APRSDRegexCommandPluginBase): def setup(self): """Ensure that the plugin has been configured.""" try: + LOG.info("Looking for weewx.mqtt.host config entry") utils.check_config_option(self.config, ["services", "weewx", "mqtt", "host"]) self.enabled = True except Exception as ex: @@ -38,6 +41,7 @@ class WeewxMQTTPlugin(plugin.APRSDRegexCommandPluginBase): def create_threads(self): if self.enabled: + LOG.info("Creating WeewxMQTTThread") return WeewxMQTTThread( msg_queues=mqtt_queue, config=self.config, @@ -54,12 +58,58 @@ class WeewxMQTTPlugin(plugin.APRSDRegexCommandPluginBase): if self.enabled: # see if there are any weather messages in the queue. - msg = mqtt_queue.get(timeout=1) + msg = None + LOG.info("Looking for a message") + if not mqtt_queue.empty(): + msg = mqtt_queue.get(timeout=1) + else: + msg = mqtt_queue.get(timeout=30) + if not msg: return "No Weewx data" else: - temp = msg["outTemp"] - return temp + LOG.info(f"Got a message {msg}") + # Wants format of 71.5F/54.0F Wind 1@49G7 54% + if "outTemp_F" in msg: + temperature = "{:0.2f}F".format(float(msg["outTemp_F"])) + dewpoint = "{:0.2f}F".format(float(msg["dewpoint_F"])) + else: + temperature = "{:0.2f}C".format(float(msg["outTemp_C"])) + dewpoint = "{:0.2f}C".format(float(msg["dewpoint_C"])) + + wind_direction = "{:0.0f}".format(float(msg["windDir"])) + if "windSpeed_mps" in msg: + wind_speed = "{:0.0f}".format(float(msg["windSpeed_mps"])) + wind_gust = "{:0.0f}".format(float(msg["windGust_mps"])) + else: + wind_speed = "{:0.0f}".format(float(msg["windSpeed_mph"])) + wind_gust = "{:0.0f}".format(float(msg["windGust_mph"])) + + wind = "{}@{}G{}".format( + wind_speed, + wind_direction, + wind_gust, + ) + + humidity = "{:0.0f}%".format(float(msg["outHumidity"])) + + ts = int("{:0.0f}".format(float(msg["dateTime"]))) + ts = datetime.datetime.fromtimestamp(ts) + + wx = "{} {}/{} Wind {} {}".format( + ts, + temperature, + dewpoint, + wind, + humidity, + ) + LOG.debug( + "Got weather {} -- len {}".format( + wx, + len(wx), + ), + ) + return wx else: return "WeewxMQTT Not enabled" @@ -73,24 +123,40 @@ class WeewxMQTTThread(threads.APRSDThread): self.setup() def setup(self): + LOG.info("Creating mqtt client") self._mqtt_host = self.config["services"]["weewx"]["mqtt"]["host"] self._mqtt_port = self.config["services"]["weewx"]["mqtt"]["port"] - self.client = mqtt.Client(self._mqtt_host, self._mqtt_port, 60) + self._mqtt_user = self.config["services"]["weewx"]["mqtt"]["user"] + self._mqtt_pass = self.config["services"]["weewx"]["mqtt"]["password"] + LOG.info( + "Connecting to mqtt {}:XXXX@{}:{}".format( + self._mqtt_user, + self._mqtt_host, + self._mqtt_port, + ), + ) + self.client = mqtt.Client(client_id="WeewxMQTTPlugin") self.client.on_connect = self.on_connect self.client.on_message = self.on_message + self.client.connect(self._mqtt_host, self._mqtt_port, 60) + self.client.username_pw_set(username="hemna", password="ass") def on_connect(self, client, userdata, flags, rc): - LOG.info(f"Connected to MQTT {self._mqtt_host}") + LOG.info(f"Connected to MQTT {self._mqtt_host} ({rc})") client.subscribe("weather/loop") def on_message(self, client, userdata, msg): - LOG.debug("adding msg to queue") - mqtt_queue.put(msg.payload) + #LOG.info(msg.payload) + wx_data = json.loads(msg.payload) + LOG.debug(f"Got WX data {wx_data}") + mqtt_queue.put(wx_data) def stop(self): - self.client.disconnect() + LOG.info("calling disconnect") self.thread_stop = True + self.client.disconnect() def loop(self): + LOG.info("Looping bitch") self.client.loop_forever() return True