1 | #!/usr/bin/python
|
2 |
|
3 | from flask import Flask
|
4 | from flask_restful import Resource, Api
|
5 | from flask_restful import reqparse
|
6 | from flask.ext.mysql import MySQL
|
7 |
|
8 |
|
9 |
|
10 | mysql = MySQL()
|
11 | app = Flask(__name__)
|
12 |
|
13 | # MySQL configurations
|
14 | app.config['MYSQL_DATABASE_USER'] = 'pi'
|
15 | app.config['MYSQL_DATABASE_PASSWORD'] = 'xxxxxxxx'
|
16 | app.config['MYSQL_DATABASE_DB'] = 'xxxxxxxx'
|
17 | app.config['MYSQL_DATABASE_HOST'] = 'localhost'
|
18 |
|
19 |
|
20 | mysql.init_app(app)
|
21 |
|
22 | api = Api(app)
|
23 |
|
24 | class test(Resource):
|
25 | def post(self):
|
26 | try:
|
27 | # Parse the arguments
|
28 |
|
29 | parser = reqparse.RequestParser()
|
30 | parser.add_argument('ID', type=str, help='Id of Item')
|
31 | args = parser.parse_args()
|
32 |
|
33 | id = str(args['ID'])
|
34 | conn = mysql.connect()
|
35 | cursor = conn.cursor()
|
36 | cursor.execute("SELECT * FROM `PowerSystem` WHERE ID=id")
|
37 |
|
38 |
|
39 |
|
40 | data = cursor.fetchall()
|
41 |
|
42 | items_list=[];
|
43 | for item in data:
|
44 | i = {
|
45 | 'ID':str(item[0]),
|
46 | 'Timestamp':str(item[1]),
|
47 | 'batteryVoltage':str(item[2]),
|
48 | 'batteryCurrent':str(item[3]),
|
49 | 'solarVoltage':str(item[4]),
|
50 | 'solarCurrent':str(item[5]),
|
51 | 'loadVoltage':str(item[6]),
|
52 | 'loadCurrent':str(item[7]),
|
53 | 'batteryPower':str(item[8]),
|
54 | 'solarPower':str(item[9]),
|
55 | 'loadPower':str(item[10]),
|
56 | 'batteryCharge':str(item[11])
|
57 | }
|
58 | items_list.append(i)
|
59 |
|
60 | return {'StatusCode':'200','Items':items_list}
|
61 |
|
62 | except Exception as e:
|
63 | return {'error': str(e)}
|
64 | api.add_resource(test, '/test')
|
65 |
|
66 |
|
67 | if __name__ == '__main__':
|
68 | app.run(debug=True,host='0.0.0.0')
|
69 |
|
70 | app.run(debug=True,host='0.0.0.0')
|