From d12251a92791740ce1a1c89046d0461425376b24 Mon Sep 17 00:00:00 2001 From: macs Date: Fri, 18 Sep 2015 10:49:37 +0200 Subject: [PATCH 1/4] added url callback functionality --- iotqatools/simulators/thirdPartySimulator.py | 80 +++++++++++++------- 1 file changed, 54 insertions(+), 26 deletions(-) diff --git a/iotqatools/simulators/thirdPartySimulator.py b/iotqatools/simulators/thirdPartySimulator.py index 470ad1c..e424f1f 100644 --- a/iotqatools/simulators/thirdPartySimulator.py +++ b/iotqatools/simulators/thirdPartySimulator.py @@ -1,17 +1,21 @@ #!/usr/bin/python + __author__ = 'macs' -from flask import Flask, request, Response +from flask import Flask, request, Response, g from sys import argv +from time import sleep +import threading import random import json +import requests app = Flask(__name__) # Default arguments -port = 7010 -host = '0.0.0.0' +port = 6500 +host = '127.0.0.1' # Arguments from command line if len(argv) > 1: @@ -20,40 +24,62 @@ @app.route('/sync/request', methods=['POST']) def treat_sync_request(): - global lastRequest, myResponse, cont + global lastRequest, sync_response, cont - my_result = {} + print "Recived sync request" + #Increase the number of requests cont += 1 + #Store the last request lastRequest = request.data - jdata = json.loads(request.data) - my_result["result"] = 'action-' + jdata['action'] + ', extra-' + jdata['extra'] - resp = Response() - return Response(response=json.dumps(my_result), status=200, content_type='application/json', headers=resp.headers) + print request.data + return Response(response=sync_response, status=200, content_type='application/json') @app.route('/async/create', methods=['POST']) def treat_async_create(): - global asyncRequest + global lastRequest, async_request, url_callback, async_response - uid = str(random.randint(1, 999)) - asyncRequest[uid] = '{"state": "P"}' - print asyncRequest - myresp = '{"id": ' + uid + ', "details": {"rgb" : "66CCDD", "t": 2}}' - return Response(response=myresp, status=200, content_type='application/json') + print "Recived async create" + #Store the last request + lastRequest = request.data + #generates the externalId + uid = str(random.randint(1, 9999999)) + #Retrieve callback url and buttonId from request + url_callback = json.loads(request.data)['callbackUrl'] + button_id = json.loads(request.data)['buttonId'] + async_response['externalId'] = str(uid) + async_response['buttonId'] = str(button_id) + async_response['details'] = {} + async_response['details']['rgb'] = '66CCDD' + async_response['details']['t'] = '2' + print str(async_response) + #Invoke callback response + t = threading.Thread(target=invoke_ca) + t.start() + return Response(response='Create Received OK', status=200) + + +def invoke_ca(): + #Wait until request is finished + sleep(3) + #Send data to urlCallback + headers = {'Accept': 'application/json', 'content-type': 'application/json'} + r = requests.post(url_callback, data=json.dumps(async_response), headers=headers) + return r @app.route('/async/requests', methods=['POST','GET']) def treat_async_request_all(): - global asyncRequest + global async_request - return Response(response=str(asyncRequest), status=200) + return Response(response=str(async_request), status=200) @app.route('/async/request/', methods=['POST','GET']) def treat_async_request(uid): - global asyncRequest + global async_request - return Response(response=asyncRequest[uid], status=200, content_type='application/json') + return Response(response=async_request[uid], status=200, content_type='application/json') @app.route('/setResponseToError', methods=['GET']) @@ -76,8 +102,7 @@ def treat_set_response_to_ok(): def treat_last(): global lastRequest - resp_last = Response() - return Response(response=lastRequest, status=200, content_type='application/json', headers=resp_last.headers) + return Response(response=lastRequest, status=200) @app.route('/count', methods=['GET']) @@ -88,9 +113,9 @@ def count(): @app.route('/reset', methods=['GET']) def reset(): - global lastRequest, asyncRequest, cont + global lastRequest, async_request, cont lastRequest = '' - asyncRequest = {} + async_request = {} cont = 0 return Response(status=200) @@ -98,9 +123,12 @@ def reset(): # Globals lastRequest = '' responseError = '' -myResponse = '{"details": {"rgb":"66CC00","t":2}}' +sync_response = '{"details": {"rgb":"66CC00","t":2}}' +async_response = {} cont = 0 -asyncRequest = {} +async_request = {} +url_callback = "http://localhost:9999" + if __name__ == '__main__': - app.run(host=host, port=port) + app.run(host=host, port=port, debug=True) From 46dea266a33e86733f4778f2a7430156f4d27f83 Mon Sep 17 00:00:00 2001 From: macs Date: Wed, 23 Sep 2015 11:34:10 +0200 Subject: [PATCH 2/4] Added delay to sync request --- iotqatools/simulators/thirdPartySimulator.py | 97 ++++++++++++++------ 1 file changed, 68 insertions(+), 29 deletions(-) diff --git a/iotqatools/simulators/thirdPartySimulator.py b/iotqatools/simulators/thirdPartySimulator.py index e424f1f..7bcb50b 100644 --- a/iotqatools/simulators/thirdPartySimulator.py +++ b/iotqatools/simulators/thirdPartySimulator.py @@ -24,58 +24,87 @@ @app.route('/sync/request', methods=['POST']) def treat_sync_request(): - global lastRequest, sync_response, cont + global last_request, sync_response, cont, delay print "Recived sync request" - #Increase the number of requests + # Increase the number of requests cont += 1 - #Store the last request - lastRequest = request.data - print request.data - return Response(response=sync_response, status=200, content_type='application/json') + + # Store the last request + last_request = request.data + + # Set delay if needed + if delay is not '': + sleep(int(delay)) + delay = '' + + # Retrieve request information + service = request.headers['fiware-service'] + subservice = request.headers['fiware-servicepath'] + button_id = json.loads(request.data)['button'] + headers = {'Accept': 'application/json', 'content-type': 'application/json', 'fiware-service': service, + 'fiware-servicepath': subservice} + + # Generate sync response + sync_response['externalId'] = generate_uid() + sync_response['buttonId'] = button_id + sync_response['details'] = {} + sync_response['details']['rgb'] = '66CCDD' + sync_response['details']['t'] = '2' + + return Response(response=json.dumps(sync_response), status=200, headers=headers) @app.route('/async/create', methods=['POST']) def treat_async_create(): - global lastRequest, async_request, url_callback, async_response + global last_request, async_request, url_callback, async_response, service, subservice print "Recived async create" - #Store the last request - lastRequest = request.data - #generates the externalId - uid = str(random.randint(1, 9999999)) - #Retrieve callback url and buttonId from request - url_callback = json.loads(request.data)['callbackUrl'] - button_id = json.loads(request.data)['buttonId'] - async_response['externalId'] = str(uid) + # Store the last request + last_request = request.data + + # Retrieve callback url and buttonId from request + service = request.headers['fiware-service'] + subservice = request.headers['fiware-servicepath'] + url_callback = json.loads(request.data)['callback'] + button_id = json.loads(request.data)['button'] + + # Compose the async response + async_response['externalId'] = str(generate_uid()) async_response['buttonId'] = str(button_id) async_response['details'] = {} async_response['details']['rgb'] = '66CCDD' async_response['details']['t'] = '2' - print str(async_response) - #Invoke callback response + + # Invoke callback response t = threading.Thread(target=invoke_ca) t.start() return Response(response='Create Received OK', status=200) def invoke_ca(): - #Wait until request is finished + # Wait until request is finished sleep(3) - #Send data to urlCallback - headers = {'Accept': 'application/json', 'content-type': 'application/json'} + + # Send data to urlCallback + headers = {'Accept': 'application/json', 'content-type': 'application/json', 'fiware-service': service, + 'fiware-servicepath': subservice} r = requests.post(url_callback, data=json.dumps(async_response), headers=headers) return r -@app.route('/async/requests', methods=['POST','GET']) +def generate_uid(): + return str(random.randint(1, 9999999)) + + +@app.route('/async/requests', methods=['POST', 'GET']) def treat_async_request_all(): global async_request return Response(response=str(async_request), status=200) -@app.route('/async/request/', methods=['POST','GET']) +@app.route('/async/request/', methods=['POST', 'GET']) def treat_async_request(uid): global async_request @@ -98,11 +127,19 @@ def treat_set_response_to_ok(): return Response(response='Simulator set to OK', status=200) +@app.route('/setDelayToSync', methods=['GET']) +def treat_set_delay_to_sync(): + global delay + + delay = request.args.get('delay') + return Response(response='Delay to next sync request set to ' + delay, status=200) + + @app.route('/last', methods=['GET']) def treat_last(): - global lastRequest + global last_request - return Response(response=lastRequest, status=200) + return Response(response=last_request, status=200) @app.route('/count', methods=['GET']) @@ -113,22 +150,24 @@ def count(): @app.route('/reset', methods=['GET']) def reset(): - global lastRequest, async_request, cont - lastRequest = '' + global last_request, async_request, cont + last_request = '' async_request = {} cont = 0 return Response(status=200) # Globals -lastRequest = '' +last_request = '' responseError = '' -sync_response = '{"details": {"rgb":"66CC00","t":2}}' +sync_response = {} async_response = {} cont = 0 async_request = {} +service = '' +subservice = '' url_callback = "http://localhost:9999" - +delay = '' if __name__ == '__main__': app.run(host=host, port=port, debug=True) From 3bd8c8b1953eede240552975a7b0c59a86016f83 Mon Sep 17 00:00:00 2001 From: macs Date: Thu, 24 Sep 2015 11:46:42 +0200 Subject: [PATCH 3/4] Fixed uid repeated --- iotqatools/simulators/thirdPartySimulator.py | 37 ++++++-------------- 1 file changed, 11 insertions(+), 26 deletions(-) diff --git a/iotqatools/simulators/thirdPartySimulator.py b/iotqatools/simulators/thirdPartySimulator.py index 7bcb50b..07a7799 100644 --- a/iotqatools/simulators/thirdPartySimulator.py +++ b/iotqatools/simulators/thirdPartySimulator.py @@ -57,7 +57,7 @@ def treat_sync_request(): @app.route('/async/create', methods=['POST']) def treat_async_create(): - global last_request, async_request, url_callback, async_response, service, subservice + global last_request, url_callback, service, subservice print "Recived async create" # Store the last request @@ -70,45 +70,33 @@ def treat_async_create(): button_id = json.loads(request.data)['button'] # Compose the async response - async_response['externalId'] = str(generate_uid()) - async_response['buttonId'] = str(button_id) - async_response['details'] = {} + async_response = {'externalId': str(generate_uid()), 'buttonId': str(button_id), 'details': {}} async_response['details']['rgb'] = '66CCDD' async_response['details']['t'] = '2' # Invoke callback response - t = threading.Thread(target=invoke_ca) + t = threading.Thread(target=invoke_ca, args=(async_response,)) t.start() return Response(response='Create Received OK', status=200) -def invoke_ca(): +def invoke_ca(async_response): # Wait until request is finished - sleep(3) + print str(async_response['externalId']) + sleep(5) # Send data to urlCallback headers = {'Accept': 'application/json', 'content-type': 'application/json', 'fiware-service': service, 'fiware-servicepath': subservice} + print str(async_response['externalId']) r = requests.post(url_callback, data=json.dumps(async_response), headers=headers) return r def generate_uid(): - return str(random.randint(1, 9999999)) - - -@app.route('/async/requests', methods=['POST', 'GET']) -def treat_async_request_all(): - global async_request - - return Response(response=str(async_request), status=200) - - -@app.route('/async/request/', methods=['POST', 'GET']) -def treat_async_request(uid): - global async_request - - return Response(response=async_request[uid], status=200, content_type='application/json') + uid = str(random.randint(1, 9999999)) + print uid + return uid @app.route('/setResponseToError', methods=['GET']) @@ -150,9 +138,8 @@ def count(): @app.route('/reset', methods=['GET']) def reset(): - global last_request, async_request, cont + global last_request, cont last_request = '' - async_request = {} cont = 0 return Response(status=200) @@ -161,9 +148,7 @@ def reset(): last_request = '' responseError = '' sync_response = {} -async_response = {} cont = 0 -async_request = {} service = '' subservice = '' url_callback = "http://localhost:9999" From 0dd2b5b5abbe2a655837a5b76697c52323683200 Mon Sep 17 00:00:00 2001 From: macs Date: Thu, 24 Sep 2015 12:20:00 +0200 Subject: [PATCH 4/4] app logger added --- iotqatools/simulators/thirdPartySimulator.py | 31 +++++++++----------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/iotqatools/simulators/thirdPartySimulator.py b/iotqatools/simulators/thirdPartySimulator.py index 07a7799..e22d342 100644 --- a/iotqatools/simulators/thirdPartySimulator.py +++ b/iotqatools/simulators/thirdPartySimulator.py @@ -24,17 +24,19 @@ @app.route('/sync/request', methods=['POST']) def treat_sync_request(): - global last_request, sync_response, cont, delay + global last_request, cont, delay - print "Recived sync request" + app.logger.info("Received sync request") # Increase the number of requests cont += 1 # Store the last request last_request = request.data + app.logger.info('Request data received: ' + last_request) # Set delay if needed if delay is not '': + app.logger.info("Delay stated") sleep(int(delay)) delay = '' @@ -46,22 +48,22 @@ def treat_sync_request(): 'fiware-servicepath': subservice} # Generate sync response - sync_response['externalId'] = generate_uid() - sync_response['buttonId'] = button_id - sync_response['details'] = {} + sync_response = {'externalId': generate_uid(), 'buttonId': button_id, 'details': {}} sync_response['details']['rgb'] = '66CCDD' sync_response['details']['t'] = '2' + app.logger.info('Response sent: ' + json.dumps(sync_response)) return Response(response=json.dumps(sync_response), status=200, headers=headers) @app.route('/async/create', methods=['POST']) def treat_async_create(): - global last_request, url_callback, service, subservice + global last_request - print "Recived async create" + app.logger.info("Recived async create") # Store the last request last_request = request.data + app.logger.info('Request data received: ' + last_request) # Retrieve callback url and buttonId from request service = request.headers['fiware-service'] @@ -75,27 +77,26 @@ def treat_async_create(): async_response['details']['t'] = '2' # Invoke callback response - t = threading.Thread(target=invoke_ca, args=(async_response,)) + t = threading.Thread(target=invoke_ca, args=(async_response, url_callback, service, subservice)) t.start() return Response(response='Create Received OK', status=200) -def invoke_ca(async_response): +def invoke_ca(async_response, url_callback, service, subservice): # Wait until request is finished - print str(async_response['externalId']) - sleep(5) + sleep(3) # Send data to urlCallback headers = {'Accept': 'application/json', 'content-type': 'application/json', 'fiware-service': service, 'fiware-servicepath': subservice} - print str(async_response['externalId']) + app.logger.info('Response sent to ' + url_callback + ' is: ' + json.dumps(async_response)) r = requests.post(url_callback, data=json.dumps(async_response), headers=headers) return r def generate_uid(): uid = str(random.randint(1, 9999999)) - print uid + app.logger.info('Generated uid: ' + uid) return uid @@ -147,11 +148,7 @@ def reset(): # Globals last_request = '' responseError = '' -sync_response = {} cont = 0 -service = '' -subservice = '' -url_callback = "http://localhost:9999" delay = '' if __name__ == '__main__':