-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added start of a DataFeed extension. Still early, not ready for prime…
…-time. Added an example of a DataFeed client in a new examples folder (more coming soon).
- Loading branch information
Showing
4 changed files
with
115 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import click | ||
|
||
from mygeotab import ext, API, dates | ||
|
||
|
||
class ExceptionDataFeedListener(ext.DataFeedListener): | ||
def __init__(self, api): | ||
self.api = api | ||
self._devices = {} | ||
super(ext.DataFeedListener, self).__init__() | ||
|
||
def on_data(self, data): | ||
for d in data: | ||
device = self._devices.get(d['device']['id']) | ||
if not device: | ||
devices = self.api.search('Device', id=d['device']['id'], results_limit=1) | ||
if len(devices) > 0: | ||
device = devices[0] | ||
self._devices[device['id']] = device | ||
else: | ||
device = d['device'] | ||
date = dates.localize_datetime(d['activeFrom']) | ||
click.echo('[{0}] {1}'.format(date, device.get('name', '**Unknown Vehicle'))) | ||
|
||
def on_error(self, error): | ||
click.secho(error, fg='red') | ||
|
||
|
||
@click.command(help="A console data feeder example") | ||
@click.argument('database', nargs=1, required=False) | ||
@click.option('--user', '-u', prompt=True) | ||
@click.option('--password', '-p', prompt=True, hide_input=True) | ||
@click.option('--server', default=None, help='The server (ie. my4.geotab.com)') | ||
@click.option('--interval', '-i', type=click.IntRange(5, 300)) | ||
def main(database=None, user=None, password=None, server=None, interval=60): | ||
api = API(database=database, username=user, password=password, server=server) | ||
api.authenticate() | ||
ext.DataFeed(api, ExceptionDataFeedListener(api), 'ExceptionEvent', interval=interval).start() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
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 @@ | ||
click |
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,51 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from threading import Thread | ||
from time import sleep | ||
|
||
|
||
class DataFeedListener(object): | ||
def on_data(self, data): | ||
return | ||
|
||
def on_error(self, error): | ||
return False | ||
|
||
|
||
class DataFeed(object): | ||
def __init__(self, api, listener, type_name, interval, search=None): | ||
self.api = api | ||
self.listener = listener | ||
self.type_name = type_name | ||
self.interval = interval | ||
self.search = search | ||
self.running = False | ||
self._version = None | ||
self._thread = None | ||
|
||
def _result(self, data): | ||
if self.listener.on_result(data) is False: | ||
self.running = False | ||
|
||
def _run(self): | ||
while self.running: | ||
try: | ||
result = self.api.call('GetFeed', type_name=self.type_name, search=self.search, | ||
from_version=self._version) | ||
self._version = result['toVersion'] | ||
self.listener.on_data(result['data']) | ||
except Exception as e: | ||
if self.listener.on_error(e) is False: | ||
break | ||
if not self.running: | ||
break | ||
sleep(self.interval) | ||
self.running = False | ||
|
||
def start(self, async=True): | ||
self.running = True | ||
if async: | ||
self._thread = Thread(target=self._run) | ||
self._thread.start() | ||
else: | ||
self._run() |