Skip to content
This repository has been archived by the owner on Jun 8, 2022. It is now read-only.

Commit

Permalink
Make functions async to not block main processes.
Browse files Browse the repository at this point in the history
Bump version to 0.7
  • Loading branch information
ksya committed Apr 4, 2020
1 parent 647b1ea commit 8544127
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
10 changes: 5 additions & 5 deletions aionefit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ def __init__(self, serial_number, access_key, password,
encryption=self.encryption,
nefit_client=self)

def connect(self):
async def connect(self):
self.xmppclient.connect()

def disconnect(self):
async def disconnect(self):
self.xmppclient.disconnect()

def raw_message_callback(self, msg, source):
async def raw_message_callback(self, msg, source):
decodeSuccess = False
if msg['type'] in ('chat', 'normal'):
headers = msg['body'].split("\n")[:-1]
Expand All @@ -76,7 +76,7 @@ def raw_message_callback(self, msg, source):
try:
data = json.loads(response)
if self.message_callback:
self.message_callback(data)
await self.message_callback(data)
except json.decoder.JSONDecodeError as e:
_LOGGER.error('Error parsing message %s', msg)
_LOGGER.error(e)
Expand All @@ -92,7 +92,7 @@ def raw_message_callback(self, msg, source):
if source == 'message':
_LOGGER.error('Error decrypting message. Password incorrect?')
if self.failed_auth_handler:
self.failed_auth_handler('auth_error_password')
await self.failed_auth_handler('auth_error_password')

def get(self, path):
"""Construct a "GET command"
Expand Down
47 changes: 23 additions & 24 deletions aionefit/provider/slixmpp_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,15 @@ def __init__(self, jid, password, encryption, nefit_client=None):
# set the various callback handlers
self.add_event_handler('session_start', self.session_start)
self.add_event_handler('session_end', self.session_end)
self.add_event_handler('failed_auth', self.on_failed_auth)
self.add_event_handler('auth_success', self.on_auth_success)
self.add_event_handler('message', self.message_callback)
self.add_event_handler('carbon_received', self.carbonmsg_recv_callblack)
self.add_event_handler('carbon_sent', self.carbonmsg_sent_callblack)
self.add_event_handler('failed_auth', self.on_failed_auth)
self.add_event_handler('auth_success', self.on_auth_success)
self.register_plugin('xep_0199') #ping
self.register_plugin('xep_0280') #carbons
self.encryption = encryption


def on_failed_auth(self, event):
"""Callback handler for an unsuccessfull authentication.
"""
if self.nefit_client.failed_auth_handler:
self.nefit_client.failed_auth_handler(event)
else:
_LOGGER.error('failed_auth event: %s', event)
raise SystemError('Invalid login. Check credentials ' +
'(serial_number, access_key).')

def on_auth_success(self, event):
"""Callback handler for a successfull authentication.
"""
_LOGGER.debug('auth_success event: %s', event)

def session_start(self, event):
"""Callback handler for the session start.
"""
Expand All @@ -56,24 +40,39 @@ def session_start(self, event):
self['xep_0280'].enable()
self.connected_event.set()

def session_end(self, event):
async def session_end(self, event):
_LOGGER.debug("Connection was closed")

if self.nefit_client.session_end_callback:
self.nefit_client.session_end_callback()
await self.nefit_client.session_end_callback()

self.disconnected_event.set()

def message_callback(self, msg):
async def on_failed_auth(self, event):
"""Callback handler for an unsuccessfull authentication.
"""
if self.nefit_client.failed_auth_handler:
await self.nefit_client.failed_auth_handler(event)
else:
_LOGGER.error('failed_auth event: %s', event)
raise SystemError('Invalid login. Check credentials ' +
'(serial_number, access_key).')

def on_auth_success(self, event):
"""Callback handler for a successfull authentication.
"""
_LOGGER.debug('auth_success event: %s', event)

async def message_callback(self, msg):
"""Callback handler for a received message.
"""
self.nefit_client.raw_message_callback(msg, 'message')
await self.nefit_client.raw_message_callback(msg, 'message')
self.message_event.set()

def carbonmsg_recv_callblack(self, msg):
async def carbonmsg_recv_callblack(self, msg):
"""Callback handler for a received carbon message.
"""
self.nefit_client.raw_message_callback(msg['carbon_received'], 'carbon')
await self.nefit_client.raw_message_callback(msg['carbon_received'], 'carbon')

def carbonmsg_sent_callblack(self, msg):
"""Callback handler for a sent carbon message.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
EMAIL = 'marconfus@gmail.com'
AUTHOR = 'Marco Reichwald'
REQUIRES_PYTHON = '>=3.5.0'
VERSION = "0.6"
VERSION = "0.7"

# What packages are required for this module to be executed?
REQUIRED = [
Expand Down

0 comments on commit 8544127

Please sign in to comment.