示例#1
0
    def __init__(self, url, name, user=None, passwd=None):
        Thread.__init__(self)
        self.session = load_session()
        self.should_stop = Event()
        self.url = url
        self.path = urlsplit(url).path

        headers = {}
        if user is not None:
            if passwd is None: passwd = ""
            headers['Authorization'] = "Basic " + base64.encodestring(
                "%s:%s" % (user, passwd))[:-1]

        self.http_pool = urllib3.connection_from_url(self.url, headers=headers)

        self.composite = None
        self.composite_bg = None
        self.composite_start = time()

        s = self.session
        q = s.query(SensorGroup).filter_by(name=name)
        if (q.count() != 1):
            raise SensorNotFoundError("Invalid sensor group name")
        self.group = q[0]
        q = s.query(Sensor).filter_by(group=self.group)
        if (q.count < 1):
            raise SensorNotFoundError("Sensor group contains no image sensors")
        self.sensor = q[0]
示例#2
0
    def __init__(self, url, name, user=None, passwd=None):
        Thread.__init__(self)
        self.session = load_session()
        self.should_stop = Event()
        self.url = url
        self.path = urlsplit(url).path

        headers = {}
        if user is not None:
            if passwd is None: passwd = ""
            headers['Authorization'] = "Basic " + base64.encodestring("%s:%s" % (user, passwd))[:-1]

        self.http_pool = urllib3.connection_from_url(self.url, headers=headers)

        self.composite = None
        self.composite_bg = None
        self.composite_start = time()

        s = self.session
        q = s.query(SensorGroup).filter_by(name=name)
        if(q.count() != 1):
            raise SensorNotFoundError("Invalid sensor group name")
        self.group = q[0]
        q = s.query(Sensor).filter_by(group=self.group)
        if(q.count < 1):
            raise SensorNotFoundError("Sensor group contains no image sensors")
        self.sensor = q[0]
示例#3
0
#!/usr/bin/python
"""Scrape data from Google's weather API."""

from doppelserver.models import Sensor, SensorGroup, StaticSample, load_session
from sqlalchemy.exc import IntegrityError

from lxml import etree

from dateutil import parser, tz
import urllib
import re

session = load_session()
weather_query = session.query(Sensor).join(SensorGroup).filter(
    SensorGroup.name =="cambridge google weather")
temperature_sensor = weather_query.filter(Sensor.type == "temperature").first()
humidity_sensor = weather_query.filter(Sensor.type == "humidity").first()

def parse_xml(xml):
    weather_base = xml.xpath('/xml_api_reply/weather')[0]
    time_string = weather_base.xpath('forecast_information/current_date_time/@data')[0]
    time = parser.parse(time_string).astimezone(tz.gettz('America/New_York'))
    temperature = int(weather_base.xpath('current_conditions/temp_c/@data')[0])
    humidity_string = weather_base.xpath('current_conditions/humidity/@data')[0]
    humidity = int(re.findall("(\d+)", humidity_string)[0])
    return time, temperature, humidity

if __name__ == "__main__":
    try:
        f = urllib.urlopen("http://www.google.com/ig/api?weather=Cambridge,%20MA")
        xml = etree.XML(f.read())
示例#4
0
#!/usr/bin/python
from doppelserver.models import StaticSample, load_session, HourSample
from sqlalchemy import func
import datetime

session = load_session()

def data_for_hour(start):
    end = start + datetime.timedelta(hours=1)
    # return the average of all data for an hour, starting from start
    # note the parentheses around the comparisons in the filter; this is
    # required because & binds tighter than comparison!
    return session.query(StaticSample.sensor_id, func.avg(StaticSample.data)) \
        .filter((StaticSample.time >= start) & (StaticSample.time < end)) \
        .group_by(StaticSample.sensor_id).all()

if __name__ == "__main__":
    now = datetime.datetime.now()
    last_hour = datetime.datetime.now() - datetime.timedelta(hours=1,
                                                             minutes=now.minute,
                                                             seconds=now.second,
                                                             microseconds=now.microsecond)
    try:
        for (id, datum) in data_for_hour(last_hour):
            session.add(HourSample(sensor_id=id, time=last_hour, data=datum))
    except IntegrityError:
        session.rollback()
示例#5
0
 def __init__(self, td):
     Thread.__init__(self)
     self.td = td
     self.should_stop = Event()
     self.session = load_session()
     self.last_ran = 0
示例#6
0
 def __init__(self, td):
     Thread.__init__(self)
     self.td = td
     self.should_stop = Event()
     self.session = load_session()
     self.last_ran = 0