mirror of
https://github.com/gryf/python-linak-desk-control.git
synced 2025-12-17 03:20:28 +01:00
Added logging class, adjusted argparser.
This commit is contained in:
@@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import ctypes
|
import ctypes
|
||||||
|
import logging
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
@@ -53,6 +54,55 @@ HEIGHT_MOVE_UPWARDS = 32768
|
|||||||
HEIGHT_MOVE_END = 32769
|
HEIGHT_MOVE_END = 32769
|
||||||
|
|
||||||
|
|
||||||
|
class Logger:
|
||||||
|
"""
|
||||||
|
Simple logger class with output on console only
|
||||||
|
"""
|
||||||
|
def __init__(self, logger_name):
|
||||||
|
"""
|
||||||
|
Initialize named logger
|
||||||
|
"""
|
||||||
|
self._log = logging.getLogger(logger_name)
|
||||||
|
self.setup_logger()
|
||||||
|
self._log.set_verbose = self.set_verbose
|
||||||
|
|
||||||
|
def __call__(self):
|
||||||
|
"""
|
||||||
|
Calling this object will return configured logging.Logger object with
|
||||||
|
additional set_verbose() method.
|
||||||
|
"""
|
||||||
|
return self._log
|
||||||
|
|
||||||
|
def set_verbose(self, verbose_level):
|
||||||
|
"""
|
||||||
|
Change verbosity level. Default level is warning.
|
||||||
|
"""
|
||||||
|
ver_map = {0: logging.CRITICAL,
|
||||||
|
1: logging.ERROR,
|
||||||
|
2: logging.WARNING,
|
||||||
|
3: logging.INFO,
|
||||||
|
4: logging.DEBUG}
|
||||||
|
self._log.setLevel(ver_map.get(verbose_level, ver_map[4]))
|
||||||
|
|
||||||
|
def setup_logger(self):
|
||||||
|
"""
|
||||||
|
Create setup instance and make output meaningful :)
|
||||||
|
"""
|
||||||
|
if self._log.handlers:
|
||||||
|
# need only one handler
|
||||||
|
return
|
||||||
|
|
||||||
|
console_handler = logging.StreamHandler(sys.stderr)
|
||||||
|
console_handler.set_name("console")
|
||||||
|
console_formatter = logging.Formatter("%(levelname)s: %(message)s")
|
||||||
|
console_handler.setFormatter(console_formatter)
|
||||||
|
self._log.addHandler(console_handler)
|
||||||
|
self._log.setLevel(logging.WARNING)
|
||||||
|
|
||||||
|
|
||||||
|
LOG = Logger(__name__)()
|
||||||
|
|
||||||
|
|
||||||
class Status(object):
|
class Status(object):
|
||||||
positionLost = True
|
positionLost = True
|
||||||
antiColision = True
|
antiColision = True
|
||||||
@@ -297,7 +347,7 @@ class LinakController(object):
|
|||||||
if not self._is_status_report_not_ready(buf):
|
if not self._is_status_report_not_ready(buf):
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
print('Device not ready!')
|
LOG.error('Device not ready!')
|
||||||
|
|
||||||
self._set_status_report()
|
self._set_status_report()
|
||||||
time.sleep(0.001)
|
time.sleep(0.001)
|
||||||
@@ -326,13 +376,8 @@ class LinakController(object):
|
|||||||
else:
|
else:
|
||||||
retry_count = max_retry
|
retry_count = max_retry
|
||||||
|
|
||||||
print(
|
LOG.info('Current height: %s; target height: %s; '
|
||||||
'Current height: {:d}; target height: {:d}; distance: {:d}'.format(
|
'distance: %s', r.ref1.pos, target, distance)
|
||||||
r.ref1.pos,
|
|
||||||
target,
|
|
||||||
distance
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
if retry_count == 0:
|
if retry_count == 0:
|
||||||
break
|
break
|
||||||
@@ -349,27 +394,34 @@ class LinakController(object):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
parser = argparse.ArgumentParser(description='Get the control on your desk!')
|
parser = argparse.ArgumentParser(description='Get the control on your '
|
||||||
parser.add_argument('command', choices=['move', 'height'], help='Command to execute.')
|
'desk!')
|
||||||
parser.add_argument('height', type=int, nargs='?', help='For command "move", give the destination height.')
|
subparsers = parser.add_subparsers(help='supported commands',
|
||||||
|
dest='subcommand')
|
||||||
|
subparsers.required = True
|
||||||
|
parser_status = subparsers.add_parser('status', help='Get status of the '
|
||||||
|
'device')
|
||||||
|
parser_move = subparsers.add_parser('move', help='Move to the desired '
|
||||||
|
'height')
|
||||||
|
parser_move.add_argument('height', type=int)
|
||||||
|
parser.add_argument("-v", "--verbose", help='be verbose. Adding more "v" '
|
||||||
|
'will increase verbosity', action="count", default=0)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
if args.command == 'move' and not args.height:
|
|
||||||
sys.stderr.write('Height missing in case of move!\n')
|
LOG.set_verbose(args.verbose)
|
||||||
parser.print_help()
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
co = LinakController()
|
co = LinakController()
|
||||||
try:
|
try:
|
||||||
if args.command == 'move':
|
if args.subcommand == 'move':
|
||||||
r = co.move(args.height)
|
r = co.move(args.height)
|
||||||
if r:
|
if r:
|
||||||
print('Command executed successfuly')
|
LOG.info('Command executed successfuly')
|
||||||
else:
|
else:
|
||||||
print('Command failed')
|
LOG.error('Command failed')
|
||||||
elif args.command == 'height':
|
elif args.subcommand == 'status':
|
||||||
h, hcm = co.get_height()
|
h, hcm = co.get_height()
|
||||||
print('Current height is: {:d} / {:.2f} cm'.format(h, hcm))
|
LOG.info('Current height is: %s / %.2fcm', h, hcm)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
co.close()
|
co.close()
|
||||||
raise e
|
raise e
|
||||||
|
|||||||
Reference in New Issue
Block a user