Пример #1
0
def callback(ch, method, properties, body):
    global store
    try:
        rt = time.time()
        body = body.decode()
        d = parse_message(body)
        if d is None:
            logger.warning('Unrecognized: ' + body)
        else:
            d['ReceptionTime'] = rt
            print('= = = = = = = = = = = = = = =')
            pretty_print(d)

            for k in d.keys():
                try:
                    if math.isnan(d[k]):
                        d[k] = None
                except TypeError:
                    pass

            store.insert(d['node'], d)

        ch.basic_ack(delivery_tag=method.delivery_tag)
        
    except UnicodeDecodeError:
        # ignore malformed messages
        logger.warning(body)
        ch.basic_ack(delivery_tag=method.delivery_tag)
    except (MySQLdb.ProgrammingError, MySQLdb.OperationalError):
        logger.exception('db error')
        store = Storage()
    except:
        logger.exception(body)
        raise
Пример #2
0
def taskSampling():
    try:
        if debt <= 0:
            return

        if not is_rpi():
            red_on()
            usr0_on()

        d = sampling_core()
        if d is None or len(d) <= 0:
            print('sampling_core() returns nothing')
            return

        print('= = = = = = = = = =')
        pretty_print(d)

        #tmp = {c.get('comtag',c['dbtag']):d[c['dbtag']] for c in config.conf}
        #outqueue.append(tmp)
        outqueue.append(d)

        # This turns the "local" sample (a dict) into a message that base
        # stations expect. This way the base station's log2mysql.py can
        # be reused.
        # In the future these bbb nodes should ALL double as base stations,
        # listening and parsing all messages in the air.

        m = send(None, d, src=SENDER_ID)
        #socket.send(m)

        if RABBITMQ_ENABLED:
            global channel, connection
            if connection is None or channel is None:
                connection, channel = init_rabbit(nodeid, cred['rabbitmq'])
            channel.basic_publish(exchange=exchange,
                                  routing_key=routing_key,
                                  body=m,
                                  properties=pika.BasicProperties(
                                      delivery_mode=2,
                                      content_type='text/plain',
                                      expiration=str(72 * 3600 * 1000)))

        if not is_rpi():
            red_off()
            usr0_off()

        payback()
        if debt > 0:
            logger.debug('debt={}'.format(debt))
            #reactor.callLater(2*random(), taskSampling)
        else:
            logger.debug('all debts are paid')
    except pika.exceptions.ConnectionClosed:
        connection, channel = None, None
        logging.error('connection closed'
                      )  # connection to the local exchange closed? wut?
    except:
        logger.exception('')
Пример #3
0
def callback(ch, method, properties, body):
    try:
        rt = time.time()
        body = body.decode()
        d = parse_message(body)
        if d is None:
            logger.warning('Unrecognized: ' + body)
        else:
            d['ReceptionTime'] = rt
            print('= = = = = = = = = = = = = = =')
            pretty_print(d)

        ch.basic_ack(delivery_tag=method.delivery_tag)

    except UnicodeDecodeError:
        # ignore malformed messages
        logger.warning(body)
        ch.basic_ack(delivery_tag=method.delivery_tag)
    except:
        logger.exception(body)
        raise
Пример #4
0
def callback(ch, method, properties, body):
    global redis_server
    try:
        rt = time.time()
        body = body.decode()
        d = parse_message(body)
        if d is None:
            logger.warning('Unrecognized: ' + body)
        else:
            d['ReceptionTime'] = rt
            print('= = = = = = = = = = = = = = =')
            pretty_print(d)

            for k in d.keys():
                try:
                    if math.isnan(d[k]):
                        d[k] = None
                except TypeError:
                    pass

            for k,v in d.items():
                #print(k,v)
                # frigging json everywhere... "impedance mismatch"...
                redis_server.set('latest:{}:{}'.format(d['node'], k),
                                 json.dumps((d['ReceptionTime'], v)),
                                 ex=int(redis_TTL_second),
                                 )

        ch.basic_ack(delivery_tag=method.delivery_tag)

    except UnicodeDecodeError:
        # ignore malformed messages
        logger.warning(body)
        ch.basic_ack(delivery_tag=method.delivery_tag)
    except KeyboardInterrupt:
        raise
    except:
        logger.exception(body)
Пример #5
0
async def taskSampling():
    global should_continue, debt
    global connection, channel

    while should_continue:
        assert debt >= 0

        try:
            if debt > 0:
                logger.debug(f"owe {debt} sample(s)")

                if not is_rpi():
                    red_on()
                    usr0_on()

                d = sampling_core()
                if d is not None and len(d) > 0:
                    logger.debug(d)

                    print('= = = = = = = = = =')
                    pretty_print(d)

                    outqueue.append(d)

                    m = send(None, d, src=SENDER_ID)

                    if RABBITMQ_ENABLED:
                        if connection is None or channel is None:
                            connection, channel = init_rabbit(
                                nodeid, cred['rabbitmq'])
                        channel.basic_publish(exchange=exchange,
                                              routing_key=routing_key,
                                              body=m,
                                              properties=pika.BasicProperties(
                                                  delivery_mode=2,
                                                  content_type='text/plain',
                                                  expiration=str(7 * 24 *
                                                                 3600 * 1000)))

                    if not is_rpi():
                        red_off()
                        usr0_off()

                    payback()

                else:
                    logger.info('sampling_core() returns nothing')
            else:
                logger.debug("Doesn't owe a sample.")

        except pika.exceptions.ConnectionClosed:
            connection, channel = None, None
            logger.error('connection closed'
                         )  # connection to the local exchange closed? wut?

        except KeyboardInterrupt:
            raise

        except:
            logger.exception('')
            #raise

        if debt > 0:
            await asyncio.sleep(INTERVAL / NGROUP / 10)
        else:
            await asyncio.sleep(INTERVAL / NGROUP / 2)