Exemplo n.º 1
0
def print_MAC_address(address, user):
    if config.print_all_MACs:
        if is_unknown_user(name=user.name):
            log(message="MAC detected: {address}".format(address=address))
        else:
            log(message="MAC detected: {address}; owned by {name}".format(
                address=address, name=user.name))
Exemplo n.º 2
0
 def __init__(self, system, name=config.unknown_user_prefix, song=config.need_to_assign, length=config.time_max_song_length, arrival=datetime.datetime.now()):
     if is_unknown_user(name=name):
         self.name = name + generate_random_suffix()
     else:
         self.name = name
     if song == config.need_to_assign:
         self.song = system.music_player.get_random_song()
     else:
         self.song = song
     self.length = length
     self.arrival = arrival
Exemplo n.º 3
0
    def add_new_address(self, address, user_name=config.unknown_user_prefix):
        # if the address is new, add it to our dict
        if address not in self.all_addresses:
            user = User(system=self, name=user_name)
            self.all_addresses[address] = user

            with open(config.data_file, 'a') as f:
                f.write("{address},{name},{song}\n".format(address=address, name=user.name, song=user.song))

            if is_unknown_user(name=user.name):
                log(message="A new unknown device with address {address} has been added and has been assigned {song}.".format(address=address, song=user.song))
            else:
                log(message="{name} has been added and has been assigned {song}.".format(name=user.name, song=user.song))
Exemplo n.º 4
0
    def __init__(self):
        log(message="Initializing...")
        # local_payload_cache uses mac_id ("address") as key, with the value being a dictionary of mac_id and datetime value of last time the api was pinged.
        # Cache is only used if use_cache is set to True in config.py
        self.local_payload_cache = {}       
        self.music_player = MusicPlayer()
        self.waiting_for_input = False
        self.input_timeout()
        log(message="Waiting for tcpdump to provide input...")

        while True:
            # handle input as network traffic from tcpdump
            addresses = get_MAC(line=stdin.readline())
            self.waiting_for_input = False
            for address in addresses:
                ## Pings Hummingbird Django server. If use_cache is set to True, use self.local_payload_cache as a cache.
                if config.use_cache:
                    if address.lower() in self.local_payload_cache:
                        # payload is a tuple of address and last_checked_datetime
                        payload = self.local_payload_cache[address.lower()]
                        # Set a cache for cache_time_seconds (defaults to 60 seconds)             
                        if (datetime.datetime.now() - payload['last_sent_dt']).seconds > config.cache_time_seconds:
                            r = requests.get("http://127.0.0.1:8000/hummingbird/build_user_from_device/", params=payload)
                            payload['last_sent_dt'] = datetime.datetime.now()
                            self.local_payload_cache[address.lower()] = payload
                            cached = False
                        else:
                            cached = True
                    else:
                        payload = {'mac_id': address.lower(), 'last_sent_dt': datetime.datetime.now()}
                        self.local_payload_cache[address.lower()] = payload                     
                        r = requests.get("http://127.0.0.1:8000/hummingbird/build_user_from_device/", params=payload)               
                        cached = False
                else:
                    payload = {'mac_id': address.lower(), 'last_sent_dt': datetime.datetime.now()}
                    r = requests.get("http://127.0.0.1:8000/hummingbird/build_user_from_device/", params=payload)               
                    cached = False

                ## Convert the string representation of a dicitonary with user info into a dictionary object.
                user_dict = r.json()
                
                if not cached and user_dict!=0:
                    user = User(system=self, name=user_dict['name'], song=user_dict['song'], length=float(user_dict['length']), arrival=datetime.datetime.strptime(user_dict['last_played'],'%Y-%m-%d %H:%M:%S'))
                    # play the user's song (if theirs hasn't played already)
                    # if user.song != settings.DO_NOT_PLAY and user.has_not_played_today():
                    if user.has_not_played_today():
                        if not is_unknown_user(name=user.name) or config.play_unknowns:
                            log(message="Detected activity from {name}.".format(name=user.name))
                            user.queue_song(music_player=self.music_player)
                            updated = requests.get("http://127.0.0.1:8000/hummingbird/update_last_played/", params=payload)
