diff --git a/cloudbrain/publishers/sensor_publisher.py b/cloudbrain/publishers/sensor_publisher.py index eadecd5..efa0859 100644 --- a/cloudbrain/publishers/sensor_publisher.py +++ b/cloudbrain/publishers/sensor_publisher.py @@ -8,45 +8,63 @@ _SUPPORTED_DEVICES = get_supported_devices() -def parse_args(): - parser = argparse.ArgumentParser() - - parser.add_argument('-i', '--device_id', required=True, - help="A unique ID to identify the device you are sending data from. " - "For example: 'octopicorn2015'") - parser.add_argument('-m', '--mock', action='store_true', required=False, - help="Use this flag to generate mock data for a " - "supported device name %s" % _SUPPORTED_DEVICES) - parser.add_argument('-n', '--device_name', required=True, - help="The name of the device your are sending data from. " - "Supported devices are: %s" % _SUPPORTED_DEVICES) - parser.add_argument('-c', '--cloudbrain', default=RABBITMQ_ADDRESS, - help="The address of the CloudBrain instance you are sending data to, for pika publisher.\n" - "Use " + RABBITMQ_ADDRESS + " to send data to our hosted service. \n" - "Otherwise use 'localhost' if running CloudBrain locally") - parser.add_argument('-o', '--output', default=None, - help="The named pipe you are sending data to (e.g. /tmp/eeg_pipe), for pipe publisher.\n" - "The publisher will create the pipe.\n" - "By default this is the standard output.") - - parser.add_argument('-b', '--buffer_size', default=10, - help='Size of the buffer ') - parser.add_argument('-p', '--device_port', help="Port used for OpenBCI Device.") - - parser.add_argument('-P', '--publisher', default="pika", - help="The subscriber to use to get the data.\n" - "Possible options are pika, pipe.\n" - "The default is pika.") - - - opts = parser.parse_args() - if opts.device_name == "openbci" and opts.device_port is not None: - parser.error("You have to specify a port for the OpenBCI device!") - return opts +def validate_opts(opts): + """ + validate that we've got the right options + + @param opts: (list) options to validate + @retun opts_valid: (bool) 1 if opts are valid. 0 otherwise. + """ + opts_valid = True + if (opts.device_name == "openbci") and (opts.device_port is None): + opts_valid = False + return opts_valid + + +def get_args_parser(): + parser = argparse.ArgumentParser() + + parser.add_argument('-i', '--device_id', required=True, + help="A unique ID to identify the device you are sending data from. " + "For example: 'octopicorn2015'") + parser.add_argument('-m', '--mock', action='store_true', required=False, + help="Use this flag to generate mock data for a " + "supported device name %s" % _SUPPORTED_DEVICES) + parser.add_argument('-n', '--device_name', required=True, + help="The name of the device your are sending data from. " + "Supported devices are: %s" % _SUPPORTED_DEVICES) + parser.add_argument('-c', '--cloudbrain', default=RABBITMQ_ADDRESS, + help="The address of the CloudBrain instance you are sending data to, for pika publisher.\n" + "Use " + RABBITMQ_ADDRESS + " to send data to our hosted service. \n" + "Otherwise use 'localhost' if running CloudBrain locally") + parser.add_argument('-o', '--output', default=None, + help="The named pipe you are sending data to (e.g. /tmp/eeg_pipe), for pipe publisher.\n" + "The publisher will create the pipe.\n" + "By default this is the standard output.") + + parser.add_argument('-b', '--buffer_size', default=10, + help='Size of the buffer ') + parser.add_argument('-p', '--device_port', help="Port used for OpenBCI Device.") + + parser.add_argument('-P', '--publisher', default="pika", + help="The subscriber to use to get the data.\n" + "Possible options are pika, pipe.\n" + "The default is pika.") + + return parser + + +def get_opts(): + parser = get_args_parser() + opts = parser.parse_args() + opts_valid = validate_opts(opts) + if not opts_valid: + parser.error("You have to speficy the OpenBCI port") + return opts def main(): - opts = parse_args() + opts = get_opts() mock_data_enabled = opts.mock device_name = opts.device_name device_id = opts.device_id @@ -84,7 +102,7 @@ def run(device_name='muse', if mock_data_enabled: from cloudbrain.connectors.MockConnector import MockConnector as Connector - + if publisher_type not in ['pika', 'pipe']: raise ValueError("'%s' is not a valid publisher type. Valid types are %s." % (publisher_type, "pika, pipe")) @@ -93,7 +111,7 @@ def run(device_name='muse', if publisher_type == 'pika': publishers = {metric: PikaPublisher(device_name, device_id, cloudbrain_address, metric) for metric in metrics} elif publisher_type == 'pipe': - publishers = {metric: PipePublisher(device_name, device_id, metric, pipe_name) for metric in metrics} + publishers = {metric: PipePublisher(device_name, device_id, metric, pipe_name) for metric in metrics} for publisher in publishers.values(): publisher.connect() @@ -116,6 +134,3 @@ def run(device_name='muse', if __name__ == "__main__": main() #run('muse', False, 'marion', RABBITMQ_ADDRESS) - - - diff --git a/cloudbrain/run.py b/cloudbrain/run.py index 5ddf0d7..ca03e87 100644 --- a/cloudbrain/run.py +++ b/cloudbrain/run.py @@ -2,14 +2,34 @@ import subprocess import sys -import cloudbrain.publishers.sensor_publisher +from cloudbrain.publishers.sensor_publisher import get_args_parser, run -def publish(args): - sys.argv = args or ['-h'] - cloudbrain.publishers.sensor_publisher.main() -def subscribe(args): - return NotImplemented +def publish(args): + parser = get_args_parser() + + opts = parser.parse_args(args) + + mock_data_enabled = opts.mock + device_name = opts.device_name + device_id = opts.device_id + cloudbrain_address = opts.cloudbrain + buffer_size = opts.buffer_size + device_port = opts.device_port + pipe_name = opts.output + publisher = opts.publisher + + run(device_name, + mock_data_enabled, + device_id, + cloudbrain_address, + buffer_size, + device_port, + pipe_name, + publisher) + +def subscribe(): + raise NotImplemented("Subscribe is not implemented") def parse_args(): parser = argparse.ArgumentParser() @@ -23,11 +43,11 @@ def parse_args(): help="Subscribe to data stream - For example: cloudbrain subscribe -n muse -i octopicorn") subscribe_parser.set_defaults(func=subscribe) - args, unknown = parser.parse_known_args() - args.func(unknown) + args, remaining_args = parser.parse_known_args() + args.func(remaining_args) def main(): - parse_args() + parse_args() if __name__ == "__main__": main() diff --git a/requirements.txt b/requirements.txt index 457c19a..ff0d4af 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ --e git+git@github.com:marionleborgne/cloudbrain.git@01629005fda226b36dfe8d91f97cb821c72b739a#egg=cloudbrain-make-buildable pika==0.9.14 pyliblo==0.9.2 +pyserial==2.7 wheel==0.24.0 diff --git a/setup.py b/setup.py index 89f6e68..daf73d0 100644 --- a/setup.py +++ b/setup.py @@ -13,10 +13,10 @@ def read(fname): setup(name="cloudbrain", - version="0.2.0", + version="0.2.1", description="Platform for real-time sensor data analysis and visualization.", packages=find_packages(), - install_requires=['pika', 'pyliblo'], + install_requires=['pika', 'pyliblo', 'pyserial'], include_package_data=True, long_description=read("README.md"), license='GNU Affero General Public License v3',