2020-03-29 15:27:36 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2020-03-29 16:38:40 +02:00
|
|
|
import datetime
|
2020-03-29 16:48:59 +02:00
|
|
|
import communicator
|
2020-03-29 15:27:36 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
Main Flask RESTful API
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = "@tormakris"
|
|
|
|
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
|
|
|
__module_name__ = "consumerlocator"
|
|
|
|
__version__text__ = "1"
|
|
|
|
|
2020-03-29 15:54:52 +02:00
|
|
|
|
2020-03-29 15:26:01 +02:00
|
|
|
class ConsumerLocator:
|
2020-03-29 15:54:52 +02:00
|
|
|
|
2020-03-29 16:46:28 +02:00
|
|
|
"""
|
|
|
|
Manages the list of consumers.
|
|
|
|
"""
|
|
|
|
|
2020-03-29 15:26:01 +02:00
|
|
|
def __init__(self):
|
2020-03-29 16:46:28 +02:00
|
|
|
"""
|
|
|
|
Initialize class.
|
|
|
|
"""
|
2020-03-29 16:38:40 +02:00
|
|
|
self.consumerList = [{"Host": "KnownHost", "State": True, "LastOk": datetime.datetime.now()}]
|
|
|
|
self.currentConsumer = self.consumerList[0]["Host"]
|
2020-03-29 15:54:52 +02:00
|
|
|
|
2020-03-29 16:50:23 +02:00
|
|
|
def learnconsumerlist(self):
|
2020-03-29 15:54:52 +02:00
|
|
|
""""
|
|
|
|
Learns the list of consumers.
|
|
|
|
"""
|
2020-03-29 16:48:59 +02:00
|
|
|
recievedConsumerList = communicator.discoveravailableconsumers()
|
2020-03-29 16:38:40 +02:00
|
|
|
for consumer in recievedConsumerList:
|
|
|
|
self.consumerList.append({"Host": consumer, "State": True, "LastOk": datetime.datetime.now()})
|
|
|
|
self.updateConsumerList()
|
|
|
|
|
2020-03-29 16:50:23 +02:00
|
|
|
def updateconsumerlist(self):
|
2020-03-29 16:38:40 +02:00
|
|
|
"""
|
|
|
|
Updates the consumer list based on their availability.
|
|
|
|
"""
|
|
|
|
removeList = []
|
|
|
|
for consumer in self.consumerList:
|
2020-03-29 16:48:59 +02:00
|
|
|
if not communicator.isconsumeravailable(consumer["Host"]):
|
2020-03-29 16:38:40 +02:00
|
|
|
consumer["State"] = False
|
|
|
|
if datetime.datetime.now() - consumer["LastOk"] > datetime.timedelta(hours=1):
|
|
|
|
removeList.append(consumer)
|
|
|
|
else:
|
|
|
|
consumer["LastOk"] = datetime.datetime.now()
|
|
|
|
for rem in removeList:
|
|
|
|
self.consumerList.remove(rem)
|
2020-03-29 15:54:52 +02:00
|
|
|
|
2020-03-29 16:50:23 +02:00
|
|
|
def updateconsumer(self):
|
2020-03-29 15:54:52 +02:00
|
|
|
"""
|
2020-03-29 16:38:40 +02:00
|
|
|
Checks all the consumers in the list and updates the current consumer if necessary.
|
2020-03-29 16:46:28 +02:00
|
|
|
:return: the current consumer or None if there are no available customers at the moment.
|
2020-03-29 15:54:52 +02:00
|
|
|
"""
|
2020-03-29 16:38:40 +02:00
|
|
|
self.updateConsumerList()
|
|
|
|
|
|
|
|
if not self.checkConsumer():
|
|
|
|
|
|
|
|
newCurrentConsumer = None
|
|
|
|
|
|
|
|
for consumer in self.consumerList:
|
|
|
|
if consumer["State"]:
|
|
|
|
newCurrentConsumer = consumer
|
|
|
|
break
|
|
|
|
|
|
|
|
self.currentConsumer = newCurrentConsumer
|
|
|
|
|
|
|
|
if self.currentConsumer is not None:
|
|
|
|
return self.currentConsumer["Host"]
|
|
|
|
else:
|
|
|
|
return None
|
2020-03-29 15:54:52 +02:00
|
|
|
|
2020-03-29 16:50:23 +02:00
|
|
|
def getcurrentconsumer(self):
|
2020-03-29 15:54:52 +02:00
|
|
|
"""
|
|
|
|
Returns the currently selected consumer.
|
|
|
|
:return: the current consumer
|
|
|
|
"""
|
2020-03-29 16:38:40 +02:00
|
|
|
return self.currentConsumer["Host"]
|
2020-03-29 15:54:52 +02:00
|
|
|
|
2020-03-29 16:50:23 +02:00
|
|
|
def checkconsumer(self):
|
2020-03-29 15:54:52 +02:00
|
|
|
"""
|
|
|
|
Check the consumers health.
|
|
|
|
:return: True if OK, False if fail
|
|
|
|
"""
|
2020-03-29 16:48:59 +02:00
|
|
|
if communicator.isconsumeravailable(self.currentConsumer["Host"]):
|
2020-03-29 15:54:52 +02:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|