/
rulesRestServer.py
executable file
·121 lines (102 loc) · 2.95 KB
/
rulesRestServer.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
#!/usr/bin/env python2.7
# -*- encoding: utf-8 -*-
"""
Home-rest
~~~~~~~~~
:copyright: (c) 2013 by Aurélien Chabot <aurelien@chabot.fr>
:license: LGPLv3, see COPYING for more details.
"""
try:
from flask import Flask, request, abort, json
from subprocess import call, Popen, PIPE
from traceback import print_exc
import threading
import json
import urllib2
import ConfigParser
import sys, os, time, datetime
import sqlite3
except ImportError as error:
print 'ImportError: ', str(error)
exit(1)
try:
sys.path.insert(0, '../rest/')
sys.path.insert(0, '/usr/local/bin/')
from restClientLib import get_nodes, set_switch, update_sensor, update_switch
except ImportError as error:
print 'Custom py ImportError: ', str(error)
exit(1)
if len(sys.argv) > 1:
config = sys.argv[1]
else:
print("You need to provide a configuration")
exit(1)
app = Flask(__name__)
app.config.from_pyfile(config, silent=True)
try:
fd = open(app.config["SERVERS"])
servers = json.load(fd)
except:
print("Invalid configuration file")
exit(1)
def dict_factory(cursor, row):
d = {}
for idx,col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
db = sqlite3.connect(app.config['DB'])
db.row_factory = dict_factory
################### RULES API
def createRulesTable():
c = db.cursor()
c.execute('''CREATE TABLE rules (
id integer PRIMARY KEY AUTOINCREMENT, sensor_host text, sensor_id integer, switch_host text, switch_id interger,
start_time timestamp, end_time timestamp, temp real)''')
db.commit()
#createRulesTable()
# Get list of rules
@app.route("/rules/")
def get_rules():
try:
c = db.cursor()
return json.dumps(c.execute('SELECT * FROM rules').fetchall())
except:
print_exc()
return json.dumps({})
# Get one rule
@app.route("/rules/<int:ruleId>/", methods = ["GET"])
def get_rule(ruleId):
try:
c = db.cursor()
return json.dumps(c.execute('SELECT * FROM rules WHERE id=:id', { "id": ruleId }).fetchall())
except:
print_exc()
return json.dumps({})
# Add one rule
@app.route("/rules/", methods = ["POST"])
def add_rule():
try:
c = db.cursor()
c.execute('''INSERT INTO rules
( switch_host, switch_id, sensor_host, sensor_id, start_time, end_time, temp)
VALUES (:switch_host, :switch_id, :sensor_host, :sensor_id, :start_time, :end_time, :temp)''',
{"switch_host" : request.form["switch_host"], "switch_id" : request.form["switch_id"],
"sensor_host" : request.form["sensor_host"], "sensor_id" : request.form["sensor_id"],
"start_time" : request.form['start_time'], "end_time" : request.form['end_time'],
"temp" : request.form['temp']})
db.commit()
except:
print_exc()
return get_rules()
# Del one rule
@app.route("/rules/<int:ruleId>/", methods = ["DELETE"])
def del_rule(ruleId):
try:
c = db.cursor()
c.execute('DELETE FROM rules WHERE id=:id', { "id": ruleId })
db.commit()
except:
print_exc()
return json.dumps({})
# Rest server for monitor configuration
app.run("0.0.0.0", app.config["PORT"])