diff --git a/src/GlucoseCheck.py b/src/GlucoseCheck.py index f69f9ea..f985860 100644 --- a/src/GlucoseCheck.py +++ b/src/GlucoseCheck.py @@ -1,12 +1,14 @@ import requests import time import paho.mqtt.client as mqtt +import hashlib +import binascii from datetime import datetime from collections import deque ##### Mqtt vars ##### -broker_address = "192.168.1.160" -broker_port = 1883 +broker_address = "192.168.1.160" +broker_port = 1883 recieve_topic = "telegrambot/in/glucose/gj" send_topic = "telegrambot/out/gj" brokercreds = ["hass","Bruk#5"] @@ -14,18 +16,21 @@ brokercreds = ["hass","Bruk#5"] ##### Variables ##### # API JWT token, Account-Id and Patient_Id -jwt_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImM1ZGVhZGZiLTNlZDYtMTFlYS1hZjZmLTAyNDJhYzExMDAwYSIsImZpcnN0TmFtZSI6IkdlcnRqYW4iLCJsYXN0TmFtZSI6Iktvb2xlbiIsImNvdW50cnkiOiJOTCIsInJlZ2lvbiI6ImV1Iiwicm9sZSI6InBhdGllbnQiLCJlbWFpbCI6Imdqa29vbGVuQGdtYWlsLmNvbSIsInMiOiJsbHUuYW5kcm9pZCIsInNpZCI6IjkzYTZmMmU2LWRjNDQtNDA0Mi04YjBiLTcxMGQ3MzAyMzMyNSIsInRhc2tUeXBlIjoidG91IiwiZXhwIjoxNzYwMTkzMDM2LCJpYXQiOjE3NjAxODk0MzYsImp0aSI6ImU0NTRlYzljLTE2ZTMtNDQwMy05YjYyLTc0NjAxZTkzNDFjOSJ9.hiyrVjE2kWvBr2DjtPojdTDtkqNrPI57HubXYl4Kko0" -Account_ID = "dad20d62aeef891d94bda411af269900180cb7b84a0ce51d60b52463339935b1" +jwt_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImM1ZGVhZGZiLTNlZDYtMTFlYS1hZjZmLTAyNDJhYzExMDAwYSIsImZpcnN0TmFtZSI6IkdlcnRqYW4iLCJsYXN0TmFtZSI6Iktvb2xlbiIsImNvdW50cnkiOiJOTCIsInJlZ2lvbiI6ImV1Iiwicm9sZSI6InBhdGllbnQiLCJzIjoibGx1LmFuZHJvaWQiLCJzaWQiOiIyZDM3NTJmNy01M2EwLTQ2N2ItOTAyNS02YmIzODBmZTYxZWEiLCJleHAiOjE3NzY1MDYwNTIsImlhdCI6MTc2MDk1NDA1MiwianRpIjoiNDYzYmRiZjQtNjJiNi00N2I4LWJiNzYtN2FmMmJjNGY2YjkzIn0.0pxP3zagyWaTF-r0F9mHOtSsbsMwz3lUvzEnQQmDNek" +#Account_ID = "dad20d62aeef891d94bda411af269900180cb7b84a0ce51d60b52463339935b1" Patient_ID = "c5deadfb-3ed6-11ea-af6f-0242ac11000a" # API endpoint for login #url = f"https://api-eu.libreview.io/llu/connections/{Patient_ID}/graph" url = f"https://libreview-proxy.onrender.com/eu/auth/continue/tou" +digest = hashlib.sha256(Patient_ID.encode('utf-8')).digest() +Account_ID = binascii.hexlify(digest).decode('utf-8') + # Headers headers = { - "content-type": "application/json", # This nececcery + "Accept": "application/json", # This nececcery "product": "llu.android", # This nececcery - "version": "4.13.0", # Version older then 4.12.0 gives error. + "version": "4.16", # Version older then 4.12.0 gives error. "Authorization": f"Bearer {jwt_token}", # JWT_token for login "Account-Id" : Account_ID # Extra login credentials } @@ -50,7 +55,7 @@ limits_max_hours = 4 # nr of hours in the above lists last_bolus_time = time.time()-3600 last_bolus_type = 0 -GLUC_READOUT_PERIOD = 5 # in minutes +GLUC_READOUT_PERIOD = 1 #5 # in minutes GLUC_SLOPE_PERIOD = 30 # in minutes GLUC_BUFFER_LENGTH = round(GLUC_SLOPE_PERIOD/GLUC_READOUT_PERIOD) gluc_buffer = deque([6.0] * GLUC_BUFFER_LENGTH, maxlen=GLUC_BUFFER_LENGTH) # The gluc_buffer always contains only the last xx elements. Initialized on 6.0 @@ -80,7 +85,7 @@ def main(url_main, headers_main): response = requests.get(url_main, headers=headers_main) # json=payload if response.status_code == 200: # If request has succeeded data = response.json() - + gluc_value = glucose_value(data) print(f"current level: {gluc_value}") @@ -105,7 +110,6 @@ def main(url_main, headers_main): (gluc_slope > limits_table[last_bolus_type][int(hours_since_last_bolus)][1])): print("Hey joh check je glucosepeil !") Mqtt.publish(send_topic, f"Hey joh check peil ! (lastbolus -{round(hours_since_last_bolus, 1)})") - else: # Request has failed print(f"❌ Error: Could not connect to server: {response.status_code}") diff --git a/src/Glucose_http_jwt.py b/src/Glucose_http_jwt.py index 4db1bca..ad058ec 100644 --- a/src/Glucose_http_jwt.py +++ b/src/Glucose_http_jwt.py @@ -1,7 +1,8 @@ import requests # API endpoint for login -url = "https://api-eu.libreview.io/llu/auth/login" # Replace with the actual API URL +#url = "https://api-eu.libreview.io/llu/auth/login" # Replace with the actual API URL +url = "https://libreview-proxy.onrender.com/eu/llu/auth/login" # Replace with the actual API URL # Login credentials payload = { @@ -14,9 +15,9 @@ headers = { 'accept-encoding': 'gzip', 'cache-control': 'no-cache', 'connection': 'Keep-Alive', - 'content-type': 'application/json', + 'Accept': 'application/json', 'product': 'llu.android', - 'version': '4.7' + 'version': '4.16' } # Send the POST request @@ -24,7 +25,7 @@ response = requests.post(url, json=payload, headers=headers) if response.status_code == 200: data = response.json() - + # Ensure the response contains the expected structure if data.get("status") == 0 and "authTicket" in data.get("data", {}): jwt_token = data["data"]["authTicket"]["token"] @@ -33,4 +34,4 @@ if response.status_code == 200: print("Status = ", data.get("status")) print("Login failed: Unexpected response format") else: - print("Login failed:", response.status_code, response.text) \ No newline at end of file + print("Login failed:", response.status_code, response.text)