Exemplo n.º 5
0
 def __init__(self,
              system,
              name=config.unknown_user_prefix,
              song=config.need_to_assign,
              length=config.time_max_song_length,
              arrival=datetime.datetime.now()):
     if is_unknown_user(name=name):
         self.name = name + generate_random_suffix()
     else:
         self.name = name
     if song == config.need_to_assign:
         self.song = system.music_player.get_random_song()
     else:
         self.song = song
     self.length = length
     self.arrival = arrival
Exemplo n.º 6
0
    def add_new_address(self, address, user_name=config.unknown_user_prefix):
        # if the address is new, add it to our dict
        if address not in self.all_addresses:
            user = User(system=self, name=user_name)
            self.all_addresses[address] = user

            with open(config.data_file, 'a') as f:
                f.write("{address},{name},{song}\n".format(address=address,
                                                           name=user.name,
                                                           song=user.song))

            if is_unknown_user(name=user.name):
                log(message=
                    "A new unknown device with address {address} has been added and has been assigned {song}."
                    .format(address=address, song=user.song))
            else:
                log(message="{name} has been added and has been assigned {song}."
                    .format(name=user.name, song=user.song))
Exemplo n.º 7
0
    def __init__(self):
        log(message="Initializing...")
        # local_payload_cache uses mac_id ("address") as key, with the value being a dictionary of mac_id and datetime value of last time the api was pinged.
        # Cache is only used if use_cache is set to True in config.py
        self.local_payload_cache = {}
        self.music_player = MusicPlayer()
        self.waiting_for_input = False
        self.input_timeout()
        log(message="Waiting for tcpdump to provide input...")

        while True:
            # handle input as network traffic from tcpdump
            addresses = get_MAC(line=stdin.readline())
            self.waiting_for_input = False
            for address in addresses:
                ## Pings Hummingbird Django server. If use_cache is set to True, use self.local_payload_cache as a cache.
                if config.use_cache:
                    if address.lower() in self.local_payload_cache:
                        # payload is a tuple of address and last_checked_datetime
                        payload = self.local_payload_cache[address.lower()]
                        # Set a cache for cache_time_seconds (defaults to 60 seconds)
                        if (datetime.datetime.now() - payload['last_sent_dt']
                            ).seconds > config.cache_time_seconds:
                            r = requests.get(
                                "http://127.0.0.1:8000/hummingbird/build_user_from_device/",
                                params=payload)
                            payload['last_sent_dt'] = datetime.datetime.now()
                            self.local_payload_cache[address.lower()] = payload
                            cached = False
                        else:
                            cached = True
                    else:
                        payload = {
                            'mac_id': address.lower(),
                            'last_sent_dt': datetime.datetime.now()
                        }
                        self.local_payload_cache[address.lower()] = payload
                        r = requests.get(
                            "http://127.0.0.1:8000/hummingbird/build_user_from_device/",
                            params=payload)
                        cached = False
                else:
                    payload = {
                        'mac_id': address.lower(),
                        'last_sent_dt': datetime.datetime.now()
                    }
                    r = requests.get(
                        "http://127.0.0.1:8000/hummingbird/build_user_from_device/",
                        params=payload)
                    cached = False

                ## Convert the string representation of a dicitonary with user info into a dictionary object.
                user_dict = r.json()

                if not cached and user_dict != 0:
                    user = User(system=self,
                                name=user_dict['name'],
                                song=user_dict['song'],
                                length=float(user_dict['length']),
                                arrival=datetime.datetime.strptime(
                                    user_dict['last_played'],
                                    '%Y-%m-%d %H:%M:%S'))
                    # play the user's song (if theirs hasn't played already)
                    # if user.song != settings.DO_NOT_PLAY and user.has_not_played_today():
                    if user.has_not_played_today():
                        if not is_unknown_user(
                                name=user.name) or config.play_unknowns:
                            log(message="Detected activity from {name}.".
                                format(name=user.name))
                            user.queue_song(music_player=self.music_player)
                            updated = requests.get(
                                "http://127.0.0.1:8000/hummingbird/update_last_played/",
                                params=payload)
Exemplo n.º 8
0
def print_MAC_address(address, user):
	if config.print_all_MACs:
		if is_unknown_user(name=user.name):
			log(message="MAC detected: {address}".format(address=address))
		else:
			log(message="MAC detected: {address}; owned by {name}".format(address=address, name=user.name))