update_node_info is now able to update django models with external info from node-OpenDroneMap

pull/17/head
Piero Toffanin 2016-09-21 18:07:19 -04:00
rodzic 98b66013fe
commit 804193b9a1
4 zmienionych plików z 39 dodań i 27 usunięć

Wyświetl plik

@ -0,0 +1 @@
default_app_config = 'nodeodm.apps.NodeodmConfig'

Wyświetl plik

@ -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()
return self.client.server.get_info().result()
@check_client
def options(self):
return self.client.server.get_options().result()

Wyświetl plik

@ -2,6 +2,6 @@ from __future__ import unicode_literals
from django.apps import AppConfig
class NodeodmConfig(AppConfig):
name = 'nodeodm'
verbose_name = 'Node Management'

Wyświetl plik

@ -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())
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()