Created
June 6, 2021 20:32
-
-
Save drewrothstein/fdea4cfe4ca862bb94e1a6f20332f0a4 to your computer and use it in GitHub Desktop.
Automower Prometheus Exporter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import requests | |
import time | |
from prometheus_client import Gauge, start_http_server | |
# Source: github.com/chrisz/pyhusmow | |
# ./husmow --login <> --password <> server | |
DATA_API = 'http://127.0.0.1:1234/status' | |
PORT_API = 8182 | |
TIME_BETWEEN_CHECKS_SECONDS = 30 | |
start_http_server(PORT_API) | |
gauge_lat = Gauge('automower_lat', 'Automower Location Latitude') | |
gauge_long = Gauge('automower_long', 'Automower Location Longitude') | |
def get_location(): | |
r = requests.get(DATA_API) | |
# [{'latitude': 89.0499532, 'longitude': -100.8958507, 'gpsStatus': 'USING_GPS_MAP'}] | |
data = r.json()['lastLocations'] | |
# {'latitude': 89.0499532, 'longitude': -100.8958507, 'gpsStatus': 'USING_GPS_MAP'} | |
loc0 = data[0] | |
return loc0 | |
def location_changed(current_latitude, current_longitude, previous_latitude, previous_longitude): | |
if current_latitude == previous_latitude and current_longitude == previous_longitude: | |
print('Location has not changed') | |
return False | |
else: | |
print('Location has changed') | |
return True | |
def write_metric(latitude, longitude): | |
gauge_lat.set(latitude) | |
gauge_long.set(longitude) | |
previous_latitude = 0 | |
previous_longitude = 0 | |
# Write metrics if the location has changed. | |
while True: | |
loc0 = get_location() | |
current_latitude = loc0['latitude'] | |
current_longitude = loc0['longitude'] | |
if location_changed(current_latitude, current_longitude, previous_latitude, previous_longitude): | |
write_metric(current_latitude, current_longitude) | |
previous_latitude = current_latitude | |
previous_longitude = current_longitude | |
time.sleep(TIME_BETWEEN_CHECKS_SECONDS) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment