def __init__(self): self.current_milli_time = lambda: int(round(time.time() * 1000)) self.pressed_array = [] self.released_array = [] self.db_handler = DatabaseHandler() self.last_event = 0 self.authenticate = False
def give_test(self): subjects_list = DatabaseHandler().get_subjects_list() correct_answers = int(self.students_list[self.name]['Correct']) incorrect_answers = int(self.students_list[self.name]['Incorrect']) print(subjects_list) subject = input('Enter Subject: ') if subject not in subjects_list: raise Exception('No Such Subject Plz choose the right one') quizes = DatabaseHandler().get_subject_quiz(subject) for quiz_name, quiz_data in quizes.items(): print(quiz_data['Question']) for i in range(len(quiz_data['Options'])): print(f"{i+1}. {quiz_data['Options'][i]}") answer = int(input('Choose your answer: ')) answer = quiz_data['Options'][answer - 1] if answer == quiz_data['Answer']: print('Correct Answer') correct_answers += 1 else: print('Incorrect Answer') incorrect_answers += 1 self.students_list[self.name]['Correct'] = str(correct_answers) self.students_list[self.name]['Incorrect'] = str(incorrect_answers) DatabaseHandler().update_students_list(self.students_list)
def add_problems(self): quizzes_list = DatabaseHandler().get_tests_list() subject_name = input("Enter Subject Name: ").strip() quiz_name = input("Enter Quiz name: ").strip() question = input("Enter question: ").strip() options = [] for i in range(4): temp = input(f"Enter option {i}: ").strip() options.append(temp) answer = input('Enter answer for the question : ') if subject_name not in quizzes_list.keys(): print('No Such Subject Exists') # raise Exception('No such subject found') new_quiz_data = { "author": self.name, "Question": question, "Options": options, "Answer": answer } if subject_name not in quizzes_list: quizzes_list[subject_name] = dict() quizzes_list[subject_name][quiz_name] = new_quiz_data DatabaseHandler().add_new_quiz(quizzes_list)
def delete(self, entry_id): db = DatabaseHandler() success = db.delete_entry(entry_id) if success: return '', 204, {'Access-Control-Allow-Origin': '*'} else: api.abort(500, 'Server failed to upsert entry.')
def load_from_api(n): temp_json_loader = JsonLoader() temp_json_loader.load_data_from_api(n) temp_json_loader.modify_file() temp_database_handler = DatabaseHandler() temp_database_handler.insert_data_into_person( temp_json_loader.data["results"]) click.echo("Api loaded")
def check_that_user_liked_all_photos(self, instagram_username): database_handler = DatabaseHandler() links = database_handler.get_latest_photos(20) for link in links: usernames = self.get_likes(str(link[0])) if not str(instagram_username) in usernames: return False return True
def add_teacher(**kwargs): # check for name and pass if not 'name' in kwargs.keys() or not 'password' in kwargs.keys(): raise Exception('Not enough data to create such Teacher') elif Teacher.exists(kwargs['name']): raise Exception('Teacher already exists') teachers_list = DatabaseHandler().get_teachers_list() teachers_list[kwargs['name']] = kwargs['password'] DatabaseHandler().update_teachers_list(teachers_list)
def write(self): mkdir_p(self.path) db_handler = DatabaseHandler("SampleDB", pkg=self.pkg) provider = Provider(classname="ItemProvider", pkg=self.pkg) db_triggers = DatabaseTriggers(pkg=self.pkg) db_triggers.add(*self.triggers) db_views = DatabaseViews(pkg=self.pkg) db_views.add(*self.views) # Generate dbitem files for table in self.tables: item = DBItem(table, pkg=self.pkg) filename = item.classname + ".java" fpath = os.path.join(self.path, filename) with open(fpath, 'w') as javafile: javafile.write(str(item)) # Add to other classes db_handler.add_dbitems(item) provider.add_dbitems(item) # Abstract DBItem fpath = os.path.join(self.path, "DBItem.java") with open(fpath, 'w') as javafile: javafile.write(dbitem.DBITEM_CLASS.format(pkg=self.pkg)) # Triggers fpath = os.path.join(self.path, "DatabaseTriggers.java") with open(fpath, 'w') as javafile: javafile.write(str(db_triggers)) # Views fpath = os.path.join(self.path, "DatabaseViews.java") with open(fpath, 'w') as javafile: javafile.write(str(db_views)) # Database handler fpath = os.path.join(self.path, db_handler.classname + ".java") with open(fpath, 'w') as javafile: javafile.write(str(db_handler)) # Provider fpath = os.path.join(self.path, provider.classname + ".java") with open(fpath, 'w') as javafile: javafile.write(str(provider)) # And print manifest stuff self.print_manifest(provider)
def tests_by_me(self): # add tests to check this method all_tests = DatabaseHandler().get_tests_list() my_tests = dict() for subject_name, subject_data in all_tests.items(): for name, test_data in subject_data.items(): if test_data['author'] == self.name: my_tests[name] = test_data return my_tests
def add_student(**kwargs): # check for name and pass if not 'name' in kwargs.keys() or not 'password' in kwargs.keys(): raise Exception('Not enough data to create such Student') elif Student.exists(kwargs['name']): raise Exception('Student already exists') students_list = DatabaseHandler().get_students_list() students_list[kwargs['name']] = {'Correct': 0, 'Incorrect': 0} students_list[kwargs['name']]['password'] = kwargs['password'] DatabaseHandler().update_students_list(students_list)
class KeyEventLogger: __metaclass__ = Singleton def __init__(self): self.current_milli_time = lambda: int(round(time.time() * 1000)) self.pressed_array = [] self.released_array = [] self.db_handler = DatabaseHandler() self.last_event = 0 self.authenticate = False def calculate(self): # Remove last enter click from equation self.pressed_array.pop(-1) self.released_array.pop(-1) for pressed_time in self.pressed_array: print(pressed_time) for released_time in self.released_array: print(released_time) if self.authenticate: cond = self.db_handler.authenticate(self.pressed_array, self.released_array) else: cond = self.db_handler.register(self.pressed_array, self.released_array) self.pressed_array = [] self.released_array = [] return cond def insert_pressed_event(self): if len(self.pressed_array) is 0: self.last_event = 0 self.pressed_array.insert(0, 0) else: self.pressed_array.insert(len(self.pressed_array), self.current_milli_time() - self.last_event) self.last_event = self.current_milli_time() def insert_released_event(self): if len(self.pressed_array) is not 0: self.released_array.insert(len(self.released_array), self.current_milli_time() - self.last_event) self.last_event = self.current_milli_time() # def backspace_clicked(self): # self.pressed_array.pop(-1) # self.released_array.pop(-1) def mode(self, bool_auth): if bool_auth: self.authenticate = True else: self.authenticate = False def has_pressed(self): return len(self.pressed_array)
def authenticate_user(self): self.logger.mode(1) db = DatabaseHandler() cond = True while cond: username = raw_input("Type your username\n") cond = db.check_available(username) if cond: print("Username does not exist") print("Type your password") listener = KeyListener() listener.stop()
def create_analysis_file(year): data = DatabaseHandler() player_ids = data.get_all_player_ids() with open('nfl_player_variance_%s.csv' % year, 'a') as f: f.write('name,mean_points,variance\n') for i in player_ids: games = data.get_games_by_player(i, year=year) name = data.get_name_by_player_id(i) career_vector = \ [calculate_fantasy_points(data.get_game(game)) for game in games] stats = generate_statistics(career_vector) f.write('%s,%s,%s\n' % (name, stats['mean_points'], stats['variance']))
def __init__(self, username, localip): self.dbhandle = DatabaseHandler() self.scanning_event = { 'openports': {}, 'closed_ports': [], 'os': 'unknown' } self.exploit_events = [] self.post_exploit_events = [] self.username = username self.system_ip = localip self.os = "unknown" self.scanning_record = None
def post(self): data = api.payload entry = EntryDataObject( data['name'], data['category'], data['amount'], data['is_asset'], data['entry_id'] if 'entry_id' in data else None) db = DatabaseHandler() entry_id = db.upsert_entry(entry) if entry_id: entry.entry_id = entry_id return entry.to_json(), 201, {'Access-Control-Allow-Origin': '*'} else: api.abort(500, 'Server failed to upsert entry.')
class TestDatabaseHandler(unittest.TestCase): def setUp(self): self.db = DatabaseHandler() def test_get(self): results = self.db.get_entries() self.assertTrue(results) def test_new_entry(self): new_entry = EntryDataObject('foo', 'bar', 100.35) result = self.db.upsert_entry(new_entry) self.assertTrue(result) # cleanup self.db.delete_entry(result) def test_invalid_entry(self): new_entry = EntryDataObject('foo', 'bar', 'amount') result = self.db.upsert_entry(new_entry) self.assertFalse(result) def test_delete_entry(self): # setup new_entry = EntryDataObject('foo', 'bar', 100) result = self.db.upsert_entry(new_entry) success = self.db.delete_entry(result) self.assertTrue(success) def test_update_entry(self): existing_entry = EntryDataObject('foo', 'bar', 6, False, 8) result = self.db.upsert_entry(existing_entry) self.assertTrue(result)
def consume(first_letter): player_db = DatabaseHandler() players = get_active_players(first_letter) for player in players: first_name = player.get('name', '').split(' ')[0] last_name = player.get('name', '').split(' ')[1] position = player.get('position') url = player.get('link') player_id = player_db.write_player( first_name, last_name, position, url) game_dict = parse_game_log(url) for game in game_dict: player_db.write_game(player_id, game) # Take a rest between players so pro football ref doesn't get upset sleep(1)
def __init__(self): threading.Thread.__init__(self) self.db_hdlr = DatabaseHandler('measurements.db') self.alive = True node0 = [(80, 65), (64, 62), (61, 57), (56, 53), (52, 48), (47, 46), (45, 44), (43, 42), (41, 0)] node1 = [(77, 63), (62, 58), (57, 55), (54, 53), (52, 49), (48, 47), (46, 44), (43, 42), (41, 0)] node2 = [(78, 64), (63, 60), (59, 56), (55, 54), (53, 49), (48, 45), (44, 43), (42, 41), (40, 0)] self.tables = {0 : node0, 1 : node1, 2 : node2}
def new_user(self): self.logger.mode(0) db = DatabaseHandler() cond = False while not cond: username = raw_input("Type desired username\n") cond = db.check_available(username) if not cond: print("Username not available, try another one\n") print("Type your password " + str(NUMBER_SAMPLES) + " times\n") i = 0 while i < NUMBER_SAMPLES: listener = KeyListener() i += 1 if i != NUMBER_SAMPLES: print("Type your password again, only " + str(NUMBER_SAMPLES - i) + " times left\n") listener.stop()
def main(): database_handler = DatabaseHandler() if predict_prices_using_price_parameters: filename = 'estimated_prices.csv' else: filename = 'estimated_prices_based_on_no_price_parameters.csv' try: with open(filename, mode='a') as estimated_prices_file: estimated_prices_writer = csv.writer(estimated_prices_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) for bucket in classification_buckets: model_filename = get_model_filename_b(bucket) model = keras_load_model(model_filename) if predict_prices_using_price_parameters: procedure = 'GetDataToParcelsValuation' else: procedure = 'GetDataToParcelsValuationWithoutPriceParameters' df_parcels_to_valuation = database_handler.execute_query( "EXEC dbo.{} " "@LimitDate = {}, " "@BucketType={}, " "@ExcludedList='{}'".format(procedure, limit_date, parcel_prices_mapping[bucket], excluded_values)) data_size = df_parcels_to_valuation.shape[1] # skip the first attribute - OBJECTID and the last attribute - Sale_Amount, which will be predicted x = df_parcels_to_valuation.iloc[:, 1:data_size - 1] prediction = model.predict(x) for (prediction_value, object_id) in zip(prediction, df_parcels_to_valuation['OBJECTID']): if prediction_value[0] < 0: prediction_value[0] = 0 estimated_prices_writer.writerow( [object_id, np.uint64(round(prediction_value[0], 0))]) finally: database_handler.close_connection()
def __init__(self, **kwargs): self.config = ConfigHandler("config") self.main_db = asyncio.get_event_loop().run_until_complete( DatabaseHandler.create_instance()) self.up_time_start_time = get_current_time() super(Bot, self).__init__(command_prefix=self.prefix_callable, help_command=None, description=self.config["bot_description"], case_insensitive=True, **kwargs)
def __init__(self, email, password, first_name, last_name): self.email = email self.password_not_hashed = password self.password = DatabaseHandler.password_hash(password) self.first_name = first_name self.last_name = last_name self.phone = "" self.admin = False self.address = {}
def add_to_history(): if request.method == 'POST': title = request.form['name'] rating = request.form['rating'] #print("POSTing title: " + title + " with rating: " + rating) movie = match_movie(title, movies_list) movie['Rating'] = rating with DatabaseHandler("user1") as db: db.store(movie) return redirect(url_for('recommendations'))
async def get_coins_helper(user_id: int) -> int: query = "SELECT COINS FROM USERS WHERE ID=?" async with DatabaseHandler.get_connection().execute(query, (user_id, )) as cursor: results = await cursor.fetchone() if results is None: return 0 else: return results[0]
def update_details(self, **kwargs): if 'name' not in kwargs.keys(): kwargs['name'] = self.name if 'password' not in kwargs.keys(): kwargs['password'] = self.password del self.students_list[self.name] self.students_list[kwargs['name']]['password'] = kwargs['password'] self.name = kwargs['name'] self.password = kwargs['password'] DatabaseHandler().update_students_list(self.students_list)
def main(): print("Welcome!") user = input("Enter username: \n") with DatabaseHandler(f"{user}") as db: db.create_table(user) all_rows = db.read_all_rows() #check if there is prior info on user if len(all_rows) > 0: movies_recommended = find_movie(all_rows) print("Passed!! Stage 2 is next") else: print("It seems this is your first time using this program.") first_time(user)
def __init__(self, name, password): if not self.exists(name): raise Exception('No such student exists') self.is_student = True name = name.strip().lower() password = password.strip().lower() self.students_list = DatabaseHandler().get_students_list() if self.students_list[name]['password'] != password: raise Exception('Wrong Credentials') self.name = name self.password = password
def recommendations(): #TO-DO: for some reason the recommendations seem to always be the same. or maybe its me doing it wrong somehow final_recommendations = [] with DatabaseHandler('user1') as handler: movies_seen = handler.read_all_rows() recommended_movies_indexes = get_recommendations(movies_seen)[1] #from the movies list in theater, extract the indexes that match with the ones that the get recommendations returned for i in range(len(recommended_movies_indexes)): for j in range(len(movies_list)): if recommended_movies_indexes[i] == movies_list[j][0]: final_recommendations.append(movies_list[j]) return render_template('recommendations.html', title='Recommended', movies=final_recommendations)
def recommendations(movies_watched, df): cos_sim = find_similarity(df) movies = [] indexes = [] db = DatabaseHandler("user1") #Initialize an empty Series object to sum all the scores across several movies that the user has seen summed_score_series = pd.Series(0, dtype="float64") #TO-DO: Handle case if for some reason no movie is in the list for title in movies_watched: #Find index for the movie index = index_from_title(df, title) #Save all the indexes for the movies that the user has seen (used for filtering later) indexes += indexes_from_title(df, title) #Create a series of all the others titles and their similarity score_series = pd.Series(cos_sim[index - 1]) #Fetch user rating and invert the similarity values if the user did not like the movie rating = db.get_rating(title) print(f"{title} has rating: {rating}") if (rating == -1): score_series = score_series.apply(lambda x: 1 - x) #Add the series to a summed series that aggregates the similarity scores for all movies prev. seen summed_score_series = summed_score_series.add(score_series, fill_value=0) db.close_connection() #Sort the series with the most similar one at index 0 summed_score_series = summed_score_series.sort_values(ascending=False) #Create a list containing the indexes for the top movies. #If no top movie has been seen => 0-10. If one is seen already => 0-11 #TO-DO:Handle if the dataframe containes less movies than we expect top_indexes = list(summed_score_series.iloc[0:(10 + len(indexes))].index) #Remove index for movie already seen. Should result in a list length of 10 top_indexes_filtered = [n for n in top_indexes if n not in indexes] for i in top_indexes_filtered: #print(f'adding movie{(list(df.index)[i])}') movies.append(list(df.index)[i]) return movies[0:10]
def startAnalyzing(self, traffic_type, traffic_trace): ''' This function actually called after pktgen measurements are done, and it instantiate results_analyzer and visualizer class, to analyze the successful result files and create plots of the results :return: ''' tt = traffic_type trace = traffic_trace #to indicate the email_adapter, which traffic type is analyzed is_synthetic = True if tt == "realistic": is_synthetic=False self.log.debug("Analyzing trace (%s,%s)" % (tt,trace)) curframe = inspect.currentframe() calframe = inspect.getouterframes(curframe, 2) self.log.debug('caller name: %s' % calframe[1][3]) # #synthetic and realistic results are process differently, so # #different class variables are used to store the data # Pktgen (re)start(s) finished, analyze results results_analyzer = ResultsAnalyzer(self.config, trafficType=tt, traffic_trace=trace) # after analyzation is done, visualize results results = results_analyzer.getResultsDict() visualizer = Visualizer(config=self.config, results=results, type=tt, traffic_trace=trace) #check whether user wants to store the results in the database if not self.no_database: database_handler = DatabaseHandler(config=self.config, results=results, type=tt, traffic_trace=trace) # send notification email -- Last boolean parameter indicates synthetic case if (self.config['email_adapter'] is not None) and \ (not self.config['email_adapter'].sendResultsMail(trace, is_synthetic)): self.log.warn("Sending email did not succeed...SKIPPING")
class RcvThread(threading.Thread): """A tread class that receives readings over the network""" def __init__(self, cli_sock, node_name): threading.Thread.__init__(self) self.cli_sock = cli_sock self.node_name = node_name self.db_hdlr = DatabaseHandler('measurements.db') self.alive = True def run(self): # Keep the receive streaming socket open fd = self.cli_sock.fileno() while self.alive: # Check if there is something to read on the socket # or if the socket has thrown exceptions for 20s rlist, wlist, xlist = select.select( [fd], [], [fd], 5) if xlist: break if rlist: # Receive data on the socket data = self.cli_sock.recv(7).strip() tag_id = data[:4] rss = data[4:] self.db_hdlr.ins_reading(self.node_name, tag_id, rss) #print data, len(data), self.node_name # Close this client socket self.cli_sock.close() self.db_hdlr.close_db(); def stop(self): self.alive = False def get_node_name(self): return self.node_name
class Main: """A class for establishing connections""" def start_db_hdlr(self, db): self.db_hdlr = DatabaseHandler(db) self.db_hdlr.flush_rss_readings() self.db_hdlr.check_pos_tbl() self.db_hdlr.check_rsl_tbl() def start_net_srv(self): self.net_srv = NetworkServer() self.net_srv.start() def start_serial_conn(self, port, timeout): self.serial_conn = SerialConnection(port, timeout) self.serial_conn.open() self.serial_conn.flush_input() def start_trilat(self): self.trilat = Trilateration() self.trilat.start() def run(self): try: while True: data = self.serial_conn.read().strip() tag_id = data[:4] rss = data[4:] self.db_hdlr.ins_reading(2, tag_id, rss) except KeyboardInterrupt: self.db_hdlr.close_db() self.net_srv.stop() self.net_srv.join(60) self.trilat.stop() self.trilat.join(20) self.serial_conn.close()
def parse_requests(table_name, filename='humans.json'): try: ips, requests = _extract_http_and_time( filename) ##requests is a list of tuple formated_requests = _format_requests( requests ) #get the requests as a dict in the format the database accepts formated_requests_clean = remove_bot_req(formated_requests) with DatabaseHandler(table_name) as db: db.create_table(table_name) for req in formated_requests_clean: db.store(req) return ips except Exception as e: raise e
def first_time(user): user_rating = ask_user() movie_list = get_movie_list() #this part will need to be changed, i added a new class called movies.py so that i dont need to run the scraper everytime to get the movie info order_in_preference = [] for item in user_rating: for movie in movie_list: if item['Genre'] in movie['Genre'] and movie not in order_in_preference: order_in_preference.append(movie) print("--------------------------------------------------------------") print("Based on your prefernces, we recommend that you watch one of the following movies:") limit = min(5,len(order_in_preference)) for i in range (0,limit): print(f"{i+1}. " + order_in_preference[i]['Original_title']) watched = int(input(f"Which movie (1-{limit}) have you watched?\n")) while True: if watched < 1 or watched > limit: watched = int(input(f"Please select a number between 1 and {limit}:\n")) else: break print(f"Your selection ({order_in_preference[watched-1]['Original_title'].strip()}) has been saved!") with DatabaseHandler(user) as db: db.store(order_in_preference[watched-1])
def save_data_in_db(doc): database_handler = DatabaseHandler() database_handler.prepare_query([doc['title'] , doc['link'],doc['image'], doc['desc'], doc['author'], doc['comment'], doc['digest'], str(doc['category']), doc['source'], str(doc['tags'])]) database_handler.executor() database_handler.close()
def start_db_hdlr(self, db): self.db_hdlr = DatabaseHandler(db) self.db_hdlr.flush_rss_readings() self.db_hdlr.check_pos_tbl() self.db_hdlr.check_rsl_tbl()
class TransactionHandler: def __init__(self, username, localip): self.dbhandle = DatabaseHandler() self.scanning_event = { 'openports': {}, 'closed_ports': [], 'os': 'unknown' } self.exploit_events = [] self.post_exploit_events = [] self.username = username self.system_ip = localip self.os = "unknown" self.scanning_record = None def set_open_ports(self, open_ports: dict): if DEBUG: print("Open Ports for " + self.username + "(" + self.system_ip + "): " + str(open_ports)) self.scanning_event['openports'] = open_ports def set_cves(self, cves_data: dict): for port, port_cves in cves_data.items(): try: self.scanning_event['openports'][str(port)]['cves'] = port_cves except Exception as e: print("Cannot find Key for Port " + str(port)) def set_os(self, os_str): self.os = os_str def set_closed_ports(self, closed_ports: list): self.scanning_event['closed_ports'] = closed_ports def add_exploit_event(self, exploit: str, payload: str, engine: str, success: bool, port: int, result: dict): self.exploit_events.append({ 'exploit': exploit, 'payload': payload, 'engine': engine, 'success': success, 'result': result, 'port': port }) def clear_exploit_events(self): self.exploit_events.clear() def add_post_exploit_event(self, post_exploit: str, success: bool, data: str, engine: str): self.post_exploit_events.append({ 'post': post_exploit, 'data': data, 'success': success, 'engine': engine }) def update_db(self): self.scanning_record = self.dbhandle.insert_scanning_log( self.scanning_event['openports'], self.username, self.system_ip, self.os, self.scanning_event['closed_ports'])['event'] if DEBUG: print("Adding Scanning Record: " + str(self.scanning_record)) if self.exploit_events != []: for event in self.exploit_events: exploit_record = self.dbhandle.insert_exploitation_log( self.username, self.system_ip, event['exploit'], event['payload'], event['engine'], str(event['port']), event['success'], self.scanning_record) if DEBUG: print("Exploiting Record: " + str(exploit_record.values())) # event['result']['exploit_event'] = exploit_record if self.post_exploit_events != []: for event in self.post_exploit_events: post_exploit_record = self.dbhandle.insert_post_exploitation_log( self.username, self.system_ip, event['post'], event['data'], event['engine'], event['success'], self.scanning_record) if DEBUG: print("Post Exploitation Record: " + str(exploit_record.values()))
def __init__(self): ## Handles the database queries # # (see database_handler.DatabaseHandler) self._db_handler = DatabaseHandler() # Create new user using platform credentials rapp_add_new_user_from_platform_topic = \ rospy.get_param("rapp_add_new_user_from_platform_topic") if not rapp_add_new_user_from_platform_topic: msg = "Add new user from platform topic does not exist" RappUtilities.rapp_print(msg, 'ERROR') add_new_user_from_platform_srv = \ rospy.Service(rapp_add_new_user_from_platform_topic, AddNewUserFromPlatformSrv, self.add_new_user_from_platform_callback) # Create new user using store credentials rapp_add_new_user_from_store_topic = \ rospy.get_param("rapp_add_new_user_from_store_topic") if not rapp_add_new_user_from_store_topic: msg = "Add new user from store topic does not exist" RappUtilities.rapp_print(msg, 'ERROR') add_new_user_srv = rospy.Service(rapp_add_new_user_from_store_topic, AddNewUserFromStoreSrv, self.add_new_user_from_store_callback) # Token authentication service authenticate_token_topic = \ rospy.get_param("rapp_authenticate_token_topic") if not authenticate_token_topic: rospy.logerr("Application authentication: " + "Token authentication topic does not exist") authenticate_token_service = \ rospy.Service(authenticate_token_topic, UserTokenAuthenticationSrv, self.authenticate_token_callback) # Login using store credentials login_from_store_topic = \ rospy.get_param("rapp_login_user_from_store") if not login_from_store_topic: msg = "Login user from store topic does not exist" RappUtilities.rapp_print(msg, 'ERROR') login_user_from_store_srv = \ rospy.Service(login_from_store_topic, UserLoginSrv, self.login_from_store_callback) # Login login_topic = \ rospy.get_param("rapp_login_user") if not login_topic: msg = "Login user topic does not exist" RappUtilities.rapp_print(msg, 'ERROR') login_user_srv = \ rospy.Service(login_topic, UserLoginSrv, self.login_callback)
class ApplicationAuthenticationManager: ## @brief ROS Service initializations def __init__(self): ## Handles the database queries # # (see database_handler.DatabaseHandler) self._db_handler = DatabaseHandler() # Create new user using platform credentials rapp_add_new_user_from_platform_topic = \ rospy.get_param("rapp_add_new_user_from_platform_topic") if not rapp_add_new_user_from_platform_topic: msg = "Add new user from platform topic does not exist" RappUtilities.rapp_print(msg, 'ERROR') add_new_user_from_platform_srv = \ rospy.Service(rapp_add_new_user_from_platform_topic, AddNewUserFromPlatformSrv, self.add_new_user_from_platform_callback) # Create new user using store credentials rapp_add_new_user_from_store_topic = \ rospy.get_param("rapp_add_new_user_from_store_topic") if not rapp_add_new_user_from_store_topic: msg = "Add new user from store topic does not exist" RappUtilities.rapp_print(msg, 'ERROR') add_new_user_srv = rospy.Service(rapp_add_new_user_from_store_topic, AddNewUserFromStoreSrv, self.add_new_user_from_store_callback) # Token authentication service authenticate_token_topic = \ rospy.get_param("rapp_authenticate_token_topic") if not authenticate_token_topic: rospy.logerr("Application authentication: " + "Token authentication topic does not exist") authenticate_token_service = \ rospy.Service(authenticate_token_topic, UserTokenAuthenticationSrv, self.authenticate_token_callback) # Login using store credentials login_from_store_topic = \ rospy.get_param("rapp_login_user_from_store") if not login_from_store_topic: msg = "Login user from store topic does not exist" RappUtilities.rapp_print(msg, 'ERROR') login_user_from_store_srv = \ rospy.Service(login_from_store_topic, UserLoginSrv, self.login_from_store_callback) # Login login_topic = \ rospy.get_param("rapp_login_user") if not login_topic: msg = "Login user topic does not exist" RappUtilities.rapp_print(msg, 'ERROR') login_user_srv = \ rospy.Service(login_topic, UserLoginSrv, self.login_callback) ## @brief Add new platform user using platform credentials # # @param req [rapp_platform_ros_communications::ApplicationAuthentication::AddNewUserFromPlatformSrvRequest] The service request # # @return res [rapp_platform_ros_communications::ApplicationAuthentication::AddNewUserFromPlatformSrvResponse] The service response def add_new_user_from_platform_callback(self, req): res = AddNewUserFromPlatformSrvResponse() try: self._verify_user_credentials( req.creator_username, req.creator_password) self._db_handler.validate_user_role(req.creator_username) self._validate_username_format(req.new_user_username) except RappError as e: res.error = e.value return res try: unique_username = \ self._verify_new_username_uniqueness(req.new_user_username) except RappError as e: res.error = e.value return res else: if unique_username != '': res.error = 'Username exists' res.suggested_username = unique_username return res try: self._add_new_user_to_db( req.new_user_username, req.new_user_password, req.creator_username, req.language ) except RappError as e: res.error = e.value return res ## @brief Add new platform user using rapp_store credentials # # @param req [rapp_platform_ros_communications::ApplicationAuthentication::AddNewUserFromStoreSrvRequest] The service request # # @return res [rapp_platform_ros_communications::ApplicationAuthentication::AddNewUserFromStoreSrvResponse] The service response def add_new_user_from_store_callback(self, req): res = AddNewUserFromStoreSrvResponse() res.error = '' # Verify that username -> alphanumeric + dash + underscore try: self._validate_username_format(req.username) except RappError as e: res.error = e.value return res if not self._db_handler.verify_store_device_token(req.device_token): res.error = 'Invalid user' return res # Verify that username is unique, i.e. does not exist try: unique_username = \ self._verify_new_username_uniqueness(req.username) except RappError as e: res.error = e.value return res else: if unique_username != '': res.error = 'Username exists' res.suggested_username = unique_username return res # Add new user to the database try: self._add_new_user_to_db( req.username, req.password, 'rapp_store', req.language ) except RappError as e: res.error = e.value return res ## @brief Login existing user using platform device token # # @param req [rapp_platform_ros_communications::ApplicationAuthentication::UserLoginSrvRequest] The service request # # @return res [rapp_platform_ros_communications::ApplicationAuthentication::UserLoginSrvResponse] The service response def login_callback(self, req): res = UserLoginSrvResponse() try: self._verify_user_credentials(req.username, req.password) except RappError as e: res.error = e.value return res if not self._db_handler.verify_platform_device_token(req.device_token): res.error = 'Invalid user' return res if self._db_handler.verify_active_robot_session( req.username, req.device_token): res.error = 'Session already active' return res rand_str = \ ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits + string.punctuation) for _ in range(64)) hash_str = sha256_crypt.encrypt(rand_str) index = hash_str.find('$', 3) hash_str = hash_str[index+1:] new_token = base64.b64encode(hash_str) try: self._db_handler.write_new_application_token( req.username, req.device_token, new_token) except RappError as e: res.error = 'Wrong credentials' else: res.error = '' res.token = new_token return res ## @brief Login existing user using store device token # # @param req [rapp_platform_ros_communications::ApplicationAuthentication::UserLoginSrvRequest] The service request # # @return res [rapp_platform_ros_communications::ApplicationAuthentication::UserLoginSrvResponse] The service response def login_from_store_callback(self, req): res = UserLoginSrvResponse() try: self._verify_user_credentials(req.username, req.password) except RappError as e: res.error = e.value return res if not self._db_handler.verify_store_device_token(req.device_token): res.error = 'Invalid user' return res else: try: self._db_handler.add_store_token_to_device(req.device_token) except RappError as e: res.error = 'Wrong credentials' return res if self._db_handler.verify_active_robot_session( req.username, req.device_token): res.error = 'Session already active' return res # Generate token rand_str = \ ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits + string.punctuation) for _ in range(64)) hash_str = sha256_crypt.encrypt(rand_str) index = hash_str.find('$', 3) hash_str = hash_str[index+1:] res.token = base64.b64encode(hash_str) try: self._db_handler.write_new_application_token( req.username, req.device_token, res.token) except RappError as e: res.error = 'Wrong credentials' else: res.error = '' return res ## @brief Authenticate token # # @param req [rapp_platform_ros_communications::ApplicationAuthentication::UserTokenAuthenticationSrvRequest] The service request # # @return res [rapp_platform_ros_communications::ApplicationAuthentication::UserTokenAuthenticationSrvResponse] The service response def authenticate_token_callback(self, req): res = UserTokenAuthenticationSrvResponse() res.error = '' res.username = '' if self._db_handler.verify_active_application_token(req.token): res.username = self._db_handler.get_token_user(req.token) else: res.error = 'Invalid token' return res ## @brief Verify username and password # # @param username [string] The username # @param password [string] Password associated with username # # @exception RappError Username does not exist or password does not match def _verify_user_credentials(self, username, password): passwd = self._db_handler.get_user_password(username) if bcrypt.hashpw(password, passwd) != passwd: raise RappError("Wrong Credentials") ## @brief Verify that new username is unique # # @param username [string] The username # # @return suggestion [string] A suggested username if the provided already exists # # @exception RappError Username exists and can not find a username suggestion def _verify_new_username_uniqueness(self, username): if self._db_handler.username_exists(username): counter = 0 while True and counter <= 10: counter += 1 suggestion = '_' + \ ''.join(random.SystemRandom().choice(string.digits) for _ in range(5)) if not self._db_handler.username_exists( username + suggestion): return username + suggestion raise RappError('Could specify a username suggestion') else: return '' ## @brief Verify that new username complies with a set of rules # # Alphanumerics, dashes and underscores are allowed # # @param username [string] The username # # @exception RappError Username violates rules def _validate_username_format(self, username): if not re.match("^[\w\d_-]*$", username) or len(username) < 5 or \ len(username) > 15: raise RappError('Invalid username characters') ## @brief Create password hash and store to the databse # # @param username [string] The username # @param password [string] The password def _add_new_user_to_db(self, new_user_username, new_user_password, creator_username, language): password_hash = bcrypt.hashpw(new_user_password, bcrypt.gensalt()) self._db_handler.add_new_user( new_user_username, password_hash, creator_username, language)
def __init__(self, cli_sock, node_name): threading.Thread.__init__(self) self.cli_sock = cli_sock self.node_name = node_name self.db_hdlr = DatabaseHandler('measurements.db') self.alive = True
class Trilateration(threading.Thread): """A class computing a tags position based on reader measurements""" def __init__(self): threading.Thread.__init__(self) self.db_hdlr = DatabaseHandler('measurements.db') self.alive = True node0 = [(80, 65), (64, 62), (61, 57), (56, 53), (52, 48), (47, 46), (45, 44), (43, 42), (41, 0)] node1 = [(77, 63), (62, 58), (57, 55), (54, 53), (52, 49), (48, 47), (46, 44), (43, 42), (41, 0)] node2 = [(78, 64), (63, 60), (59, 56), (55, 54), (53, 49), (48, 45), (44, 43), (42, 41), (40, 0)] self.tables = {0 : node0, 1 : node1, 2 : node2} def run(self): while self.alive: rowcount = self.db_hdlr.get_rowcount() if rowcount < 3: continue positions = self.db_hdlr.get_positions() readings = self.db_hdlr.get_readings() self.trilateration(positions, readings) sleep(3) self.db_hdlr.close_db() def stop(self): self.alive = False def convert_readings(self, readings): dists = [] for node_name, rss in readings: #dist = 1 / math.sqrt(rss) if rss <= 61: if node_name == 0: rss += 3 elif node_name == 1: rss += 6 else: rss += 12 dist = self.rss_to_dist(node_name, rss) self.db_hdlr.update_dist(node_name, dist) dists.append(dist) print 'Node', node_name, 'with rss', rss, 'has dist', dist return dists def rss_to_dist(self, node_name, rss): rss_tbl = self.tables[node_name] new_min = 0 for old_min, old_max in rss_tbl: if rss <= old_min and rss >= old_max: return self.linear_convertion(rss, old_min, old_max, new_min) new_min += 1 def linear_convertion(self, rss, old_min, old_max, new_min): #old_range = float(abs(old_max - old_min)) #return float(abs(rss - old_min)) / old_range + new_min old_range = float(old_min - old_max) return ((float(old_min - rss) / old_range) + new_min) def trilateration(self, positions, readings): maxzero = 0.0 p1 = numpy.array(positions[0]) p2 = numpy.array(positions[1]) p3 = numpy.array(positions[2]) r1, r2, r3 = self.convert_readings(readings) ex = p2 - p1 # d = |p2 - p1| d = numpy.linalg.norm(ex) if d <= maxzero: # p1 and p2 are concentric print 'p1 and p2 are concentric' return # ex = (p2 - p1) / |p2 - p1| ex = ex / d # t1 = p3 - p1 t1 = p3 - p1 # i = ex . (p3 - p1) i = numpy.dot(ex, t1) # t2 = ex(ex . (p3 - p1)) t2 = ex * i # ey = p3 - p1 - ex(ex . (p3 - p1)) ey = t1 - t2 # t = |p3 - p1 - ex(ex . (p3 - p1))| t = numpy.linalg.norm(ey) if t > maxzero: # p1, p2, p3 are not collinear # ey = (t1 - t2) / |t1 - t2| ey = ey / t # j = ey . (p3 - p1) j = numpy.dot(ey, t1) else: j = 0.0 if math.fabs(j) <= maxzero: # p1, p2, p3 are collinear # Is point p1 +/- (r1 along the axis) the intersection? ts = [p1 + ex*r1, p1 - ex*r1] for t in ts: if ((math.fabs(numpy.linalg.norm(p2 / t) - r2) <= maxzero) and (math.fabs(numpy.linalg.norm(p3 / t) - r3) <= maxzero)): p = t print 'p1, p2, p3 are collinear' print 'p1 +/- ex*r1 is the only intersection point' print 'p', p self.db_hdlr.update_pnt(p,p) return print 'p1, p2, p3 are collinear but no solution exists' return # ez = ex x ey ez = numpy.cross(ex, ey) print 'ex', ex print 'ey', ey print 'ez', ez print 'd', d print 'i', i print 'j', j # x = (r1^2 - r2^2) / 2d + d / 2 x = (math.pow(r1, 2) - math.pow(r2, 2)) / (2*d) + d / 2 print 'x', x # y = (r1^2 - r3^2 + i^2) / (2*j) + j / 2 - x * i / j y = ((math.pow(r1, 2) - math.pow(r3, 2) + math.pow(i, 2)) / (2*j) + j / 2 - x * i / j) print 'y', y # z = r1^2 - x^2 - y^2 z = math.pow(r1, 2) - math.pow(x, 2) - math.pow(y, 2) # Check the sign of the z dimension if z < -maxzero: print 'z < 0' #return elif z > maxzero: z = math.sqrt(z) print 'z > 0' else: z = 0 print 'z = 0' #z = 0 print 'z', z t = p1 + x*ex + y*ey p1 = t + z*ez print 'p1', p1 p2 = t - z*ez print 'p2', p2 self.db_hdlr.update_pnt(p1,p2)