def menu(): print("[", current_time, "]", " ====Encode/Decode===") # Print the time & a title optionmenu = (input(""" -------------------------------- | [1] Decode | | [2] Encode | | [3] Exit | --------------------------------\n""")) if optionmenu == "1": # Decode optie2 = (input(""" Do you want to decode from a file or manual input ? [1] From file [2] From manual input\n""")) if optie2 == "1": text = Path('text2decode.txt').read_text() # read from a text file text = text.replace('\n', '') decoder.break_text(text) decoder.decode_text(text) print("We've hacked the code, here is the decoded version:") print("") decoder.get_decode(text) elif optie2 == "2": text = input("Input the text you want to decode\n") if num_there(text): print( "error! your text contains numbers - Returning to the menu" ) # Check for numbers time.sleep(2) menu() decoder.break_text(text) decoder.decode_text(text) print("we've hacked the code, here is the decoded version:") print("") decoder.get_decode(text) elif optie2 != "1" or "2": print("Invalid option we will return you to the menu" ) # return to the menu menu() elif optionmenu == "2": # encoding optie2 = (input(""" Do you want to encode from a file or manual input ? [1] From file [2] From manual input\n""")) if optie2 == "1": text = Path('text2encode.txt').read_text() # read from a text file text = text.replace('\n', '') decoder.break_text(text) decoder.code_text(text) print("We've coded your top secret message:") print("") decoder.get_code(text) elif optie2 == "2": text = input("Input the text you want to code:\n") if num_there(text): print( "error! your text contains numbers - Returning to the menu" ) time.sleep(2) menu() else: if any(char in invalidcharacters for char in text): print( "error! your text contains special characters - Returning to the menu" ) time.sleep(2) menu() decoder.break_text(text) decoder.code_text(text) print("We've coded your top secret message:") print("") decoder.get_code(text) elif optie2 != "1" or "2": print("Invalid option we will return you to the menu" ) # return to the menu menu() elif optionmenu == "3": print("See you next time!") sys.exit(0) elif optionmenu != "1" or "2" or "3" or "4": print("Error! not a valid input - Please try again") time.sleep(1) print("") print("") menu()
conn.send('#T') #send ack if (data_p[1] == 'Picture'): with open('test.bmp', 'wb') as f: print "Opened test.bmp" while True: data = conn.recv(BUFFER_SIZE) if not data: f.close() print "file closed" break f.write(data) if (data_p[1] == 'Auth'): if (len(data_p) >= 2): print "status changed to " + data_p[2] user_code = decoder.get_code(image_name) #decoder.log("log-uploadviagateway",data_p[0],data_p[2],user_code) #if(gcloud_demo.claim_space(data_p[0],user_code)): if (not random.getrandbits(1)): #decoder.upload_blob("images-uploadviagateway",image_name,data_p[0] + "_image.jpg") conn.send('#A') #send ack print "Sent Auth" else: conn.send('#D') #send deny print "Sent Deny" except socket.error: print socket.error print "The socket erorr occurred" pass except KeyboardInterrupt: print "Ctrl+C pressed"
def SenorTowerToBuffer(conn, SensorTower, cloud_queue): #receive data from sensor tower. Blocks for now !!. data = conn.recv(1024) #set sleep flags high that must be low in order for the sensor tower to sleep sleep_flag_car_event = High sleep_flag_photo_request = High #parse data parsed_data = data.split(',') spot_id = int(parsed_data[0]) event = parsed_data[1] #set some varaibles equal to the exsisting class struct SensorTower[spot_id].conn = conn SensorTower[spot_id].address = address SensorTower[spot_id].established = True status = parsed_data[2] if event == 'StatusChanged': if(status == 'Taken'): print("Spot status changed to taken") conn.send("#P") #data = conn.recv(1024) elif(status == 'Empty'): cloud_queue.put(spot_id, status, 0) conn.send("#A") else: print("ERROR") print() elif event == 'Picture': with open('test.bmp','wb') as f: while(True): data = conn.recv(BUFFER_SIZE) if not data: f.close(); print "file closed" break f.write(data) print("Got picture") elif event == 'AnyTasks': print() elif event == 'Auth': cloud_queue.put(spot_id, status, decoder.get_code("test.bmp")) while True: #print("taken and waiting on cloud") if SensorTower[spot_id].cloud_auth_status != NOT_UPDATED: print("got cloud auth, sending back to sensorB") if(SensorTower[spot_id].cloud_auth_status == 1): conn.send("#A") print("#A") conn.close() print("disconnected") return if(SensorTower[spot_id].cloud_auth_status == 0): conn.send("#D") print("#D") conn.close() print("disconnected") return SensorTower[spot_id].cloud_auth_status = NOT_UPDATED sleep_flag_car_event = Low break else print("ERROR") #if the sleep flags are both low then send an ack back to let the sensor tower know it can sleep print(sleep_flag_car_event) print(sleep_flag_photo_request) if sleep_flag_car_event == Low and sleep_flag_photo_request == Low: conn.send('#A') print("tell sensor tower to sleep") conn.close() print("disconnected")
import decoder qr=decoder.get_code("test.bmp") print qr
def BufferToCloud(cloud_queue, SensorTower): # checks if the cloud queue isnt empty and forwards the data at the top of the FIFO queue to the cloud while True: if not cloud_queue.empty: #blocks if it is empty spot_id, status, qr_code = cloud_queue.get() print("about to push to cloud") #if its taken, forward to cloud with cloud API ClaimSpace(), and set return to the cloud authentication status if status == 'taken': #qr_code = "1234" #request image #save image SensorTower[spot_id].cloud_auth_status = gcloud_demo.claim_space(spot_id,decoder.get_code("test.bmp")) # !! claim space in database from the specific spaceID and qrCode for authentication print("pushed taken to cloud") #if its empty, forward to cloud with cloud API FreeSpace() elif status == 'empty': cloud_demo.free_space(spot_id) # SensorTower[spot_id].cloud_auth_status = 1 print("pushed empty to cloud")