#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Standart, System and Third party
import sqlalchemy as db
from sqlalchemy.sql import func

from http.client import HTTPSConnection
from base64 import b64encode
from threading import Timer

# ROS
import rospy


class CollectorNode:

    def __init__(self):
        rospy.init_node('collector')
        rospy.loginfo('Launching collector node...')

        # Init DB
        engine = db.create_engine(rospy.get_param('~db_url')[:-1])
        self.connection = engine.connect()
        self.solar = self.__init_table(engine, rospy.get_param('~table_name'))

        self.rest_api_key = rospy.get_param('~rest_api_key')[:-1]

        rospy.loginfo('Collector node is ready!')

    def __init_table(self, engine, table_name):
        metadata = db.MetaData()
        if not engine.dialect.has_table(engine, table_name):
            rospy.loginfo("Initialazing DB with a table name '{}'".format(table_name))

            solar = db.Table(table_name, metadata,
                             db.Column('id', db.Integer, primary_key=True),
                             db.Column('measure', db.TEXT),
                             db.Column('time_stamp', db.TIMESTAMP(True), server_default=func.now()))
            metadata.create_all(engine)
            return solar
        return db.Table(table_name, metadata, autoload=True, autoload_with=engine)

    def spin(self):
        def collect_data():
            c = HTTPSConnection("rest01.alphaopen.com")
            userAndPass = b64encode(
                bytes(self.rest_api_key, 'utf-8')).decode("ascii")
            headers = {'Authorization': 'Basic %s' % userAndPass}

            rospy.loginfo("Sending a request...")
            try:
                c.request('GET', '/api/v1/object/', headers=headers)
                # get the response back
                res = c.getresponse()

                if res.status == 200:
                    # at this point you could check the status etc
                    # this gets the page text
                    data = res.read().decode("utf-8")

                    rospy.loginfo("Recieved the data: {}...".format(data[:30]))

                    ins = db.insert(self.solar).values(measure=data)
                    res = self.connection.execute(ins)
            except:
                rospy.logerr("[Collector] Got an error. Retrying...")
            finally:
                rospy.loginfo("[Collector] Waiting 60 sec")
                Timer(60, collect_data).start()

        # Commented for test purspose
        collect_data()
        rospy.spin()


if __name__ == '__main__':
    CollectorNode().spin()

7f8e87f3e13c79fab1bbf1143ec40ad50ebda7de
<mask token>


class CollectorNode:
    <mask token>

    def __init_table(self, engine, table_name):
        metadata = db.MetaData()
        if not engine.dialect.has_table(engine, table_name):
            rospy.loginfo("Initialazing DB with a table name '{}'".format(
                table_name))
            solar = db.Table(table_name, metadata, db.Column('id', db.
                Integer, primary_key=True), db.Column('measure', db.TEXT),
                db.Column('time_stamp', db.TIMESTAMP(True), server_default=
                func.now()))
            metadata.create_all(engine)
            return solar
        return db.Table(table_name, metadata, autoload=True, autoload_with=
            engine)

    def spin(self):

        def collect_data():
            c = HTTPSConnection('rest01.alphaopen.com')
            userAndPass = b64encode(bytes(self.rest_api_key, 'utf-8')).decode(
                'ascii')
            headers = {'Authorization': 'Basic %s' % userAndPass}
            rospy.loginfo('Sending a request...')
            try:
                c.request('GET', '/api/v1/object/', headers=headers)
                res = c.getresponse()
                if res.status == 200:
                    data = res.read().decode('utf-8')
                    rospy.loginfo('Recieved the data: {}...'.format(data[:30]))
                    ins = db.insert(self.solar).values(measure=data)
                    res = self.connection.execute(ins)
            except:
                rospy.logerr('[Collector] Got an error. Retrying...')
            finally:
                rospy.loginfo('[Collector] Waiting 60 sec')
                Timer(60, collect_data).start()
        collect_data()
        rospy.spin()


<mask token>

<mask token>


