71 lines
2.3 KiB
Python
71 lines
2.3 KiB
Python
import requests
|
||
import json
|
||
import os
|
||
from dotenv import load_dotenv
|
||
import time
|
||
from datetime import datetime
|
||
|
||
# Load information for owencloud and telegram
|
||
load_dotenv()
|
||
|
||
# Get Token for OwenCloud
|
||
def get_token():
|
||
login = os.getenv("LOGIN")
|
||
password = os.getenv("PASSWORD")
|
||
if not login or not password:
|
||
raise ValueError("Логин или пароль не заданы в .env")
|
||
|
||
data = {'login': login, 'password': password}
|
||
headers = {
|
||
"Content-Type": "application/json",
|
||
"Accept": "application/json"
|
||
}
|
||
req = requests.post("https://api.owencloud.ru/v1/auth/open/", json=data, headers=headers)
|
||
if req.status_code == 200:
|
||
answer = req.json()
|
||
token = "Bearer " + answer["token"]
|
||
return token
|
||
else:
|
||
print(f"Ошибка авторизации: {req.status_code}, {req.text}")
|
||
return None
|
||
|
||
# Device parsing
|
||
def main():
|
||
token = get_token()
|
||
if not token:
|
||
print("Не удалось получить токен. Завершаем работу.")
|
||
return
|
||
|
||
headers = {'Authorization': token}
|
||
data = json.dumps({'filter': ''})
|
||
|
||
device_ids = {
|
||
"ПБКТ-2 - Ш. 6 линия": "278575",
|
||
"ПБКТ-3 - Ш. 1 линия": "296613"
|
||
}
|
||
|
||
for device_name, device_id in device_ids.items():
|
||
req = requests.post(f"https://api.owencloud.ru/v1/device/last-dates/{device_id}", data=data, headers=headers)
|
||
|
||
if req.status_code != 200:
|
||
print(f"Ошибка запроса устройства {device_name}: {req.status_code}, {req.text}")
|
||
continue
|
||
|
||
nswer = req.json()
|
||
last_dt = int(nswer["last_dt"])
|
||
last_update_time = datetime.fromtimestamp(last_dt)
|
||
current_time = datetime.now()
|
||
|
||
time_difference = current_time - last_update_time
|
||
|
||
print(device_name)
|
||
print(f"Время последнего обновления: {last_update_time.strftime('%Y-%m-%d %H:%M:%S')}")
|
||
|
||
if time_difference.total_seconds() > 300:
|
||
print(f"⚠️ {device_name}: обновление данных задерживается более чем на 5 минут.")
|
||
else:
|
||
print("Обновление в порядке.")
|
||
print('---')
|
||
|
||
if __name__ == "__main__":
|
||
main()
|