improve logging
This commit is contained in:
parent
1427ba1623
commit
160cec7691
2 changed files with 25 additions and 6 deletions
|
@ -1,3 +1,6 @@
|
||||||
|
[qrcode_reader]
|
||||||
|
debug = true
|
||||||
|
|
||||||
[connection]
|
[connection]
|
||||||
port = /dev/ttyACM0
|
port = /dev/ttyACM0
|
||||||
|
|
||||||
|
|
|
@ -24,8 +24,17 @@ import sys
|
||||||
import time
|
import time
|
||||||
import serial
|
import serial
|
||||||
import urllib
|
import urllib
|
||||||
|
import logging
|
||||||
import requests
|
import requests
|
||||||
import configparser
|
import configparser
|
||||||
|
from requests.packages.urllib3.exceptions import InsecureRequestWarning
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger('qrcode_reader')
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
logging.getLogger('requests').setLevel(logging.WARNING)
|
||||||
|
logging.getLogger('urllib3').setLevel(logging.WARNING)
|
||||||
|
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
|
||||||
|
|
||||||
|
|
||||||
class Connector():
|
class Connector():
|
||||||
|
@ -56,7 +65,7 @@ class Connector():
|
||||||
req.raise_for_status()
|
req.raise_for_status()
|
||||||
req.connection.close()
|
req.connection.close()
|
||||||
except requests.exceptions.ConnectionError as ex:
|
except requests.exceptions.ConnectionError as ex:
|
||||||
print('unable to connect to %s: %s' % (self.login_url, ex))
|
logger.error('unable to connect to %s: %s' % (self.login_url, ex))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
def checkin(self, code):
|
def checkin(self, code):
|
||||||
|
@ -68,29 +77,34 @@ class Connector():
|
||||||
checkin_url = self.checkin_url + '?' + urllib.parse.urlencode(params)
|
checkin_url = self.checkin_url + '?' + urllib.parse.urlencode(params)
|
||||||
json = dict(self.cfg['actions'])
|
json = dict(self.cfg['actions'])
|
||||||
req = self.session.put(checkin_url, json=json)
|
req = self.session.put(checkin_url, json=json)
|
||||||
|
error = False
|
||||||
try:
|
try:
|
||||||
req.raise_for_status()
|
req.raise_for_status()
|
||||||
msg += 'ok'
|
msg += 'ok'
|
||||||
except requests.exceptions.HTTPError as ex:
|
except requests.exceptions.HTTPError as ex:
|
||||||
|
error = True
|
||||||
msg += 'error: %s' % req.json().get('message')
|
msg += 'error: %s' % req.json().get('message')
|
||||||
print(msg)
|
if not error:
|
||||||
|
logger.info(msg)
|
||||||
|
else:
|
||||||
|
logger.warning(msg)
|
||||||
req.connection.close()
|
req.connection.close()
|
||||||
|
|
||||||
|
|
||||||
def scan(port):
|
def scan(port):
|
||||||
retry = 1
|
retry = 1
|
||||||
while True:
|
while True:
|
||||||
print('waiting for connection on port %s...' % port)
|
logger.debug('waiting for connection on port %s...' % port)
|
||||||
try:
|
try:
|
||||||
ser = serial.Serial(port=port, timeout=1)
|
ser = serial.Serial(port=port, timeout=1)
|
||||||
break
|
break
|
||||||
except serial.serialutil.SerialException as ex:
|
except serial.serialutil.SerialException as ex:
|
||||||
if retry >= 20:
|
if retry >= 20:
|
||||||
print('unable to connect: %s' % ex)
|
logger.error('unable to connect: %s' % ex)
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
retry += 1
|
retry += 1
|
||||||
print('connected to %s' % port)
|
logger.info('connected to %s' % port)
|
||||||
ser_io = io.TextIOWrapper(io.BufferedRWPair(ser, ser, 1), newline='\r', line_buffering=True)
|
ser_io = io.TextIOWrapper(io.BufferedRWPair(ser, ser, 1), newline='\r', line_buffering=True)
|
||||||
while True:
|
while True:
|
||||||
line = ser_io.readline().strip()
|
line = ser_io.readline().strip()
|
||||||
|
@ -102,9 +116,11 @@ def scan(port):
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
cfg = configparser.ConfigParser()
|
cfg = configparser.ConfigParser()
|
||||||
cfg.read('qrcode_reader.ini')
|
cfg.read('qrcode_reader.ini')
|
||||||
|
if cfg['qrcode_reader'].getboolean('debug'):
|
||||||
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
connector = Connector(cfg)
|
connector = Connector(cfg)
|
||||||
try:
|
try:
|
||||||
for code in scan(port=cfg['connection']['port']):
|
for code in scan(port=cfg['connection']['port']):
|
||||||
connector.checkin(code)
|
connector.checkin(code)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print('exiting...')
|
logger.info('exiting...')
|
||||||
|
|
Loading…
Reference in a new issue