From e1a7c4023b39f442bb99729149997a1c32a80b3a Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Sat, 11 Jun 2022 17:39:18 +0300 Subject: [PATCH] sql_app: tools: Iot door emulator for testing in absence of door Signed-off-by: HeshamTB --- sql_app/tools/emulate_door.py | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 sql_app/tools/emulate_door.py diff --git a/sql_app/tools/emulate_door.py b/sql_app/tools/emulate_door.py new file mode 100644 index 0000000..2c7bbe8 --- /dev/null +++ b/sql_app/tools/emulate_door.py @@ -0,0 +1,40 @@ +# EE495 +# Hesham T. Banafa +# Jun 11th, 2022 + +from time import sleep +import requests + + +def poll(poll_url: str, data: dict, headers: dict) -> dict: + res : requests.Response = \ + requests.post(poll_url, json=data, headers=headers) + #print('sent ', data) + print(res.text, res, res.reason) + if res.status_code != 200: return None + return res.json() + +def emulate(poll_url, token_in: str): + mac = "94:b9:7e:fb:57:1a" + polling_interval_secons = 1 + polling_headers = { + 'accept' : 'application/json', + 'Content-Type': 'application/json' + } + stop = False + state = False + while (not stop): + sleep(polling_interval_secons) + data = { + 'bluetooth_mac': mac, + 'state': state, + 'token': token_in + } + data_dict = poll(poll_url, data, polling_headers) + if not data_dict: continue + if data_dict['open_command']: state = True + + +if __name__ == '__main__': + emulate("https://ibs.cronos.typedef.cf:4040/iotdevice/door/status", + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJibHVldG9vdGhfbWFjIjoiOTQ6Yjk6N2U6ZmI6NTc6MWEifQ.oRbL0U70g8HGkKIOnwkesDiB40VWTPmwIWiysvP-hXA") \ No newline at end of file