Add vodynoi and examples
This commit is contained in:
parent
f7787232fc
commit
5895cf6440
6 changed files with 116 additions and 0 deletions
0
json.json
Normal file
0
json.json
Normal file
1
response.txt
Normal file
1
response.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"name":"Bad Request","message":"Syntax error","code":0,"status":400}
|
||||
18
test.sh
Executable file
18
test.sh
Executable file
|
|
@ -0,0 +1,18 @@
|
|||
#!/bin/bash
|
||||
|
||||
url="https://api.owencloud.ru/v1/auth/open"
|
||||
login="v.baldin@vrh.ru"
|
||||
password="балдиНврх"
|
||||
|
||||
# Данные для отправки
|
||||
data="login=$login&password=$password"
|
||||
|
||||
# Выполнение POST-запроса с использованием curl
|
||||
response=$(curl -s -w "%{http_code}" -o response.txt -X POST "$url" \
|
||||
-H "Accept: */*" \
|
||||
-H "Content-Type: application/x-www-form-urlencoded" \
|
||||
-d "$data")
|
||||
|
||||
# Печать кода статуса и тела ответа
|
||||
echo "HTTP Status Code: $response"
|
||||
cat response.txt
|
||||
4
vodynoi/.env
Normal file
4
vodynoi/.env
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
LOGIN=openbsdtop@yandex.ru
|
||||
PASSWORD=D5TnRdGMSM1OvHYHqvmw
|
||||
TELEGRAM_TOKEN=7981938716:AAHim9nsJ9hjMTrU1yTyVjZ_T3fKmf5mvEM
|
||||
TELEGRAM_CHANNEL_ID=-1002486475101
|
||||
4
vodynoi/env-examples
Normal file
4
vodynoi/env-examples
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
LOGIN=openbsdtop@yandex.ru
|
||||
PASSWORD=D5TnRdGMSM1OvHYHqvmw
|
||||
TELEGRAM_TOKEN=7981938716:AAHim9nsJ9hjMTrU1yTyVjZ_T3fKmf5mvEM
|
||||
TELEGRAM_CHANNEL_ID=-1002486475101
|
||||
89
vodynoi/main.py
Normal file
89
vodynoi/main.py
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
import requests
|
||||
import json
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Load information for owencloud and telegram
|
||||
load_dotenv()
|
||||
#Send To Telegram
|
||||
def send_telegram(text: str):
|
||||
token = os.getenv("TELEGRAM_TOKEN")
|
||||
channel_id = os.getenv("TELEGRAM_CHANNEL_ID")
|
||||
if not token or not channel_id:
|
||||
raise ValueError("Telegram токен или ID канала не заданы в .env")
|
||||
|
||||
url = f"https://api.telegram.org/bot{token}/sendMessage"
|
||||
|
||||
r = requests.post(url, data={
|
||||
"chat_id": channel_id,
|
||||
"text": text
|
||||
})
|
||||
print(r.text)
|
||||
if r.status_code != 200:
|
||||
raise Exception(f"Ошибка отправки в Telegram: {r.text}")
|
||||
#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
|
||||
|
||||
device_id = "296613"
|
||||
headers = {'Authorization': token}
|
||||
data = json.dumps({'filter': ''})
|
||||
|
||||
req = requests.post(f"https://api.owencloud.ru/v1/device/{device_id}", data=data, headers=headers)
|
||||
if req.status_code != 200:
|
||||
print(f"Ошибка запроса устройства: {req.status_code}, {req.text}")
|
||||
return
|
||||
|
||||
nswer = req.json()
|
||||
|
||||
if "parameters" not in nswer:
|
||||
print("Не удалось найти параметры в ответе.")
|
||||
return
|
||||
|
||||
parameters = nswer['parameters']
|
||||
|
||||
#Find temperature
|
||||
try:
|
||||
temperature_1 = next(param for param in parameters if param['name'] == "Температура_1 (НИЗ)")
|
||||
temperature_2 = next(param for param in parameters if param['name'] == "Температура_2 (ВЕРХ)")
|
||||
except StopIteration:
|
||||
print("Не удалось найти необходимые параметры.")
|
||||
return
|
||||
|
||||
message = (f"Шача:\n"
|
||||
f"Температура_1 (НИЗ): {temperature_1['value']}\n"
|
||||
f"Температура_2 (ВЕРХ): {temperature_2['value']}")
|
||||
|
||||
print(message)
|
||||
|
||||
# Send to Telegram
|
||||
try:
|
||||
send_telegram(message)
|
||||
except Exception as e:
|
||||
print(f"Ошибка отправки сообщения: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Reference in a new issue