Ejemplo n.º 1
0
def get_chart_data(couchdb_url, chart_info, logger):

    # Get the data from couch
    #
    couch_query = couchdb_url + '/_design/doc/_view/attribute_value?'\
                     + 'startkey=["{0}","{1}",{2}]&endkey=["{0}"]&descending=true&limit=60'.format(
                     chart_info['attribute'], chart_info['couchdb_name'], '{}')

    logger.info('prepared couchdb query: {}'.format(couch_query))

    try:
        r = requests.get(
            couch_query,
            auth=(couchdb_username,
                  decrypt(couchdb_password_b64_cipher).decode('utf-8')))

        if r.status_code != 200:
            logger.error(
                'local couchdb return an error code: {}, {}...'.format(
                    r.status_code, r.text[0:100]))
            return None
        else:
            return r.json()

    except:
        logger.error(
            'Cannot connect to the local Couchdb instance: {}, {}'.format(
                exc_info()[0],
                exc_info()[1]))
        return None
Ejemplo n.º 2
0
def get_jws(path_name, camera_id):
    """ create a jws token
        hmac_secret_key_b64_cipher - A 64 byte random value shared between the JWT client and the JWT server.
    """

    return jws.sign(claim_info(get_file_hash(path_name), extract_timestamp(path_name), camera_id), 
                    decrypt(hmac_secret_key_b64_cipher),
                    algorithm='HS256')
Ejemplo n.º 3
0
def logDB(r, comment=''):

    log_record = {
        'timestamp': r['ts'],
        'name': '{} {}'.format(r['subject'], r['attribute']),
        'status': 'Success',
        'attribute': r['attribute'],
        'value': r['value'],
        'units': r['units'],
        'comment': comment
    }

    logger.info('couchd db write: {}, {}, {}, {}, {}'.format(
        log_record['name'], log_record['status'], log_record['attribute'],
        log_record['value'], log_record['comment']))

    json_data = json.dumps(log_record)
    headers = {'content-type': 'application/json'}
    """
    if there is a network problem like a DNS failure, or refused connection the Requests library will
    raise a ConnectionError exception.  With invalid HTTP responses, Requests will also raise an HTTPError 
    exception, but these are rare.  If a request times out, a Timeout exception will be raised.
    If and when a request exceeds the preconfigured number of maximum redirections, then a TooManyRedirects
    exception will be raised.
    """

    try:
        r = requests.post(
            local_couchdb_url,
            data=json_data,
            headers=headers,
            auth=(couchdb_username,
                  decrypt(couchdb_password_b64_cipher).decode('utf-8')))
        if r.status_code != 201:
            logger.error(
                'local couchdb return an error code: {}, {}...'.format(
                    r.status_code, r.text[0:100]))
    except:
        logger.error('cannot post data to the local couchdb: {}, {}'.format(
            exc_info()[0],
            exc_info()[1]))
Ejemplo n.º 4
0
def start_paho_mqtt_client(args, app_state, publish_queue):

    # TODO - investigate how to tell the broker to keep sessions alive between disconnections.
    #        This may be help or maybe not.
    try:
        mqtt_client = mqtt.Client(args['mqtt_client_id'])

        # Configure the client callback functions
        #- mqtt_client.on_connect = on_connect
        mqtt_client.on_connect = make_on_connect(args['mqtt_client_id'])
        mqtt_client.on_message = make_on_message(app_state, publish_queue)
        mqtt_client.on_publish = on_publish
        mqtt_client.on_disconnect = on_disconnect
        mqtt_client.on_subscribe = on_subscribe

        mqtt_client.enable_logger(logger)

        mqtt_client.tls_set()

        # TODO: This software is currently embedded in the fopd codebase. fopd is intended to be run as an
        # locally unattended service so a client certificate should be used here, not a username, password pair.
        #
        mqtt_client.username_pw_set(args['mqtt_username'],
                                    decrypt(args['mqtt_password_b64_cipher']))

        mqtt_client.connect(args['mqtt_url'], args['mqtt_port'], 60)

        # Start the MQTT client - loop_start causes the mqtt_client to spawn a backround thread which
        #                         handles the mqtt communications.  The loop_start call thus
        #                         returns control to this thread immediately.
        mqtt_client.loop_start()

        return mqtt_client
    except:
        logger.error(
            'Unable to create an MQTT client: {} {}, exiting...'.format(
                exc_info()[0],
                exc_info()[1]))
        return None
Ejemplo n.º 5
0
 def create_jwt(subject):
     return jws.sign(claim_info(subject),
                     decrypt(hmac_secret_key_b64_cipher),
                     algorithm='HS256')
Ejemplo n.º 6
0
def decrypt_util(pt):
    print(decrypt(pt))
    return 'OK'