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:
Sasha MOREL 2016-05-26 16:57:48 +02:00
parent 70b99252b2
commit bf95364048
2 changed files with 100 additions and 47 deletions

View File

@ -132,6 +132,10 @@ class Common ():
self.sensors_status_value_ok = 'OK' self.sensors_status_value_ok = 'OK'
# I don't have any other value for the moment # 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): def bytes2file (self, b):
''' '''
Gives a file-like class from a Bytes Gives a file-like class from a Bytes
@ -152,6 +156,29 @@ class Common ():
r = Image.open (f) r = Image.open (f)
return (r) 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): class HomeSFR (Common):
def __init__ (self, username = None, password = None, cookies = None, debug = False, autologin = True): def __init__ (self, username = None, password = None, cookies = None, debug = False, autologin = True):
''' '''
@ -238,6 +265,13 @@ class HomeSFR (Common):
else: else:
return (False) return (False)
def do_autologin (self):
'''
Trigger the autologin
'''
if (self.autologin and self.test_login () == False):
self.login ()
def logout (self): def logout (self):
''' '''
Logs out from HomeBySFR service Logs out from HomeBySFR service
@ -264,8 +298,7 @@ class HomeSFR (Common):
Sets the detection mode 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 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.do_autologin ()
self.login ()
if mode == MODE_OFF: if mode == MODE_OFF:
m = self.mode_off m = self.mode_off
elif mode == MODE_CUSTOM: elif mode == MODE_CUSTOM:
@ -287,8 +320,7 @@ class HomeSFR (Common):
Gets the detection mode Gets the detection mode
Returns one of MODE_OFF, MODE_ON and MODE_CUSTOM constants, or None if something went wrong 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.do_autologin ()
self.login ()
r = self.base_url + self.mode_get_path r = self.base_url + self.mode_get_path
if self.DEBUG: if self.DEBUG:
print ('Getting ' + r) print ('Getting ' + r)
@ -309,8 +341,7 @@ class HomeSFR (Common):
''' '''
Returns a list of sensors' ids. Returns a list of sensors' ids.
''' '''
if (self.autologin and self.test_login () == False): self.do_autologin ()
self.login ()
r = self.base_url + self.sensors_list r = self.base_url + self.sensors_list
a = self.bytes2file (self.opener.open (r).read ()) a = self.bytes2file (self.opener.open (r).read ())
b = ET.parse (a) 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 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 The available ids can be got from the list_sensors method
''' '''
if (self.autologin and self.test_login () == False): self.do_autologin ()
self.login ()
r = Sensor (id, self.opener) r = Sensor (id, self.opener)
r.refresh () r.refresh ()
return (r) return (r)
@ -343,12 +373,21 @@ class HomeSFR (Common):
''' '''
Get a Camera object from the sensor's id Get a Camera object from the sensor's id
''' '''
self.do_autologin ()
if (self.autologin and self.test_login () == False): if (self.autologin and self.test_login () == False):
self.login () self.login ()
r = Camera (id, self.opener) r = Camera (id, self.opener)
r.refresh () r.refresh ()
return (r) 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): class Sensor (Common):
''' '''
Class used to read easily the sensors Class used to read easily the sensors
@ -367,23 +406,10 @@ class Sensor (Common):
''' '''
Gets or refresh the data for the sensor 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 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 self.sensor_dict = None
for i in b.findall (self.sensors_label): self.sensor_dict = self.get_xml_elements (r, self.sensors_label, self.sensors_label_id) [self.id]
if (i.get (self.sensors_label_id) == self.id):
self.sensor_dict = build_tree (i)
break
def get_raw (self): 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 ## Manage cameras
### Get video ### Get video
## Manage logs ## Manage logs
## Generalize the Sensor.refresh () method in the Common class
authors = ( authors = (
'Gilles "Almtesh" Émilien MOREL', 'Gilles "Almtesh" Émilien MOREL',
) )
name = 'homesfr for Python 3' name = 'homesfr for Python 3'
version = '0.12-20160525' version = '0.13-20160526'
# Settable modes # Settable modes
MODE_OFF = 0 MODE_OFF = 0
@ -128,6 +129,10 @@ class Common ():
self.sensors_status_value_ok = 'OK' self.sensors_status_value_ok = 'OK'
# I don't have any other value for the moment # 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): def bytes2file (self, b):
''' '''
Gives a file-like class from a Bytes Gives a file-like class from a Bytes
@ -148,6 +153,29 @@ class Common ():
r = Image.open (f) r = Image.open (f)
return (r) 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): class HomeSFR (Common):
def __init__ (self, username = None, password = None, cookies = None, debug = False, autologin = True): def __init__ (self, username = None, password = None, cookies = None, debug = False, autologin = True):
''' '''
@ -234,6 +262,13 @@ class HomeSFR (Common):
else: else:
return (False) return (False)
def do_autologin (self):
'''
Trigger the autologin
'''
if (self.autologin and self.test_login () == False):
self.login ()
def logout (self): def logout (self):
''' '''
Logs out from HomeBySFR service Logs out from HomeBySFR service
@ -260,8 +295,7 @@ class HomeSFR (Common):
Sets the detection mode 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 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.do_autologin ()
self.login ()
if mode == MODE_OFF: if mode == MODE_OFF:
m = self.mode_off m = self.mode_off
elif mode == MODE_CUSTOM: elif mode == MODE_CUSTOM:
@ -283,8 +317,7 @@ class HomeSFR (Common):
Gets the detection mode Gets the detection mode
Returns one of MODE_OFF, MODE_ON and MODE_CUSTOM constants, or None if something went wrong 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.do_autologin ()
self.login ()
r = self.base_url + self.mode_get_path r = self.base_url + self.mode_get_path
if self.DEBUG: if self.DEBUG:
print ('Getting ' + r) print ('Getting ' + r)
@ -305,8 +338,7 @@ class HomeSFR (Common):
''' '''
Returns a list of sensors' ids. Returns a list of sensors' ids.
''' '''
if (self.autologin and self.test_login () == False): self.do_autologin ()
self.login ()
r = self.base_url + self.sensors_list r = self.base_url + self.sensors_list
a = self.bytes2file (self.opener.open (r).read ()) a = self.bytes2file (self.opener.open (r).read ())
b = ET.parse (a) 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 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 The available ids can be got from the list_sensors method
''' '''
if (self.autologin and self.test_login () == False): self.do_autologin ()
self.login ()
r = Sensor (id, self.opener) r = Sensor (id, self.opener)
r.refresh () r.refresh ()
return (r) return (r)
@ -339,12 +370,21 @@ class HomeSFR (Common):
''' '''
Get a Camera object from the sensor's id Get a Camera object from the sensor's id
''' '''
self.do_autologin ()
if (self.autologin and self.test_login () == False): if (self.autologin and self.test_login () == False):
self.login () self.login ()
r = Camera (id, self.opener) r = Camera (id, self.opener)
r.refresh () r.refresh ()
return (r) 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): class Sensor (Common):
''' '''
Class used to read easily the sensors Class used to read easily the sensors
@ -363,23 +403,10 @@ class Sensor (Common):
''' '''
Gets or refresh the data for the sensor 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 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 self.sensor_dict = None
for i in b.findall (self.sensors_label): self.sensor_dict = self.get_xml_elements (r, self.sensors_label, self.sensors_label_id) [self.id]
if (i.get (self.sensors_label_id) == self.id):
self.sensor_dict = build_tree (i)
break
def get_raw (self): def get_raw (self):
''' '''