diff --git a/example.py b/example.py index 9049694..44b57a3 100644 --- a/example.py +++ b/example.py @@ -1,5 +1,6 @@ from pyowlet.PyOwlet import PyOwlet import sys +import time from pprint import pprint if len(sys.argv) != 3: @@ -12,6 +13,19 @@ print('\n\n\n') pyowletClient = PyOwlet(sys.argv[1], sys.argv[2]) + + print('Our client is instantiated and should have populated attributes') + + print('Baby Name:' + pyowletClient.baby_name) + print('\n\n\n') + + print('These properties will fetch new information automatically if the information is older than 15 seconds.\n') + print('You can also bypass the ratelimiting and refresh the properties manually\n') + + pyowletClient.update_properties() + + print('Or query for raw measurements individually\n') + properties = [ 'OXYGEN_LEVEL', 'HEART_RATE', @@ -24,7 +38,8 @@ 'SOCK_CONNECTION', ] + # Get individual raw properties for measure in properties: - val = pyowletClient.get_property(measure) + val = pyowletClient.get_properties(measure) print(val) - print('\n\n\n') + print('\n') diff --git a/pyowlet/PyOwlet.py b/pyowlet/PyOwlet.py index 1349b3b..2bbbc3d 100644 --- a/pyowlet/PyOwlet.py +++ b/pyowlet/PyOwlet.py @@ -7,19 +7,23 @@ logging.basicConfig(filename='pyowlet.log', level=logging.DEBUG) -class PyOwlet: +class PyOwlet(object): def __init__(self, username, password): self.auth_token = None self.expire_time = 0 + self.prop_expire_time = 0 self.username = username self.password = password self.headers = None self.auth_header = None + self.monitored_properties = [] self.auth_token = self.login(username, password) self.dsn = self.get_dsn() + self.update_properties() + def get_auth_header(self): ''' Get the auth token. If the current token has not expired, return that. @@ -47,16 +51,47 @@ def get_dsn(self): # dsn = json_data[0]['device']['dsn'] return json_data[0]['device']['dsn'] - def get_property(self, measure): + def get_properties(self, measure=None): properties_url = 'https://ads-field.aylanetworks.com/apiv1/dsns/{}/properties'.format( self.dsn) - measure_url = properties_url + '/' + measure - response = requests.get(measure_url, headers=self.get_auth_header()) - data = response.json()['property'] + if measure is not None: + properties_url = properties_url + '/' + measure + + response = requests.get(properties_url, headers=self.get_auth_header()) + data = response.json() + + if measure is not None: + return data['property'] + return data + def update_properties(self): + + data = self.get_properties() + + for value in data: + name = value['property']['name'].lower() + val = value['property']['value'] + + if name not in self.monitored_properties: + self.monitored_properties.append(name) + + self.__setattr__(name, val) + + self.prop_expire_time = time.time() + 30 + + def __getattribute__(self, attr): + + monitored = object.__getattribute__(self, 'monitored_properties') + prop_exp = object.__getattribute__(self, 'prop_expire_time') + + if attr in monitored and prop_exp <= time.time(): + self.update_properties() + + return object.__getattribute__(self, attr) + def login(self, email, password): """Logs in to the Owlet API service to get access token.