From b28bab318e948be6dd86ae3caf5f7586183a3123 Mon Sep 17 00:00:00 2001 From: Bruno Fagundez Date: Fri, 28 Mar 2014 11:07:12 -0300 Subject: [PATCH 1/3] removed light httpd --- .gitignore | 2 + index.html | 191 -------------------------------------------------- lighttpd.conf | 33 --------- 3 files changed, 2 insertions(+), 224 deletions(-) create mode 100644 .gitignore delete mode 100644 index.html delete mode 100644 lighttpd.conf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..854a5a5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.pyc +venv diff --git a/index.html b/index.html deleted file mode 100644 index e03e2db..0000000 --- a/index.html +++ /dev/null @@ -1,191 +0,0 @@ - - - - - - Power Control - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - - - -
- - - - -
- - - -
- -
- alt -
-

Nexa Switch - 1

-
- - - Password: - - -
-
-
-
- -
- alt -
-

Nexa Switch - 2

-
- - - Password: - - -
-
-
-
- -
- alt -
-

Nexa Switch - 3

-
- - - Password: - - -
-
-
-
- - -
- alt -
-

TESTING

-
- Device: - -
- Time: -
- Password: - - -
-
-
-
-
- - -
- - - - - Foration 2014 - - - - - - -
-
- - - - - - - diff --git a/lighttpd.conf b/lighttpd.conf deleted file mode 100644 index f54262d..0000000 --- a/lighttpd.conf +++ /dev/null @@ -1,33 +0,0 @@ -server.modules = ( - "mod_access", - "mod_alias", - "mod_compress", - "mod_redirect", - "mod_cgi", -# "mod_rewrite", -) - -server.document-root = "/var/www" -server.upload-dirs = ( "/var/cache/lighttpd/uploads" ) -server.errorlog = "/var/log/lighttpd/error.log" -server.pid-file = "/var/run/lighttpd.pid" -server.username = "www-data" -server.groupname = "www-data" -server.port = 80 - - -index-file.names = ( "index.php", "index.html", "index.lighttpd.html" ) -url.access-deny = ( "~", ".inc" ) -static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" ) - -compress.cache-dir = "/var/cache/lighttpd/compress/" -compress.filetype = ( "application/javascript", "text/css", "text/html", "text/plain" ) - -# default listening port for IPv6 falls back to the IPv4 port -include_shell "/usr/share/lighttpd/use-ipv6.pl " + server.port -include_shell "/usr/share/lighttpd/create-mime.assign.pl" -include_shell "/usr/share/lighttpd/include-conf-enabled.pl" - -$HTTP["url"] =~ "^/cgi-bin/" { - cgi.assign = (".py" => "/usr/bin/python") -} From 457c95f1520804679c800714cbcc981fa3c13ae9 Mon Sep 17 00:00:00 2001 From: Bruno Fagundez Date: Fri, 28 Mar 2014 11:14:50 -0300 Subject: [PATCH 2/3] tested endpoints --- power.py | 98 ++++++++++++++++++++++---------------------------------- 1 file changed, 39 insertions(+), 59 deletions(-) diff --git a/power.py b/power.py index cd61f70..a983117 100644 --- a/power.py +++ b/power.py @@ -1,66 +1,46 @@ +# add flask here +from flask import Flask +app = Flask(__name__) +app.debug = True +# keep your code import time import cgi -form = cgi.FieldStorage() - -print "Content-Type: text/html" -print -print '' -print 'Power' - -if not ("device" in form and "power" in form and "pass" in form): - print "

Incorrect params have been provided." - print "" - print "" - print """""" - print "" - exit() #end here if errors - -device_id, power, powerOnTime = int(form['device'].value), form['power'].value, int(form['time'].value) - -if not ('pass' in form and form['pass'].value == "p@ssw0rd"): - print "

Password is incorrect." - exit() - from tellcore.telldus import TelldusCore core = TelldusCore() devices = core.devices() - -if not (power in ['off','on','time'] and - device_id <= len(devices)): - print "

Incorrect values for the params have been provided." - print "" - print "" - print """""" - print "" - exit() #end here if errors - device = devices[device_id] -if power == 'on': - device.turn_on() -if power == 'off': - device.turn_off() -if powerOnTime > 0: - device.turn_on() - time.sleep(powerOnTime) - device.turn_off() -print '

Device ID %s has been turned %s for %s seconds' % (device_id,power,powerOnTime) -print "" -print "" -print """""" -print "" +# define a "power ON api endpoint" +@app.route("/API/v1.0/power-on/",methods=['POST']) +def powerOnDevice(deviceId): + payload = {} + #get the device by id somehow + device = devices[deviceId] + # get some extra parameters + # let's say how long to stay on + params = request.get_json() + try: + device.turn_on() + payload['success'] = True + return payload + except: + payload['success'] = False + # add an exception description here + return payload + +# define a "power OFF api endpoint" +@app.route("/API/v1.0/power-off/",methods=['POST']) +def powerOffDevice(deviceId): + payload = {} + #get the device by id somehow + device = devices[deviceId] + try: + device.turn_off() + payload['success'] = True + return payload + except: + payload['success'] = False + # add an exception description here + return payload + +app.run() From 0fa4495bc7b9db89e7250b509a97f6a2f6c23d4e Mon Sep 17 00:00:00 2001 From: Bruno Fagundez Date: Fri, 28 Mar 2014 11:15:33 -0300 Subject: [PATCH 3/3] removed device finding version 1 --- power.py | 1 - 1 file changed, 1 deletion(-) diff --git a/power.py b/power.py index a983117..1733635 100644 --- a/power.py +++ b/power.py @@ -8,7 +8,6 @@ from tellcore.telldus import TelldusCore core = TelldusCore() devices = core.devices() -device = devices[device_id] # define a "power ON api endpoint" @app.route("/API/v1.0/power-on/",methods=['POST'])