-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
27 lines (24 loc) · 941 Bytes
/
main.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
from sseclient import SSEClient
import requests
import json
import re
import requests
from .ditto_threads import DittoCurrentStateThread
import os
from dotenv import load_dotenv
load_dotenv()
DITTO_THING_SUFIX_REGEX = os.getenv('DITTO_THING_SUFIX_REGEX')
DITTO_BASE_URL = os.getenv('DITTO_BASE_URL')
url = DITTO_BASE_URL + '/api/2/things'
hearders = {'Accept': 'text/event-stream'}
response = requests.get(url=url, stream=True, headers=hearders)
client = SSEClient(response)
for event in client.events():
if event.data:
data = json.loads(event.data)
print(data['thingId'])
split = data['thingId'].split(':')
if len(data) > 1 and re.fullmatch(DITTO_THING_SUFIX_REGEX, split[1]):
thread = DittoCurrentStateThread(split[1])
thread.run()
#else: thread = dittoGeneralInfoThread(split[1]) #it will commented until it is found if it is necessary