import boto3 from sys import exc_info import io from logger import get_sub_logger from nacl_fop import decrypt from config.config import image_file_bucket_name, fop_aws_access_key_id_b64_cipher, fop_aws_secret_access_key_b64_cipher logger = get_sub_logger('boto3_fop') class S3Session(): def __init__(self): pass def __enter__(self): try: self.s3 = boto3.Session( aws_access_key_id=decrypt( fop_aws_access_key_id_b64_cipher).decode('utf-8'), aws_secret_access_key=decrypt( fop_aws_secret_access_key_b64_cipher).decode('utf-8'), region_name="us-east-2").resource('s3') return self except: logger.error( 'Exception encountered while connecting to s3: {}, {}, {}'. format(exc_info()[0], exc_info()[1],
# FYI: Look at https://morioh.com/p/1888b256c69c?f=5c22e47440738156a7078a19 for code on how to send a # WhatsApp message. # from sys import exc_info from twilio.rest import Client from config.config import twilio_account_sid_b64_cipher, twilio_auth_token_b64_cipher, twilio_from_number_b64_cipher from logger import get_sub_logger from nacl_fop import decrypt logger = get_sub_logger('twilio_fop') def send_text(text_number, body): try: client = Client( decrypt(twilio_account_sid_b64_cipher).decode('utf-8'), decrypt(twilio_auth_token_b64_cipher).decode('utf-8')) message = client.messages\ .create( body = body, from_ = decrypt(twilio_from_number_b64_cipher).decode('utf-8'), to = text_number) return {'error': False} except:
from uuid import uuid4 from logging import getLogger from requests import post from jose import jws from nacl_fop import decrypt from config.private_key import fop_jose_id, fopdcw_jose_id from config.config import jws_secret_key_b64_enc from logger import get_sub_logger # Note: This module uses JWT security (via jose). Paseto is another system for implemeting token based security. logger = get_sub_logger('jose_fop') def extract_timestamp(path_name) -> 'timestamp': dt = path_name.split("/")[-1].split(".")[0] return datetime(int(dt[0:4]), int(dt[4:6]), int(dt[6:8]), hour=int(dt[9:11]), minute=int(dt[12:14]), second=int(dt[15:17]), tzinfo=timezone.utc).timestamp() def get_jwt_issuer(jws_value: 'jws b64encoded json') -> str: # Split X.Y.Z, decode Y, interpret as JSON and then extract the JSON attribute named iss. # TODO - Note the padding on the b64 value. Python complains that the JWT claims JSON is # not padded correctly if this padding is not added. Need to research this. It seems # messy to add padding. Why don't the strings arrive correctly padded.
import datetime import hashlib from decimal import Decimal from sys import exc_info from logger import get_sub_logger logger = get_sub_logger(__name__) # Originally adapted from Django code at https://docs.djangoproject.com/en/2.1/_modules/django/utils/encoding/#is_protected_type _PROTECTED_TYPES = ( type(None), int, float, Decimal, datetime.datetime, datetime.date, datetime.time, ) def is_protected_type(obj): """Determine if the object instance is of a protected type. Objects of protected types are preserved as-is when passed to force_text(strings_only=True). """ return isinstance(obj, _PROTECTED_TYPES)
from config.config import mqtt_url, mqtt_port, mqtt_username, mqtt_password from flask_socketio import emit from logger import get_sub_logger from nacl_fop import decrypt import paho.mqtt.client as mqtt from sys import exc_info from time import time logger = get_sub_logger('mqtt') def up_time(repl_state): return time() - repl_state['start_time'] def make_on_message_handler(socketio): logger.info('making message handler') socketio.emit('response', 'making message handler') def on_message(client, userdata, message): msg = 'MQTT: userdata: {}, payload: {}'.format(userdata, message.payload.decode('utf-8')) logger.info(msg) socketio.emit('response', msg) return on_message def on_subscribe(mqtt, userdata, mid, granted_qos): msg = 'MQTT Subscribed, data: {}, mid: {}'.format(userdata, mid) logger.info(msg) """- def mqtt_subscribe_all(repl_state, emit): # subscribe to all incoming topics repl_state['mqtt_client'].subscribe('#', 2)
from datetime import datetime, timedelta from sys import exc_info import csv import io import json from logger import get_sub_logger from DbConnection import DbConnection from config.config import dbconfig from nacl_fop import decrypt_dict_vals logger = get_sub_logger('data') def get_device_data_json(device_uuid, start_date, end_date, utc_offset): try: q = """select ea.name as attribute, seo.units as units, seo.utc_timestamp + interval '%s' hour as sample_time, seo.measurement_value as value, d.local_name as device_local_name, es.name as subject_name, esl.location_guid as subject_location_id from environment_observation as eo inner join scalar_environment_observation as seo on eo.id = seo.environment_observation_id inner join environment_attribute as ea on eo.environment_attribute_id = ea.id inner join environment_subject_location as esl on esl.guid = eo.environment_subject_location_guid inner join environment_subject as es on esl.environment_subject_id = es.id inner join device as d on d.guid = eo.participant_guid where eo.participant_guid = %s and utc_timestamp >= timestamp %s and utc_timestamp < timestamp %s order by seo.utc_timestamp desc; """ with DbConnection(decrypt_dict_vals(dbconfig, {'password'})) as cur:
from datetime import datetime, timedelta from DbConnection import DbConnection from logger import get_sub_logger from nacl_fop import decrypt_dict_vals from python.boto3_fop import S3Session from config.config import dbconfig logger = get_sub_logger('image') def get_image_file_v2(s3_file_key): with S3Session() as s3: return s3.get_s3_image(s3_file_key) #- return get_s3_image(s3_file_key) def get_newest_image_uuid(camera_uuid): """ TODO - finish the docs and the code for this routine """ with DbConnection(decrypt_dict_vals(dbconfig, {'password'})) as cur: sql = """select s3_reference from phenotype_observation inner join phenotype_image on phenotype_observation.id = phenotype_image.phenotype_observation_id where phenotype_observation.participant_guid = %s order by phenotype_observation.utc_timestamp desc;""" cur.execute(sql, (camera_uuid, )) assert (cur.rowcount > 0), 'No image is available for this camera.'
# TODO: Look at using D3.js as the charting tool import json from datetime import datetime, timedelta from os import getcwd from sys import exc_info import pygal import requests from DbConnection import DbConnection from config.config import dbconfig from nacl_fop import decrypt_dict_vals from logger import get_sub_logger logger = get_sub_logger('generate_chart') enable_display_unit_error_msg = None # TODO: Current the system supports converting from celsius to fahrenheit. As the need arises # add more unit conversions. # def apply_unit_conversion(val_tree, chart_info): global enable_display_unit_error_msg if 'display_units' in chart_info: if chart_info['display_units'].lower() == 'fahrenheit': if 'units' in val_tree['value']: if val_tree['value']['units'].lower() == 'celsius':