Exemplo n.º 1
0
    def capture(self, use_mongo=True, send_message=True):
        """
        Helper function to return serial sensor info.

        Reads each of the connected sensors. If a value is received, attempts
        to parse the value as json.

        Returns:
            sensor_data (dict):     Dictionary of sensors keyed by sensor name.
        """

        sensor_data = dict()

        # Read from all the readers
        for sensor_name, reader_info in self.serial_readers.items():
            reader = reader_info['reader']

            # Get the values
            self.logger.debug("Reading next serial value")
            try:
                sensor_info = reader.get_reading()
            except IndexError:
                continue

            time_stamp = sensor_info[0]
            sensor_value = sensor_info[1]
            try:
                self.logger.debug("Got sensor_value from {}".format(sensor_name))
                data = yaml.load(sensor_value.replace('nan', 'null'))
                data['date'] = time_stamp

                sensor_data[sensor_name] = data

                if send_message:
                    self.send_message({'data': data}, channel='environment')
            except yaml.parser.ParserError:
                self.logger.warning("Bad JSON: {0}".format(sensor_value))
            except ValueError:
                self.logger.warning("Bad JSON: {0}".format(sensor_value))
            except TypeError:
                self.logger.warning("Bad JSON: {0}".format(sensor_value))
            except Exception as e:
                self.logger.warning("Bad JSON: {0}".format(sensor_value))

            if use_mongo and len(sensor_data) > 0:
                if self.db is None:
                    self.db = PanMongo()
                    self.logger.info('Connected to PanMongo')
                self.db.insert_current('environment', sensor_data)
        else:
            self.logger.debug("No sensor data received")

        return sensor_data
Exemplo n.º 2
0
def main(date, auto_confirm=False):
    db = PanMongo()

    seq_ids = db.observations.distinct(
        'sequence_id', {'date': {'$gte': Time(date).datetime}})
    imgs = [record['data']['file_path'] for record in db.observations.find(
        {'sequence_id': {'$in': seq_ids}}, {'data.file_path': 1})]

    dirs = set([img[0:img.rindex('/') - 1].replace('/var/panoptes/images/fields/', '')
                for img in imgs])

    if auto_confirm is False:
        print("Found the following dirs for {}:".format(date))
        pprint(dirs)
        if input("Proceed (Y/n): ") == 'n':
            return

    for d in console.ProgressBar(dirs):
        run_cmd = ['gsutil', '-mq', 'cp', '-r', '/var/panoptes/images/fields/{}/*.fz'.format(d),
                   'gs://panoptes-survey/PAN001/{}/'.format(d)]

        try:
            completed_process = subprocess.run(run_cmd, stdout=subprocess.PIPE)

            if completed_process.returncode != 0:
                print("Problem uploading")
                print(completed_process.stdout)
        except Exception as e:
            print("Problem uploading: {}".format(e))
Exemplo n.º 3
0
def main(unit_id=None, upload=True, bucket='unit_sensors', **kwargs):
    assert unit_id is not None, warnings.warn("Must supply PANOPTES unit id, e.g. PAN001")

    console.color_print('Connecting to mongo')
    db = PanMongo()

    console.color_print('Exporting data')
    archived_files = db.export(**kwargs)

    if upload:
        storage = PanStorage(unit_id=unit_id, bucket=bucket)
        console.color_print("Uploading files:")

        for f in archived_files:
            r_fn = storage.upload(f)
            console.color_print("\t{:40s}".format(f), 'green', "\t->\t", 'red', r_fn, 'blue')
Exemplo n.º 4
0
    def __init__(self, *args, **kwargs):
        # Load the default and local config files
        global _config
        if _config is None:
            ignore_local_config = kwargs.get('ignore_local_config', False)
            _config = config.load_config(ignore_local=ignore_local_config)

        self.__version__ = __version__

        # Update with run-time config
        if 'config' in kwargs:
            _config.update(kwargs['config'])

        self._check_config(_config)
        self.config = _config

        self.logger = kwargs.get('logger')
        if not self.logger:
            self.logger = get_root_logger()

        self.config['simulator'] = hardware.get_simulator_names(
            config=self.config, kwargs=kwargs)

        # Set up connection to database
        db = kwargs.get('db', self.config['db']['name'])
        _db = PanMongo(db=db)

        self.db = _db
Exemplo n.º 5
0
    def get_table_data(self, data_file):
        """ Get the table data

        If a `data_file` (csv) is passed, read from that, otherwise use mongo

        """
        table = None

        col_names = ('ambient_temp_C', 'sky_temp_C', 'sky_condition',
                     'wind_speed_KPH', 'wind_condition',
                     'gust_condition', 'rain_frequency',
                     'rain_condition', 'safe', 'pwm_value',
                     'rain_sensor_temp_C', 'date')

        col_dtypes = ('f4', 'f4', 'U15',
                      'f4', 'U15',
                      'U15', 'f4',
                      'U15', bool, 'f4',
                      'f4', 'O')

        if data_file is not None:
            table = Table.from_pandas(pd.read_csv(data_file, parse_dates=True))
        else:
            # -------------------------------------------------------------------------
            # Grab data from Mongo
            # -------------------------------------------------------------------------
            import pymongo
            from pocs.utils.database import PanMongo

            print('  Retrieving data from Mongo database')
            db = PanMongo()
            entries = [x for x in db.weather.find(
                {'date': {'$gt': self.start, '$lt': self.end}}).sort([
                    ('date', pymongo.ASCENDING)])]

            table = Table(names=col_names, dtype=col_dtypes)

            for entry in entries:
                pd.to_datetime(pd.Series(entry['date']))
                data = {'date': pd.to_datetime(entry['date'])}
                for key, val in entry['data'].items():
                    if key in col_names:
                        if key != 'date':
                            data[key] = val

                table.add_row(data)

        table.sort('date')
        return table
