-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
64 lines (54 loc) · 1.67 KB
/
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import asyncio
import ssl
import json
import pprint
import os
import logging
from typing import Dict, List
import socket
import shelve
import websockets
from zeroconf import ServiceBrowser, Zeroconf
from config import logging_format, server_uri, ultimaker_credentials_filename
from printers import PrinterListener
from sign import Sign
logging.basicConfig(level=logging.INFO, format=logging_format)
X_API_KEY = os.environ['X_API_KEY']
SLEEP_TIME: int = 2
ssl_context = ssl.create_default_context()
sign_pins = Sign.setup()
zeroconf = Zeroconf()
shelf = shelve.open(ultimaker_credentials_filename)
listener = PrinterListener(shelf)
browser = ServiceBrowser(zeroconf, "_ultimaker._tcp.local.", listener)
async def send_status():
async with websockets.connect(server_uri, ssl=ssl_context) as websocket:
while True:
logging.info(f'Preparing update')
status_json_dict = {
'printers': listener.printer_jsons(),
'sign': sign_pins.as_value_dict(),
'key': X_API_KEY
}
status_json_str = json.dumps(status_json_dict)
logging.info(
f'Sending update {pprint.pformat(status_json_dict, depth=3, compact=True)}'
)
await websocket.send(status_json_str)
logging.info(f'Update complete, sleeping for a bit...')
shelf.sync()
await asyncio.sleep(SLEEP_TIME)
loop = asyncio.get_event_loop()
try:
while True:
try:
loop.run_until_complete(send_status())
except Exception as serr:
if type(serr) is KeyboardInterrupt:
raise serr
logging.warning(
f"Exception while sending status, attempting to connect and send again: {serr}"
)
finally:
shelf.close()
zeroconf.close()