forked from MeshAddicts/meshinfo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
58 lines (46 loc) · 1.64 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
#!/usr/bin/env python3
import asyncio
import datetime
import json
from zoneinfo import ZoneInfo
import os
import discord
from dotenv import load_dotenv
from api import api
from bot import discord as discord_bot
from config import Config
from memory_data_store import MemoryDataStore
from mqtt import MQTT
load_dotenv()
config = Config.load()
data = MemoryDataStore(config)
data.update('mqtt_connect_time', datetime.datetime.now(ZoneInfo(config['server']['timezone'])))
async def main():
global config
global data
# output app banner from file banner
banner = open('banner', 'r').read()
print(banner)
version = json.loads(open('version.json', 'r').read())
print(f"Version: {version['version']} (git sha: {version['git_sha']})")
print()
if not os.path.exists(config['paths']['output']):
os.makedirs(config['paths']['output'])
if not os.path.exists(config['paths']['data']):
os.makedirs(config['paths']['data'])
os.environ['TZ'] = config['server']['timezone']
data.load()
await data.save()
# await data.backup()
async with asyncio.TaskGroup() as tg:
loop = asyncio.get_event_loop()
api_server = api.API(config, data)
tg.create_task(api_server.serve(loop))
if config['broker']['enabled'] is True:
mqtt = MQTT(config, data)
tg.create_task(mqtt.connect())
if config['integrations']['discord']['enabled'] is True:
bot = discord_bot.DiscordBot(command_prefix="!", intents=discord.Intents.all(), config=config, data=data)
tg.create_task(bot.start_server())
if __name__ == "__main__":
asyncio.run(main())