Exemplo n.º 6
0
class ArduinoSerialMonitor(object):

    """
        Monitors the serial lines and tries to parse any data recevied
        as JSON.

        Checks for the `camera_box` and `computer_box` entries in the config
        and tries to connect. Values are updated in the mongo db.
    """

    def __init__(self, auto_detect=False, *args, **kwargs):
        self.config = load_config(config_files='peas')
        self.logger = get_root_logger()

        assert 'environment' in self.config
        assert type(self.config['environment']) is dict, \
            self.logger.warning("Environment config variable not set correctly. No sensors listed")

        self.db = None
        self.messaging = None

        # Store each serial reader
        self.serial_readers = dict()

        if auto_detect:
            for port_num in range(9):
                port = '/dev/ttyACM{}'.format(port_num)
                if os.path.exists(port):
                    self.logger.debug("Trying to connect on {}".format(port))

                    sensor_name = None
                    serial_reader = self._connect_serial(port)

                    num_tries = 5
                    self.logger.debug("Getting name on {}".format(port))
                    while num_tries > 0:
                        try:
                            data = serial_reader.get_reading()
                        except yaml.parser.ParserError:
                            pass
                        except AttributeError:
                            pass
                        else:
                            try:
                                if 'name' in data:
                                    sensor_name = data['name']
                                    num_tries = 0
                            except Exception as e:
                                self.logger.warning("Read on serial: {}".format(e))
                        num_tries -= 1

                    if sensor_name is not None:
                        self.serial_readers[sensor_name] = {
                            'reader': serial_reader,
                        }
        else:
            # Try to connect to a range of ports
            for sensor_name in self.config['environment'].keys():
                try:
                    port = self.config['environment'][sensor_name]['serial_port']
                except TypeError:
                    continue
                except KeyError:
                    continue

                serial_reader = self._connect_serial(port)
                self.serial_readers[sensor_name] = {
                    'reader': serial_reader,
                    'port': port,
                }

    def _connect_serial(self, port):
        if port is not None:
            self.logger.debug('Attempting to connect to serial port: {}'.format(port))
            serial_reader = SerialData(port=port)
            self.logger.debug(serial_reader)

            try:
                serial_reader.connect()
                serial_reader.start()
            except Exception as e:
                self.logger.warning('Could not connect to port: {}'.format(port))

            return serial_reader

    def disconnect(self):
        for sensor_name, reader_info in self.serial_readers.items():
            reader = reader_info['reader']
            reader.stop()

    def send_message(self, msg, channel='environment'):
        if self.messaging is None:
            self.messaging = PanMessaging.create_publisher(6510)

        self.messaging.send_message(channel, msg)

    def capture(self, use_mongo=True, send_message=True):
        """
        Helper function to return serial sensor info.

        Reads each of the connected sensors. If a value is received, attempts
        to parse the value as json.

        Returns:
            sensor_data (dict):     Dictionary of sensors keyed by sensor name.
        """

        sensor_data = dict()

        # Read from all the readers
        for sensor_name, reader_info in self.serial_readers.items():
            reader = reader_info['reader']

            # Get the values
            self.logger.debug("Reading next serial value")
            try:
                sensor_info = reader.get_reading()
            except IndexError:
                continue

            time_stamp = sensor_info[0]
            sensor_value = sensor_info[1]
            try:
                self.logger.debug("Got sensor_value from {}".format(sensor_name))
                data = yaml.load(sensor_value.replace('nan', 'null'))
                data['date'] = time_stamp

                sensor_data[sensor_name] = data

                if send_message:
                    self.send_message({'data': data}, channel='environment')
            except yaml.parser.ParserError:
                self.logger.warning("Bad JSON: {0}".format(sensor_value))
            except ValueError:
                self.logger.warning("Bad JSON: {0}".format(sensor_value))
            except TypeError:
                self.logger.warning("Bad JSON: {0}".format(sensor_value))
            except Exception as e:
                self.logger.warning("Bad JSON: {0}".format(sensor_value))

            if use_mongo and len(sensor_data) > 0:
                if self.db is None:
                    self.db = PanMongo()
                    self.logger.info('Connected to PanMongo')
                self.db.insert_current('environment', sensor_data)
        else:
            self.logger.debug("No sensor data received")

        return sensor_data
Exemplo n.º 7
0
import pandas
import seaborn

from astropy import units as u
from matplotlib import markers
from matplotlib import pyplot as plt

from pocs.utils.database import PanMongo

seaborn.set_style('darkgrid')

db = PanMongo()

color = {
    'alt_east': 'green',
    'alt_west': 'blue',
    'az_east': 'red',
    'az_west': 'purple'
}

data = {}
info = {}

# Lookup all the dates
dates = db.drift_align.distinct('start_time')
dates = dict(zip(range(len(dates)), dates))

# Find out which dates to use
for k, v in dates.items():
    print('{:2d}: {}'.format(k, v))
Exemplo n.º 8
0
def db():
    return PanMongo(db='panoptes_testing')
Exemplo n.º 9
0
def get_mongodb():
    from pocs.utils.database import PanMongo
    return PanMongo()
Exemplo n.º 10
0
def db():
    return PanMongo(db='huntsman_testing')