From e9ed6bf9b472f70fe86bfb2406f6b8c08fbba31a Mon Sep 17 00:00:00 2001 From: reedts Date: Thu, 17 May 2018 14:19:05 +0200 Subject: [PATCH] Fixed database backend --- pywatts/{models.py => db.py} | 9 +++++++++ pywatts/fetchdata.py | 32 ++++++++++++++++---------------- 2 files changed, 25 insertions(+), 16 deletions(-) rename pywatts/{models.py => db.py} (73%) diff --git a/pywatts/models.py b/pywatts/db.py similarity index 73% rename from pywatts/models.py rename to pywatts/db.py index 141a2a8..f505ecb 100644 --- a/pywatts/models.py +++ b/pywatts/db.py @@ -1,5 +1,8 @@ from peewee import * from playhouse import sqlite_ext +from playhouse.sqlite_ext import SqliteExtDatabase + +db = SqliteExtDatabase('pywatts.db') class WeatherStation(Model): @@ -11,6 +14,9 @@ class WeatherStation(Model): state = CharField() id = CharField(unique=True) + class Meta: + database = db + class Result(Model): station = ForeignKeyField(WeatherStation) @@ -18,3 +24,6 @@ class Result(Model): temperature = sqlite_ext.JSONField() wind_speed = sqlite_ext.JSONField() + class Meta: + database = db + diff --git a/pywatts/fetchdata.py b/pywatts/fetchdata.py index 3b663ff..a75f7c0 100644 --- a/pywatts/fetchdata.py +++ b/pywatts/fetchdata.py @@ -1,10 +1,8 @@ from pypvwatts import PVWatts -from pywatts.models import * +from pywatts.db import * from pathlib import Path -db = SqliteDatabase('pywatts.db') - def fetch_data(from_lon, to_lon, from_lat, to_lat, step_size=1): my_api_key = '' @@ -28,28 +26,30 @@ def fetch_data(from_lon, to_lon, from_lat, to_lat, step_size=1): return results -def store_data(db_name, result_list): +def store_data(result_list): db.connect() - db.create_tables([WeatherStation, Result]) + + if not db.table_exists(WeatherStation): + db.create_tables([WeatherStation, Result]) for result in result_list: - if WeatherStation.select().where(WeatherStation.id == result.location).exists(): + if WeatherStation.select().where(WeatherStation.id == result.station_info['location']).exists(): print("Data for station %s already in database. Skipping." % result.station_info.location) continue station = WeatherStation.create( - longitude=result.station_info.lon, - latitude=result.station_info.lat, - location=result.station_info.location, - elevation=result.station_info.elevation, - city=result.station_info.city, - state=result.station_info.state, - id=result.station_info.location, + longitude=result.station_info['lon'], + latitude=result.station_info['lat'], + location=result.station_info['location'], + elevation=result.station_info['elev'], + city=result.station_info['city'], + state=result.station_info['state'], + id=result.station_info['location'], ) Result.create( station=station, - dc_output=result.outputs.dc, - temperature=result.outputs.tamb, - wind_speed=result.outputs.wspd + dc_output=result.outputs['dc'], + temperature=result.outputs['tamb'], + wind_speed=result.outputs['wspd'] )