Added log retrievment

Python2:
- Added the Common.get_xml_elements () for multiple usage of this routine
- Added HomeSFR.do_autologin () to trigger the autologin
- Removed autologin code in methods which have it
- Added HomeSFR.get_logs () method

Python3:
- Added the Common.get_xml_elements () for multiple usage of this routine
- Added HomeSFR.do_autologin () to trigger the autologin
- Removed autologin code in methods which have it
- Added HomeSFR.get_logs () method
This commit is contained in:
Gilles MOREL 2016-05-26 16:57:48 +02:00
parent 6facbe84f8
commit 9f2f4a06d1
2 changed files with 100 additions and 47 deletions

View File

@ -131,6 +131,10 @@ class Common ():
self.sensors_status_field = 'status'
self.sensors_status_value_ok = 'OK'
# I don't have any other value for the moment
# Logs
self.logs_path = '/getlog?page=1&nbparpage=10000' # should retrieve all available logs
self.logs_labels = 'LOG'
def bytes2file (self, b):
'''
@ -151,6 +155,29 @@ class Common ():
f = self.bytes2file (b)
r = Image.open (f)
return (r)
def get_xml_elements (self,url, label, id_label=None):
def build_tree (element):
r = {}
for i in element.getchildren ():
if i.getchildren () == []:
r.update ({i.tag: i.text})
else:
r.update ({i.tag: build_tree (i)})
return (r)
a = self.bytes2file (self.opener.open (url).read ())
a.seek (0)
b = ET.parse (a)
if id_label == None:
r = []
for i in b.findall (label):
r.append (build_tree (i))
return (tuple (r))
else:
r = {}
for i in b.findall (label):
r.update ({i.get (id_label): build_tree (i)})
return (r)
class HomeSFR (Common):
def __init__ (self, username = None, password = None, cookies = None, debug = False, autologin = True):
@ -238,6 +265,13 @@ class HomeSFR (Common):
else:
return (False)
def do_autologin (self):
'''
Trigger the autologin
'''
if (self.autologin and self.test_login () == False):
self.login ()
def logout (self):
'''
Logs out from HomeBySFR service
@ -264,8 +298,7 @@ class HomeSFR (Common):
Sets the detection mode
For the current version, use the MODE_OFF, MODE_ON and MODE_CUSTOM constants and always returns True, but raises an exception if a problem happens
'''
if (self.autologin and self.test_login () == False):
self.login ()
self.do_autologin ()
if mode == MODE_OFF:
m = self.mode_off
elif mode == MODE_CUSTOM:
@ -287,8 +320,7 @@ class HomeSFR (Common):
Gets the detection mode
Returns one of MODE_OFF, MODE_ON and MODE_CUSTOM constants, or None if something went wrong
'''
if (self.autologin and self.test_login () == False):
self.login ()
self.do_autologin ()
r = self.base_url + self.mode_get_path
if self.DEBUG:
print ('Getting ' + r)
@ -309,8 +341,7 @@ class HomeSFR (Common):
'''
Returns a list of sensors' ids.
'''
if (self.autologin and self.test_login () == False):
self.login ()
self.do_autologin ()
r = self.base_url + self.sensors_list
a = self.bytes2file (self.opener.open (r).read ())
b = ET.parse (a)
@ -324,8 +355,7 @@ class HomeSFR (Common):
Returns a Sensor object for the sensor id or None if sensor is not found
The available ids can be got from the list_sensors method
'''
if (self.autologin and self.test_login () == False):
self.login ()
self.do_autologin ()
r = Sensor (id, self.opener)
r.refresh ()
return (r)
@ -343,11 +373,20 @@ class HomeSFR (Common):
'''
Get a Camera object from the sensor's id
'''
self.do_autologin ()
if (self.autologin and self.test_login () == False):
self.login ()
r = Camera (id, self.opener)
r.refresh ()
return (r)
def get_logs (self):
'''
Return the whole logs in a form of tuple of dicts, as returned byt the site
'''
self.do_autologin ()
a = self.base_url + self.logs_path
return (self.get_xml_elements (a, self.logs_labels))
class Sensor (Common):
'''
@ -367,23 +406,10 @@ class Sensor (Common):
'''
Gets or refresh the data for the sensor
'''
def build_tree (element):
r = {}
for i in element.getchildren ():
if i.getchildren () == []:
r.update ({i.tag: i.text})
else:
r.update ({i.tag: build_tree (i)})
return (r)
r = self.base_url + self.sensors_list
a = self.bytes2file (self.opener.open (r).read ())
a.seek (0)
b = ET.parse (a)
self.sensor_dict = None
for i in b.findall (self.sensors_label):
if (i.get (self.sensors_label_id) == self.id):
self.sensor_dict = build_tree (i)
break
self.sensor_dict = self.get_xml_elements (r, self.sensors_label, self.sensors_label_id) [self.id]
def get_raw (self):
'''

View File

@ -10,12 +10,13 @@ This is a wrap aroud website, this could stop working without prior notice
## Manage cameras
### Get video
## Manage logs
## Generalize the Sensor.refresh () method in the Common class
authors = (
'Gilles "Almtesh" Émilien MOREL',
)
name = 'homesfr for Python 3'
version = '0.12-20160525'
version = '0.13-20160526'
# Settable modes
MODE_OFF = 0
@ -127,6 +128,10 @@ class Common ():
self.sensors_status_field = 'status'
self.sensors_status_value_ok = 'OK'
# I don't have any other value for the moment
# Logs
self.logs_path = '/getlog?page=1&nbparpage=10000' # should retrieve all available logs
self.logs_labels = 'LOG'
def bytes2file (self, b):
'''
@ -147,6 +152,29 @@ class Common ():
f = self.bytes2file (b)
r = Image.open (f)
return (r)
def get_xml_elements (self,url, label, id_label=None):
def build_tree (element):
r = {}
for i in element.getchildren ():
if i.getchildren () == []:
r.update ({i.tag: i.text})
else:
r.update ({i.tag: build_tree (i)})
return (r)
a = self.bytes2file (self.opener.open (url).read ())
a.seek (0)
b = ET.parse (a)
if id_label == None:
r = []
for i in b.findall (label):
r.append (build_tree (i))
return (tuple (r))
else:
r = {}
for i in b.findall (label):
r.update ({i.get (id_label): build_tree (i)})
return (r)
class HomeSFR (Common):
def __init__ (self, username = None, password = None, cookies = None, debug = False, autologin = True):
@ -234,6 +262,13 @@ class HomeSFR (Common):
else:
return (False)
def do_autologin (self):
'''
Trigger the autologin
'''
if (self.autologin and self.test_login () == False):
self.login ()
def logout (self):
'''
Logs out from HomeBySFR service
@ -260,8 +295,7 @@ class HomeSFR (Common):
Sets the detection mode
For the current version, use the MODE_OFF, MODE_ON and MODE_CUSTOM constants and always returns True, but raises an exception if a problem happens
'''
if (self.autologin and self.test_login () == False):
self.login ()
self.do_autologin ()
if mode == MODE_OFF:
m = self.mode_off
elif mode == MODE_CUSTOM:
@ -283,8 +317,7 @@ class HomeSFR (Common):
Gets the detection mode
Returns one of MODE_OFF, MODE_ON and MODE_CUSTOM constants, or None if something went wrong
'''
if (self.autologin and self.test_login () == False):
self.login ()
self.do_autologin ()
r = self.base_url + self.mode_get_path
if self.DEBUG:
print ('Getting ' + r)
@ -305,8 +338,7 @@ class HomeSFR (Common):
'''
Returns a list of sensors' ids.
'''
if (self.autologin and self.test_login () == False):
self.login ()
self.do_autologin ()
r = self.base_url + self.sensors_list
a = self.bytes2file (self.opener.open (r).read ())
b = ET.parse (a)
@ -320,8 +352,7 @@ class HomeSFR (Common):
Returns a Sensor object for the sensor id or None if sensor is not found
The available ids can be got from the list_sensors method
'''
if (self.autologin and self.test_login () == False):
self.login ()
self.do_autologin ()
r = Sensor (id, self.opener)
r.refresh ()
return (r)
@ -339,11 +370,20 @@ class HomeSFR (Common):
'''
Get a Camera object from the sensor's id
'''
self.do_autologin ()
if (self.autologin and self.test_login () == False):
self.login ()
r = Camera (id, self.opener)
r.refresh ()
return (r)
def get_logs (self):
'''
Return the whole logs in a form of tuple of dicts, as returned byt the site
'''
self.do_autologin ()
a = self.base_url + self.logs_path
return (self.get_xml_elements (a, self.logs_labels))
class Sensor (Common):
'''
@ -363,23 +403,10 @@ class Sensor (Common):
'''
Gets or refresh the data for the sensor
'''
def build_tree (element):
r = {}
for i in element.getchildren ():
if i.getchildren () == []:
r.update ({i.tag: i.text})
else:
r.update ({i.tag: build_tree (i)})
return (r)
r = self.base_url + self.sensors_list
a = self.bytes2file (self.opener.open (r).read ())
a.seek (0)
b = ET.parse (a)
self.sensor_dict = None
for i in b.findall (self.sensors_label):
if (i.get (self.sensors_label_id) == self.id):
self.sensor_dict = build_tree (i)
break
self.sensor_dict = self.get_xml_elements (r, self.sensors_label, self.sensors_label_id) [self.id]
def get_raw (self):
'''