Handling errors cases

This commit is contained in:
chabisik 2022-12-22 09:06:28 +01:00
parent 7b234fc708
commit d75122ad5c

View file

@ -12,8 +12,9 @@ app = flask.Flask(import_name=__name__, template_folder=".")
try: try:
sys.path.insert(0,'/home/pi/information') sys.path.insert(0,'/home/pi/information')
import config
except: except:
print("Unable to add 'information' folder into system path") print("Unable to add 'information' folder into system path OR 'config.py' not found")
lock = threading.Lock() lock = threading.Lock()
def opener(lck): def opener(lck):
@ -83,32 +84,33 @@ def receiver():
print(data_dict) print(data_dict)
response_events = {"events":[]} response_events = {"events":[]}
#---action=incoming--- #---action=incoming---
if data_dict["action"]=="incoming": if "action" in data_dict and data_dict["action"]=="incoming":
if "please" in data_dict["message"].lower(): if "please" in data_dict["message"].lower():
op = threading.Thread(target=opener, args=(lock,)) op = threading.Thread(target=opener, args=(lock,))
op.start() op.start()
response_events["events"].append({"event":"log","message":"Server received "+data_dict["message_type"]+" from "+data_dict["from"]}) response_events["events"].append({"event":"log","message":"Server received "+data_dict["message_type"]+" from "+data_dict["from"]})
return flask.jsonify(response_events) return flask.jsonify(response_events)
#---action=outgoing--- #---action=outgoing---
if data_dict["action"]=="outgoing": if "action" in data_dict and data_dict["action"]=="outgoing":
response_events["events"].append({"event":"log","message":"Server received outgoing action"}) response_events["events"].append({"event":"log","message":"Server received outgoing action"})
return flask.jsonify(response_events) return flask.jsonify(response_events)
#---action=send_status--- #---action=send_status---
if data_dict["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"}) response_events["events"].append({"event":"log","message":"Server received send_status action"})
return flask.jsonify(response_events) return flask.jsonify(response_events)
#---action=forward_sent--- #---action=forward_sent---
if data_dict["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"}) response_events["events"].append({"event":"log","message":"Server received forward_sent action"})
return flask.jsonify(response_events) return flask.jsonify(response_events)
#---action=amqp_started--- #---action=amqp_started---
if data_dict["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"}) response_events["events"].append({"event":"log","message":"Server received amqp_started action"})
return flask.jsonify(response_events) return flask.jsonify(response_events)
#---action=device_status--- #---action=device_status---
if data_dict["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"]}) response_events["events"].append({"event":"log","message":"Server received notification of "+data_dict["status"]})
return flask.jsonify(response_events) return flask.jsonify(response_events)
return flask.abort(400)
if __name__=="__main__": if __name__=="__main__":