-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestScriptManual.fcgi
More file actions
executable file
·102 lines (82 loc) · 3.58 KB
/
TestScriptManual.fcgi
File metadata and controls
executable file
·102 lines (82 loc) · 3.58 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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#from cgi import escape
import sys, os
from flup.server.fcgi import WSGIServer
import subprocess
import testpython
import urlparse
import cgi
import datetime
import time
import json
import MySQLdb
def app(environ, start_response):
connection = MySQLdb.connect(host="localhost", # your host, usually localhost
user="root", # your username
passwd="danilo", # your password
db="azure_machine_learning") # name of the data base
# you must create a Cursor object. It will let
# you execute all the queries you need
cursor = connection.cursor()
start_response('200 OK', [('Content-Type', 'text/html')])
#yield environ['REQUEST_METHOD']
#yield environ['QUERY_STRING']
#yield environ['SCRIPT_NAME']+environ['PATH_INFO']+environ['QUERY_STRING']
#yield environ['QUERY_STRING']
if len(str(environ['QUERY_STRING'])) > 0:
parsed = cgi.parse_qs(environ['QUERY_STRING'])
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
parsed['Morning']=[]
parsed['Noon']=[]
parsed['Evening']=[]
parsed['Midnight']=[]
parsed['AC Setting']=[]
if parsed['Time'][0]=="morning":
parsed['Morning'].append('1')
parsed['Noon'].append('0')
parsed['Evening'].append('0')
parsed['Midnight'].append('0')
elif parsed['Time'][0]=="noon":
parsed['Morning'].append('0')
parsed['Noon'].append('1')
parsed['Evening'].append('0')
parsed['Midnight'].append('0')
elif parsed['Time'][0]=="evening":
parsed['Morning'].append('0')
parsed['Noon'].append('0')
parsed['Evening'].append('1')
parsed['Midnight'].append('0')
elif parsed['Time'][0]=="midnight":
parsed['Morning'].append('0')
parsed['Noon'].append('0')
parsed['Evening'].append('0')
parsed['Midnight'].append('1')
response = subprocess.check_output(["python", "RequestResponse.py", str(parsed["Temperature"][0]), str(parsed["Wind"][0]), str(parsed["Humidity"][0]), str(parsed["Temperature Preference"][0]), str(parsed["Morning"][0]), str(parsed["Noon"][0]), str(parsed["Evening"][0])])
response = json.loads(response)
parsed['AC Setting'].append(str(response['Results']['output1']['value']['Values'][0][7]))
#yield response
cursor.execute("""INSERT INTO online_interface VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",(st, str(parsed["Temperature"][0]), str(parsed["Wind"][0]), str(parsed["Humidity"][0]), str(parsed["Temperature Preference"][0]), str(parsed["Morning"][0]), str(parsed["Noon"][0]), str(parsed["Evening"][0]), str(parsed["Midnight"][0]), str(parsed["AC Setting"][0])))
connection.commit()
# Use all the SQL you like
cursor.execute("""SELECT * FROM online_interface ORDER BY Timestamp DESC""")
yield json.dumps(cursor.fetchall())
#yield "["
#for row in cursor.fetchall() :
# yield "["
# for k in row:
# yield '"'+k+'"'
# yield ","
# yield "],"
#yield "]"
#yield json.dumps(parsed)
#yield parsed['temperature'][0]
#yield parsed['humidity'][0]
#yield parsed['wind'][0]
#yield parsed['time'][0]
# close the cursor object
#cursor.close()
# close the connection
connection.close()
WSGIServer(app).run()