Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pyc
venv
191 changes: 0 additions & 191 deletions index.html

This file was deleted.

33 changes: 0 additions & 33 deletions lighttpd.conf

This file was deleted.

97 changes: 38 additions & 59 deletions power.py
Original file line number Diff line number Diff line change
@@ -1,66 +1,45 @@
# 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 '<!doctype html>'
print '<title>Power</title>'

if not ("device" in form and "power" in form and "pass" in form):
print "<p class=error> Incorrect params have been provided."
print "<script>"
print "function goBack()"
print "{"
print "window.history.back()"
print "}"
print "</script>"
print "<body>"
print """<button onclick="goBack()">Go Back</button>"""
print "</body>"
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 "<p class=errr> 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 "<p class=err> Incorrect values for the params have been provided."
print "<script>"
print "function goBack()"
print "{"
print "window.history.back()"
print "}"
print "</script>"
print "<body>"
print """<button onclick="goBack()">Go Back</button>"""
print "</body>"
exit() #end here if errors

device = devices[device_id]
if power == 'on':
device.turn_on()
if power == 'off':
device.turn_off()
# define a "power ON api endpoint"
@app.route("/API/v1.0/power-on/<deviceId>",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/<deviceId>",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

if powerOnTime > 0:
device.turn_on()
time.sleep(powerOnTime)
device.turn_off()
print '<p class=success> Device ID %s has been turned %s for %s seconds' % (device_id,power,powerOnTime)
print "<script>"
print "function goBack()"
print "{"
print "window.history.back()"
print "}"
print "</script>"
print "<body>"
print """<button onclick="goBack()">Go Back</button>"""
print "</body>"
app.run()