diff --git a/.gitignore b/.gitignore index 02bf5ba..2d85ddc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .venv/ .vscode/ +src/backups/ diff --git a/src/GlucoseCheck.py b/src/GlucoseCheck.py index 2bbb5b0..f985860 100644 --- a/src/GlucoseCheck.py +++ b/src/GlucoseCheck.py @@ -1,6 +1,8 @@ import requests import time import paho.mqtt.client as mqtt +import hashlib +import binascii from datetime import datetime from collections import deque @@ -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 @@ -88,7 +93,7 @@ def main(url_main, headers_main): gluc_buffer.append(gluc_value) old_value = list(gluc_buffer)[0] print(f"old level: {old_value}") - gluc_slope = (gluc_value-old_value)* 60 / 30 # slope is gluc_level per hour (normalized) + gluc_slope = (gluc_value-old_value)* 60 / 30 # slope is gluc_level per hour (normalized) print(f"slope: {gluc_slope}") print(f"time.time: {time.time()}") @@ -106,7 +111,6 @@ def main(url_main, headers_main): 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}") except requests.exceptions.JSONDecodeError: @@ -159,7 +163,7 @@ def on_message(client, userdata, msg): #This runs everytime a mqtt message Mqtt = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) #Creates a mqqt object Mqtt.on_connect = on_connect #Create callback on connect -Mqtt.on_message = on_message #Create callback on message +Mqtt.on_message = on_message #Create callback on message Mqtt.username_pw_set(brokercreds[0], password=brokercreds[1]) #Set User and Password Mqtt.connect(broker_address, broker_port, 60) #Set Connection settings 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) diff --git a/src/TeleBot.py b/src/TeleBot.py index b42e419..ba95b0d 100644 --- a/src/TeleBot.py +++ b/src/TeleBot.py @@ -7,7 +7,7 @@ import paho.mqtt.client as mqtt from time import sleep ##### Mqtt vars ##### -broker_address = "192.168.1.250" +broker_address = "192.168.1.160" broker_port = 1883 recieve_topic = "telegrambot/out/#" send_topic_server = "telegrambot/in/server/" @@ -107,4 +107,4 @@ def main():#This is the main loop if __name__ == '__main__': main() - \ No newline at end of file +