2020-03-29 18:12:16 +02:00
|
|
|
import json
|
|
|
|
from flask import request, current_app, jsonify
|
2020-03-29 17:08:53 +02:00
|
|
|
from flask_classful import FlaskView
|
2020-04-04 13:53:26 +02:00
|
|
|
from db import redis_client
|
2020-03-29 17:08:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
class SyncView(FlaskView):
|
|
|
|
|
2020-03-29 18:12:16 +02:00
|
|
|
def post(self):
|
|
|
|
remote_uuid = request.json['uuid']
|
|
|
|
remote_ip = request.remote_addr
|
|
|
|
|
2020-04-04 13:53:26 +02:00
|
|
|
# load the currently available consumer list from the redis database
|
2020-03-29 18:12:16 +02:00
|
|
|
consumer_list = json.loads((redis_client.get("consumer_list") or b"{}").decode('utf-8'))
|
|
|
|
|
|
|
|
if remote_uuid not in consumer_list.keys():
|
2020-04-04 13:53:26 +02:00
|
|
|
# display newly registered consumer
|
2020-03-29 18:12:16 +02:00
|
|
|
current_app.logger.info(f"New consumer registered (unknown UUID): {remote_uuid} at {remote_ip}")
|
2020-04-04 13:53:26 +02:00
|
|
|
else:
|
2020-03-29 18:12:16 +02:00
|
|
|
if consumer_list[remote_uuid]['ip'] != remote_ip:
|
2020-04-04 13:53:26 +02:00
|
|
|
# log address changes
|
2020-03-29 18:12:16 +02:00
|
|
|
current_app.logger.info(f"Address of consumer {remote_uuid} changed to {remote_ip}")
|
|
|
|
|
2020-04-04 13:53:26 +02:00
|
|
|
# update consumer list redis databasse
|
2020-03-29 18:12:16 +02:00
|
|
|
consumer_list.update(
|
|
|
|
{remote_uuid: {"ip": remote_ip}}
|
|
|
|
)
|
|
|
|
redis_client.set("consumer_list", json.dumps(consumer_list).encode('utf-8'))
|
|
|
|
|
2020-04-04 13:53:26 +02:00
|
|
|
# return with the current UUID
|
2020-03-29 18:12:16 +02:00
|
|
|
response = {
|
|
|
|
"uuid": current_app.config['LOCAL_UUID']
|
|
|
|
}
|
|
|
|
return jsonify(response)
|