From 9a65887650cfc577bf895cd929fc20cb6dba7da9 Mon Sep 17 00:00:00 2001 From: Russell Jimmies Date: Tue, 19 Dec 2023 19:19:44 +0000 Subject: [PATCH 1/6] Issue #29: basic skeleton, retrieves config for certain methods --- custom_exceptions.py | 3 +++ microscope_info.py | 49 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 microscope_info.py diff --git a/custom_exceptions.py b/custom_exceptions.py index 7de693e7..6a53731c 100644 --- a/custom_exceptions.py +++ b/custom_exceptions.py @@ -64,3 +64,6 @@ class ValidateEnvVariablesError(Exception): class ServerError(Exception): pass + +class OpenApiError(Exception): + pass diff --git a/microscope_info.py b/microscope_info.py new file mode 100644 index 00000000..1c3ed274 --- /dev/null +++ b/microscope_info.py @@ -0,0 +1,49 @@ +import json +import requests +from custom_exceptions import ( + OpenApiError +) + +methods = [ + "getVersion", + "getFieldOfView", + "getZoomDirect", + "getFocusDirect", +] + +api_url = "http://192.168.0.101/" +params = {"id": 6969} +headers = {'Content-Type': 'application/json'} + +def post_request(api_url, method, params, headers): + url = f"{api_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) + print(resp.text) + return resp.json() + except OpenApiError as e: + print(f"Request Error: {e}") + + +def get_microscope_configuration(): + config = {} + for method in methods: + resp = post_request(api_url, method, params, headers) + config[method] = resp["result"] + return json.dumps(config) + + +if __name__ == "__main__": + try: + config = get_microscope_configuration() + if config: + print(config) + except OpenApiError as e: + print(f"Error: {e}") From a9c7e8248ac9a98d1c0214bd2b1f52e31be95827 Mon Sep 17 00:00:00 2001 From: Russell Jimmies Date: Tue, 19 Dec 2023 21:06:35 +0000 Subject: [PATCH 2/6] Issue #29: retrieves more config info, convert hex values to str --- microscope_info.py | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/microscope_info.py b/microscope_info.py index 1c3ed274..9a592aaa 100644 --- a/microscope_info.py +++ b/microscope_info.py @@ -1,14 +1,15 @@ import json import requests -from custom_exceptions import ( - OpenApiError -) +from custom_exceptions import OpenApiError methods = [ - "getVersion", - "getFieldOfView", - "getZoomDirect", - "getFocusDirect", + "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 ] api_url = "http://192.168.0.101/" @@ -31,13 +32,26 @@ def post_request(api_url, method, params, headers): 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(api_url, method, params, headers) - config[method] = resp["result"] - return json.dumps(config) + 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__": From a09e5273511a43efd080c5319d38f8f8dd62ae22 Mon Sep 17 00:00:00 2001 From: Russell Jimmies Date: Wed, 20 Dec 2023 16:32:12 +0000 Subject: [PATCH 3/6] Issue #29: env var --- .env.template | 1 + microscope_info.py | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/.env.template b/.env.template index 50eee12f..e690bfde 100644 --- a/.env.template +++ b/.env.template @@ -6,3 +6,4 @@ NACHET_SUBSCRIPTION_ID= NACHET_RESOURCE_GROUP= NACHET_WORKSPACE= NACHET_MODEL= +MICROSCOPE_URL= diff --git a/microscope_info.py b/microscope_info.py index 9a592aaa..f5f3c80b 100644 --- a/microscope_info.py +++ b/microscope_info.py @@ -1,6 +1,10 @@ import json import requests from custom_exceptions import OpenApiError +from dotenv import load_dotenv +import os + +load_dotenv() methods = [ "getVersion", # Returns microscope version @@ -12,12 +16,12 @@ "getSharpness" # Returns position of the Sharpness slider ] -api_url = "http://192.168.0.101/" +MICROSCOPE_URL = os.getenv("MICROSCOPE_URL") params = {"id": 6969} headers = {'Content-Type': 'application/json'} -def post_request(api_url, method, params, headers): - url = f"{api_url}?jsonrpc=2.0&method={method}&id={params['id']}" +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", @@ -42,7 +46,7 @@ def is_hex(s): def get_microscope_configuration(): config = {} for method in methods: - resp = post_request(api_url, method, params, headers) + resp = post_request(MICROSCOPE_URL, method, params, headers) result = resp["result"] # Check if the response is in hexadecimal and convert it From ae8884f7375ba0b55ccfa4052e52ca3a01ff469b Mon Sep 17 00:00:00 2001 From: Russell Jimmies Date: Wed, 20 Dec 2023 16:35:47 +0000 Subject: [PATCH 4/6] Issue #29: removed print --- microscope_info.py | 1 - 1 file changed, 1 deletion(-) diff --git a/microscope_info.py b/microscope_info.py index f5f3c80b..5993a363 100644 --- a/microscope_info.py +++ b/microscope_info.py @@ -31,7 +31,6 @@ def post_request(MICROSCOPE_URL, method, params, headers): try: resp = requests.post(url, data=data, headers=headers) - print(resp.text) return resp.json() except OpenApiError as e: print(f"Request Error: {e}") From 4bbd11a3a1319a53c52d8e91a60a263ca4009f05 Mon Sep 17 00:00:00 2001 From: Russell Jimmies Date: Thu, 21 Dec 2023 19:14:59 +0000 Subject: [PATCH 5/6] Fixes #29: moved script into package, requirements --- .env.template | 1 + custom_exceptions.py | 3 +- microscope/__init__.py | 0 microscope/microscope_info.py | 61 ++++++++++++++++++++++++++++++++ microscope_info.py | 66 ----------------------------------- requirements.txt | 1 + 6 files changed, 65 insertions(+), 67 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 e690bfde..6b4d6694 100644 --- a/.env.template +++ b/.env.template @@ -7,3 +7,4 @@ NACHET_RESOURCE_GROUP= NACHET_WORKSPACE= NACHET_MODEL= MICROSCOPE_URL= +METHODS= diff --git a/custom_exceptions.py b/custom_exceptions.py index 6a53731c..beb575af 100644 --- a/custom_exceptions.py +++ b/custom_exceptions.py @@ -65,5 +65,6 @@ class ValidateEnvVariablesError(Exception): class ServerError(Exception): pass -class OpenApiError(Exception): + +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}") diff --git a/requirements.txt b/requirements.txt index 5ef34973..aaf28f79 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ quart quart-cors python-dotenv hypercorn +requests From 63ff5d7bf5f363524ddeccbca8939633e67709ad Mon Sep 17 00:00:00 2001 From: Russell Jimmies Date: Thu, 21 Dec 2023 20:01:28 +0000 Subject: [PATCH 6/6] Fixes #29: methods env var fix, try catch in get_microscope_configuration --- microscope/microscope_info.py | 24 +++++++++++++++--------- tests/test_microscope_info.py | 0 2 files changed, 15 insertions(+), 9 deletions(-) create mode 100644 tests/test_microscope_info.py diff --git a/microscope/microscope_info.py b/microscope/microscope_info.py index 1cf8dd8b..946a3e31 100644 --- a/microscope/microscope_info.py +++ b/microscope/microscope_info.py @@ -3,12 +3,13 @@ import uuid import logging import requests -from custom_exceptions import MicroscopeQueryError from dotenv import load_dotenv +from custom_exceptions import MicroscopeQueryError load_dotenv() -METHODS = os.getenv("METHODS") +methods_str = os.getenv("METHODS", "[]") +METHODS = json.loads(methods_str) MICROSCOPE_URL = os.getenv("MICROSCOPE_URL") params = {"id": int(uuid.uuid4())} HEADERS = {'Content-Type': 'application/json'} @@ -28,7 +29,7 @@ def post_request(MICROSCOPE_URL, method, params, headers=HEADERS): return resp.json() except requests.RequestException as e: logging.error(f"Request Error: {e}") - raise MicroscopeQueryError(f"OpenApiError: {e}") from e + raise MicroscopeQueryError(f"MicroscopeQueryError: {e}") from e def is_hex(s): try: @@ -40,14 +41,19 @@ def is_hex(s): def get_microscope_configuration(METHODS): config = {} for method in METHODS: - resp = post_request(MICROSCOPE_URL, method, params, HEADERS) - result = resp["result"] + try: + 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) + # 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 + config[method] = result + + except MicroscopeQueryError as mqe: + config[method] = None + logging.error(f"MicroscopeQueryError: {mqe}") return config diff --git a/tests/test_microscope_info.py b/tests/test_microscope_info.py new file mode 100644 index 00000000..e69de29b