2020-09-07 21:48:16 +00:00
|
|
|
import importlib
|
|
|
|
import threading
|
|
|
|
import time
|
2020-09-09 01:52:50 +00:00
|
|
|
import traceback
|
2020-09-21 01:08:23 +00:00
|
|
|
import json
|
2020-09-07 21:48:16 +00:00
|
|
|
|
2020-09-06 21:39:21 +00:00
|
|
|
from flask import Flask
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
2020-10-14 02:26:50 +00:00
|
|
|
from munch import Munch
|
2020-09-07 21:48:16 +00:00
|
|
|
from watchdog.observers import Observer
|
|
|
|
from watchdog.events import PatternMatchingEventHandler
|
2020-09-06 22:10:00 +00:00
|
|
|
|
2020-09-07 21:48:16 +00:00
|
|
|
import bot
|
2020-09-08 21:33:42 +00:00
|
|
|
|
2020-10-14 02:26:50 +00:00
|
|
|
global_state = Munch()
|
2020-09-07 21:48:16 +00:00
|
|
|
g = global_state
|
2020-09-08 01:28:56 +00:00
|
|
|
g.connection = False
|
2020-09-09 01:52:50 +00:00
|
|
|
g.mcdata = False
|
2020-09-08 21:33:42 +00:00
|
|
|
g.pos = False
|
2020-09-09 21:58:19 +00:00
|
|
|
g.inv = {}
|
2020-10-14 02:26:50 +00:00
|
|
|
g.objects = {}
|
2020-09-25 06:03:22 +00:00
|
|
|
g.window = None
|
|
|
|
g.job = None
|
2020-10-19 21:49:14 +00:00
|
|
|
g.correction_count = 0
|
2020-12-02 11:12:51 +00:00
|
|
|
g.holding = 0
|
2020-09-06 22:10:00 +00:00
|
|
|
|
2020-09-06 21:39:21 +00:00
|
|
|
@app.route('/')
|
|
|
|
def hello_world():
|
2020-09-21 01:08:23 +00:00
|
|
|
data = json.dumps(g, default=lambda o: str(o), indent=4)
|
|
|
|
|
|
|
|
response = app.response_class(
|
|
|
|
response=data,
|
|
|
|
status=200,
|
|
|
|
mimetype='application/json'
|
|
|
|
)
|
|
|
|
return response
|
2020-09-06 22:10:00 +00:00
|
|
|
|
2020-09-07 21:48:16 +00:00
|
|
|
reload_timeout = time.time()
|
2020-09-06 22:10:00 +00:00
|
|
|
|
2020-09-07 21:48:16 +00:00
|
|
|
def main():
|
|
|
|
def reload_bot(event):
|
|
|
|
global reload_timeout
|
2020-09-08 01:28:56 +00:00
|
|
|
if time.time() - reload_timeout > 2.0:
|
2020-09-07 21:48:16 +00:00
|
|
|
reload_timeout = time.time()
|
|
|
|
print('Reloading...')
|
2020-09-08 01:28:56 +00:00
|
|
|
g.running = False
|
2020-09-06 22:43:20 +00:00
|
|
|
|
2020-09-07 21:48:16 +00:00
|
|
|
event_handler = PatternMatchingEventHandler(patterns=['*.py'], ignore_patterns=['./main.py'])
|
|
|
|
event_handler.on_any_event = reload_bot
|
2020-09-06 22:10:00 +00:00
|
|
|
|
2020-09-07 21:48:16 +00:00
|
|
|
observer = Observer()
|
|
|
|
observer.schedule(event_handler, '.', recursive=True)
|
|
|
|
observer.start()
|
2020-09-06 22:10:00 +00:00
|
|
|
|
2020-09-07 21:48:16 +00:00
|
|
|
try:
|
|
|
|
while True:
|
2020-09-09 01:52:50 +00:00
|
|
|
try:
|
|
|
|
g.running = True
|
|
|
|
bot.bot(global_state)
|
|
|
|
importlib.reload(bot)
|
|
|
|
except BaseException as e:
|
|
|
|
g.running = True
|
|
|
|
traceback.print_exc()
|
|
|
|
print('Locking...')
|
|
|
|
while g.running:
|
|
|
|
time.sleep(1)
|
2020-09-09 21:58:19 +00:00
|
|
|
importlib.reload(bot)
|
2020-09-07 21:48:16 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
observer.stop()
|
|
|
|
observer.join()
|
2020-09-06 22:10:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-09-07 21:48:16 +00:00
|
|
|
threading.Thread(target=app.run).start()
|
2020-09-08 04:31:55 +00:00
|
|
|
time.sleep(1)
|
2020-09-06 22:10:00 +00:00
|
|
|
main()
|