Skip to content

Commit

Permalink
Merge pull request #19 from HumanDynamics/features/badges_loader
Browse files Browse the repository at this point in the history
hub tool for loading badges from a csv file
  • Loading branch information
OrenLederman authored Jun 12, 2017
2 parents 42e77da + fea04df commit cc7a154
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion src/badge_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import shlex
import subprocess
import signal
import csv

import logging
import json
Expand Down Expand Up @@ -499,17 +500,26 @@ 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'
, choices=('audio', 'proximity', 'both','none'), required=False
, 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')

Expand All @@ -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
Expand All @@ -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()

Expand All @@ -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)
25 changes: 25 additions & 0 deletions src/badge_manager_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions src/badge_manager_standalone.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit cc7a154

Please sign in to comment.