summaryrefslogtreecommitdiff
path: root/heap/bin/wettermichel.py
blob: 4c0af6c7792720b5539e0e708f61da628e30d43e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
# -*- coding:utf8 -*-
import httplib
from StringIO import StringIO

from lxml import objectify

"""
from sqlalchemy import create_engine, text
from sqlalchemy.ext.sqlsoup import SqlSoup

soup = SqlSoup(
    create_engine("postgresql://ebus:ebus@10.2.2.26:5432/ebus") )
"""
def read():
    conn = httplib.HTTPConnection("www.wettermichel.de")
    conn.request("GET", "/weatherdata/params3.xml")
    result = conn.getresponse()
    assert result.status == 200, 'Site answered with Status 200'

    xml = objectify.parse( result )
    conn.close()
    return xml

def get_sensor_id(sensor_name):
    try:
        sensor_id = soup.sensor.filter(soup.sensor.name == sensor_name).one()
        return sensor_id.id
    except:
        conn = soup.connection()
        conn.execute(text("""INSERT INTO sensor(name) VALUES (:name)"""),
                     name = sensor_name)
        soup.commit()
        return soup.sensor.filter(soup.sensor.name == sensor_name).one().id

def insert(sensor_name,value_int=None, value_float=None, value_string=None):
    sql = text("""INSERT INTO value(
          timestamp, sensor_id, type, value_int, value_float, value_string)
          VALUES (
          NOW(), :sensor_id, :type, :value_int, :value_float, :value_string)""")
    conn = soup.connection()
    ttype = value_int != None and "int" or \
            value_float != None and "float" or \
            value_string != None and "string" or \
            None
    assert ttype != None, 'ttype is set'
    conn.execute(sql, sensor_id=get_sensor_id(sensor_name),
                      type=ttype,
                      value_int=value_int,
                      value_float=value_float,
                      value_string=value_string)
    soup.commit()

###############

xml = read()
for keyval in xml.xpath("/data/*"):
    if keyval.tag == "entry":
	continue

    print type(keyval), keyval.tag, keyval.text
    continue
    if isinstance(keyval, objectify.IntElement):
        insert("de.wettermichel.%s"%keyval.tag,
               value_int=int(keyval.text))
    elif isinstance(keyval, objectify.FloatElement):
        insert("de.wettermichel.%s"%keyval.tag,
               value_float=float(keyval.text))
    elif isinstance(keyval, objectify.StringElement):
	     
	pass
        insert("de.wettermichel.%s"%keyval.tag,
               value_string=keyval.text)
    else:
        print "ignore other: %s %s" % (keyval.tag, keyval.__class__)