From 531adac641fedf278c899500acd3a7164f624a43 Mon Sep 17 00:00:00 2001 From: Christopher McAvaney Date: Wed, 21 Oct 2020 15:59:36 +1100 Subject: [PATCH] initial commit --- Weewx/README.md | 5 ++++ Weewx/__init__.py | 1 + Weewx/weewx.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 Weewx/README.md create mode 100644 Weewx/__init__.py create mode 100644 Weewx/weewx.py diff --git a/Weewx/README.md b/Weewx/README.md new file mode 100644 index 0000000..ff60992 --- /dev/null +++ b/Weewx/README.md @@ -0,0 +1,5 @@ +Weewx +===== +Fairly simple class to interface with the Weewx database. + +Only one method to get outside temperature at this point, possibly more in the future. diff --git a/Weewx/__init__.py b/Weewx/__init__.py new file mode 100644 index 0000000..3ed0f4f --- /dev/null +++ b/Weewx/__init__.py @@ -0,0 +1 @@ +__author__ = 'Christopher McAvaney' diff --git a/Weewx/weewx.py b/Weewx/weewx.py new file mode 100644 index 0000000..3c8b370 --- /dev/null +++ b/Weewx/weewx.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python +# -* coding: utf-8 *- + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Developed 2017 by Christopher McAvaney +# Intended for own use, but could be used by anybody who is using Weewx with MySQL database. + +import MySQLdb +from MySQLdb.constants import ER + +class WeewxInfo: + db_cnx = None + + def __init__(self, weewx_user, weewx_password, weewx_host, weewx_database): + # connect to database + try: + self.db_cnx = MySQLdb.connect(user=weewx_user, passwd=weewx_password, host=weewx_host, db=weewx_database) + except MySQLdb.Error as err: + if err.args[0] == ER.ACCESS_DENIED_ERROR: + print("%s: Something is wrong with your user name or password" % (self.__class__.__name__)) + elif err.args[0] == ER.BAD_DB_ERROR: + print("%s: Database does not exist" % (self.__class__.__name__)) + else: + print("%s: %s" % (self.__class__.__name__, err)) + raise + + def getCurrentOutsideTemp(self): + db_cursor = self.db_cnx.cursor() + + query = """ + -- o = observations + -- within the last 15 minutes + select + from_unixtime(o.dateTime) day_for_timestamp, + round((o.outTemp - 32) * 5/9, 1) last_outTemp + from + archive o + where + o.dateTime >= (unix_timestamp(now()) - (15 * 60)) + order by + o.dateTime desc + limit 0,1 + """ + + db_cursor.execute(query) + data = db_cursor.fetchone() + rows = db_cursor.rowcount + db_cursor.close() + + temp = None + if rows != 0: + temp = data[1] + + return temp + + def __exit__(self, exc_type, exc_value, traceback): + self.db_cnx.close() + +# END OF FILE