class CassandraSubscriber(object): """ Subscribes and writes data to a file """ def __init__(self, device_name, device_id, rabbitmq_address, metric): self.subscriber = PikaSubscriber(device_name=device_name, device_id=device_id, rabbitmq_address=rabbitmq_address, metric_name=metric) self.metric = metric self.device_name = device_name self.device_id = device_id self.num_channels = get_num_channels(device_name, metric) self.cassandra_dao = CassandraDAO() def start(self): """ Consume and write data to file :return: """ self.cassandra_dao.connect() self.subscriber.connect() self.subscriber.consume_messages(self.write_to_cassandra) def stop(self): """ Unsubscribe and close file :return: """ self.subscriber.disconnect() self.file.close_file(self.write_to_cassandra) def write_to_cassandra(self, ch, method, properties, body): buffer_content = json.loads(body) for record in buffer_content: timestamp = record["timestamp"] channel_data = [record["channel_%s" % i] for i in xrange(self.num_channels)] self.cassandra_dao.store_data(timestamp, self.device_id, self.device_name, self.metric, channel_data)
class CassandraSubscriber(object): """ Subscribes and writes data to a file """ def __init__(self, device_name, device_id, rabbitmq_address, metric): self.subscriber = PikaSubscriber(device_name=device_name, device_id=device_id, rabbitmq_address=rabbitmq_address, metric_name=metric) self.metric = metric self.device_name = device_name self.device_id = device_id self.num_channels = get_num_channels(device_name, metric) self.cassandra_dao = CassandraDAL() def start(self): """ Consume and write data to file :return: """ self.cassandra_dao.connect() self.subscriber.connect() self.subscriber.consume_messages(self.write_to_cassandra) def stop(self): """ Unsubscribe and close file :return: """ self.subscriber.disconnect() self.file.close_file(self.write_to_cassandra) def write_to_cassandra(self, ch, method, properties, body): buffer_content = json.loads(body) for record in buffer_content: timestamp = record["timestamp"] channel_data = [ record["channel_%s" % i] for i in xrange(self.num_channels) ] self.cassandra_dao.store_data(timestamp, self.device_id, self.device_name, self.metric, channel_data)
d.append(float(row['channel_5']) - float(row['channel_3'])) # print(msg['left']) # print(len(msg)) data.extend(d) data = data[-N_points-extra:] if count == 1: update_plot() count = 0 else: count += 1 eeg_subscriber = PikaSubscriber(device_name, device_id, host, metric) eeg_subscriber.connect() eeg_subscriber.consume_messages(consume_eeg) # # loop to update the data # while True: # try: # try: # x = float(s.readline().strip()) # except ValueError: # continue # data.append(x) # data = data[-100:] # # set the new data
class FileWriterSubscriber(object): """ Subscribes and writes data to a file """ def __init__(self, device_name, device_id, rabbitmq_address, metric): self.subscriber = PikaSubscriber(device_name=device_name, device_id=device_id, rabbitmq_address=rabbitmq_address, metric_name=metric) self.metric = metric self.device_name = device_name self.device_id = device_id self.csv_writer = None self.file = None self.headers = None def get_headers(self): """ Generate the CSV headers for that metric. :return: CSV headers """ num_channels = get_num_channels(self.device_name,self.metric) headers = ['timestamp'] + ['channel_%s' % i for i in xrange(num_channels)] return headers def init_file(self): """ Open file and write headers. :return: """ if not os.path.exists("data"): os.mkdir("data") file_path = os.path.join("data", "%s_%s_%s.csv" % (self.device_id, self.device_name, self.metric)) self.file = open(file_path, 'wb') self.csv_writer = csv.writer(self.file) self.csv_writer.writerow(self.headers) def start(self): """ Consume and write data to file :return: """ self.headers = self.get_headers() self.init_file() self.subscriber.connect() self.subscriber.consume_messages(self.write) def stop(self): """ Unsubscribe and close file :return: """ self.subscriber.disconnect() self.file.close_file() def write(self, ch, method, properties, body): buffer_content = json.loads(body) for record in buffer_content: self.csv_writer.writerow([record[column_name] for column_name in self.headers])