diff --git a/.gitignore b/.gitignore index 89e509d..bb1c7b4 100644 --- a/.gitignore +++ b/.gitignore @@ -152,4 +152,8 @@ cython_debug/ #.idea/ # VSCode -.vscode/ \ No newline at end of file +.vscode/ + +# misc +OpenHaystack.app/ +extract.txt \ No newline at end of file diff --git a/OpenHaystack.zip b/OpenHaystack.zip new file mode 100644 index 0000000..bea4404 Binary files /dev/null and b/OpenHaystack.zip differ diff --git a/main.py b/main.py index 7341d87..40f12c5 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,4 @@ -import requests -import json +import requests, json, subprocess, re import paho.mqtt.client as mqtt def OpenHayStackLocation(): @@ -12,19 +11,31 @@ def on_connect(client, userdata, flags, rc): def on_message(client, userdata, msg): if msg.topic == "/data": + loc_lat, loc_lon = None, None + try: + subprocess.run("./OpenHaystack.app/Contents/MacOS/OpenHaystack > extract.txt", capture_outout=True, shell=True, timeout=5) + except Exception as e: + pass + with open("extract.txt", "r") as output: + lines = output.readlines() + for line in reversed(lines): + if "FindMyLocationReport" in line: + loc_lat = re.findall("latitude: (-?[0-9]*\.[0-9]*)", line)[0] + loc_lon = re.findall("longitude: (-?[0-9]*\.[0-9]*)", line)[0] + break + subprocess.run("pkill OpenHaystack", shell=True) data = json.loads(msg.payload) firebase_url = "https://leg-barkr.nw.r.appspot.com/readings/save" firebase_headers = { "Content-Type": "application/json", "Device-ID": data["devID"] } - loc_lat, loc_lon = OpenHayStackLocation() firebase_data = { "air_temp": data["air_temp"], "cumulative_steps_today": data["day_steps"], "humidity": data["hum_perc"], - "latitude": loc_lat, - "longitude": loc_lon, + "latitude": float(loc_lat), + "longitude": float(loc_lon), "skin_temp": data['pet_temp'] } r = requests.post(url = firebase_url, json = firebase_data, params = None, headers = firebase_headers) # add any other necessary values in request