Question: Need help improving and fixing this code to make it more efficient and make the loop work properly the issue with the script is that
Need help improving and fixing this code to make it more efficient and make the loop work properly
the issue with the script is that it enters an infinite loop while waiting for the user to approve the authentication request. This means that the script will continue to check the status of the request indefinitely, even after the user has approved it. This can be a problem if the user needs to approve multiple requests or if the script needs to handle multiple users.
To fix this issue, the script needs to be modified to include a mechanism to stop the loop once the user has approved the request. One approach is to include a timer that will terminate the loop after a certain amount of time (e.g., 10 seconds), even if the user has not yet approved the request. Another approach is to use a flag variable to track whether the user has approved the request and exit the loop once the flag is set to true.
from authy.api import AuthyApiClient import serial import time import sys import io import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.OUT) GPIO.setup(27, GPIO.OUT)
arduino = serial.Serial('/dev/ttyACM0', 9600) #arduino_data = arduino.readline()
data =[] for i in range(50): b = arduino.readline() string_n = b.decode() string = string_n.rstrip() s = str(string) print(s) data.append(s) time.sleep(0.1) if s == " 21 F8 7C 26": authy_api = AuthyApiClient('jtJEfq9PPAdHOALNrt8FsVescO1C6RrZ')
response = authy_api.one_touch.send_request( 593152871, #adam "Requesting login for Encation", seconds_to_expire=120) if response.ok(): print(response.get_uuid()) else: print(response.errors())
time.sleep(8)
old_stdout = sys.stdout sys.stdout = buffer = io.StringIO()
if response.ok(): status = authy_api.one_touch.get_approval_status(response.get_uuid()) print(status.content) sys.stdout = old_stdout statuscontent = buffer.getvalue() print(statuscontent) index = statuscontent.find("approved") if index == 339: print('yes') GPIO.output(17, True) GPIO.output(27, True) time.sleep(5) GPIO.output(17, False) GPIO.output(27, False) else: print('no') if s == " D3 99 12 2E": authy_api = AuthyApiClient('jtJEfq9PPAdHOALNrt8FsVescO1C6RrZ')
response = authy_api.one_touch.send_request( 596467975, #rachel "Requesting login for Encation", seconds_to_expire=120) if response.ok(): print(response.get_uuid()) else: print(response.errors())
#time.sleep(8)
old_stdout = sys.stdout sys.stdout = buffer = io.StringIO() #sys.stdout = old_stdout #statuscontent = buffer.getvalue()
if response.ok(): status = authy_api.one_touch.get_approval_status(response.get_uuid()) print(status.content) sys.stdout = old_stdout statuscontent = buffer.getvalue() print(statuscontent) index = statuscontent.find("approved") #halp = statuscontent.find("pending") #print(halp) #print(index) #n = 10 #while halp == 339: # status = authy_api.one_touch.get_approval_status(response.get_uuid()) # print(status.content) # sys.stdout = old_stdout # statuscontent = buffer.getvalue() # halp = statuscontent.find("pending") # print(halp) # while n > 0: # status = authy_api.one_touch.get_approval_status(response.get_uuid()) # print(status.content) # sys.stdout = old_stdout # statuscontent = buffer.getvalue() # halp = statuscontent.find("pending") # print(halp) # time.sleep(1) # n -= 1 # break n = 10 while index != 339: #print(index) old_stdout = sys.stdout sys.stdout = buffer = io.StringIO() if response.ok(): status = authy_api.one_touch.get_approval_status(response.get_uuid()) print(status.content) sys.stdout = old_stdout statuscontent = buffer.getvalue() print(index) print(statuscontent) index = statuscontent.find("approved") time.sleep(1) #n -= 1 if index == 339: print('yes') GPIO.output(17, True) GPIO.output(27, True) time.sleep(5) GPIO.output(17, False) GPIO.output(27, False) else: print('no')
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
