diff --git a/README.md b/README.md index c8a1ea8..3ec919d 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,28 @@ docker-compose up -d ## Deployment as a swarm TBD +# Other useful commands +## Loading badges from a CSV files +You can now load a list of badges from a CSV files of the following structure: user name,email address,badge mac . The +files should have no header row. + +This command must be executed from a hub that is recognizable by the server, and the file needs to be copied to the hub +so it is recognized. therefore, you have two options: +1. Running a dev hub, and copying the file to a directoy recognized by the hub. For example, if you copy it to the +config folder, you can run: + ``` +docker-compose -f dev_ubuntu.yml run openbadge-hub-py -m server load_badges -f ../config/badges_to_load.csv + ``` +2. Another option is to run the command from a production hub. In this case, you can use 'docker cp' to copy the file +into the container, and then run the load_badges command. For example: +``` +docker-compose up # if not already up +docker cp config/badges_to_load.csv `docker-compose ps -q openbadge-hub-py`:/config/badges_to_load.csv +docker-compose down +docker-compose run openbadge-hub-py -m server load_badges -f ../config/badges_to_load.csv +docker-compose up -d +``` + # MISC ## Updating BlueZ One of the main requirements for the hub is the BlueZ library. Raspbian and Ubuntu already have BlueZ installed, but it diff --git a/src/badge_hub.py b/src/badge_hub.py index 640b685..f2c1509 100755 --- a/src/badge_hub.py +++ b/src/badge_hub.py @@ -7,6 +7,7 @@ import shlex import subprocess import signal +import csv import logging import json @@ -499,10 +500,18 @@ def start_all_devices(mgr): else: logger.info("No need to start {}".format(device['mac'])) - time.sleep(2) # allow BLE time to disconnect +def load_badges(mgr, csv_file_name): + logger.info("Loading badged from: {}".format(csv_file_name)) + with open(csv_file_name, 'r') as csvfile: + badgereader = csv.reader(csvfile, delimiter=',') + for row in badgereader: + logger.info("Asking to create badge: {}".format(row)) + mgr.create_badge(row[0],row[1],row[2]) + + def add_pull_command_options(subparsers): pull_parser = subparsers.add_parser('pull', help='Continuously pull data from badges') pull_parser.add_argument('-r','--start_recording' @@ -510,6 +519,7 @@ def add_pull_command_options(subparsers): , default='both' , dest='start_recording',help='data recording option') + def add_scan_command_options(subparsers): pull_parser = subparsers.add_parser('scan', help='Continuously scan for badges') @@ -522,6 +532,12 @@ def add_start_all_command_options(subparsers): st_parser = subparsers.add_parser('start_all', help='Start recording on all devices in whitelist') +def add_load_badges_command_options(subparsers): + lb_parser = subparsers.add_parser('load_badges', help='Loads badges from a CSVfile') + lb_parser.add_argument('-f', '--csv_file', required=True + , type=str + , help='Badges CSV file to load. Structure: name,email,mac_address') + if __name__ == "__main__": import time import argparse @@ -540,6 +556,7 @@ def add_start_all_command_options(subparsers): add_scan_command_options(subparsers) add_sync_all_command_options(subparsers) add_start_all_command_options(subparsers) + add_load_badges_command_options(subparsers) args = parser.parse_args() @@ -562,4 +579,7 @@ def add_start_all_command_options(subparsers): if args.mode == "start_all": start_all_devices(mgr) + if args.mode == "load_badges": + load_badges(mgr, args.csv_file) + exit(0) diff --git a/src/badge_manager_server.py b/src/badge_manager_server.py index 3ef997a..0130343 100644 --- a/src/badge_manager_server.py +++ b/src/badge_manager_server.py @@ -173,6 +173,31 @@ def send_badge(self, mac): except Exception as e: self.logger.error('Error sending updated badge into to server: {}'.format(e)) + def create_badge(self, name, email, mac): + """ + Creates a badge using the giving information + :param name: user name + :param email: user email + :param mac: badge mac + :return: + """ + try: + data = { + 'name': name, + 'email': email, + 'badge': mac, + } + + self.logger.info("Creating new badge : {}".format(data)) + response = requests.post(BADGES_ENDPOINT, data=data, headers=request_headers()) + if response.ok is False: + s = traceback.format_exc() + raise Exception('Error creating badge {}. Status: {}, Error: {}, {}'.format(data, response.status_code, + response.text, s)) + except Exception as e: + s = traceback.format_exc() + self.logger.error('Error creating new badge. Error: {} ,{}'.format(e,s)) + @property def badges(self): if self._badges is None: diff --git a/src/badge_manager_standalone.py b/src/badge_manager_standalone.py index 4b83ab1..162fefb 100644 --- a/src/badge_manager_standalone.py +++ b/src/badge_manager_standalone.py @@ -86,6 +86,18 @@ def send_badge(self, mac): """ pass # not implemented in standalone + def create_badge(self, name, email, mac): + """ + Creates a badge using the giving information + :param name: user name + :param email: user email + :param mac: badge mac + :return: + """ + self.logger.debug("Command 'create_badge' is not implemented for standalone mode'") + pass # not implemented in standalone + + @property def badges(self): if self._badges is None: