diff --git a/src/badge.py b/src/badge.py index e2673e0..ee18560 100644 --- a/src/badge.py +++ b/src/badge.py @@ -308,9 +308,9 @@ def badge_id(self): def observed_id(self): return self.observed_id - @property + @property def project_id(self): - return self.project_id + return self.project_id @property def last_proximity_ts(self): @@ -324,6 +324,10 @@ def last_contacted_ts(self): def last_unsync_ts(self): return self.__last_unsync_ts + @property + def last_seen_ts(self): + return self.__last_seen_ts + @property def last_voltage(self): return self.last_voltage @@ -389,11 +393,11 @@ def is_newer_audio_ts(self, audio_ts_int, audio_ts_fract): # no value is set yetyet return True - def __init__(self,addr,logger, key,badge_id,project_id, init_audio_ts_int=None, init_audio_ts_fract=None, init_proximity_ts=None, init_voltage=None, init_contact_ts=None,init_unsync_ts=None, observed_id=None): + def __init__(self,addr,logger, key,badge_id,project_id, init_audio_ts_int=None, init_audio_ts_fract=None, init_proximity_ts=None, init_voltage=None, init_contact_ts=None,init_unsync_ts=None, init_seen_ts=None, observed_id=None): #if self.children.get(key): # return self.children.get(key) - self.children[key] = self - self.key = key + self.children[key] = self + self.key = key self.addr = addr self.logger = adapter = BadgeAddressAdapter(logger, {'addr': addr}) self.badge_id = badge_id @@ -410,6 +414,7 @@ def __init__(self,addr,logger, key,badge_id,project_id, init_audio_ts_int=None, self.__last_proximity_ts = init_proximity_ts self.__last_contacted_ts = init_contact_ts self.__last_unsync_ts = init_unsync_ts + self.__last_seen_ts = init_seen_ts def connect(self): self.logger.info("Connecting to {}".format(self.addr)) @@ -425,8 +430,8 @@ def disconnect(self): # sends status request with UTC time to the badge def sendStatusRequest(self): - long_epoch_seconds, ts_fract = now_utc_epoch() - self.dlg.expected = Expect.status + long_epoch_seconds, ts_fract = now_utc_epoch() + self.dlg.expected = Expect.status return self.conn.write(' badge.last_proximity_ts: - #self.logger.debug("Updating {} with new proximity timestamp: {}".format(mac, server_proximity_ts)) - badge.last_proximity_ts = server_proximity_ts - - else: - #self.logger.debug("Keeping existing proximity timestamp for {}. Server value was: {}" - # .format(mac, server_proximity_ts)) - pass - - if server_ts_last_contacted > badge.last_contacted_ts: - badge.last_contacted_ts = server_ts_last_contacted - else: - pass - - if server_ts_last_unsync > badge.last_unsync_ts: - badge.last_unsync_ts = server_ts_last_unsync - else: - pass - - # updates project id and badge id - badge.badge_id = server_badge.badge_id - badge.project_id = server_badge.project_id - def pull_badges_list(self): - # first time we read from server - if self._badges is None: - server_badges = self._read_badges_list_from_server(retry=True) - self._badges = server_badges - else: - # update list - server_badges = self._read_badges_list_from_server(retry=False) - for mac in server_badges: - if mac not in self._badges: - # new badge - self._badges[mac] = server_badges[mac] - else: - # existing badge. Update if needed - # audio - badge = self._badges[mac] - server_badge = server_badges[mac] - self._update_badge_with_server_badge(badge,server_badge) + self._badges = self._read_badges_list_from_server(retry=True) def pull_badge(self, mac): """ @@ -168,10 +104,19 @@ def pull_badge(self, mac): badge = self._badges[mac] server_badge = self._read_badge_from_server(badge.key) if server_badge is None: + # this could happen if the badge is removed from the server in between iterations self.logger.warn("Could not find device {} in server, or communication problem".format(badge.key)) + return False + elif server_badge.addr != mac: + # this could happen if a badge is reassigned in between iterations + # and would result in associating the data from the badge with the wrong user + self.logger.warn( + "Badge / Server Mac Address mismatch for badge: {}" + .format(mac)) + return False else: - # update timestamps if more recent - self._update_badge_with_server_badge(badge, server_badge) + self._badges[mac] = server_badge + return True def send_badge(self, mac): """ @@ -181,7 +126,7 @@ def send_badge(self, mac): """ try: badge = self._badges[mac] - data = { + data = { 'observed_id': badge.observed_id, 'last_audio_ts': badge.last_audio_ts_int, 'last_audio_ts_fract': badge.last_audio_ts_fract, @@ -243,7 +188,7 @@ def badges(self): logging.basicConfig() logger = logging.getLogger('badge_server') logger.setLevel(logging.DEBUG) - + mgr = BadgeManagerServer(logger=logger) mgr.pull_badges_list() print(mgr.badges) diff --git a/src/badge_manager_standalone.py b/src/badge_manager_standalone.py index 8dfff1f..d0f70ab 100644 --- a/src/badge_manager_standalone.py +++ b/src/badge_manager_standalone.py @@ -71,26 +71,18 @@ def _read_file(self,device_file): return badges def pull_badges_list(self): - # first time we read as is - if self._badges is None: + if not self._badges: file_badges = self._read_file(self._device_file) self._badges = file_badges - else: - # update list - file_badges = self._read_file(self._device_file) - for mac in file_badges: - if mac not in self._badges: - # new badge - self.logger.debug("Found new badge in file: {}".format(mac)) - self._badges[mac] = file_badges[mac] def pull_badge(self, mac): """ Contacts to server (if responding) and updates the given badge data :param mac: - :return: + :return success?: """ - pass # not implemented + # Return success so it doesn't break everything else + return True def send_badge(self, mac): """ diff --git a/src/beacon_manager_server.py b/src/beacon_manager_server.py index 34ba5ec..bb191f6 100644 --- a/src/beacon_manager_server.py +++ b/src/beacon_manager_server.py @@ -88,47 +88,16 @@ def _read_beacon_from_server(self, beacon_key, retry=False, retry_delay_sec=5): return None - def _update_beacon_with_server_beacon(self,beacon,server_beacon): - """ - Update the beacon properties from the server's copy - :param beacon: - :param server_beacon: - :return: - """ - - # updates project id and badge id - beacon.badge_id = server_beacon.badge_id - beacon.project_id = server_beacon.project_id - def pull_beacons_list(self): - # first time we read from server - if self._beacons is None: - server_beacons = self._read_beacons_list_from_server(retry=True) - self._beacons = server_beacons - else: - # update list - server_beacons = self._read_beacons_list_from_server(retry=False) - for mac in server_beacons: - if mac not in self._beacons: - # new beacon - self._beacons[mac] = server_beacons[mac] - else: - beacon = self._beacons[mac] - server_beacon = server_beacons[mac] - self._update_beacon_with_server_beacon(beacon,server_beacon) + self._beacons = self._read_beacons_list_from_server(retry=True) def pull_beacon(self, mac): """ - Contacts to server (if responding) and updates the given badge data + Contacts to server (if responding) and updates the given beacon data :param mac: :return: """ - beacon = self._beacons[mac] - server_beacon = self._read_beacon_from_server(beacon.key) - if server_beacon is None: - self.logger.warn("Could not find device {} in server, or communication problem".format(beacon.key)) - else: - self._update_beacon_with_server_beacon(beacon, server_beacon) + raise NotImplementedError() def send_beacon(self, mac): """ diff --git a/src/beacon_manager_standalone.py b/src/beacon_manager_standalone.py index 9c7ee5d..60db89f 100644 --- a/src/beacon_manager_standalone.py +++ b/src/beacon_manager_standalone.py @@ -43,10 +43,10 @@ def _read_file(self,device_file): devices = [] for line in devices_macs: - if not line.lstrip().startswith('#'): - device_details = line.split() - devices.append(device_details[0]) - badge_project_ids.append(device_details[1:3]) + if not line.lstrip().startswith('#'): + device_details = line.split() + devices.append(device_details[0]) + badge_project_ids.append(device_details[1:3]) #mapping badge id and project id to mac address @@ -68,18 +68,9 @@ def _read_file(self,device_file): return beacons def pull_beacons_list(self): - # first time we read as is - if self._beacons is None: + if not self._beacons: file_beacons = self._read_file(self._device_file) self._beacons = file_beacons - else: - # update list - file_beacons = self._read_file(self._device_file) - for mac in file_beacons: - if mac not in self._beacons: - # new badge - self.logger.debug("Found new badge in file: {}".format(mac)) - self._beacons[mac] = file_beacons[mac] def pull_beacon(self, mac): """ @@ -87,7 +78,7 @@ def pull_beacon(self, mac): :param mac: :return: """ - pass # not implemented + raise NotImplementedError() def send_beacon(self, mac): """