-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from smartfog/development
all features merging into master after 1st may 2020
- Loading branch information
Showing
82 changed files
with
15,656 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
## Contributing to FogFlow | ||
|
||
|
||
### sign up your contributor agreement license | ||
|
||
If you are interested to make any contribution to FogFlow, please download the contributor license agreement listed below and send us you signed agreement via email [email protected] | ||
|
||
|
||
[FogFlow Entity Contributor License Agreement](https://github.com/smartfog/fogflow/blob/development/FogFlow-Entity.pdf) | ||
|
||
|
||
[FogFlow Individual Contributor License Agreement](https://github.com/smartfog/fogflow/blob/development/FogFlow-Individual.pdf) | ||
|
||
### contribute code to FogFlow | ||
|
||
*Before opening a pull request*, review the | ||
[Contributing to FogFlow guide](https://fogflow.readthedocs.io/en/latest/guideline.html). | ||
|
||
|
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
docker build -t "python-fog-function" . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[{ | ||
"command": "CONNECT_BROKER", | ||
"brokerURL": "http://180.179.214.208:8070" | ||
}, { | ||
"command": "SET_OUTPUTS", | ||
"id": "Stream.ChildFound.01", | ||
"type": "ChildFound" | ||
}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
FROM python:2.7-alpine | ||
|
||
RUN mkdir /task | ||
ADD main.py /task | ||
ADD requirements.txt /task | ||
WORKDIR /task | ||
|
||
RUN pip install -r requirements.txt | ||
|
||
CMD ["python", "./main.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
from flask import Flask, jsonify, abort, request, make_response | ||
import requests | ||
import json | ||
import time | ||
import datetime | ||
import threading | ||
import os | ||
|
||
app = Flask(__name__, static_url_path='') | ||
|
||
# global variables | ||
|
||
brokerURL = '' | ||
outputs = [] | ||
timer = None | ||
lock = threading.Lock() | ||
counter = 0 | ||
create = 0 | ||
|
||
|
||
@app.errorhandler(400) | ||
def not_found(error): | ||
return make_response(jsonify({'error': 'Bad request'}), 400) | ||
|
||
|
||
@app.errorhandler(404) | ||
def not_found(error): | ||
return make_response(jsonify({'error': 'Not found'}), 404) | ||
|
||
|
||
@app.route('/admin', methods=['POST']) | ||
def admin(): | ||
if not request.json: | ||
abort(400) | ||
|
||
configObjs = request.json | ||
handleConfig(configObjs) | ||
|
||
return jsonify({'responseCode': 200}) | ||
|
||
|
||
@app.route('/notifyContext', methods=['POST']) | ||
def notifyContext(): | ||
print '=============notify=============' | ||
if not request.json: | ||
abort(400) | ||
|
||
objs = readContextElements(request.json) | ||
global counter | ||
counter = counter + 1 | ||
|
||
print objs | ||
|
||
handleNotify(objs) | ||
|
||
return jsonify({'responseCode': 200}) | ||
|
||
|
||
def element2Object(element): | ||
ctxObj = {} | ||
for key in element: | ||
ctxObj[key] = element[key] | ||
return ctxObj | ||
|
||
|
||
def object2Element(ctxObj): | ||
ctxElement = {} | ||
|
||
for key in ctxObj: | ||
ctxElement[key] = ctxObj[key] | ||
return ctxElement | ||
|
||
|
||
def readContextElements(data): | ||
|
||
ctxObjects = [] | ||
|
||
for response in data['contextResponses']: | ||
if response['statusCode']['code'] == 200: | ||
ctxObj = element2Object(response['contextElement']) | ||
ctxObjects.append(ctxObj) | ||
return ctxObjects | ||
|
||
|
||
def handleNotify(contextObjs): | ||
for ctxObj in contextObjs: | ||
processInputStreamData(ctxObj) | ||
|
||
|
||
def processInputStreamData(obj): | ||
print '===============receive context entity====================' | ||
print obj | ||
|
||
global counter | ||
counter = counter + 1 | ||
|
||
|
||
def handleConfig(configurations): | ||
global brokerURL | ||
global num_of_outputs | ||
for config in configurations: | ||
if config['command'] == 'CONNECT_BROKER': | ||
brokerURL = config['brokerURL'] | ||
if config['command'] == 'SET_OUTPUTS': | ||
outputs.append({'id': config['id'], 'type': config['type']}) | ||
|
||
|
||
def handleTimer(): | ||
global timer | ||
|
||
# publish the counting result | ||
|
||
entity = {} | ||
entity['id'] = 'urn:ngsi-ld:result.01' | ||
entity['type'] = 'Result' | ||
entity['counter'] = counter | ||
|
||
publishResult(entity) | ||
timer = threading.Timer(10, handleTimer) | ||
timer.start() | ||
|
||
|
||
# update request on broker | ||
|
||
def update(resultCtxObj): | ||
global brokerURL | ||
if brokerURL.endswith('/ngsi10') == True: | ||
brokerURL = brokerURL.rsplit('/', 1)[0] | ||
if brokerURL == '': | ||
return | ||
|
||
ctxElement = object2Element(resultCtxObj) | ||
|
||
id = ctxElement['id'] | ||
ctxElement.pop('id') | ||
ctxElement.pop('type') | ||
headers = {'Accept': 'application/ld+json', | ||
'Content-Type': 'application/ld+json'} | ||
response = requests.patch(brokerURL + '/ngsi-ld/v1/entities/' + id | ||
+ '/attrs', data=json.dumps(ctxElement), | ||
headers=headers) | ||
return response.status_code | ||
|
||
|
||
def sendDataToBroker(resultCtxObj): | ||
global create | ||
if create == 0: | ||
response = cretaeRequest(resultCtxObj) | ||
if response != 201: | ||
print 'failed to send the create request' | ||
else: | ||
print 'Entity already has been created trying for update request....' | ||
response = update(resultCtxObj) | ||
if response != 204: | ||
print 'failed to send the updte request' | ||
|
||
|
||
def publishResult(result): | ||
resultCtxObj = {} | ||
resultCtxObj['id'] = result['id'] | ||
resultCtxObj['type'] = result['type'] | ||
data = {} | ||
data['type'] = 'Property' | ||
data['value'] = result['counter'] | ||
resultCtxObj['count'] = data | ||
sendDataToBroker(resultCtxObj) | ||
|
||
|
||
# create request on broker | ||
|
||
def cretaeRequest(ctxObj): | ||
global brokerURL | ||
global create | ||
if brokerURL.endswith('/ngsi10') == True: | ||
brokerURL = brokerURL.rsplit('/', 1)[0] | ||
if brokerURL == '': | ||
return | ||
|
||
ctxElement = object2Element(ctxObj) | ||
|
||
headers = {'Accept': 'application/ld+json', | ||
'Content-Type': 'application/ld+json', | ||
'Link': '<{{link}}>; rel="https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld"; type="application/ld+json"'} | ||
response = requests.post(brokerURL + '/ngsi-ld/v1/entities/', | ||
data=json.dumps(ctxElement), | ||
headers=headers) | ||
if response.status_code == 201: | ||
create = create + 1 | ||
if response.status_code != 201: | ||
create = 1 | ||
return response.status_code | ||
|
||
|
||
if __name__ == '__main__': | ||
handleTimer() | ||
|
||
myport = int(os.environ['myport']) | ||
|
||
myCfg = os.environ['adminCfg'] | ||
adminCfg = json.loads(myCfg) | ||
handleConfig(adminCfg) | ||
|
||
app.run(host='0.0.0.0', port=myport) | ||
|
||
# timer.cancel() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Flask==1.0.2 | ||
requests>=2.20.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"entities": [ | ||
{ | ||
"type": "Camera", | ||
"isPattern": false, | ||
"id": "Stream.Camera.02" | ||
} | ||
], | ||
"reference": "http://192.168.1.102:36772" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#start a container for test | ||
#docker run -p 8009:8080 -t -i facefinder /bin/bash | ||
|
||
#configurate | ||
curl -X POST "http://192.168.1.80:8009/admin" -d @config.json --header "Content-Type:application/json" --header "Accept:application/json" | ||
|
||
#issue a subscription to get the input data | ||
curl -X POST "http://192.168.1.80:8091/ngsi10/subscribeContext" -d @subscriptionCamera.json --header "Content-Type:application/json" --header "Accept:application/json" | ||
curl -X POST "http://192.168.1.80:8091/ngsi10/subscribeContext" -d @subscriptionChildLost.json --header "Content-Type:application/json" --header "Accept:application/json" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM scratch | ||
FROM alpine | ||
ADD broker / | ||
CMD ["/broker"] | ||
|
Oops, something went wrong.