class CollectorNode:

    def __init__(self):
        rospy.init_node('collector')
        rospy.loginfo('Launching collector node...')
        engine = db.create_engine(rospy.get_param('~db_url')[:-1])
        self.connection = engine.connect()
        self.solar = self.__init_table(engine, rospy.get_param('~table_name'))
        self.rest_api_key = rospy.get_param('~rest_api_key')[:-1]
        rospy.loginfo('Collector node is ready!')

    def __init_table(self, engine, table_name):
        metadata = db.MetaData()
        if not engine.dialect.has_table(engine, table_name):
            rospy.loginfo("Initialazing DB with a table name '{}'".format(
                table_name))
            solar = db.Table(table_name, metadata, db.Column('id', db.
                Integer, primary_key=True), db.Column('measure', db.TEXT),
                db.Column('time_stamp', db.TIMESTAMP(True), server_default=
                func.now()))
            metadata.create_all(engine)
            return solar
        return db.Table(table_name, metadata, autoload=True, autoload_with=
            engine)

    def spin(self):

        def collect_data():
            c = HTTPSConnection('rest01.alphaopen.com')
            userAndPass = b64encode(bytes(self.rest_api_key, 'utf-8')).decode(
                'ascii')
            headers = {'Authorization': 'Basic %s' % userAndPass}
            rospy.loginfo('Sending a request...')
            try:
                c.request('GET', '/api/v1/object/', headers=headers)
                res = c.getresponse()
                if res.status == 200:
                    data = res.read().decode('utf-8')
                    rospy.loginfo('Recieved the data: {}...'.format(data[:30]))
                    ins = db.insert(self.solar).values(measure=data)
                    res = self.connection.execute(ins)
            except:
                rospy.logerr('[Collector] Got an error. Retrying...')
            finally:
                rospy.loginfo('[Collector] Waiting 60 sec')
                Timer(60, collect_data).start()
        collect_data()
        rospy.spin()


<mask token>

<mask token>


class CollectorNode:

    def __init__(self):
        rospy.init_node('collector')
        rospy.loginfo('Launching collector node...')
        engine = db.create_engine(rospy.get_param('~db_url')[:-1])
        self.connection = engine.connect()
        self.solar = self.__init_table(engine, rospy.get_param('~table_name'))
        self.rest_api_key = rospy.get_param('~rest_api_key')[:-1]
        rospy.loginfo('Collector node is ready!')

    def __init_table(self, engine, table_name):
        metadata = db.MetaData()
        if not engine.dialect.has_table(engine, table_name):
            rospy.loginfo("Initialazing DB with a table name '{}'".format(
                table_name))
            solar = db.Table(table_name, metadata, db.Column('id', db.
                Integer, primary_key=True), db.Column('measure', db.TEXT),
                db.Column('time_stamp', db.TIMESTAMP(True), server_default=
                func.now()))
            metadata.create_all(engine)
            return solar
        return db.Table(table_name, metadata, autoload=True, autoload_with=
            engine)

    def spin(self):

        def collect_data():
            c = HTTPSConnection('rest01.alphaopen.com')
            userAndPass = b64encode(bytes(self.rest_api_key, 'utf-8')).decode(
                'ascii')
            headers = {'Authorization': 'Basic %s' % userAndPass}
            rospy.loginfo('Sending a request...')
            try:
                c.request('GET', '/api/v1/object/', headers=headers)
                res = c.getresponse()
                if res.status == 200:
                    data = res.read().decode('utf-8')
                    rospy.loginfo('Recieved the data: {}...'.format(data[:30]))
                    ins = db.insert(self.solar).values(measure=data)
                    res = self.connection.execute(ins)
            except:
                rospy.logerr('[Collector] Got an error. Retrying...')
            finally:
                rospy.loginfo('[Collector] Waiting 60 sec')
                Timer(60, collect_data).start()
        collect_data()
        rospy.spin()


if __name__ == '__main__':
    CollectorNode().spin()

import sqlalchemy as db
from sqlalchemy.sql import func
from http.client import HTTPSConnection
from base64 import b64encode
from threading import Timer
import rospy


