diff --git a/carom/cli.py b/carom/cli.py index 5d82c43..d06cd4d 100644 --- a/carom/cli.py +++ b/carom/cli.py @@ -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() diff --git a/carom/table.py b/carom/table.py index a8b9eb1..cc14d8f 100644 --- a/carom/table.py +++ b/carom/table.py @@ -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') diff --git a/carom/utils.py b/carom/utils.py index cfbe019..eb62e70 100644 --- a/carom/utils.py +++ b/carom/utils.py @@ -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, + ) +