Commit 8e6b0fba authored by Florent Fourcot's avatar Florent Fourcot Committed by Romain Bignon

Change sachsen module to use ICapGauge

parent 3b26a7a6
......@@ -21,14 +21,14 @@
from __future__ import with_statement
from .browser import SachsenBrowser
from weboob.capabilities.gauge import ICapWaterLevel
from weboob.capabilities.gauge import ICapGauge, GaugeSensor, Gauge
from weboob.tools.backend import BaseBackend
__all__ = ['SachsenLevelBackend']
class SachsenLevelBackend(BaseBackend, ICapWaterLevel):
class SachsenLevelBackend(BaseBackend, ICapGauge):
NAME = 'sachsen'
MAINTAINER = u'Florent Fourcot'
EMAIL = 'weboob@flo.fourcot.fr'
......@@ -37,14 +37,47 @@ class SachsenLevelBackend(BaseBackend, ICapWaterLevel):
DESCRIPTION = u"Level of Sachsen river"
BROWSER = SachsenBrowser
def iter_gauge_history(self, id):
return self.browser.iter_history(id)
def iter_gauges(self, pattern=None):
if pattern is None:
for gauge in self.browser.get_rivers_list():
yield gauge
else:
lowpattern = pattern.lower()
for gauge in self.get_rivers_list():
if lowpattern in gauge.name.lower() or lowpattern in gauge.river.lower():
yield gauge
def get_last_measure(self, id):
return self.browser.last_seen(id)
def _get_gauge_by_id(self, id):
for gauge in self.browser.get_rivers_list():
if id == gauge.id:
return gauge
return None
def iter_gauges(self, pattern=None):
def _get_sensor_by_id(self, id):
for gauge in self.browser.get_rivers_list():
for sensor in gauge.sensors:
if id == sensor.id:
return sensor
return None
def iter_sensors(self, gauge, pattern=None):
if not isinstance(gauge, Gauge):
gauge = self._get_gauge_by_id(gauge)
if pattern is None:
return self.browser.get_rivers_list()
for sensor in gauge.sensors:
yield sensor
else:
return self.browser.search(pattern)
lowpattern = pattern.lower()
for sensor in gauge.sensors:
if lowpattern in sensor.name.lower():
yield sensor
def iter_gauge_history(self, sensor):
if not isinstance(sensor, GaugeSensor):
sensor = self._get_sensor_by_id(sensor)
return self.browser.iter_history(sensor)
def get_last_measure(self, sensor):
if not isinstance(sensor, GaugeSensor):
sensor = self._get_sensor_by_id(sensor)
return sensor.lastvalue
......@@ -45,16 +45,6 @@ class SachsenBrowser(BaseBrowser):
self.location('/de/wu/umwelt/lfug/lfug-internet/hwz/inhalt_re.html')
return self.page.get_rivers_list()
def iter_history(self, id):
self.location('/de/wu/umwelt/lfug/lfug-internet/hwz/MP/%d/index.html' % int(id))
return self.page.iter_history()
def last_seen(self, id):
self.location('/de/wu/umwelt/lfug/lfug-internet/hwz/MP/%d/index.html' % int(id))
return self.page.last_seen()
def search(self, pattern):
lowpattern = pattern.lower()
for gauge in self.get_rivers_list():
if lowpattern in gauge.name.lower() or lowpattern in gauge.river.lower():
yield gauge
def iter_history(self, sensor):
self.location('/de/wu/umwelt/lfug/lfug-internet/hwz/MP/%d/index.html' % int(sensor.gaugeid))
return self.page.iter_history(sensor)
......@@ -19,8 +19,8 @@
from datetime import datetime, date, time
from weboob.tools.browser import BasePage
from weboob.capabilities.gauge import Gauge, GaugeMeasure
from weboob.capabilities.base import NotAvailable
from weboob.capabilities.gauge import Gauge, GaugeMeasure, GaugeSensor
from weboob.capabilities.base import NotAvailable, NotLoaded
__all__ = ['ListPage', 'HistoryPage']
......@@ -32,74 +32,87 @@ class ListPage(BasePage):
data = pegel.attrib['onmouseover'].strip('pegelein(').strip(')').replace(",'", ",").split("',")
gauge = Gauge(int(data[7]))
gauge.name = unicode(data[0].strip("'"))
gauge.river = unicode(data[1])
gauge.city = gauge.name.split(' ')[0] # TODO: real regexp to remove the number
gauge.object = unicode(data[1])
sensors = []
try:
lastdate = date(*reversed([int(x) for x in data[2].split(' ')[0].split(".")]))
lasttime = time(*[int(x) for x in data[2].split(' ')[1].split(":")])
gauge.lastdate = datetime.combine(lastdate, lasttime)
except:
gauge.lastdate = NotAvailable
try:
gauge.level = float(data[3])
lastdate = datetime.combine(lastdate, lasttime)
except:
gauge.level = NotAvailable
try:
gauge.flow = float(data[4])
except:
gauge.flow = NotAvailable
lastdate = NotAvailable
bildforecast = data[5]
if bildforecast == "pf_gerade.png":
gauge.forecast = u"stable"
forecast = u"stable"
elif bildforecast == "pf_unten.png":
gauge.forecast = u"Go down"
forecast = u"Go down"
elif bildforecast == "pf_oben.png":
gauge.forecast = u"Go up"
forecast = u"Go up"
else:
gauge.forecast = NotAvailable
forecast = NotAvailable
try:
level = float(data[3])
levelsensor = GaugeSensor(gauge.id + "-level")
levelsensor.name = u"Level"
# TODO levelsensor.unit =
levelsensor.forecast = forecast
lastvalue = GaugeMeasure()
lastvalue.level = level
lastvalue.date = lastdate
# TODO lastvalue.alarm =
levelsensor.lastvalue = lastvalue
levelsensor.history = NotLoaded
levelsensor.gaugeid = gauge.id
sensors.append(levelsensor)
except:
pass
try:
flow = float(data[4])
flowsensor = GaugeSensor(gauge.id + "-flow")
flowsensor.name = u"Flow"
# TODO flowsensor.unit =
flowsensor.forecast = forecast
lastvalue = GaugeMeasure()
lastvalue.level = flow
lastvalue.date = lastdate
# TODO lastvalue.alarm =
flowsensor.lastvalue = lastvalue
flowsensor.history = NotLoaded
flowsensor.gaugeid = gauge.id
sensors.append(flowsensor)
except:
pass
gauge.sensors = sensors
yield gauge
class HistoryPage(BasePage):
def iter_history(self):
def iter_history(self, sensor):
table = self.document.getroot().cssselect('table[width="215"]')
first = True
for line in table[0].cssselect("tr"):
if first:
first = False
continue
lines = table[0].cssselect("tr")
lines.pop(0) # remove header
lines.pop(0) # remove first value (already in lastvalue)
for line in lines:
history = GaugeMeasure()
leveldate = date(*reversed([int(x) for x in line[0].text_content().split(' ')[0].split(".")]))
leveltime = time(*[int(x) for x in line[0].text_content().split(' ')[1].split(":")])
history.date = datetime.combine(leveldate, leveltime)
try:
history.level = float(line[1].text_content())
except:
history.level = NotAvailable
try:
history.flow = float(line[2].text_content())
except:
history.flow = NotAvailable
if sensor.name == u"Level":
try:
history.level = float(line[1].text_content())
except:
history.level = NotAvailable
elif sensor.name == u"Flow":
try:
history.level = float(line[2].text_content())
except:
history.level = NotAvailable
# TODO: history.alarm
yield history
def first_value(self, table, index):
first = NotAvailable
for lignes in table[0].cssselect("tr"):
try:
valeur = float(lignes[index].text_content())
if (valeur > 1.0):
first = valeur
return first
except:
continue
return first
def last_seen(self):
tables = self.document.getroot().cssselect('table[width="215"]')
gauge = GaugeMeasure()
gauge.level = self.first_value(tables, 1)
gauge.flow = self.first_value(tables, 2)
return gauge
......@@ -29,8 +29,8 @@ class SachsenTest(BackendTest):
self.assertTrue(len(l) > 0)
gauge = l[0]
history = list(self.backend.iter_gauge_history(gauge.id))
self.backend.load_gauge_history(gauge)
self.assertTrue(len(history) > 0)
self.assertTrue(len(gauge.history) > 0)
self.assertTrue(self.backend.get_last_measure(gauge.id) is not None)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment