-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
77 lines (64 loc) · 2.36 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#--------------------------------------------------#
#THIS IS THE VERSION FOR THE Local testing using external static IP
#flask run
#--------------------------------------------------#
import json
from flask import Flask, request, Response
import requests
from datetime import date
from elasticsearch import Elasticsearch
import os
if 'ELASTICSEARCH_URL' in os.environ:
es_url = os.environ['ELASTICSEARCH_URL']
else:
print("Please provide Elasticsearch URL as Environment Variable")
exit()
if es_url[0:7] == 'http://':
es_url_trim = es_url[7:]
else:
es_url_trim = es_url
es_url = "http://" + es_url
app = Flask(__name__)
#CORS(app)
#app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
#es_url = "104.196.0.115"
es = Elasticsearch([{'host': es_url_trim, 'port': 9200}])
@app.route('/device/<device_id>', methods=['GET'])
def add_new_device(device_id):
#Maybe some authentication can be added to prevent unauthorized people from adding new devices
data = {
"mappings": {
"properties": {
"timestamp": {
"type": "date"
},
"location": {
"type": "geo_point"
}
}
}
}
x = requests.put(es_url+":9200/"+device_id, json = data)
return (x.text,str(x.status_code))
@app.route('/sensor/data', methods=['POST'])
def add_sensor_data():
inputData = request.json
device_id = inputData['device_id']
today = date.today()
currdate = today.strftime("%Y-%m-%d")
lastcontact = currdate + str("T") + str(inputData['timestamp'])
data = {'device_id':inputData['device_id'], 'timestamp':lastcontact, 'altitude':inputData['altitude'], 'location':{'lat':inputData['latitude'], 'lon':inputData['longitude']}, 'aq1':{'pm10':inputData['aq1']['pm10'], 'pm75':inputData['aq1']['pm75'], 'pm25':inputData['aq1']['pm25']}, 'aq2':{'pm10':inputData['aq2']['pm10'], 'pm75':inputData['aq2']['pm75'], 'pm25':inputData['aq2']['pm25']}, 'aq3':{'pm10':inputData['aq3']['pm10'], 'pm75':inputData['aq3']['pm75'], 'pm25':inputData['aq3']['pm25']}, 'battery_level':inputData['battery_level']}
mn = es.index(index=device_id, body=data)
return Response(status=200)
@app.route('/data', methods=['GET'])
def get_data():
data = es.search()
return data
#Testing routes
@app.route('/')
def homepage():
val=es.index(index='my_index', id=1, body={'text': 'this is a test'})
return val
@app.route('/test')
def test():
return("Works")