121 lines
No EOL
4.6 KiB
Python
121 lines
No EOL
4.6 KiB
Python
import os
|
|
import sys
|
|
import flask
|
|
import evdev
|
|
import requests
|
|
import time
|
|
import threading
|
|
import subprocess
|
|
import urllib.parse
|
|
|
|
app = flask.Flask(import_name=__name__, template_folder=".")
|
|
|
|
try:
|
|
sys.path.insert(0,'/home/pi/information')
|
|
import config
|
|
except:
|
|
print("Unable to add 'information' folder into system path OR 'config.py' not found")
|
|
|
|
lock = threading.Lock()
|
|
def opener(lck):
|
|
with lck:
|
|
for _ in range(5):
|
|
requests.get("http://192.168.0.103/open")
|
|
|
|
def localtunnel_launcher(port=8000, wanted_subdomain="open-domodoor"):
|
|
result = subprocess.run(args="lt --port {} --subdomain {}".format(port,wanted_subdomain), shell=True, executable="/bin/bash")
|
|
print("Process returned:", result)
|
|
|
|
def remote_handler(lck):
|
|
n_attempts = 0
|
|
remote = None
|
|
while remote is None:
|
|
#---search for remote---
|
|
input_devices = os.listdir("/dev/input")
|
|
for device in input_devices:
|
|
try:
|
|
dev = evdev.InputDevice("/dev/input/"+device)
|
|
if "Wireless Present" in dev.name and "Keyboard" in dev.name and "usb" in dev.phys:
|
|
remote = dev
|
|
print("Remote found")
|
|
break
|
|
except:
|
|
pass
|
|
#---start retrieving events if remote detected---
|
|
if remote is not None:
|
|
try:
|
|
for event in remote.read_loop():
|
|
if event.type==evdev.ecodes.EV_KEY and event.code==evdev.ecodes.KEY_B and event.value==1:
|
|
print("Command received from remote")
|
|
opener(lck)
|
|
except:
|
|
remote = None
|
|
n_attempts += 1
|
|
print("Attempt "+str(n_attempts)+" fails in finding remote")
|
|
time.sleep(3)
|
|
|
|
#------ENDPOINTS------
|
|
|
|
@app.route(rule="/")
|
|
def index():
|
|
return flask.render_template("index.html")
|
|
|
|
@app.route(rule="/state")
|
|
def state_checker():
|
|
return "200 OK (working)\n"
|
|
|
|
@app.route(rule="/from_internet", methods=["POST"])
|
|
def receiver_from_internet():
|
|
data = flask.request.get_data(as_text=True)
|
|
data_dict = urllib.parse.parse_qs(qs=data)
|
|
print(data_dict)
|
|
if "pwd_retriever" in data_dict and data_dict["pwd_retriever"][0]=='please':
|
|
op = threading.Thread(target=opener, args=(lock,))
|
|
op.start()
|
|
return flask.render_template("success.html")
|
|
else:
|
|
return flask.render_template("fail.html")
|
|
|
|
@app.route(rule="/receiving", methods=["POST"])
|
|
def receiver():
|
|
data = flask.request.get_data(as_text=True)
|
|
data_dict = urllib.parse.parse_qs(qs=data)
|
|
for p in data_dict: data_dict[p]=data_dict[p][0]
|
|
print(data_dict)
|
|
response_events = {"events":[]}
|
|
#---action=incoming---
|
|
if "action" in data_dict and data_dict["action"]=="incoming":
|
|
if "please" in data_dict["message"].lower():
|
|
op = threading.Thread(target=opener, args=(lock,))
|
|
op.start()
|
|
response_events["events"].append({"event":"log","message":"Server received "+data_dict["message_type"]+" from "+data_dict["from"]})
|
|
return flask.jsonify(response_events)
|
|
#---action=outgoing---
|
|
if "action" in data_dict and data_dict["action"]=="outgoing":
|
|
response_events["events"].append({"event":"log","message":"Server received outgoing action"})
|
|
return flask.jsonify(response_events)
|
|
#---action=send_status---
|
|
if "action" in data_dict and data_dict["action"]=="send_status":
|
|
response_events["events"].append({"event":"log","message":"Server received send_status action"})
|
|
return flask.jsonify(response_events)
|
|
#---action=forward_sent---
|
|
if "action" in data_dict and data_dict["action"]=="forward_sent":
|
|
response_events["events"].append({"event":"log","message":"Server received forward_sent action"})
|
|
return flask.jsonify(response_events)
|
|
#---action=amqp_started---
|
|
if "action" in data_dict and data_dict["action"]=="amqp_started":
|
|
response_events["events"].append({"event":"log","message":"Server received amqp_started action"})
|
|
return flask.jsonify(response_events)
|
|
#---action=device_status---
|
|
if "action" in data_dict and data_dict["action"]=="device_status":
|
|
response_events["events"].append({"event":"log","message":"Server received notification of "+data_dict["status"]})
|
|
return flask.jsonify(response_events)
|
|
return flask.abort(400)
|
|
|
|
|
|
if __name__=="__main__":
|
|
remote_handler_thread = threading.Thread(target=remote_handler, args=(lock,))
|
|
remote_handler_thread.start()
|
|
localtunnel_thread = threading.Thread(target=localtunnel_launcher)
|
|
localtunnel_thread.start()
|
|
app.run(host="0.0.0.0", port=8000) |