-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.py
More file actions
125 lines (81 loc) · 2.73 KB
/
web.py
File metadata and controls
125 lines (81 loc) · 2.73 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
from flask import Flask
from pymongo import MongoClient
import os
import time
app = Flask(__name__)
# Debug switches for RH openshift
DEBUG='true'
app.config['PROPAGATE_EXCEPTIONS'] = True
os.environ['TZ'] = 'Europe/London'
time.tzset()
""" Mongo DB string, this will soon contain parameters from the
config-secure.yml configuration file
"""
mongoDBString = ('mongodb://username:password' + os.environ['OPENSHIFT_MONGODB_DB_HOST'] + ':' + os.environ['OPENSHIFT_MONGODB_DB_PORT'] + '/')
""" Location functions
These functions are used to take the location values submitted as part of the
get request and use them as a update variable for the database.
The retrieval function in the core nead application [which sites on the pi]
uses a very simple if else to parse the database faux locations and return the
real values we want to use.
"""
@app.route('/location/<location>')
def location(location):
# Some nasty nasty input vertification
if (location == "location1"):
updateDBLocation('location1')
elif (location == "location2"):
updateDBLocation('location2')
elif (location == "location3"):
updateDBLocation('location3')
string = ("complete")
return string
def updateDBLocation(locationDetail):
client = MongoClient(mongoDBString)
# For local mongo testing
# client = MongoClient()
db = client.dashdb
queryResult = db.nead.update (
{ "widget": "location" },
{ "$set":
{ "location": locationDetail }
},
upsert=False, multi=False
)
@app.route('/location/display')
def displayLocation():
client = MongoClient(mongoDBString)
# For local mongo testing
# client = MongoClient()
db = client.dashdb
cursor = db.nead.find()
for object in cursor:
data = object['location']
return data
""" Coffee functions
Push the current date and time of the request for /coffee/plus to the mongo database
"""
@app.route('/coffee/plus')
def updateCoffeeCount():
currentDate = time.strftime("%Y-%m-%d")
currentTime = time.strftime("%H:%M")
client = MongoClient(mongoDBString)
# For local mongo testing
# client = MongoClient()
db = client.dashdb
result = db.coffee.insert({"currentDate": currentDate, "currentTime": currentTime})
return "complete"
@app.route('/coffee/status')
def showCoffeeCount():
currentDate = time.strftime("%Y-%m-%d")
count = 0
client = MongoClient(mongoDBString)
db = client.dashdb
cursor = db.coffee.find({'currentDate': currentDate })
for object in cursor:
data = object['currentDate']
count = count + 1
return str(count)
if __name__ == '__main__':
app.run(port=8051,debug="True")