-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c304b16
commit 2fe848e
Showing
6 changed files
with
176 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"modules": [ | ||
{ | ||
"name": "MQTTConverterSource", | ||
"package": "cloudbrain.modules.sources.mqtt", | ||
"options": { | ||
"mqtt_routing_key": "YOUR_ROUTING_KEY", | ||
"username": "YOUR_EMAIL", | ||
"password": "YOUR_PASSWORD" | ||
}, | ||
"publishers": [ | ||
{ | ||
"name": "PikaPublisher", | ||
"package": "cloudbrain.publishers.rabbitmq", | ||
"options": { | ||
"rabbitmq_user": "YOUR_EMAIL", | ||
"rabbitmq_pwd": "YOUR_PASSWORD" | ||
}, | ||
"base_routing_key": "some_unique_key", | ||
"metrics": [ | ||
{ | ||
"metric_name": "eeg", | ||
"num_channels": 8, | ||
"buffer_size": 10 | ||
} | ||
] | ||
} | ||
], | ||
"subscribers": [] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import json | ||
import logging | ||
import pika | ||
from uuid import uuid4 | ||
|
||
from cloudbrain.core.config import get_config | ||
from cloudbrain.core.auth import CloudbrainAuth | ||
from cloudbrain.modules.interface import ModuleInterface | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def _convert_new_chunk_to_old_chunk(new_chunk): | ||
""" | ||
Convert new chunk (i.e. with new data model) to old chunk. | ||
More on the new data model: https://github.com/NeuroJS/ | ||
eeg-stream-data-model/issues/1#issuecomment-309515243 | ||
:param new_chunk: (dict) | ||
Chunk of data with the new data model. | ||
:return old_chunk: (list of dicts) | ||
Chunk of data with the old data model | ||
""" | ||
old_chunk = [] | ||
for sample in new_chunk['chunk']: | ||
old_sample = {'timestamp': sample['timestamp']} | ||
for i in range(len(sample['data'])): | ||
old_sample['channel_%s' % i] = sample['data'][i] | ||
old_chunk.append(old_sample) | ||
return old_chunk | ||
|
||
|
||
def _get_vhost(username, password): | ||
config = get_config() | ||
auth = CloudbrainAuth(config['authUrl']) | ||
return auth.get_vhost(username, password) | ||
|
||
|
||
def _setup_mqtt_channel(username, password, host, vhost, exchange, routing_key, | ||
queue_name): | ||
credentials = pika.PlainCredentials(username, password) | ||
connection = pika.BlockingConnection( | ||
pika.ConnectionParameters(host=host, | ||
credentials=credentials, | ||
virtual_host=vhost)) | ||
channel = connection.channel() | ||
|
||
channel.exchange_declare(exchange=exchange, exchange_type='topic', | ||
durable=True) | ||
|
||
result = channel.queue_declare(queue_name, exclusive=True) | ||
queue_name = result.method.queue | ||
|
||
channel.queue_bind(exchange=exchange, | ||
queue=queue_name, | ||
routing_key=routing_key) | ||
return channel | ||
|
||
|
||
class MQTTConverterSource(ModuleInterface): | ||
""" | ||
Subscribe to MQTT topic with given routing key, convert data chunks | ||
from old data model to new, and publish on AMQP exchange. | ||
""" | ||
|
||
def __init__(self, | ||
subscribers, | ||
publishers, | ||
mqtt_routing_key, | ||
username, | ||
password): | ||
|
||
super(MQTTConverterSource, self).__init__(subscribers, publishers) | ||
_LOGGER.debug("Subscribers: %s" % self.subscribers) | ||
_LOGGER.debug("Publishers: %s" % self.publishers) | ||
|
||
self.threads = [] | ||
self.mqtt_routing_key = mqtt_routing_key | ||
self.username = username | ||
self.password = password | ||
self.exchange = 'amq.topic' | ||
vhost = _get_vhost(username, password) | ||
host = get_config()['rabbitHost'] | ||
self.queue_name = 'MQTTConverterSource-' + str(uuid4()) | ||
self.mqtt_channel = _setup_mqtt_channel(username, password, host, vhost, | ||
self.exchange, mqtt_routing_key, | ||
self.queue_name) | ||
|
||
def start(self): | ||
|
||
self.mqtt_channel.basic_consume(self.callback, | ||
queue=self.queue_name, | ||
exclusive=True, | ||
no_ack=True) | ||
|
||
self.mqtt_channel.start_consuming() | ||
|
||
def callback(self, ch, method, properties, body): | ||
new_chunk = json.loads(body) | ||
old_chunk = _convert_new_chunk_to_old_chunk(new_chunk) | ||
|
||
for publisher in self.publishers: | ||
for metric_buffer in publisher.metric_buffers.values(): | ||
metric_name = metric_buffer.name | ||
routing_key = "%s:%s" % ( | ||
publisher.base_routing_key, metric_name) | ||
publisher._rabbitmq_publish(routing_key, old_chunk) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1 @@ | ||
# Store the version here so: | ||
# 1) we don't load dependencies by storing it in __init__.py | ||
# 2) we can import it in setup.py for the same reason | ||
# 3) we can import it into your module module | ||
# | ||
# Source: https://stackoverflow.com/a/16084844 | ||
__version__ = '0.0.18' | ||
__version__ = '0.0.20' |