diff --git a/nodeodm/__init__.py b/nodeodm/__init__.py index e69de29b..ffe36097 100644 --- a/nodeodm/__init__.py +++ b/nodeodm/__init__.py @@ -0,0 +1 @@ +default_app_config = 'nodeodm.apps.NodeodmConfig' \ No newline at end of file diff --git a/nodeodm/api_client.py b/nodeodm/api_client.py index adc38844..715f051d 100644 --- a/nodeodm/api_client.py +++ b/nodeodm/api_client.py @@ -2,28 +2,33 @@ A wrapper around Bravado to communicate with a node-OpenDroneMap node. """ from bravado.client import SwaggerClient - -def check_client(func): - def check(self, *args, **kwargs): - """ - Makes sure that the client has been instantiated. - Sometimes this will fail (rest endpoint might be offline), - so we need to handle it gracefully... - """ - if not self.client: - try: - self.client = SwaggerClient.from_url('http://{}:{}/swagger.json'.format(host, port)) - except ConnectionError as err: - print("ProcessingNode {}:{} seems offline: {}".format(self.host, self.port, err)) - return None - - return func(self, *args, **kwargs) +from requests import ConnectionError class ApiClient: + def check_client(func): + def check(self, *args, **kwargs): + """ + Makes sure that the client has been instantiated. + Sometimes this will fail (rest endpoint might be offline), + so we need to handle it gracefully... + """ + if not hasattr(self, 'client'): + try: + self.client = SwaggerClient.from_url('http://{}:{}/swagger.json'.format(self.host, self.port)) + except ConnectionError as err: + print("{}:{} seems offline: {}".format(self.host, self.port, err)) + return None + return func(self, *args, **kwargs) + return check + def __init__(self, host, port): self.host = host self.port = port @check_client def info(self): - return self.client.server.get_info().result() \ No newline at end of file + return self.client.server.get_info().result() + + @check_client + def options(self): + return self.client.server.get_options().result() \ No newline at end of file diff --git a/nodeodm/apps.py b/nodeodm/apps.py index 6244ad05..0b581827 100644 --- a/nodeodm/apps.py +++ b/nodeodm/apps.py @@ -2,6 +2,6 @@ from __future__ import unicode_literals from django.apps import AppConfig - class NodeodmConfig(AppConfig): name = 'nodeodm' + verbose_name = 'Node Management' diff --git a/nodeodm/models.py b/nodeodm/models.py index 30ce6df4..f1cdb5e6 100644 --- a/nodeodm/models.py +++ b/nodeodm/models.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals from django.db import models from django.contrib.postgres import fields +from django.utils import timezone from .api_client import ApiClient class ProcessingNode(models.Model): @@ -16,19 +17,24 @@ class ProcessingNode(models.Model): super(ProcessingNode, self).__init__(*args, **kwargs) # Initialize api client - if self.hostname != None and self.port != None: - print("HI!") - self.api_client = ApiClient(self.hostname, self.port) + self.api_client = ApiClient(self.hostname, self.port) def __str__(self): - print("OH!"); - self.update_info() return '{}:{}'.format(self.hostname, self.port) - - def update_info(self): + def update_node_info(self): """ - Retrieves information from the node + Retrieves information and options from the node and saves it into the database """ - print(self.api_client.info()) \ No newline at end of file + info = self.api_client.info() + if info != None: + self.api_version = info['version'] + self.queue_count = info['taskQueueCount'] + + options = self.api_client.options() + if options != None: + self.available_options = options + self.last_refreshed = timezone.now() + + self.save()