-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
54 lines (39 loc) · 1.52 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
import toml
import uvicorn
import logging, logging.config, yaml
from transports import mqtt
from utils import utils
config_data = toml.load("config.toml")
client_id = utils.build_client_id()
def devicelogger():
return logging.getLogger('file')
def get_logconsole():
return logging.getLogger('console')
if __name__ == "__main__":
# logging.basicConfig(filename='error.log',level=logging.DEBUG)
loader = yaml.safe_load(open('logging.conf'))
logging.config.dictConfig(loader)
logfile = devicelogger()
logconsole = get_logconsole()
logfile.debug("Debug FILE -- App Started")
logconsole.debug("Debug CONSOLE -- App Started")
config_data = toml.load("config.toml")
is_http_enabled = config_data["HTTP"]["is_enabled"]
is_mqtt_enabled = config_data["MQTT"]["is_enabled"]
if is_mqtt_enabled:
mqtt.set_user_pw() # Needed for AWS and MQHIV Brokers
mqtt.connect_to_broker()
mqtt.subscribe_to_topics()
if not is_http_enabled:
mqtt.client.loop_forever()
else:
mqtt.client.loop_start()
else:
print("MQTT is disabled, to adjust this see the config.toml file")
if is_http_enabled:
host = config_data["HTTP"]["host"]
port = config_data["HTTP"]["port"]
log_Level = config_data["HTTP"]["log_Level"]
uvicorn.run("api:app", host=host, port=port, log_level=log_Level, app_dir="./transports")
else:
print("HTTP is disabled, to adjust this see the config.toml file")