-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
250 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
""" | ||
Base class for the feed manager. | ||
This allows managing feeds and their entries throughout their life-cycle. | ||
""" | ||
import logging | ||
|
||
from flightradar24_client import UPDATE_OK | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class FeedManagerBase: | ||
"""Generic Feed manager.""" | ||
|
||
def __init__(self, feed, generate_callback, update_callback, | ||
remove_callback, persistent_timestamp=False): | ||
"""Initialise feed manager.""" | ||
self._feed = feed | ||
self.feed_entries = {} | ||
self._managed_external_ids = set() | ||
self._generate_callback = generate_callback | ||
self._update_callback = update_callback | ||
self._remove_callback = remove_callback | ||
self._persistent_timestamp = persistent_timestamp | ||
|
||
def __repr__(self): | ||
"""Return string representation of this feed.""" | ||
return '<{}(feed={})>'.format( | ||
self.__class__.__name__, self._feed) | ||
|
||
async def update(self): | ||
"""Update the feed and then update connected entities.""" | ||
status, feed_entries = await self._feed.update() | ||
if status == UPDATE_OK: | ||
_LOGGER.warning("Data retrieved %s", feed_entries) | ||
# Keep a copy of all feed entries for future lookups by entities. | ||
self.feed_entries = feed_entries | ||
# For entity management the external ids from the feed are used. | ||
feed_external_ids = set(self.feed_entries) | ||
remove_external_ids = self._managed_external_ids.difference( | ||
feed_external_ids) | ||
self._remove_entities(remove_external_ids) | ||
update_external_ids = self._managed_external_ids.intersection( | ||
feed_external_ids) | ||
self._update_entities(update_external_ids) | ||
create_external_ids = feed_external_ids.difference( | ||
self._managed_external_ids) | ||
self._generate_new_entities(create_external_ids) | ||
else: | ||
_LOGGER.warning( | ||
"Update not successful, no data received from %s", self._feed) | ||
# Remove all entities. | ||
self._remove_entities(self._managed_external_ids.copy()) | ||
# Remove all feed entries and managed external ids. | ||
self.feed_entries.clear() | ||
self._managed_external_ids.clear() | ||
|
||
def _generate_new_entities(self, external_ids): | ||
"""Generate new entities for events.""" | ||
for external_id in external_ids: | ||
self._generate_callback(external_id) | ||
_LOGGER.debug("New entity added %s", external_id) | ||
self._managed_external_ids.add(external_id) | ||
|
||
def _update_entities(self, external_ids): | ||
"""Update entities.""" | ||
for external_id in external_ids: | ||
_LOGGER.debug("Existing entity found %s", external_id) | ||
self._update_callback(external_id) | ||
|
||
def _remove_entities(self, external_ids): | ||
"""Remove entities.""" | ||
for external_id in external_ids: | ||
_LOGGER.debug("Entity not current anymore %s", external_id) | ||
self._managed_external_ids.remove(external_id) | ||
self._remove_callback(external_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"x7c1469": ["7C1469", -33.8880,151.2435,167,2950,183,"4040",0,"","",1540539591,"","","",0,-64,"QFA456"], | ||
"x7c1c5d": ["7C1C5D", -33.2635,151.2188,182,12725,241,"1117",0,"","",1540539591,"","","",0,-576,"VOZ1192"], | ||
"x7c5304": ["7C5304", -34.3867,150.6932,234,17775,230,"4024",0,"","",1540539590,"","","",0,1088,""], | ||
"x7c6b28": ["7C6B28", -32.5470,150.9698,268,22175,265,"1140",0,"","",1540539591,"","","",0,-1600,"JST423"], | ||
"x7c6b39": ["7C6B39", 0.0000,0.0000,0,5000,0,"3665",0,"","",1540539564,"","","",0,0,""], | ||
"x7c6b29": ["7C6B29", -32.6581,150.0709,268,22175,265,"1140",0,"","",1540539591,"","","",0,-1600,"JST423"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters