Skip to content

Commit

Permalink
Do you believe in magic (methods)?
Browse files Browse the repository at this point in the history
  • Loading branch information
craigjmidwinter committed Dec 29, 2018
1 parent 8241f00 commit e2e341c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
19 changes: 17 additions & 2 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pyowlet.PyOwlet import PyOwlet
import sys
import time
from pprint import pprint

if len(sys.argv) != 3:
Expand All @@ -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',
Expand All @@ -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')
45 changes: 40 additions & 5 deletions pyowlet/PyOwlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit e2e341c

Please sign in to comment.