Skip to content

Commit

Permalink
updated logging management so that it will not interfere with program…
Browse files Browse the repository at this point in the history
… logging when used as a library

updated examples accordingly to provide some debug mode logging functionnality
  • Loading branch information
elric91 committed Jan 21, 2018
1 parent 18a9ca2 commit ffe61da
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 50 deletions.
12 changes: 9 additions & 3 deletions examples/async_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import serial_asyncio
import threading
from functools import partial
from pyzigate.interface import ZiGate


class AsyncSerialConnection(object):
Expand Down Expand Up @@ -41,17 +40,24 @@ def data_received(self, data):
def connection_lost(self, exc):
pass


def start_loop(loop):
loop.run_forever()
loop.close()


if __name__ == "__main__":
import logging
from pyzigate.interface import ZiGate

# Setup logging on screen, debug mode
l = logging.getLogger('zigate')
l.setLevel(logging.DEBUG)
l.addHandler(logging.StreamHandler())

# Asyncio based connection
zigate = ZiGate()

loop = asyncio.get_event_loop()
# Asyncio based connection
connection = AsyncSerialConnection(loop, zigate)

# Adding loop in a thread for testing purposes (i.e non blocking ipython console)
Expand Down
14 changes: 11 additions & 3 deletions examples/async_wifi.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import asyncio
import threading
from functools import partial
from pyzigate.interface import ZiGate


class AsyncWiFiConnection(object):
Expand Down Expand Up @@ -42,20 +41,29 @@ def data_received(self, data):
def connection_lost(self, exc):
pass


def start_loop(loop):
loop.run_forever()
loop.close()


if __name__ == "__main__":
import logging
from pyzigate.interface import ZiGate

# Setup logging on screen, debug mode
l = logging.getLogger('zigate')
l.setLevel(logging.DEBUG)
l.addHandler(logging.StreamHandler())

zigate = ZiGate()

loop = asyncio.get_event_loop()
# Asyncio based connection
zigate = ZiGate()
loop = asyncio.get_event_loop()
connection = AsyncWiFiConnection(zigate)

# Adding loop in a thread for testing purposes (i.e non blocking ipython console)
# not needed when full program is run within the event loop
t = threading.Thread(target=start_loop, args=(loop,))
t.start()

Expand Down
12 changes: 8 additions & 4 deletions examples/threaded_serial.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from pyzigate.interface import ZiGate


# Functions when used with serial & threads
class ThreadedConnection(object):
def __init__(self, device, port='/dev/ttyUSB0'):
Expand All @@ -23,8 +20,15 @@ def send(self, data):


if __name__ == "__main__":
zigate = ZiGate()
import logging
from pyzigate.interface import ZiGate

# Setup logging on screen, debug mode
l = logging.getLogger('zigate')
l.setLevel(logging.DEBUG)
l.addHandler(logging.StreamHandler())

# Thread base connection
zigate = ZiGate()
connection = ThreadedConnection(zigate)
zigate.send_data('0010')
2 changes: 2 additions & 0 deletions pyzigate/attributes_helpers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#! /usr/bin/python3
import logging
from binascii import hexlify, unhexlify
from collections import OrderedDict
from time import strftime
from .zgt_parameters import *

ZGT_LOG = logging.getLogger('zigate')

class Mixin:
"""
Expand Down
33 changes: 3 additions & 30 deletions pyzigate/interface.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#! /usr/bin/python3
import logging
from binascii import hexlify
from time import strftime
from collections import OrderedDict
import logging
from . import commands_helpers, attributes_helpers
from .zgt_parameters import *

Expand Down Expand Up @@ -38,6 +38,8 @@
b'FF02': 'Xiaomi private'
}

ZGT_LOG = logging.getLogger('zigate')


class ZiGate(commands_helpers.Mixin, attributes_helpers.Mixin):

Expand Down Expand Up @@ -549,32 +551,3 @@ def decode_data(self, data):
ZGT_LOG.debug(' - Data : {}'.format(hexlify(msg_data)))
ZGT_LOG.debug(' - RSSI : {}'.format(hexlify(data[-1:])))


# Functions when used with serial & threads
# (to be removed once examples tested)
class ThreadedConnection(object):

def __init__(self, device, port='/dev/ttyUSB0'):
import serial
import threading

self.device = device
self.cnx = serial.Serial(port, 115200, timeout=0)
self.thread = threading.Thread(target=self.read).start()
device.send_to_transport = self.send

def read(self):
while True:
bytesavailable = self.cnx.inWaiting()
if bytesavailable > 0:
self.device.read_data(self.cnx.read(bytesavailable))

def send(self, data):
self.cnx.write(data)


if __name__ == "__main__":

zigate = ZiGate()
connection = ThreadedConnection(zigate)
zigate.send_data('0010')
8 changes: 0 additions & 8 deletions pyzigate/zgt_parameters.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
#! /usr/bin/python3
# Logging
import logging
ZGT_LOG = logging.getLogger('zigate')
ZGT_LOG.setLevel(logging.DEBUG)
# Force logging output to console
_LOGSTREAM = logging.StreamHandler()
_LOGSTREAM.setLevel(logging.DEBUG)
ZGT_LOG.addHandler(_LOGSTREAM)

# states & properties
ZGT_TEMPERATURE = 'temperature'
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
setup(
name='pyzigate',
packages=['pyzigate'],
version='0.1',
version='0.1.0',
description='Interface library for ZiGate (http://zigate./fr)',
author='Frédéric HARS & Vesa YLIKYLÄ',
author_email='frederic.hars@gmail.com',
url='https://github.com/elric91/ZiGate',
download_url='https://github.com/elric91/ZiGate/archive/0.1.tar.gz',
download_url='https://github.com/elric91/ZiGate/archive/v0.1.0-beta.tar.gz',
keywords=['zigate', 'zigbee', 'python3'],
classifiers=[
'Development Status :: 4 - Beta',
Expand Down

0 comments on commit ffe61da

Please sign in to comment.