-
Notifications
You must be signed in to change notification settings - Fork 0
/
toElastic.py
executable file
·42 lines (34 loc) · 1.18 KB
/
toElastic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python3
import os
import json
import logging
from traeger import Traeger
from dotenv import load_dotenv, find_dotenv
from time import sleep
from pprint import pprint as pp
from datetime import datetime
from elasticsearch import Elasticsearch
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO)
load_dotenv(find_dotenv())
es_logger = logging.getLogger('elasticsearch')
es_logger.setLevel(logging.WARNING)
username = os.getenv("USERNAME")
password = os.getenv("PASSWORD")
myThing = os.getenv("MYTHING")
es = Elasticsearch(os.getenv("ELASTIC"))
def toElastic(client, userdata, message):
if message.topic.startswith("prod/thing/update/"):
data = json.loads(message.payload)
data.pop("custom_cook")
data['timestamp'] = datetime.utcnow()
res = es.index(index="test-index", doc_type="_doc", body=data)
print(f"Got Update for {myThing}")
t = Traeger(username, password)
t.message_callback_add(f'prod/thing/update/{myThing}', toElastic)
while True:
t.sendCommand(myThing, "90")
status = t.getStatus(myThing)
if status and status['status']['connected']:
sleep(10)
else:
sleep(120)