Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Higher level interface to BLNET #16

Merged
merged 6 commits into from
Jan 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions pyblnet/blnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,55 @@ def _turn(self, digital_id, value, can_node=None):
else:
raise EnvironmentError('Can\'t set values with blnet web disabled')

def get_value(self, name=None, id=None, type='digital', ret='value', cached=None):
"""
higher level interface to get a value or mode by name or id
ret: can be 'value', 'mode'
returns the value if ret='value' or the mode if ret='mode' as first return value
and the dictionary containing name, value, id and mode as a second return value
cached: in order to prevent polling data from BLNet with every call,
the data can be fetched once and stored and passed to this function


"""
val = None
dic = None
if name is None and id is None: return val
if cached is None:
cached = self.fetch()
for key, v in cached[type].items():
if str(v['name']) == str(name) or str(v['id']) == str(id):
val = v[ret]
dic = v

return val, dic

def get_digital_value(self, name=None, id=None, cached=None):
val, dic = self.get_value(type='digital', ret='value', name=name, id=id, cached=cached)
return val, dic

def get_digital_mode(self, name=None, id=None, cached=None):
val, dic = self.get_value(type='digital', ret='mode', name=name, id=id, cached=cached)
return val, dic

def get_analog_value(self, name=None, id=None, cached=None):
val, dic = self.get_value(type='analog', ret='value', name=name, id=id, cached=cached)
return val, dic

def get_energy_value(self, name=None, id=None, cached=None):
val, dic = self.get_value(type='energy', ret='value', name=name, id=id, cached=cached)
return val, dic

def get_speed_value(self, name=None, id=None, cached=None):
val, dic = self.get_value(type='speed', ret='value', name=name, id=id, cached=cached)
return val, dic

def get_power_value(self, name=None, id=None, cached=None):
val, dic=self.get_value(type='power', ret='value', name=name, id=id, cached=cached)
return val, dic



@staticmethod
def _convert_web(values):
"""
Expand Down
17 changes: 17 additions & 0 deletions pyblnet/tests/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ def test_blnet_fetch(self):
STATE
)


def test_blnet_fetch_fine_grained(self):
""" Test fetching data in higher level class """
fetched = STATE

def scratch_tuple(type, id, ret):
return fetched[type][id][ret], fetched[type][id]

blnet = BLNET(ADDRESS, password=PASSWORD, timeout=10, use_ta=False, web_port=self.port)
self.assertEqual(blnet.get_analog_value(id=5, cached=fetched), scratch_tuple('analog', 5, 'value'))
self.assertEqual(blnet.get_analog_value(name='TSP.oben', cached=fetched), scratch_tuple('analog', 2, 'value'))
self.assertEqual(blnet.get_digital_value(id=1, cached=fetched), scratch_tuple('digital', 1, 'value'))
self.assertEqual(blnet.get_digital_mode(id=1, cached=fetched), scratch_tuple('digital', 1, 'mode'))
# test auto-fetching
self.assertEqual(blnet.get_digital_mode(id=1), scratch_tuple('digital', 1, 'mode'))

def test_blnet_turn(self):
""" Test higher level turn methods """
blnet = BLNET(ADDRESS, password=PASSWORD, timeout=10, use_ta=False, web_port=self.port)
Expand All @@ -77,6 +93,7 @@ def test_blnet_turn(self):
blnet.turn_off(1)
self.assertEqual(self.server.get_node('1'), '1')


def test_blnet_web_analog(self):
""" Test reading analog values """
self.assertEqual(
Expand Down