Commands more failsafe for invalid configuration

This commit is contained in:
Alexander Werner 2018-11-26 23:37:50 +01:00
parent a2a33aedb4
commit 6dd6e09516
3 changed files with 49 additions and 11 deletions

View File

@ -1,5 +1,3 @@
import uuid
import click
from click_didyoumean import DYMGroup
import click_log
@ -7,7 +5,7 @@ import logging
from terminaltables import SingleTable
from carom.table import cleanup
from carom.table import get_tables, cleanup
logger = logging.getLogger('carom.cli') # hard coded module name to support directly called module
root_logger = logging.getLogger('')
@ -25,13 +23,12 @@ def get_gpio():
"""
Get the current status of all Tables.
"""
from carom.table import tables
table_data = [("Table #", "Port", "in use")]
for table in tables:
for table in get_tables():
table_data.append((table.num, table.port, table.in_use))
st = SingleTable(table_data)
st.title = "Tables"
print(st.table)
click.echo(st.table)
@ -40,7 +37,8 @@ def get_uuid():
"""
Get the UUID of the Client. This is need to add a new client to the carom server installation.
"""
click.echo(uuid.uuid5(uuid.NAMESPACE_DNS, str(uuid.getnode())))
from carom.utils import get_uuid as util_get_uuid
click.echo(util_get_uuid())
@cli.command()

View File

@ -21,7 +21,6 @@ class Table:
def __del__(self):
self.close()
@property
def in_use(self):
return self.button.is_active
@ -32,14 +31,19 @@ class Table:
def cleanup():
for table in tables:
for table in get_tables():
table.close()
def _populate_tables():
if config is None:
return []
for num, port in config['mapping'].items():
yield Table(num, port)
tables = [table for table in _populate_tables()]
def get_tables():
return [table for table in _populate_tables()]
__all__ = ('Table', 'tables', 'cleanup')
__all__ = ('Table', 'get_tables', 'cleanup')

View File

@ -1,3 +1,17 @@
import json
from datetime import datetime
import logging
import uuid
import requests
from carom.config import config
logger = logging.getLogger(__name__)
def singleton(the_class):
""" decorator for a class to make a singleton out of it """
class_instances = {}
@ -11,3 +25,25 @@ def singleton(the_class):
return class_instances[key]
return get_instance
def get_uuid():
return uuid.uuid5(uuid.NAMESPACE_DNS, str(uuid.getnode()))
def send_to_server(state, pin):
d = datetime.now()
url = config['connection']['endpoint'].format('billard/api/v1/locationdata/')
headers = {'Authorization': 'Token {}'.format(config['connection']['token']),
'Content-Type': 'application/json'}
payload = {'client_id': str(get_uuid()),
'desk_no': str(pin),
'tst': d.strftime('%Y-%m-%dT%H:%M:%S%Z'),
'on_off': state}
logger.info("Send Status: {}, {}, {}".format(str(url), str(headers), str(payload)))
requests.post(
url,
data=json.dumps(payload),
headers=headers,
)