-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
176 lines (144 loc) · 5.33 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from flask import Flask, jsonify, make_response, request
from flask_cors import CORS
from influxdb import InfluxDBClient
import json
def createApp():
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})
return app
def connectInflux():
client = InfluxDBClient(host='52.19.82.33', port=8086, database='BinBotStats', username='binbot', password='b33pb00p!!')
return client
app = createApp()
db = connectInflux()
@app.route('/')
def hello_world():
return 'Hello World!'
@app.route('/piStats')
def piStats():
timeRangeDays = request.args.get('rangeDays')
timeRangeHours = request.args.get('rangeHours')
timeRangeMinutes = request.args.get('rangeMinutes')
if timeRangeDays is None:
timeRangeDays = 1
if timeRangeHours is None:
timeRangeHours = 0
if timeRangeMinutes is None:
timeRangeMinutes = 0
if not str(timeRangeDays).isnumeric():
timeRangeDays = 1
if not str(timeRangeHours).isnumeric():
timeRangeHours = 0
if not str(timeRangeMinutes).isnumeric():
timeRangeMinutes = 0
command = 'SELECT cpu, disk, ram FROM piSystemUsage WHERE time > now() - ' + str(timeRangeDays) + 'd - ' \
+ str(timeRangeHours) + 'h - ' + str(timeRangeMinutes) + 'm'
print(command)
data = db.query(command)
usageData = []
for items in data:
for item in items:
dataPoint = {
"time": item["time"],
"cpu": item["cpu"],
"disk": item["disk"],
"ram": item["ram"],
}
usageData.append(dataPoint)
return make_response(jsonify(usageData))
@app.route('/batLevel')
def batLevel():
command = 'SELECT level from batLevel ORDER BY DESC LIMIT 1'
data = db.query(command)
for items in data:
return make_response(jsonify(items[0]))
return "Error"
@app.route('/recentMessages')
def recentMessages():
command = 'SELECT message from messages ORDER BY DESC LIMIT 5'
data = db.query(command)
messageData = []
for items in data:
for item in items:
dataPoint = {
"message": item["message"]
}
messageData.append(dataPoint)
return make_response(jsonify(messageData))
@app.route('/micKeyword')
def micKeyword():
command = 'SELECT distinct(mic_keyword) from micKeyword'
data = db.query(command)
commandData = []
for items in data:
for item in items:
command = "SELECT COUNT(mic_keyword) from micKeyword where mic_keyword = '" + item["distinct"] + "'"
counts = db.query(command)
for count in counts:
thisCount = {
"KeyWord": item["distinct"],
"Count": count[0]["count"]
}
commandData.append(thisCount)
return make_response(jsonify(commandData))
@app.route('/micAngleArrival')
def micAngleArrival():
timeRangeDays = request.args.get('rangeDays')
timeRangeHours = request.args.get('rangeHours')
timeRangeMinutes = request.args.get('rangeMinutes')
if timeRangeDays is None:
timeRangeDays = 1
if timeRangeHours is None:
timeRangeHours = 0
if timeRangeMinutes is None:
timeRangeMinutes = 0
if not str(timeRangeDays).isnumeric():
timeRangeDays = 1
if not str(timeRangeHours).isnumeric():
timeRangeHours = 0
if not str(timeRangeMinutes).isnumeric():
timeRangeMinutes = 0
command = 'SELECT mic_direction_of_arrival from micAngleArrival WHERE time > now() - ' + str(timeRangeDays) + 'd - ' \
+ str(timeRangeHours) + 'h - ' + str(timeRangeMinutes) + 'm'
print(command)
data = db.query(command)
for items in data:
dataPoint = {
"angles": items
}
return make_response(jsonify(dataPoint))
return "Empty List"
@app.route('/commandFrequency')
def commandFrequency():
command = 'SELECT distinct(command) from commands'
data = db.query(command)
commandData = []
for items in data:
for item in items:
command = "SELECT COUNT(command) from commands where command = '" + item["distinct"] + "'"
counts = db.query(command)
for count in counts:
thisCount = {
"Command": item["distinct"],
"Count" : count[0]["count"]
}
commandData.append(thisCount)
return make_response(jsonify(commandData))
@app.route('/soundFrequency')
def soundFrequency():
command = 'SELECT distinct(sound_played) from soundPlayed'
data = db.query(command)
commandData = []
for items in data:
for item in items:
command = "SELECT COUNT(sound_played) from soundPlayed where sound_played = " + str(item["distinct"])
counts = db.query(command)
for count in counts:
thisCount = {
"Sound": item["distinct"],
"Count" : count[0]["count"]
}
commandData.append(thisCount)
return make_response(jsonify(commandData))
if __name__ == '__main__':
app.run(host="0.0.0.0",debug=True)