class CollectorNode:

    def __init__(self):
        rospy.init_node('collector')
        rospy.loginfo('Launching collector node...')
        engine = db.create_engine(rospy.get_param('~db_url')[:-1])
        self.connection = engine.connect()
        self.solar = self.__init_table(engine, rospy.get_param('~table_name'))
        self.rest_api_key = rospy.get_param('~rest_api_key')[:-1]
        rospy.loginfo('Collector node is ready!')

    def __init_table(self, engine, table_name):
        metadata = db.MetaData()
        if not engine.dialect.has_table(engine, table_name):
            rospy.loginfo("Initialazing DB with a table name '{}'".format(
                table_name))
            solar = db.Table(table_name, metadata, db.Column('id', db.
                Integer, primary_key=True), db.Column('measure', db.TEXT),
                db.Column('time_stamp', db.TIMESTAMP(True), server_default=
                func.now()))
            metadata.create_all(engine)
            return solar
        return db.Table(table_name, metadata, autoload=True, autoload_with=
            engine)

    def spin(self):

        def collect_data():
            c = HTTPSConnection('rest01.alphaopen.com')
            userAndPass = b64encode(bytes(self.rest_api_key, 'utf-8')).decode(
                'ascii')
            headers = {'Authorization': 'Basic %s' % userAndPass}
            rospy.loginfo('Sending a request...')
            try:
                c.request('GET', '/api/v1/object/', headers=headers)
                res = c.getresponse()
                if res.status == 200:
                    data = res.read().decode('utf-8')
                    rospy.loginfo('Recieved the data: {}...'.format(data[:30]))
                    ins = db.insert(self.solar).values(measure=data)
                    res = self.connection.execute(ins)
            except:
                rospy.logerr('[Collector] Got an error. Retrying...')
            finally:
                rospy.loginfo('[Collector] Waiting 60 sec')
                Timer(60, collect_data).start()
        collect_data()
        rospy.spin()


if __name__ == '__main__':
    CollectorNode().spin()

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Standart, System and Third party
import sqlalchemy as db
from sqlalchemy.sql import func

from http.client import HTTPSConnection
from base64 import b64encode
from threading import Timer

# ROS
import rospy


class CollectorNode:

    def __init__(self):
        rospy.init_node('collector')
        rospy.loginfo('Launching collector node...')

        # Init DB
        engine = db.create_engine(rospy.get_param('~db_url')[:-1])
        self.connection = engine.connect()
        self.solar = self.__init_table(engine, rospy.get_param('~table_name'))

        self.rest_api_key = rospy.get_param('~rest_api_key')[:-1]

        rospy.loginfo('Collector node is ready!')

    def __init_table(self, engine, table_name):
        metadata = db.MetaData()
        if not engine.dialect.has_table(engine, table_name):
            rospy.loginfo("Initialazing DB with a table name '{}'".format(table_name))

            solar = db.Table(table_name, metadata,
                             db.Column('id', db.Integer, primary_key=True),
                             db.Column('measure', db.TEXT),
                             db.Column('time_stamp', db.TIMESTAMP(True), server_default=func.now()))
            metadata.create_all(engine)
            return solar
        return db.Table(table_name, metadata, autoload=True, autoload_with=engine)

    def spin(self):
        def collect_data():
            c = HTTPSConnection("rest01.alphaopen.com")
            userAndPass = b64encode(
                bytes(self.rest_api_key, 'utf-8')).decode("ascii")
            headers = {'Authorization': 'Basic %s' % userAndPass}

            rospy.loginfo("Sending a request...")
            try:
                c.request('GET', '/api/v1/object/', headers=headers)
                # get the response back
                res = c.getresponse()

                if res.status == 200:
                    # at this point you could check the status etc
                    # this gets the page text
                    data = res.read().decode("utf-8")

                    rospy.loginfo("Recieved the data: {}...".format(data[:30]))

                    ins = db.insert(self.solar).values(measure=data)
                    res = self.connection.execute(ins)
            except:
                rospy.logerr("[Collector] Got an error. Retrying...")
            finally:
                rospy.loginfo("[Collector] Waiting 60 sec")
                Timer(60, collect_data).start()

        # Commented for test purspose
        collect_data()
        rospy.spin()


if __name__ == '__main__':
    CollectorNode().spin()
