-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
61 lines (49 loc) · 1.95 KB
/
main.py
File metadata and controls
61 lines (49 loc) · 1.95 KB
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
import logging
import time
import config
import multiprocessing
import MachineBrain
from TelegramBot import CrownTelegramBot
from MachineBrain import MachineBrain
from events import EventQueue, EventTypes, Event
from Database import Database
def main():
"""Main entry point of the program. Imagine setup() and loop() in Arduino."""
if (config.DEVELOPMENT):
logging.basicConfig(filename="CrownPiBot.log", level=logging.DEBUG)
logging.getLogger().addHandler(logging.StreamHandler())
logging.debug("PC - Setup started.")
# Check / Init SQliteDB
db = Database()
db.check_or_init_db()
db.check_or_init_tables()
# Create Crown <> TG communication queue
queue = multiprocessing.Queue()
crown_brain = MachineBrain(queue)
telegram_bot = CrownTelegramBot()
telegram_bot.init_telegram_bot(queue)
logging.debug("PC - Setup finished.")
loop(crown_brain, telegram_bot, queue)
def loop(CrownBrain, TelegramBot, CrownTelegramQueue):
"""Loop the machine's life."""
while (True):
# Check if the Telegram bot is still running.
if (TelegramBot.enabled):
TelegramBot.check_telegram_bot()
# Check if there are any events in the CrownTelegramQueue
while not CrownTelegramQueue.empty():
event = CrownTelegramQueue.get()
CrownBrain.event_queue.add_event(event)
# Handle events from the queue.
while not CrownBrain.event_queue.empty():
event = CrownBrain.event_queue.get_event_to_handle()
if event is not None:
CrownBrain.handle_event(event)
# Advance the machine's state, maybew could be moved.
CrownBrain.advance()
time.sleep(1)
# If the queue is empty, add an idle event to prevent the machine from freezing.
CrownBrain.event_queue.add_event(Event(EventTypes.MACHINE_IDLE))
# Actually run the program...
if __name__ == "__main__":
main()