Beispiel #1
0
    def __init__(self,
                 device_name,
                 device_id,
                 metric,
                 subscriber_type="pika",
                 rabbitmq_address=None,
                 pipe_name=None):
        if subscriber_type == "pika" and rabbitmq_address is None:
            raise ValueError(
                "Pika subscriber needs to have a rabbitmq address!")

        if subscriber_type == "pika":
            self.subscriber = PikaSubscriber(device_name=device_name,
                                             device_id=device_id,
                                             rabbitmq_address=rabbitmq_address,
                                             metric_name=metric)
        elif subscriber_type == "pipe":
            self.subscriber = PipeSubscriber(device_name=device_name,
                                             device_id=device_id,
                                             metric_name=metric,
                                             pipe_name=pipe_name)
        else:
            raise ValueError(
                "'%s' is not a valid subscriber type. Valid types are %s." %
                (subscriber_type, "pika, pipe"))
        self.metric = metric
        self.device_name = device_name
        self.device_id = device_id

        self.subscriber_type = subscriber_type

        self.csv_writer = None
        self.file = None
        self.headers = None
  def __init__(self, device_name, device_id,  metric, subscriber_type="pika", rabbitmq_address=None, pipe_name=None):
    if subscriber_type == "pika" and rabbitmq_address is None:
      raise ValueError("Pika subscriber needs to have a rabbitmq address!")

    if subscriber_type == "pika":
      self.subscriber = PikaSubscriber(device_name=device_name,
                                       device_id=device_id,
                                       rabbitmq_address=rabbitmq_address,
                                       metric_name=metric)
    elif subscriber_type == "pipe":
      self.subscriber = PipeSubscriber(device_name=device_name,
                                       device_id=device_id,
                                       metric_name=metric,
                                       pipe_name=pipe_name)
    else:
      raise ValueError("'%s' is not a valid subscriber type. Valid types are %s." % (subscriber_type, "pika, pipe"))
    self.metric = metric
    self.device_name = device_name
    self.device_id = device_id

    self.subscriber_type = subscriber_type

    self.csv_writer = None
    self.file = None
    self.headers = None
Beispiel #3
0
class FileWriterSubscriber(object):
    """
  Subscribes and writes data to a file
  """
    def __init__(self,
                 device_name,
                 device_id,
                 metric,
                 subscriber_type="pika",
                 rabbitmq_address=None,
                 pipe_name=None):
        if subscriber_type == "pika" and rabbitmq_address is None:
            raise ValueError(
                "Pika subscriber needs to have a rabbitmq address!")

        if subscriber_type == "pika":
            self.subscriber = PikaSubscriber(device_name=device_name,
                                             device_id=device_id,
                                             rabbitmq_address=rabbitmq_address,
                                             metric_name=metric)
        elif subscriber_type == "pipe":
            self.subscriber = PipeSubscriber(device_name=device_name,
                                             device_id=device_id,
                                             metric_name=metric,
                                             pipe_name=pipe_name)
        else:
            raise ValueError(
                "'%s' is not a valid subscriber type. Valid types are %s." %
                (subscriber_type, "pika, pipe"))
        self.metric = metric
        self.device_name = device_name
        self.device_id = device_id

        self.subscriber_type = subscriber_type

        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.get(column_name, None) for column_name in self.headers
            ])
class FileWriterSubscriber(object):
  """
  Subscribes and writes data to a file
  """

  def __init__(self, device_name, device_id,  metric, subscriber_type="pika", rabbitmq_address=None, pipe_name=None):
    if subscriber_type == "pika" and rabbitmq_address is None:
      raise ValueError("Pika subscriber needs to have a rabbitmq address!")

    if subscriber_type == "pika":
      self.subscriber = PikaSubscriber(device_name=device_name,
                                       device_id=device_id,
                                       rabbitmq_address=rabbitmq_address,
                                       metric_name=metric)
    elif subscriber_type == "pipe":
      self.subscriber = PipeSubscriber(device_name=device_name,
                                       device_id=device_id,
                                       metric_name=metric,
                                       pipe_name=pipe_name)
    else:
      raise ValueError("'%s' is not a valid subscriber type. Valid types are %s." % (subscriber_type, "pika, pipe"))
    self.metric = metric
    self.device_name = device_name
    self.device_id = device_id

    self.subscriber_type = subscriber_type

    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.get(column_name, None) for column_name in self.headers])