Skip to content

Instantly share code, notes, and snippets.

@bazilio91
Created December 23, 2013 07:16
Show Gist options
  • Save bazilio91/8092897 to your computer and use it in GitHub Desktop.
Save bazilio91/8092897 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys, MySQLdb, time
from pywws import WeatherStation
CLASS_ID = 20
OBJECT_TITLE = u'ws'
ws = WeatherStation.weather_station()
ptr = ws.current_pos()
data = ws.get_data(ptr)
db = MySQLdb.connect(host='localhost', user='root', passwd='31415', db='smarthome')
cursor = db.cursor()
# Map with properties sql table
property_map = {
'hum_out': 'relHumOutside',
'hum_in': 'relHumInside',
'abs_pressure': 'pressureRt',
'rain': 'rainfallHour',
'wind_dir': 'windDirection',
'temp_out': 'tempOutside',
'temp_in': 'tempInside',
'wind_ave': 'windAverage',
'status': 'status'
}
# get object id
cursor.execute("SELECT id FROM objects WHERE CLASS_ID=%d AND title = '%s' LIMIT 1"
% (CLASS_ID, OBJECT_TITLE))
OBJECT_ID = cursor.fetchone()[0]
def set_property(property_name, value):
PROPERTY_ID = get_property_id(property_name)
VALUE_ID = get_value_id(PROPERTY_ID)
# if value record present - update
if VALUE_ID:
cursor.execute("UPDATE pvalues SET `VALUE`='%s', `UPDATED`=NOW() WHERE ID=%d"
% (str(value), VALUE_ID))
else:
# create new value record
cursor.execute("INSERT INTO pvalue VALUES(,%d,%d,'%s', NOW())" % (
PROPERTY_ID, OBJECT_ID, value
))
def get_property_id(title):
# get object property id
cursor.execute(
"SELECT id FROM properties WHERE CLASS_ID=%d AND title = '%s' LIMIT 1" % (
CLASS_ID, title))
return cursor.fetchone()[0]
def get_value_id(PROPERTY_ID):
# get object value id
cursor.execute(
"SELECT id FROM pvalues WHERE OBJECT_ID=%d AND PROPERTY_ID = '%s' LIMIT 1" % (
OBJECT_ID, PROPERTY_ID))
return cursor.fetchone()[0]
def update_dates():
set_property('updatedTime', time.strftime('%H:%M:%S'))
set_property('updatedDate', time.strftime('%Y-%m-%d'))
for key in data:
if key not in property_map:
print 'Unknown key:', key
else:
set_property(property_map[key], data[key])
update_dates()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment