forked from MattCordell/SolarMax_PVoutputLogger
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db17487
commit 531adac
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__author__ = 'Christopher McAvaney' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <http://www.gnu.org/licenses/>. | ||
|
||
# Developed 2017 by Christopher McAvaney <[email protected]> | ||
# 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 |