From 044009edbc5d8c47f4cc08b37b95720a5cf3b6bd Mon Sep 17 00:00:00 2001 From: Russell Jimmies Date: Thu, 21 Dec 2023 19:14:59 +0000 Subject: [PATCH] Fixes #29: moved script into package, requirements --- .env.template | 1 + custom_exceptions.py | 8 +++++ microscope/__init__.py | 0 microscope/microscope_info.py | 61 ++++++++++++++++++++++++++++++++ microscope_info.py | 66 ----------------------------------- 5 files changed, 70 insertions(+), 66 deletions(-) create mode 100644 microscope/__init__.py create mode 100644 microscope/microscope_info.py delete mode 100644 microscope_info.py diff --git a/.env.template b/.env.template index 2c467284..d19b92c5 100644 --- a/.env.template +++ b/.env.template @@ -11,3 +11,4 @@ NACHET_RESOURCE_GROUP= NACHET_WORKSPACE= NACHET_MODEL= MICROSCOPE_URL= +METHODS= diff --git a/custom_exceptions.py b/custom_exceptions.py index 48449a75..f8a901f7 100644 --- a/custom_exceptions.py +++ b/custom_exceptions.py @@ -8,3 +8,11 @@ class ProcessInferenceResultError(Exception): class ValidateEnvVariablesError(Exception): pass + + +class ServerError(Exception): + pass + + +class MicroscopeQueryError(Exception): + pass diff --git a/microscope/__init__.py b/microscope/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/microscope/microscope_info.py b/microscope/microscope_info.py new file mode 100644 index 00000000..1cf8dd8b --- /dev/null +++ b/microscope/microscope_info.py @@ -0,0 +1,61 @@ +import os +import json +import uuid +import logging +import requests +from custom_exceptions import MicroscopeQueryError +from dotenv import load_dotenv + +load_dotenv() + +METHODS = os.getenv("METHODS") +MICROSCOPE_URL = os.getenv("MICROSCOPE_URL") +params = {"id": int(uuid.uuid4())} +HEADERS = {'Content-Type': 'application/json'} + +def post_request(MICROSCOPE_URL, method, params, headers=HEADERS): + url = f"{MICROSCOPE_URL}?jsonrpc=2.0&method={method}&id={params['id']}" + + data = json.dumps({ + "jsonrpc": "2.0", + "method": method, + "id": params['id'], + }) + + try: + resp = requests.post(url, data=data, headers=headers) + resp.raise_for_status() + return resp.json() + except requests.RequestException as e: + logging.error(f"Request Error: {e}") + raise MicroscopeQueryError(f"OpenApiError: {e}") from e + +def is_hex(s): + try: + int(s, 16) + return True + except ValueError: + return False + +def get_microscope_configuration(METHODS): + config = {} + for method in METHODS: + resp = post_request(MICROSCOPE_URL, method, params, HEADERS) + result = resp["result"] + + # Check if the response is in hexadecimal and convert it + if isinstance(result, str) and is_hex(result): + result = int(result, 16) + + config[method] = result + + return config + + +if __name__ == "__main__": + try: + config = get_microscope_configuration(METHODS) + if config: + print(config) + except requests.RequestException as e: + raise MicroscopeQueryError(f"OpenApiError: {e}") from e diff --git a/microscope_info.py b/microscope_info.py deleted file mode 100644 index 5993a363..00000000 --- a/microscope_info.py +++ /dev/null @@ -1,66 +0,0 @@ -import json -import requests -from custom_exceptions import OpenApiError -from dotenv import load_dotenv -import os - -load_dotenv() - -methods = [ - "getVersion", # Returns microscope version - "getFieldOfView", # Returns FoV as an int value (μm) - "getZoomDirect", # Returns current zoom as string hex value - "getFocusDirect", # Returns current focus as string hex value - "getContrast", # Returns position of the Contrast slider - "getSaturation", # Returns position of the Saturation slider - "getSharpness" # Returns position of the Sharpness slider -] - -MICROSCOPE_URL = os.getenv("MICROSCOPE_URL") -params = {"id": 6969} -headers = {'Content-Type': 'application/json'} - -def post_request(MICROSCOPE_URL, method, params, headers): - url = f"{MICROSCOPE_URL}?jsonrpc=2.0&method={method}&id={params['id']}" - - data = json.dumps({ - "jsonrpc": "2.0", - "method": method, - "id": params['id'], - }) - - try: - resp = requests.post(url, data=data, headers=headers) - return resp.json() - except OpenApiError as e: - print(f"Request Error: {e}") - -def is_hex(s): - try: - int(s, 16) - return True - except ValueError: - return False - -def get_microscope_configuration(): - config = {} - for method in methods: - resp = post_request(MICROSCOPE_URL, method, params, headers) - result = resp["result"] - - # Check if the response is in hexadecimal and convert it - if isinstance(result, str) and is_hex(result): - result = int(result, 16) - - config[method] = result - - return json.dumps(config, indent=4) - - -if __name__ == "__main__": - try: - config = get_microscope_configuration() - if config: - print(config) - except OpenApiError as e: - print(f"Error: {e}")